2013-10-19 15:31:42 +02:00
|
|
|
/**
|
2014-09-07 22:56:49 +02:00
|
|
|
* Copyright 2013,2014 IBM Corp.
|
2013-10-19 15:31:42 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
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;
|
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
|
|
|
|
2014-05-17 19:32:13 +02:00
|
|
|
this.tick = setInterval(function() {
|
|
|
|
var now = new Date();
|
|
|
|
var hour = now.getUTCHours();
|
|
|
|
var mins = now.getUTCMinutes();
|
|
|
|
var times = SunCalc.getTimes(now, node.lat, node.lon);
|
|
|
|
var hour1 = times[node.start].getUTCHours();
|
|
|
|
var mins1 = times[node.start].getUTCMinutes();
|
|
|
|
var hour2 = times[node.end].getUTCHours();
|
|
|
|
var mins2 = times[node.end].getUTCMinutes();
|
|
|
|
var e1 = (hour*60+mins) - (hour1*60+mins1);
|
|
|
|
var e2 = (hour*60+mins) - (hour2*60+mins2);
|
2014-09-07 22:56:49 +02:00
|
|
|
var moon = parseInt(SunCalc.getMoonIllumination(now).fraction*100+0.5)/100;
|
2014-07-15 11:15:04 +02:00
|
|
|
var msg = { payload:0, topic:"sun", moon:moon };
|
2014-05-17 19:32:13 +02:00
|
|
|
if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
|
|
|
|
if (oldval == null) { oldval = msg.payload; }
|
2014-05-31 21:08:15 +02:00
|
|
|
if (msg.payload == 1) { node.status({fill:"yellow",shape:"dot",text:"day"}); }
|
|
|
|
else { node.status({fill:"blue",shape:"dot",text:"night"}); }
|
2014-05-17 19:32:13 +02:00
|
|
|
if (msg.payload != oldval) {
|
|
|
|
oldval = msg.payload;
|
2014-08-19 13:15:27 +02:00
|
|
|
node.send( [msg,msg] );
|
2014-05-17 19:32:13 +02:00
|
|
|
}
|
|
|
|
else { node.send(msg); }
|
|
|
|
}, 60000);
|
2013-10-19 15:31:42 +02:00
|
|
|
|
2014-05-17 19:32:13 +02:00
|
|
|
this.on("close", function() {
|
|
|
|
clearInterval(this.tick);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("sunrise",SunNode);
|
2013-10-19 15:31:42 +02:00
|
|
|
}
|