Skip to content

Commit

Permalink
feat(suncron): Emit preceding sun event upon receiving non-object pay…
Browse files Browse the repository at this point in the history
…load; fixes #20
  • Loading branch information
csuermann committed Jun 4, 2021
1 parent b2d56ea commit 86ec646
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The location (lat / lon) can either be entered manually or retrieved automatical

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 (-120 seconds) and all others to 0 seconds.

```
```javascript
{
"sunrise": 0,
"sunriseEnd": 0,
Expand All @@ -52,7 +52,7 @@ The offsets can be overwritten at runtime by passing a config object as `msg.pay

### Sun events

The node will emit messages at the specified sun events (respecting offsets), which will have a `msg.payload` and `msg.topic` as configured in the editor. Those messages also have a `schedule` attribute containing an object with details about the schedule of the current date. The schedule contains only events which have been configured with a payload.
The node will emit messages at the specified sun events (respecting offsets) or when it receives an inbound msg with a non-object paylod, e.g. `true`. Outbound messages will have a `msg.payload` and `msg.topic` as configured in the editor. Those messages also have a `schedule` attribute containing an object with details about the schedule of the current date. The schedule contains only events which have been configured with a payload.

Each event has the following attributes:

Expand Down
31 changes: 20 additions & 11 deletions suncron.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const dayjs = require('dayjs')
const SunCalc = require('suncalc')

module.exports = function (RED) {
function SuncronNode (config) {
function SuncronNode(config) {
RED.nodes.createNode(this, config)

const node = this

let schedule

const eventTypes = [
'sunrise',
'sunriseEnd',
Expand All @@ -29,7 +31,7 @@ module.exports = function (RED) {
let dailyCron = []

const letsGo = function () {
const schedule = calcScheduleForToday()
schedule = calcScheduleForToday()

if (config.replay === true) {
try {
Expand Down Expand Up @@ -175,7 +177,7 @@ module.exports = function (RED) {
const cron = new CronJob({
cronTime,
onTick: () => {
const schedule = calcScheduleForToday()
schedule = calcScheduleForToday()
installMsgCronjobs(schedule)
ejectSchedule(schedule)
setNodeStatusToNextEvent(schedule)
Expand All @@ -200,14 +202,14 @@ module.exports = function (RED) {
}

const setNodeStatusToNextEvent = function (schedule) {
function findNextEvent (schedule) {
function findNextEvent(schedule) {
let futureEvents = Object.keys(schedule)
.map(eventType => ({
.map((eventType) => ({
eventName: eventType,
eventTime: schedule[eventType].cronTime,
}))
.sort((e1, e2) => e1.eventTime.unix() - e2.eventTime.unix())
.filter(event => event.eventTime.isAfter(dayjs()))
.filter((event) => event.eventTime.isAfter(dayjs()))

if (futureEvents.length > 0) {
return futureEvents.shift()
Expand All @@ -230,9 +232,9 @@ module.exports = function (RED) {

const findMostRecentEvent = function (schedule) {
let pastEvents = Object.keys(schedule)
.map(eventType => schedule[eventType])
.map((eventType) => schedule[eventType])
.sort((e1, e2) => e2.cronTime.unix() - e1.cronTime.unix())
.filter(event => event.cronTime.isBefore(dayjs()))
.filter((event) => event.cronTime.isBefore(dayjs()))

if (pastEvents.length > 0) {
return pastEvents.shift()
Expand All @@ -243,7 +245,7 @@ module.exports = function (RED) {

const stopMsgCrons = function () {
if (msgCrons.length > 0) {
msgCrons.forEach(cron => {
msgCrons.forEach((cron) => {
cron.stop()
})

Expand Down Expand Up @@ -291,9 +293,9 @@ module.exports = function (RED) {
if (typeof msg.payload === 'object') {
// config object received as msg.payload
debug(`!!!! CONFIG OBJECT RECEIVED !!!`)
// ebug(msg.payload)
// debug(msg.payload)

eventTypes.forEach(eventType => {
eventTypes.forEach((eventType) => {
if (
msg.payload.hasOwnProperty(eventType) &&
Number.isInteger(msg.payload[eventType])
Expand All @@ -306,6 +308,13 @@ module.exports = function (RED) {
})

letsGo()
} else {
try {
const mostRecentEvent = findMostRecentEvent(schedule)
ejectMsg(mostRecentEvent, schedule)
} catch (e) {
debug(e)
}
}

if (done) {
Expand Down

0 comments on commit 86ec646

Please sign in to comment.