fix suncalc not handling summer nights.

to Close #463
This commit is contained in:
Dave Conway-Jones 2018-07-17 10:24:40 +01:00
parent 8f83e471b8
commit 911f739005
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 10 additions and 4 deletions

View File

@ -13,7 +13,7 @@ module.exports = function(RED) {
var node = this; var node = this;
var oldval = null; var oldval = null;
this.tick = setInterval(function() { var tick = function() {
var now = new Date(); var now = new Date();
var times = SunCalc.getTimes(now, node.lat, node.lon); var times = SunCalc.getTimes(now, node.lat, node.lon);
var nowMillis = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(),now.getUTCDate(),now.getUTCHours(),now.getUTCMinutes()); var nowMillis = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(),now.getUTCDate(),now.getUTCHours(),now.getUTCMinutes());
@ -21,6 +21,8 @@ module.exports = function(RED) {
var endMillis = Date.UTC(times[node.end].getUTCFullYear(),times[node.end].getUTCMonth(),times[node.end].getUTCDate(),times[node.end].getUTCHours(),times[node.end].getUTCMinutes()); var endMillis = Date.UTC(times[node.end].getUTCFullYear(),times[node.end].getUTCMonth(),times[node.end].getUTCDate(),times[node.end].getUTCHours(),times[node.end].getUTCMinutes());
var e1 = nowMillis - startMillis; var e1 = nowMillis - startMillis;
var e2 = nowMillis - endMillis; var e2 = nowMillis - endMillis;
if (isNaN(e1)) { e1 = 1; }
if (isNaN(e2)) { e2 = -1; }
var moon = parseInt(SunCalc.getMoonIllumination(now).fraction * 100 + 0.5) / 100; var moon = parseInt(SunCalc.getMoonIllumination(now).fraction * 100 + 0.5) / 100;
var msg = {payload:0, topic:"sun", moon:moon}; var msg = {payload:0, topic:"sun", moon:moon};
if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; } if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
@ -32,10 +34,14 @@ module.exports = function(RED) {
node.send([msg,msg]); node.send([msg,msg]);
} }
else { node.send(msg); } else { node.send(msg); }
}, 60000); }
this.tick = setInterval(function() { tick(); }, 60000);
this.tock = setTimeout(function() { tick(); }, 500);
this.on("close", function() { this.on("close", function() {
clearInterval(this.tick); if (this.tock) { clearTimeout(this.tock); }
if (this.tick) { clearInterval(this.tick); }
}); });
} }
RED.nodes.registerType("sunrise",SunNode); RED.nodes.registerType("sunrise",SunNode);

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-suncalc", "name" : "node-red-node-suncalc",
"version" : "0.0.10", "version" : "0.0.11",
"description" : "A Node-RED node to provide a signal at sunrise and sunset", "description" : "A Node-RED node to provide a signal at sunrise and sunset",
"dependencies" : { "dependencies" : {
"suncalc" : "^1.8.0" "suncalc" : "^1.8.0"