2013-10-25 19:22:45 +02:00
|
|
|
|
2014-05-17 19:32:13 +02:00
|
|
|
module.exports = function(RED) {
|
2014-06-29 00:37:19 +02:00
|
|
|
"use strict";
|
2014-05-17 19:32:13 +02:00
|
|
|
var SunCalc = require('suncalc');
|
2013-10-19 15:31:42 +02:00
|
|
|
|
2014-05-17 19:32:13 +02:00
|
|
|
function SunNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.lat = n.lat;
|
|
|
|
this.lon = n.lon;
|
|
|
|
this.start = n.start;
|
|
|
|
this.end = n.end;
|
2020-04-18 14:55:57 +02:00
|
|
|
this.soff = (n.soff || 0) * 60000; // minutes
|
|
|
|
this.eoff = (n.eoff || 0) * 60000; // minutes
|
2013-10-19 15:31:42 +02:00
|
|
|
|
2014-05-17 19:32:13 +02:00
|
|
|
var node = this;
|
|
|
|
var oldval = null;
|
2013-10-19 15:31:42 +02:00
|
|
|
|
2018-07-17 11:24:40 +02:00
|
|
|
var tick = function() {
|
2014-05-17 19:32:13 +02:00
|
|
|
var now = new Date();
|
|
|
|
var times = SunCalc.getTimes(now, node.lat, node.lon);
|
2015-03-07 21:36:22 +01:00
|
|
|
var nowMillis = Date.UTC(now.getUTCFullYear(),now.getUTCMonth(),now.getUTCDate(),now.getUTCHours(),now.getUTCMinutes());
|
|
|
|
var startMillis = Date.UTC(times[node.start].getUTCFullYear(),times[node.start].getUTCMonth(),times[node.start].getUTCDate(),times[node.start].getUTCHours(),times[node.start].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());
|
2020-04-18 14:55:57 +02:00
|
|
|
var e1 = nowMillis - startMillis - node.soff;
|
|
|
|
var e2 = nowMillis - endMillis - node.eoff;
|
|
|
|
var s1 = new Date(startMillis + node.soff);
|
|
|
|
var s2 = new Date(endMillis + node.eoff);
|
2018-07-17 11:24:40 +02:00
|
|
|
if (isNaN(e1)) { e1 = 1; }
|
|
|
|
if (isNaN(e2)) { e2 = -1; }
|
2020-04-30 12:11:11 +02:00
|
|
|
var moon = SunCalc.getMoonIllumination(now);
|
|
|
|
var moon2 = SunCalc.getMoonPosition(now, node.lat, node.lon);
|
|
|
|
moon = Object.assign(moon, moon2);
|
|
|
|
moon.altitude = moon.altitude * 180 / Math.PI;
|
|
|
|
moon.azimuth = moon.azimuth * 180 / Math.PI;
|
|
|
|
moon.parallacticAngle = moon.parallacticAngle * 180 /Math.PI;
|
|
|
|
moon.icon = "new";
|
|
|
|
if (moon.phase > 0.02) { moon.icon = "wax-cres"}
|
|
|
|
if (moon.phase > 0.22) { moon.icon = "first-quart"}
|
|
|
|
if (moon.phase > 0.28) { moon.icon = "wax-gibb"}
|
|
|
|
if (moon.phase > 0.48) { moon.icon = "full"}
|
|
|
|
if (moon.phase > 0.52) { moon.icon = "wan-gibb"}
|
|
|
|
if (moon.phase > 0.72) { moon.icon = "third-quart"}
|
|
|
|
if (moon.phase > 0.78) { moon.icon = "wan-cres"}
|
|
|
|
if (moon.phase > 0.98) { moon.icon = "new"}
|
|
|
|
moon.icon = "wi-moon-" + moon.icon;
|
|
|
|
var sun = SunCalc.getPosition(now, node.lat, node.lon);
|
|
|
|
sun.altitude = sun.altitude * 180 / Math.PI;
|
|
|
|
sun.azimuth = sun.azimuth * 180 / Math.PI;
|
|
|
|
var msg = {payload:0, topic:"sun", sun:sun, moon:moon, start:s1, end:s2, now:now};
|
2014-05-17 19:32:13 +02:00
|
|
|
if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
|
|
|
|
if (oldval == null) { oldval = msg.payload; }
|
2023-03-21 11:20:23 +01:00
|
|
|
if (msg.payload == 1) { node.status({fill:"yellow",shape:"dot",text:"sunrise.dayState"}); }
|
|
|
|
else { node.status({fill:"blue",shape:"dot",text:"sunrise.nightState"}); }
|
2014-05-17 19:32:13 +02:00
|
|
|
if (msg.payload != oldval) {
|
|
|
|
oldval = msg.payload;
|
2016-02-15 12:12:29 +01:00
|
|
|
node.send([msg,msg]);
|
2014-05-17 19:32:13 +02:00
|
|
|
}
|
|
|
|
else { node.send(msg); }
|
2018-07-17 11:24:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this.tick = setInterval(function() { tick(); }, 60000);
|
|
|
|
this.tock = setTimeout(function() { tick(); }, 500);
|
2013-10-19 15:31:42 +02:00
|
|
|
|
2014-05-17 19:32:13 +02:00
|
|
|
this.on("close", function() {
|
2018-07-17 11:24:40 +02:00
|
|
|
if (this.tock) { clearTimeout(this.tock); }
|
|
|
|
if (this.tick) { clearInterval(this.tick); }
|
2014-05-17 19:32:13 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("sunrise",SunNode);
|
2016-02-15 12:12:29 +01:00
|
|
|
};
|