From a4c649ade71c482db6ec12b19d90865849df3265 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Sat, 19 Oct 2013 14:31:42 +0100 Subject: [PATCH] New sunrise/sunset node based on suncalc npm... feedback please on utility/outputs etc. --- time/79-suncalc.html | 83 ++++++++++++++++++++++++++++++++++++++++++++ time/79-suncalc.js | 55 +++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 time/79-suncalc.html create mode 100644 time/79-suncalc.js diff --git a/time/79-suncalc.html b/time/79-suncalc.html new file mode 100644 index 00000000..92082d1d --- /dev/null +++ b/time/79-suncalc.html @@ -0,0 +1,83 @@ + + + + + + + diff --git a/time/79-suncalc.js b/time/79-suncalc.js new file mode 100644 index 00000000..e44414d9 --- /dev/null +++ b/time/79-suncalc.js @@ -0,0 +1,55 @@ +/** + * Copyright 2013 IBM Corp. + * + * 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. + **/ +var RED = require("../../red/red"); +var SunCalc = require('suncalc'); + +function SunNode(n) { + RED.nodes.createNode(this,n); + this.lat = n.lat; + this.lon = n.lon; + this.start = n.start; + this.end = n.end; + + var node = this; + var oldval = null; + + this.tick = setInterval(function() { + var now = new Date(); + var hour = now.getHours(); + var mins = now.getMinutes(); + var times = SunCalc.getTimes(now, node.lat, node.lon); + var hour1 = times[node.start].getHours(); + var mins1 = times[node.start].getMinutes(); + var hour2 = times[node.end].getHours(); + var mins2 = times[node.end].getMinutes(); + var e1 = (hour*60+mins) - (hour1*60+mins1); + var e2 = (hour*60+mins) - (hour2*60+mins2); + var moon = parseInt(SunCalc.getMoonFraction(now)*100)/100; + msg = { payload:0, topic:"sun", moon:moon }; + if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; } + if (msg.payload != oldval) { + oldval = msg.payload; + msg2 = msg; + node.send( [msg,msg2] ); + } + else { node.send(msg); } + }, 60000); + + this.on("close", function() { + clearInterval(this.tick); + }); +} +RED.nodes.registerType("sunrise",SunNode);