New sunrise/sunset node based on suncalc npm... feedback please on utility/outputs etc.

This commit is contained in:
Dave C-J 2013-10-19 14:31:42 +01:00
parent 8b6dd4271b
commit a4c649ade7
2 changed files with 138 additions and 0 deletions

83
time/79-suncalc.html Normal file
View File

@ -0,0 +1,83 @@
<!--
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.
-->
<script type="text/x-red" data-template-name="sunrise">
<div class="form-row">
<label for="node-input-lat"><i class="icon-globe"></i> Latitude</label>
<input type="text" id="node-input-lat" placeholder="51.025">
</div>
<div class="form-row">
<label for="node-input-lon"><i class="icon-globe"></i> Longitude</label>
<input type="text" id="node-input-lon" placeholder="-1.4">
</div>
<div class="form-row">
<label for="node-input-start"><i class="icon-time"></i> Start</label>
<select id="node-input-start" style='width:70%'>
<option value="sunrise">Sunrise</option>
<option value="sunriseEnd">Sunrise end</option>
<option value="dawn">Dawn, morning civil twilight starts</option>
<option value="goldenHourEnd">End of morning golden hour</option>
<option value="nauticalDawn">Morning nautical twilight starts</option>
<option value="nightEnd">Morning astronomical twilight starts</option>
</select>
</div>
<div class="form-row">
<label for="node-input-end"><i class="icon-time"></i> End</label>
<select id="node-input-end" style='width:70%'>
<option value="sunset">Sunset, civil twilight starts</option>
<option value="sunsetStart">Sunset start</option>
<option value="goldenHour">Start of evening golden hour</option>
<option value="dusk">Dusk, Evening astronomical twilight starts</option>
<option value="nauticalDusk">Evening nautical twilight starts</option>
<option value="night">Dark enough for astronomy</option>
</select>
</div>
<div class="form-row">
<label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
</script>
<script type="text/x-red" 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 first output emits a <b>msg.payload</b> of <i>1</i> or <i>0</i> every minute depending if 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>), and also once at the (re)start of the flow.</p>
<p>Also sets <b>msg.topic</b> to <i>sun</i> and <b>msg.moon</b> to the fraction of the moon between 0 and 1.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('sunrise',{
category: 'advanced-input',
color:"#ffcc66",
defaults: {
name: {value:""},
lat: {value:"", required:true, validate:RED.validators.number()},
lon: {value:"", required:true, validate:RED.validators.number()},
start: {value:"", required:true},
end: {value:"", required:true}
},
inputs:0,
outputs:2,
icon: "sun.png",
label: function() {
return this.name||"Sun rise/set";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

55
time/79-suncalc.js Normal file
View File

@ -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);