Files
DataLogger-Generic/www/app.js
2016-02-05 14:58:51 -06:00

97 lines
4.3 KiB
JavaScript

var express = require('express'),
path = require('path'),
fs = require('fs'),
logger = require('morgan'),
methodOverride = require('method-override'),
bodyParser = require('body-parser'),
errorHandler = require('errorhandler');
var app = express();
var fns;
app.locals.DB_TYPE = "SQLite"; // or "MySQL"
/**
* Configuration
*/
if (app.locals.DB_TYPE == "MySQL"){
fns = require('./functions_MySQL.js');
var mysql = require('mysql');
var db_config = {
host: 'localhost',
user: 'website',
password: 'henrypump'
};
var handleDisconnect = function () {
console.log("Handling db disconnect gracefully");
app.locals.db = mysql.createConnection(db_config);
app.locals.db.connect(function (err) {
if (err) {
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000);
}
});
app.locals.db.on('error', function (err) {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
};
handleDisconnect();
} else {
fns = require('./functions_SQLite.js');
}
app.set('port', process.env.PORT || 80);
app.set('views', path.join(__dirname, 'views'));
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');
//app.use(favicon(__dirname + '/public/img/favicon.ico'));
app.use(logger('dev'));
app.use(methodOverride());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
//app.use(express["static"](path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
app.use('/node_modules', express.static(__dirname + '/node_modules'));
/**
* Routes
*/
var angular = function(req, res) {
res.render('angularIndex');
};
app.post('/json/tag/add', fns.createTag); // Adds a tag to the scan list
app.post('/json/tag/update/', fns.updateTag); // Updates tag data
app.get('/json/tag/delete/:tag', fns.deleteTag); // Removes a tag from the scan list
app.get('/json/tag/:id', fns.getTag); // Gets a specific tag in the scan list
app.get('/json/tag', fns.getAllTags); // Lists all tags in the scan list
app.get('/json/val/:tag', fns.latestValueSingleTag); // Gets the latest value of a single tag
app.get('/json/series/:tag/:hours', fns.seriesTagValues); // Gets all the values of a tag for the last X hours
app.get('/json/valBetween/:tag/:startDatetime/:endDatetime', fns.seriesTagValuesBetween); // Gets the values of a tag between the start time and end time
app.get('/json/CSV/:tag/:startDatetime/:endDatetime', fns.seriesCSV); // Gets a CSV of the values of a tag between the start time and end time
app.get('/json/CSV/:tag/:hours', fns.seriesCSV); // Gets a CSV of the values of a tag for the last x hours
app.get('/json/all', fns.latestValueAllTags); // Gets the latest values of all tags in the scan list
app.get('/json/config', fns.getSetup); // Gets the contents of the config table
app.post('/json/config', fns.updateSetup); // Adds a new parameter to the config table
app.get('/json/logger/status', fns.checkLoggerStatus); // Gets the status of the data logger
app.get('/json/logger/restart', fns.restartLogger); // Restarts the data logger
app.get('*', angular);
/**
* Start Server
*/
connectionsArray = [];
s_port = 3000;
var server = app.listen(s_port, function () {
var host = server.address().address;
var port = server.address().port;
console.log('POConsole listening at http://%s:%s', host, port);
});