mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
add some status to suncalc (to see how it looks)
This commit is contained in:
parent
191f4412f6
commit
9adc2b2687
@ -14,44 +14,47 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
**/
|
**/
|
||||||
|
|
||||||
var RED = require(process.env.NODE_RED_HOME+"/red/red");
|
module.exports = function(RED) {
|
||||||
var SunCalc = require('suncalc');
|
var SunCalc = require('suncalc');
|
||||||
|
|
||||||
function SunNode(n) {
|
function SunNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.lat = n.lat;
|
this.lat = n.lat;
|
||||||
this.lon = n.lon;
|
this.lon = n.lon;
|
||||||
this.start = n.start;
|
this.start = n.start;
|
||||||
this.end = n.end;
|
this.end = n.end;
|
||||||
|
|
||||||
var node = this;
|
var node = this;
|
||||||
var oldval = null;
|
var oldval = null;
|
||||||
|
|
||||||
this.tick = setInterval(function() {
|
this.tick = setInterval(function() {
|
||||||
var now = new Date();
|
var now = new Date();
|
||||||
var hour = now.getUTCHours();
|
var hour = now.getUTCHours();
|
||||||
var mins = now.getUTCMinutes();
|
var mins = now.getUTCMinutes();
|
||||||
var times = SunCalc.getTimes(now, node.lat, node.lon);
|
var times = SunCalc.getTimes(now, node.lat, node.lon);
|
||||||
var hour1 = times[node.start].getUTCHours();
|
var hour1 = times[node.start].getUTCHours();
|
||||||
var mins1 = times[node.start].getUTCMinutes();
|
var mins1 = times[node.start].getUTCMinutes();
|
||||||
var hour2 = times[node.end].getUTCHours();
|
var hour2 = times[node.end].getUTCHours();
|
||||||
var mins2 = times[node.end].getUTCMinutes();
|
var mins2 = times[node.end].getUTCMinutes();
|
||||||
var e1 = (hour*60+mins) - (hour1*60+mins1);
|
var e1 = (hour*60+mins) - (hour1*60+mins1);
|
||||||
var e2 = (hour*60+mins) - (hour2*60+mins2);
|
var e2 = (hour*60+mins) - (hour2*60+mins2);
|
||||||
var moon = SunCalc.getMoonIllumination(now).fraction;
|
var moon = SunCalc.getMoonIllumination(now).fraction;
|
||||||
msg = { payload:0, topic:"sun", moon:moon };
|
msg = { payload:0, topic:"sun", moon:moon };
|
||||||
if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
|
if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
|
||||||
if (oldval == null) { oldval = msg.payload; }
|
if (oldval == null) { oldval = msg.payload; }
|
||||||
if (msg.payload != oldval) {
|
if (msg.payload == 1) { node.status({fill:"yellow",shape:"dot",text:"day"},true); }
|
||||||
oldval = msg.payload;
|
else { node.status({fill:"blue",shape:"dot",text:"night"},true); }
|
||||||
msg2 = msg;
|
if (msg.payload != oldval) {
|
||||||
node.send( [msg,msg2] );
|
oldval = msg.payload;
|
||||||
}
|
msg2 = msg;
|
||||||
else { node.send(msg); }
|
node.send( [msg,msg2] );
|
||||||
}, 60000);
|
}
|
||||||
|
else { node.send(msg); }
|
||||||
|
}, 60000);
|
||||||
|
|
||||||
this.on("close", function() {
|
this.on("close", function() {
|
||||||
clearInterval(this.tick);
|
clearInterval(this.tick);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
RED.nodes.registerType("sunrise",SunNode);
|
||||||
}
|
}
|
||||||
RED.nodes.registerType("sunrise",SunNode);
|
|
||||||
|
Loading…
Reference in New Issue
Block a user