diff --git a/.DS_Store b/.DS_Store index 28e428e..1f2a88e 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js b/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js index 76d8013..5b8140e 100644 --- a/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js +++ b/mdot/mqttArchiver/mqttArchiver/lib/mqtt/mqttClient.js @@ -11,14 +11,14 @@ var requestify = require('requestify'); var db = require('../server/db-connector').dbConnection; 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://localhost:5984'); -var db_name = 'mqttarchive'; -var dbCouch = nano.use(db_name); -*/ + var nano = require('nano')('http://localhost:5984'); + var db_name = 'mqttarchive'; + var dbCouch = nano.use(db_name); + */ 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 newObj = { - device_type: 'mDot', - evt_type: 'update', - timestamp: {}, - evt: {} + device_type: 'mDot', evt_type: 'update', timestamp: {}, evt: {} }; newObj.device_id = obj.topic.split('/')[4]; @@ -55,7 +52,6 @@ function saveToDB(data) { var doSendMessage = (obj) => { 'use strict'; - requestify.post(remoteHost, obj) .then(function(response) { // Get the response body @@ -74,8 +70,8 @@ var doInsertEntry = (obj) => { dbSave.addNewEvent(obj) .then(function(d) { 'use strict'; - // logger.info('Obj', obj.type); - // saveToDB(obj); + // logger.info('Obj', obj.type); + // saveToDB(obj); if (obj.type === 'mDot') { busEmitter.emit('sendMessage', obj); } @@ -87,14 +83,19 @@ var doInsertEntry = (obj) => { }; var lastReceived; + var watchDog = 0; +var watchDogHie = 0; var wdTimedOut = false; var watchDogTimeout = () => { 'use strict'; var now = new Date(); logger.warn( - 'Watchdog timeout. Message has not been received for over 90 seconds.'); - emailer.sendEmailV1('Watchdog timeout. Message has not been received for over 90 seconds. \n\nLast received:' + lastReceived + 'Now:' + now); + 'Watchdog timeout. Message has not been received for over 20 minutes.'); + 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; }; @@ -126,17 +127,19 @@ var mqttClient = function() { this.client = mqtt.connect('mqtt://' + orgId + address, options); this.client.on('connect', function() { - // connected = true; + // connected = true; 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)); this.client.on('connected', function() { logger.debug('mqttConnect - doConnection - Connected'); - watchDog = setTimeout(watchDogTimeout, 90000); - console.log(watchDog); - }); this.client.on('close', function() { logger.warn('mqttConnect - Connection closed'); @@ -145,7 +148,7 @@ var mqttClient = function() { this.client.on('offline', function() { logger.warn('mqttConnect - OFFLINE!'); - emailer.sendEmailV1('mqttConnect - OFFLINE!'); + }); this.client.on('error', function(e) { @@ -174,14 +177,23 @@ var mqttClient = function() { json.device = topicArray[4]; json.event = topicArray[6]; - busEmitter.emit('saveData', json); - clearTimeout(watchDog); - watchDog = setTimeout(watchDogTimeout, 90000); - lastReceived = new Date(); - if (wdTimedOut) { - emailer.sendEmailV1('Receiving again: ' + message.toString()); - wdTimedOut = false; + //busEmitter.emit('saveData', json); + //console.log(json.type); + if (json.type !== 'Ti-CC3200') { + console.log(json); + clearTimeout(watchDog); + watchDog = setTimeout(watchDogTimeout, 1200000); + 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++; }.bind(this)); diff --git a/mdot/mqttArchiver/mqttArchiver/lib/pusher.js b/mdot/mqttArchiver/mqttArchiver/lib/pusher.js new file mode 100644 index 0000000..c3a0177 --- /dev/null +++ b/mdot/mqttArchiver/mqttArchiver/lib/pusher.js @@ -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); + } +}; diff --git a/mdot/mqttArchiver/mqttArchiver/package.json b/mdot/mqttArchiver/mqttArchiver/package.json index ca77843..05cdf1f 100644 --- a/mdot/mqttArchiver/mqttArchiver/package.json +++ b/mdot/mqttArchiver/mqttArchiver/package.json @@ -28,6 +28,7 @@ "morgan": "^1.7.0", "mqtt": "^1.10.0", "mqtt_over_websockets": "0.0.1", + "node-pushover": "^0.2.2", "path": "^0.12.7", "pg-promise": "^5.2.7", "queue": "^4.0.0", @@ -83,6 +84,6 @@ "author": "Martin Donnelly ", "license": "ISC", "engines": { - "node": "6.0.0" + "node": "6.2.1" } }