Add offsets to suncalc node.

This commit is contained in:
Dave Conway-Jones 2020-04-18 13:55:57 +01:00
parent 92adb10fb3
commit 667c7588f9
No known key found for this signature in database
GPG Key ID: 302A6725C594817F
4 changed files with 29 additions and 9 deletions

View File

@ -30,6 +30,11 @@
<option value="night">Dark enough for astronomy</option>
</select>
</div>
<div class="form-row">
<label><i class="fa fa-arrows-h"></i> Offset</label>
<span style="margin-right:4px">start</span> <input type="text" id="node-input-soff" placeholder="minutes" style='width:60px;'> mins
<span style="margin-left:14px; margin-right:4px">end</span> <input type="text" id="node-input-eoff" placeholder="minutes" style='width:60px;'> mins
</div>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
@ -39,9 +44,11 @@
<script type="text/html" data-help-name="sunrise">
<p>Uses the suncalc module to generate an output at sunrise and sunset based on a specified location.</p>
<p>Several choices of definition of sunrise and sunset are available, see the <i><a href = "https://github.com/mourner/suncalc" target="_new">suncalc</a></i> module for details.</p>
<p>The start and end times can be offset by a number of minutes before (minus) or after (plus) the chosen event time.</p>
<p>The first output emits a <code>msg.payload</code> of <i>1</i> or <i>0</i> every minute depending if in between selected times or not.
The second output emits only on the transition between night to day (<i>-> 1</i>) or day to night (<i>-> 0</i>).</p>
<p>Also sets <code>msg.topic</code> to <i>sun</i> and <code>msg.moon</code> to the fraction of the moon between 0 and 1.</p>
<p>It also outputs <code>msg.start</code>, <code>msg.end</code> and <code>msg.now</code> which are todays start and end times, with offsets applied, in ISO format, and the current ISO time.</p>
<p><code>msg.topic</code> is set to <i>sun</i>, and <code>msg.moon</code> to the fraction of the moon between 0 and 1.</p>
</script>
<script type="text/javascript">
@ -53,7 +60,9 @@
lat: {value:"", required:true, validate:RED.validators.number()},
lon: {value:"", required:true, validate:RED.validators.number()},
start: {value:"sunrise", required:true},
end: {value:"sunset", required:true}
end: {value:"sunset", required:true},
soff: {value:0, validate:RED.validators.number()},
eoff: {value:0, validate:RED.validators.number()},
},
inputs:0,
outputs:2,
@ -74,6 +83,10 @@
});
}
}
if ($("#node-input-soff").val() === "") { $("#node-input-soff").val(0) }
if ($("#node-input-eoff").val() === "") { $("#node-input-eoff").val(0) }
$("#node-input-soff").spinner({});
$("#node-input-eoff").spinner({});
}
});
</script>

View File

@ -9,6 +9,8 @@ module.exports = function(RED) {
this.lon = n.lon;
this.start = n.start;
this.end = n.end;
this.soff = (n.soff || 0) * 60000; // minutes
this.eoff = (n.eoff || 0) * 60000; // minutes
var node = this;
var oldval = null;
@ -19,12 +21,14 @@ module.exports = function(RED) {
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());
var e1 = nowMillis - startMillis;
var e2 = nowMillis - endMillis;
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);
if (isNaN(e1)) { e1 = 1; }
if (isNaN(e2)) { e2 = -1; }
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, start:s1, end:s2, now:now};
if ((e1 > 0) & (e2 < 0)) { msg.payload = 1; }
if (oldval == null) { oldval = msg.payload; }
if (msg.payload == 1) { node.status({fill:"yellow",shape:"dot",text:"day"}); }

View File

@ -6,11 +6,10 @@ A <a href="http://nodered.org" target="_new">Node-RED</a> node to provide a sign
Install
-------
Run the following command in your Node-RED user directory - typically `~/.node-red`
Either use the `Node-RED Menu - Manage Palette - Install`, or run the following command in your Node-RED user directory - typically `~/.node-red`
npm install node-red-node-suncalc
Usage
-----
@ -19,10 +18,14 @@ Uses the suncalc npm to generate an output at sunrise and sunset based on a spec
Several choices of definition of sunrise and sunset are available, see the
<i><a href = "https://github.com/mourner/suncalc" target="_new">suncalc</a></i> module for details.
The start and end times can be offset by a number of minutes before (minus) or after (plus) the chosen event time.
The node provide two outputs. The first output emits a `msg.payload` of <i>1</i> or <i>0</i> every minute
depending if day-time (1) or night-time (0).
The second output emits only on the transition between night to day (<i>-> 1</i>) or day to night (<i>-> 0</i>).
It also sets the `msg.topic` to <i>sun</i> and `msg.moon` to the fraction of the moon currently visible
It also outputs <code>msg.start</code>, <code>msg.end</code> and <code>msg.now</code> which are todays start and end times, with offsets applied, in ISO format, and the current ISO time.
The `msg.topic` is set to <i>sun</i>, and `msg.moon` to the fraction of the moon currently visible
(a value between 0 for no moon and 1 for full moon).</p>

View File

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