mirror of
https://gitlab.silvrtree.co.uk/martind2000/mqttArchiver.git
synced 2025-01-10 22:05:07 +00:00
142 lines
3.3 KiB
JavaScript
142 lines
3.3 KiB
JavaScript
var express = require('express');
|
|
var path = require('path');
|
|
var http = require('http');
|
|
var ejs = require('ejs');
|
|
var morgan = require('morgan');
|
|
var cookieparser = require('cookie-parser');
|
|
var session = require('express-session');
|
|
var sessionLW = require('express-session-lw');
|
|
var methodoverride = require('method-override');
|
|
var bodyparser = require('body-parser');
|
|
var errorhandler = require('errorhandler');
|
|
var log4js = require('log4js');
|
|
var logger = log4js.getLogger();
|
|
var authentication = require('basic-authentication');
|
|
|
|
var cfenv = require('cfenv');
|
|
|
|
// var WebSocketServer = require('websocket').server;
|
|
|
|
var EventEmitter = require('events');
|
|
var busEmitter = new EventEmitter();
|
|
|
|
var mqttClient = require('./lib/mqtt/mqttClient');
|
|
|
|
var mqtt = new mqttClient.mqttClient(busEmitter);
|
|
|
|
var historianApi = require('./lib/historian/emulator.js');
|
|
|
|
var app = express();
|
|
|
|
var port = (process.env.VCAP_APP_PORT || 3011);
|
|
var host = (process.env.VCAP_APP_HOST || 'localhost');
|
|
|
|
var isProduction = false;
|
|
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
isProduction = true;
|
|
}
|
|
|
|
var restartTimer = 0;
|
|
|
|
logger.warn('isProduction:', isProduction);
|
|
|
|
var doRestart = () => {
|
|
mqtt = new mqttClient.mqttClient(busEmitter);
|
|
};
|
|
|
|
busEmitter.on('MAJORERROR', () => {
|
|
'use strict';
|
|
logger.warn('Major error discovered. Restarting MQTT Service in 10 seconds.');
|
|
|
|
clearTimeout(restartTimer);
|
|
restartTimer = setTimeout(doRestart, 10000);
|
|
|
|
});
|
|
|
|
|
|
var heartBeat = function() {
|
|
this.pingTimer = 0;
|
|
this.count = 0;
|
|
this.rate = 90000;
|
|
|
|
this.setupPing = function() {
|
|
logger.warn('Starting heartbeat...');
|
|
|
|
this.pingTimer = setTimeout(function() {this.ping();}.bind(this), 10000);
|
|
};
|
|
|
|
this.ping = function() {
|
|
var now = new Date;
|
|
var mod = this.rate - (now.getTime() % this.rate);
|
|
this.count++;
|
|
if (this.count > 5) {
|
|
this.count = 1;
|
|
}
|
|
|
|
var _newDots = ['.','.','.','.','.'];
|
|
_newDots[this.count - 1] = 'O';
|
|
logger.info(_newDots.join(''));
|
|
|
|
this.pingTimer = setTimeout(function() {this.ping();}.bind(this), mod + 10);
|
|
|
|
};
|
|
|
|
this.setupPing();
|
|
};
|
|
|
|
|
|
|
|
app.set('port', port);
|
|
app.set('views', __dirname + '/views');
|
|
app.set('view engine', 'ejs');
|
|
app.use(morgan('combined'));
|
|
app.use(cookieparser('your secret here'));
|
|
app.use(session({secret: '1234567890QWERTY', resave: false, saveUninitialized: false}));
|
|
|
|
app.use(sessionLW());
|
|
|
|
/* 'default', 'short', 'tiny', 'dev' */
|
|
app.use(methodoverride());
|
|
|
|
app.use(bodyparser.urlencoded({extended: false}));
|
|
|
|
// Parse application/json
|
|
app.use(bodyparser.json());
|
|
|
|
app.use(function(req, res, next) {
|
|
res.header('Access-Control-Allow-Origin', '*');
|
|
res.header('Access-Control-Allow-Headers', 'X-Requested-With');
|
|
next();
|
|
});
|
|
|
|
// Run npm start --production to use dist
|
|
var staticDir = isProduction ? 'dist' : 'app';
|
|
|
|
staticDir = 'app';
|
|
|
|
app.use(express.static(path.join(__dirname, staticDir)));
|
|
app.use(errorhandler({dumpExceptions: true, showStack: true}));
|
|
|
|
|
|
// Events and sockets
|
|
|
|
function originIsAllowed(origin) {
|
|
// Put logic here to detect whether the specified origin is allowed.
|
|
return true;
|
|
}
|
|
|
|
historianApi(app);
|
|
|
|
heartBeat();
|
|
|
|
app.get('*', function(req, res) {
|
|
res.status(404).render('404',{delimiter: '^'});
|
|
});
|
|
|
|
app.listen(port, function() {
|
|
logger.info('Express listening on ',host, port);
|
|
});
|
|
|