Skip to content

Commit

Permalink
website/alerts: remove loopIndex from alertInfo so it is strictly a d…
Browse files Browse the repository at this point in the history
…ictionary of devices info to fix bug
  • Loading branch information
Dave Lahr committed Feb 19, 2017
1 parent c3c291d commit 9bf7b69
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
22 changes: 10 additions & 12 deletions website/alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,20 @@ const consoleLogAlertInfo = function(alertInfo) {
//checksFunction as checkForAlerts function in this module
//sleepTime in milliseconds
//
exports.alertLoop = function(alertInfo, alertsConfig, sendAlertFunction, checksFunction, sleepTime) {
exports.alertLoop = function(alertInfo, alertsConfig, sendAlertFunction, checksFunction, sleepTime, loopIndex) {
console.log('alerts alertLoop');
consoleLogAlertInfo(alertInfo);

if (!('loopIndex' in alertInfo)) {
alertInfo.loopIndex = 0;
}
console.log('alertInfo.loopIndex: ' + alertInfo.loopIndex);
console.log('loopIndex: ' + loopIndex);

checksFunction(alertInfo, sendAlertFunction, alertsConfig);

//this if check is for testing purposes so the function can be called without
//going into the infinite loop
if (!('numLoopIterations' in alertInfo) || alertInfo.loopIndex < alertInfo.numLoopIterations) {
alertInfo.loopIndex++;
loopIndex++;

setTimeout(function() {
exports.alertLoop(alertInfo, alertsConfig, sendAlertFunction, checksFunction, sleepTime);
exports.alertLoop(alertInfo, alertsConfig, sendAlertFunction, checksFunction, sleepTime, loopIndex);
},
sleepTime);
}
Expand All @@ -52,8 +48,10 @@ exports.alertLoop = function(alertInfo, alertsConfig, sendAlertFunction, checksF
exports.buildInitialAlertInfo = function(db, callback) {
database.getDeviceMetadata(db, function(deviceMetadata) {
database.getAlertSettings(db, function(alertSettings) {
for (var id in deviceMetadata) {
var curDevice = deviceMetadata[id];
var alertInfo = deviceMetadata;

for (var id in alertInfo) {
var curDevice = alertInfo[id];
curDevice.lastTime = null;
curDevice.lastTemperature = null;

Expand All @@ -66,7 +64,7 @@ exports.buildInitialAlertInfo = function(db, callback) {
}
}

callback(deviceMetadata);
callback(alertInfo);
});
});
};
Expand Down Expand Up @@ -150,7 +148,7 @@ exports.checkForNoCommunication = function(deviceAlertInfo, alertsConfig) {
exports.checkForTemperatureAlert = function(deviceAlertInfo) {
console.log('alerts checkForTemperatureAlert');
console.log('deviceAlertInfo: ' + JSON.stringify(deviceAlertInfo));

var msg = '';
if ('alertSettings' in deviceAlertInfo) {
var alertSettings = deviceAlertInfo.alertSettings;
Expand Down
4 changes: 2 additions & 2 deletions website/config/default.json5
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
gmailPassword: 'CHANGE ME',
emailAddresses: 'CHANGE ME',
lastTimeOffset: 1800,
sleepTime: 300000,
initialDelay: 600000
sleepTime: 10000,
initialDelay: 1
},

memoryBuffer:
Expand Down
2 changes: 1 addition & 1 deletion website/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ measurementBuffer.buildAndPopulateBuffer(dbConfig.databaseCodeDirectory, dbConfi
alerts.buildInitialAlertInfo(db, function(alertInfo) {
setTimeout(function() {
alerts.alertLoop(alertInfo, alertsConfig, alerts.sendAlert, alerts.checkForAlerts,
alertsConfig.sleepTime);
alertsConfig.sleepTime, 0);
}, alertsConfig.initialDelay);

http.createServer(function (request, response) {
Expand Down
17 changes: 10 additions & 7 deletions website/test_alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const testDb = new sqlite3.Database(testFileDbPath);

testCases.testBuildInitialAlertInfo = function() {
console.log('test_alerts testBuildInitialAlertInfo');

alerts.buildInitialAlertInfo(testDb, function(alertInfo) {
var N = Object.keys(alertInfo).length;
console.log('N: ' + N);
Expand Down Expand Up @@ -212,11 +212,13 @@ testCases.testCheckForAlerts = function() {
var alertsConfig = {lastTimeOffset: 1000, enableEmail: true};

var now = (new Date()).getTime() / 1000;
var alertInfo = {myFakeDevice:{
id:'myFakeDevice',
lastTime:now,
lastTemperature:50,
alertSettings:[{comparison:'<', threshold:40.0}]}
var alertInfo = {
myFakeDevice: {
id:'myFakeDevice',
lastTime:now,
lastTemperature:50,
alertSettings:[{comparison:'<', threshold:40.0}]
}
};

alerts.checkForAlerts(alertInfo, sendAlertsFunction, alertsConfig);
Expand Down Expand Up @@ -262,7 +264,8 @@ testCases.testAlertLoop = function() {
console.log('function called');
};

alerts.alertLoop({numLoopIterations:3}, null, null, checksFunction, 100);
alerts.alertLoop({numLoopIterations:3}, null, null, checksFunction, 100, 0);
};


run_tests.run(process.argv, testCases);

0 comments on commit 9bf7b69

Please sign in to comment.