moving from IBM to Amazon

This commit is contained in:
Martin Donnelly 2016-09-29 11:32:18 +01:00
parent cc5d261f27
commit 224e847413
5 changed files with 68 additions and 32 deletions

2
app.js
View File

@ -28,7 +28,7 @@ var historianApi = require('./lib/historian/emulator.js');
var app = express();
var port = (process.env.VCAP_APP_PORT || 3010);
var port = (process.env.VCAP_APP_PORT || 3011);
var host = (process.env.VCAP_APP_HOST || 'localhost');
var isProduction = false;

View File

@ -11,25 +11,23 @@ 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';
//let remoteHost = process.env.NODE_ENV === 'production' ? 'http://mdotserver.mybluemix.net/apiv2/message' : 'http://localhost:3011/apiv2/message';
const remoteHost = process.env.NODE_ENV === 'production' ? 'http://ec2-52-211-111-57.eu-west-1.compute.amazonaws.com/apiv2/message' : 'http://localhost:3011/apiv2/message';
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 +53,6 @@ function saveToDB(data) {
var doSendMessage = (obj) => {
'use strict';
requestify.post(remoteHost, obj)
.then(function(response) {
// Get the response body
@ -74,8 +71,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,15 +84,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 20 minutes.');
logger.warn('Last received:' + lastReceived + 'Now:' + now);
emailer.sendEmailV1('Watchdog timeout. Message has not been received for over 20 Minutes. \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;
};
@ -127,17 +128,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, 1200000);
console.log(watchDog);
});
this.client.on('close', function() {
logger.warn('mqttConnect - Connection closed');
@ -146,7 +149,7 @@ var mqttClient = function() {
this.client.on('offline', function() {
logger.warn('mqttConnect - OFFLINE!');
emailer.sendEmailV1('mqttConnect - OFFLINE!');
});
this.client.on('error', function(e) {
@ -175,16 +178,23 @@ var mqttClient = function() {
json.device = topicArray[4];
json.event = topicArray[6];
busEmitter.emit('saveData', json);
clearTimeout(watchDog);
watchDog = setTimeout(watchDogTimeout, 1200000);
lastReceived = new Date();
console.log('wdTimedOut',wdTimedOut);
if (wdTimedOut) {
//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();
emailer.sendEmailV1('Receiving again: ' + message.toString());
wdTimedOut = false;
}
count++;
}.bind(this));

26
lib/pusher.js Normal file
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

@ -27,7 +27,6 @@ var remoteCN = {
password: 'PQUYLiIW4M6r7SWyZevrES_rRAULYFkp'
};
const cn = process.env.NODE_ENV === 'production' ? remoteCN : localCN;

View File

@ -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 <martind2000@gmail.com>",
"license": "ISC",
"engines": {
"node": "6.0.0"
"node": "6.2.1"
}
}