I modified the garagePoll event code to incorporate more logging to demonstrate what I am experiencing. I also changed gateway.js and inserted a console.log right after the db.update on line 639
var nowDate = new Date(Date.now());
var nodeUpdated = false;
if (nodeRightNow)
{
/*just emit a log the status to client(s) */
console.log('GARAGE POLL STATUS Node Id: ' + nodeRightNow._id + " Status: " + nodeRightNow.metrics['Status'].value);
if (nodeRightNow.metrics['Status'] &&
nodeRightNow.metrics['Status'].value == 'OPEN') {
console.log('Initial nodeRightNow.metrics[Status].openDate ' + nodeRightNow.metrics['Status'].openDate);
/* Only automatically close between 9pm and 6am and closeCount < 5 */
if ((nowDate.getHours() >= 9 || nowDate.getHours() <= 6) && nodeRightNow.metrics['Status'].closeCount < 10) {
if (nodeRightNow.metrics['Status'].openDate == null) {
nodeRightNow.metrics['Status'].openDate = nowDate;
nodeUpdated = true;
};
console.log('After assignment nodeRightNow.metrics[Status].openDate ' + nodeRightNow.metrics['Status'].openDate);
/* Elapsed time in minutes */
var elapsedTimeMinutes = Math.round(((nowDate - nodeRightNow.metrics['Status'].openDate)) / (60*1000));
console.log('GARAGE SHOULD BE CLOSED ' + elapsedTimeMinutes);
console.log('nodeRightNow.metrics[Status].openDate ' + nodeRightNow.metrics['Status'].openDate);
/* Close the door if open more than 10 minutes */
if (elapsedTimeMinutes >= 10) {
sendMessageToNode({nodeId:nodeRightNow._id, action:'CLS'});
nodeRightNow.metrics['Status'].closeCount++;
nodeRightNow.metrics['Status'].openDate = null;
nodeUpdated = true;
};
console.log('Close Count: ' + nodeRightNow.metrics['Status'].closeCount);
};
};
/* Reset the openDate to null when the hours are between 7am and 8pm
if (nowDate.getHours() < 9 && nowDate.getHours() > 6 && nodeRightNow.metrics['Status'].openDate) {
nodeRightNow.metrics['Status'].openDate = null;
nodeUpdated = true;
};
/* Reset the close count */
if ((nowDate.getHours() < 9 && nowDate.getHours() > 6 && nodeRightNow.metrics['Status'].closeCount > 0) || nodeRightNow.metrics['Status'].closeCount == null) {
nodeRightNow.metrics['Status'].closeCount = 0;
nodeUpdated = true;
};
if (nodeUpdated) {
db.update({ _id: nodeRightNow._id }, { $set : nodeRightNow}, {}, function (err, numReplaced) { console.log('gp: ['+nodeRightNow._id+'] DB-Updates:' + numReplaced);});
};
console.log('After save nodeRightNow.metrics[Status].openDate ' + nodeRightNow.metrics['Status'].openDate);
db.findOne({ _id : nodeAtScheduleTime._id }, function (err, findNode) {
console.log('After find findNode.metrics[Status].openDate ' + findNode.metrics['Status'].openDate);
});
};
});
} }
};
Here is the output from the garagePoll:
[2017-10-08T17:14:43.947Z] (sys) Starting
[10-08-17_12:14:51.307] [LOG] **** SCHEDULING EVENT - nodeId:11 event:garagePoll to run in ~1m
[10-08-17_12:14:51.320] [LOG] gateway dbNode.metrics[Status].openDate = null
[10-08-17_12:14:51.347] [LOG] gw: [11] DB-Updates:1
[10-08-17_12:14:54.409] [INFO] AUTHORIZING CONNECTION FROM 127.0.0.1:33644
[10-08-17_12:14:54.424] [INFO] NEW CONNECTION FROM 10.12.20.1
[10-08-17_12:15:51.311] [LOG] **** RUNNING SCHEDULED EVENT - nodeId:11 event:garagePoll...
[10-08-17_12:15:51.315] [LOG] **** SCHEDULING EVENT - nodeId:11 event:garagePoll to run in ~1m
[10-08-17_12:15:51.321] [LOG] GARAGE POLL STATUS Node Id: 11 Status: OPEN
[10-08-17_12:15:51.323] [LOG] Initial nodeRightNow.metrics[Status].openDate null
[10-08-17_12:15:51.324] [LOG] After assignment nodeRightNow.metrics[Status].openDate Sun Oct 08 2017 12:15:51 GMT-0500 (CDT)
[10-08-17_12:15:51.325] [LOG] GARAGE SHOULD BE CLOSED 0
[10-08-17_12:15:51.327] [LOG] nodeRightNow.metrics[Status].openDate Sun Oct 08 2017 12:15:51 GMT-0500 (CDT)
[10-08-17_12:15:51.328] [LOG] Close Count: 4
[10-08-17_12:15:51.330] [LOG] After save nodeRightNow.metrics[Status].openDate Sun Oct 08 2017 12:15:51 GMT-0500 (CDT)
[10-08-17_12:15:51.333] [LOG] gateway dbNode.metrics[Status].openDate = null
[10-08-17_12:15:51.344] [LOG] gp: [11] DB-Updates:1
[10-08-17_12:15:51.348] [LOG] After find findNode.metrics[Status].openDate Sun Oct 08 2017 12:15:51 GMT-0500 (CDT)
[10-08-17_12:15:51.354] [LOG] gw: [11] DB-Updates:1
[10-08-17_12:16:51.317] [LOG] **** RUNNING SCHEDULED EVENT - nodeId:11 event:garagePoll...
[10-08-17_12:16:51.319] [LOG] **** SCHEDULING EVENT - nodeId:11 event:garagePoll to run in ~1m
[10-08-17_12:16:51.332] [LOG] GARAGE POLL STATUS Node Id: 11 Status: OPEN
[10-08-17_12:16:51.334] [LOG] Initial nodeRightNow.metrics[Status].openDate null
[10-08-17_12:16:51.335] [LOG] After assignment nodeRightNow.metrics[Status].openDate Sun Oct 08 2017 12:16:51 GMT-0500 (CDT)
[10-08-17_12:16:51.338] [LOG] GARAGE SHOULD BE CLOSED 0
[10-08-17_12:16:51.340] [LOG] nodeRightNow.metrics[Status].openDate Sun Oct 08 2017 12:16:51 GMT-0500 (CDT)
[10-08-17_12:16:51.342] [LOG] Close Count: 4
[10-08-17_12:16:51.343] [LOG] After save nodeRightNow.metrics[Status].openDate Sun Oct 08 2017 12:16:51 GMT-0500 (CDT)
[10-08-17_12:16:51.346] [LOG] gateway dbNode.metrics[Status].openDate = null
[10-08-17_12:16:51.357] [LOG] gp: [11] DB-Updates:1
[10-08-17_12:16:51.360] [LOG] After find findNode.metrics[Status].openDate Sun Oct 08 2017 12:16:51 GMT-0500 (CDT)
[10-08-17_12:16:51.367] [LOG] gw: [11] DB-Updates:1
At the start of the garagePoll event the OpenDate is always null even though the openDate is successfully updated at the end of the code. closeCount is also not updated as well, but I am not showing it for clarity. gp: is the console.log from when garagePoll updates the node and gw: is the console.log from when the gateway updates the node.