”2016-09-27”

This commit is contained in:
Martin Donnelly 2016-09-27 16:45:03 +01:00
parent 61535d2df0
commit 3924f8f32b
4 changed files with 67 additions and 28 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -11,14 +11,14 @@ var requestify = require('requestify');
var db = require('../server/db-connector').dbConnection; var db = require('../server/db-connector').dbConnection;
var dbSave = require('../server/db-save')(db); var dbSave = require('../server/db-save')(db);
var emailer = require('../mailer'); var pusher = require('../pusher');
//var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984'); //var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
/* /*
var nano = require('nano')('http://localhost:5984'); var nano = require('nano')('http://localhost:5984');
var db_name = 'mqttarchive'; var db_name = 'mqttarchive';
var dbCouch = nano.use(db_name); var dbCouch = nano.use(db_name);
*/ */
let remoteHost = process.env.NODE_ENV === 'production' ? 'http://mdotserver.mybluemix.net/apiv2/message' : 'http://localhost:3011/apiv2/message'; let remoteHost = process.env.NODE_ENV === 'production' ? 'http://mdotserver.mybluemix.net/apiv2/message' : 'http://localhost:3011/apiv2/message';
@ -26,10 +26,7 @@ function dataBuilder(obj) {
var now = new Date(); var now = new Date();
var newObj = { var newObj = {
device_type: 'mDot', device_type: 'mDot', evt_type: 'update', timestamp: {}, evt: {}
evt_type: 'update',
timestamp: {},
evt: {}
}; };
newObj.device_id = obj.topic.split('/')[4]; newObj.device_id = obj.topic.split('/')[4];
@ -55,7 +52,6 @@ function saveToDB(data) {
var doSendMessage = (obj) => { var doSendMessage = (obj) => {
'use strict'; 'use strict';
requestify.post(remoteHost, obj) requestify.post(remoteHost, obj)
.then(function(response) { .then(function(response) {
// Get the response body // Get the response body
@ -74,8 +70,8 @@ var doInsertEntry = (obj) => {
dbSave.addNewEvent(obj) dbSave.addNewEvent(obj)
.then(function(d) { .then(function(d) {
'use strict'; 'use strict';
// logger.info('Obj', obj.type); // logger.info('Obj', obj.type);
// saveToDB(obj); // saveToDB(obj);
if (obj.type === 'mDot') { if (obj.type === 'mDot') {
busEmitter.emit('sendMessage', obj); busEmitter.emit('sendMessage', obj);
} }
@ -87,14 +83,19 @@ var doInsertEntry = (obj) => {
}; };
var lastReceived; var lastReceived;
var watchDog = 0; var watchDog = 0;
var watchDogHie = 0;
var wdTimedOut = false; var wdTimedOut = false;
var watchDogTimeout = () => { var watchDogTimeout = () => {
'use strict'; 'use strict';
var now = new Date(); var now = new Date();
logger.warn( logger.warn(
'Watchdog timeout. Message has not been received for over 90 seconds.'); 'Watchdog timeout. Message has not been received for over 20 minutes.');
emailer.sendEmailV1('Watchdog timeout. Message has not been received for over 90 seconds. \n\nLast received:' + lastReceived + 'Now:' + now); logger.warn('Last received:' + lastReceived + 'Now:' + now);
pusher.push(
'Watchdog timeout. Message has not been received for over 20 Minutes. \n\nLast received:' + lastReceived + 'Now:' + now);
wdTimedOut = true; wdTimedOut = true;
}; };
@ -126,17 +127,19 @@ var mqttClient = function() {
this.client = mqtt.connect('mqtt://' + orgId + address, options); this.client = mqtt.connect('mqtt://' + orgId + address, options);
this.client.on('connect', function() { this.client.on('connect', function() {
// connected = true; // connected = true;
logger.info('Connected to ', address); logger.info('Connected to ', address);
emailer.sendEmailV1('Connected to ' + address);
pusher.push('Connected to ' + address);
clearTimeout(watchDog);
watchDog = setTimeout(watchDogTimeout, 1200000);
lastReceived = new Date();
}.bind(this)); }.bind(this));
this.client.on('connected', function() { this.client.on('connected', function() {
logger.debug('mqttConnect - doConnection - Connected'); logger.debug('mqttConnect - doConnection - Connected');
watchDog = setTimeout(watchDogTimeout, 90000);
console.log(watchDog);
}); });
this.client.on('close', function() { this.client.on('close', function() {
logger.warn('mqttConnect - Connection closed'); logger.warn('mqttConnect - Connection closed');
@ -145,7 +148,7 @@ var mqttClient = function() {
this.client.on('offline', function() { this.client.on('offline', function() {
logger.warn('mqttConnect - OFFLINE!'); logger.warn('mqttConnect - OFFLINE!');
emailer.sendEmailV1('mqttConnect - OFFLINE!');
}); });
this.client.on('error', function(e) { this.client.on('error', function(e) {
@ -174,14 +177,23 @@ var mqttClient = function() {
json.device = topicArray[4]; json.device = topicArray[4];
json.event = topicArray[6]; json.event = topicArray[6];
busEmitter.emit('saveData', json); //busEmitter.emit('saveData', json);
clearTimeout(watchDog); //console.log(json.type);
watchDog = setTimeout(watchDogTimeout, 90000); if (json.type !== 'Ti-CC3200') {
lastReceived = new Date(); console.log(json);
if (wdTimedOut) { clearTimeout(watchDog);
emailer.sendEmailV1('Receiving again: ' + message.toString()); watchDog = setTimeout(watchDogTimeout, 1200000);
wdTimedOut = false; if (wdTimedOut) {
var now = new Date();
var s = 'Receiving again :::\n\nLast received: ' + lastReceived + 'Now: ' + now + "\n\n" + message.toString();
logger.warn(s);
pusher.push(s);
wdTimedOut = false;
}
lastReceived = new Date();
} }
count++; count++;
}.bind(this)); }.bind(this));

View File

@ -0,0 +1,26 @@
/**
*
* User: Martin Donnelly
* Date: 2016-04-08
* Time: 16:35
*
*/
var Pushover = require('node-pushover'), dateFormat = require('dateformat');
var push = new Pushover({
token: "am7tuw221casnhf7uryx8dhxw6zg1t",
user: "BE2vgFxdHJw91lVGMRYvZDDmVa5cCM"
});
var logger = require('log4js').getLogger();
const prefix = process.env.NODE_ENV === 'production' ? 'Production' : 'Dev';
const title = 'MQTT ' + prefix + ' Archiver Alert';
module.exports = {
push : function(contents) {
var now = new Date();
var msg = 'Alert logged at ' + dateFormat(now, "dddd, mmmm dS, yyyy, HH:MM:ss") + '\n\n' + contents + '';
push.send(title, msg);
}
};

View File

@ -28,6 +28,7 @@
"morgan": "^1.7.0", "morgan": "^1.7.0",
"mqtt": "^1.10.0", "mqtt": "^1.10.0",
"mqtt_over_websockets": "0.0.1", "mqtt_over_websockets": "0.0.1",
"node-pushover": "^0.2.2",
"path": "^0.12.7", "path": "^0.12.7",
"pg-promise": "^5.2.7", "pg-promise": "^5.2.7",
"queue": "^4.0.0", "queue": "^4.0.0",
@ -83,6 +84,6 @@
"author": "Martin Donnelly <martind2000@gmail.com>", "author": "Martin Donnelly <martind2000@gmail.com>",
"license": "ISC", "license": "ISC",
"engines": { "engines": {
"node": "6.0.0" "node": "6.2.1"
} }
} }