node-red-contrib-suncron
Advanced tools
Comparing version 1.2.2 to 1.3.2
{ | ||
"name": "node-red-contrib-suncron", | ||
"version": "1.2.2", | ||
"version": "1.3.2", | ||
"description": "A Node-RED node that triggers configurable outgoing messages based on the position of the sun", | ||
@@ -49,2 +49,2 @@ "main": "index.js", | ||
} | ||
} | ||
} |
@@ -30,2 +30,23 @@ # SunCron | ||
The offsets can be overwritten at runtime by passing a config object as `msg.payload` to the suncron node. All attributes are optional. Offsets need to be expressed in seconds as positive or negative integer values. Below example adjusts the offset for the `dusk` event to -2 minutes and all others to 0 seconds. | ||
``` | ||
{ | ||
"sunrise": 0, | ||
"sunriseEnd": 0, | ||
"goldenHourEnd": 0, | ||
"solarNoon": 0, | ||
"goldenHour": 0, | ||
"sunsetStart": 0, | ||
"sunset": 0, | ||
"dusk": -120, | ||
"nauticalDusk": 0, | ||
"night": 0, | ||
"nadir": 0, | ||
"nightEnd": 0, | ||
"nauticalDawn": 0, | ||
"dawn": 0 | ||
} | ||
``` | ||
## Outgoing messages | ||
@@ -32,0 +53,0 @@ |
100
suncron.js
@@ -5,4 +5,4 @@ const CronJob = require('cron').CronJob | ||
module.exports = function(RED) { | ||
function SuncronNode(config) { | ||
module.exports = function (RED) { | ||
function SuncronNode (config) { | ||
RED.nodes.createNode(this, config) | ||
@@ -32,3 +32,24 @@ | ||
const calcScheduleForToday = function() { | ||
const letsGo = function () { | ||
const schedule = calcScheduleForToday() | ||
if (config.replay === true) { | ||
try { | ||
const mostRecentEvent = findMostRecentEvent(schedule) | ||
setTimeout(() => { | ||
ejectMsg(mostRecentEvent, schedule) | ||
}, 500) | ||
} catch (e) { | ||
debug(e) | ||
} | ||
} | ||
installMsgCronjobs(schedule) | ||
debug(schedule) | ||
dailyCron = installDailyCronjob() | ||
} | ||
const calcScheduleForToday = function () { | ||
const today = new Date() | ||
@@ -82,3 +103,3 @@ const midday = new Date( | ||
const formatSchedule = function(schedule) { | ||
const formatSchedule = function (schedule) { | ||
let result = {} | ||
@@ -97,3 +118,3 @@ for (let eventType in schedule) { | ||
const installMsgCronjobs = function(schedule) { | ||
const installMsgCronjobs = function (schedule) { | ||
stopMsgCrons() | ||
@@ -142,3 +163,3 @@ | ||
const installDailyCronjob = function() { | ||
const installDailyCronjob = function () { | ||
// run daily cron 5 seconds past midnight (except for debugging: 5 seconds past the full minute) | ||
@@ -161,3 +182,3 @@ const cronTime = RED.settings.suncronMockTimes | ||
const debug = function(debugMsg) { | ||
const debug = function (debugMsg) { | ||
if (RED.settings.suncronDebug) { | ||
@@ -168,3 +189,3 @@ node.warn(debugMsg) | ||
const setNodeStatus = function(text, color = 'grey') { | ||
const setNodeStatus = function (text, color = 'grey') { | ||
node.status({ | ||
@@ -177,4 +198,4 @@ fill: color, | ||
const setNodeStatusToNextEvent = function(schedule) { | ||
function findNextEvent(schedule) { | ||
const setNodeStatusToNextEvent = function (schedule) { | ||
function findNextEvent (schedule) { | ||
let futureEvents = Object.keys(schedule) | ||
@@ -207,3 +228,3 @@ .map(eventType => ({ | ||
const findMostRecentEvent = function(schedule) { | ||
const findMostRecentEvent = function (schedule) { | ||
let pastEvents = Object.keys(schedule) | ||
@@ -221,3 +242,3 @@ .map(eventType => schedule[eventType]) | ||
const stopMsgCrons = function() { | ||
const stopMsgCrons = function () { | ||
if (msgCrons.length > 0) { | ||
@@ -233,3 +254,3 @@ msgCrons.forEach(cron => { | ||
const ejectMsg = function({ payload, payloadType, topic }, schedule) { | ||
const ejectMsg = function ({ payload, payloadType, topic }, schedule) { | ||
const castPayload = (payload, payloadType) => { | ||
@@ -254,27 +275,40 @@ if (payloadType === 'num') { | ||
node.on('close', function() { | ||
stopMsgCrons() | ||
dailyCron.stop() | ||
}) | ||
;(function() { | ||
// on startup: | ||
node.on('input', function (msg, send, done) { | ||
send = | ||
send || | ||
function () { | ||
node.send.apply(node, arguments) | ||
} | ||
if (typeof msg.payload === 'object') { | ||
// config object received as msg.payload | ||
debug(`!!!! CONFIG OBJECT RECEIVED !!!`) | ||
// ebug(msg.payload) | ||
const schedule = calcScheduleForToday() | ||
eventTypes.forEach(eventType => { | ||
if ( | ||
msg.payload.hasOwnProperty(eventType) && | ||
Number.isInteger(msg.payload[eventType]) | ||
) { | ||
debug(`new offset for ${eventType}: ${msg.payload[eventType]}`) | ||
config[`${eventType}Offset`] = Math.abs(msg.payload[eventType]) | ||
config[`${eventType}OffsetType`] = | ||
msg.payload[eventType] < 0 ? -1 : 1 | ||
} | ||
}) | ||
if (config.replay === true) { | ||
try { | ||
const mostRecentEvent = findMostRecentEvent(schedule) | ||
letsGo() | ||
} | ||
setTimeout(() => { | ||
ejectMsg(mostRecentEvent, schedule) | ||
}, 500) | ||
} catch (e) { | ||
debug(e) | ||
} | ||
if (done) { | ||
done() | ||
} | ||
}) | ||
installMsgCronjobs(schedule) | ||
debug(schedule) | ||
dailyCron = installDailyCronjob() | ||
node.on('close', function () { | ||
stopMsgCrons() | ||
dailyCron.stop() | ||
}) | ||
;(function () { | ||
// on startup: | ||
letsGo() | ||
})() | ||
@@ -281,0 +315,0 @@ } |
Sorry, the diff of this file is not supported yet
198554
396
91