Skip to content

Commit

Permalink
fix(schedule): Allow 0 as lower boundary for ON-times; fix #25
Browse files Browse the repository at this point in the history
  • Loading branch information
csuermann committed Dec 30, 2020
1 parent f97b06c commit d82a8d8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 16 deletions.
27 changes: 16 additions & 11 deletions pf.js
@@ -1,7 +1,12 @@
const random = require('random')
const dayjs = require('dayjs')
let debug = (msg) => null

function createSchedule ({
function setDebugFn(debugFn) {
debug = debugFn
}

function createSchedule({
windowBegin,
windowEnd,
minDuration,
Expand Down Expand Up @@ -35,7 +40,7 @@ function createSchedule ({

const calcRandomNumber = (min, max) => random.int(min, max)

const sumDurations = blocks =>
const sumDurations = (blocks) =>
blocks.reduce((prev, curr) => prev + curr.duration, 0)

const randomCount = calcRandomCount()
Expand All @@ -50,13 +55,13 @@ function createSchedule ({
})
}

// remove as many blocks as needed to fit the time window
// remove as many on-blocks as needed to fit the time window
while (sumDurations(onBlocks) >= windowDuration) {
onBlocks.shift()
}

// add one block if none was left:
if (onBlocks.length == 0) {
if (randomCount > 0 && onBlocks.length == 0) {
onBlocks.push({
isOn: true,
duration: Math.floor(windowDuration * 0.9), // spans 90% of the window
Expand All @@ -82,7 +87,7 @@ function createSchedule ({

const sumOfRandom = offBlocks.reduce((acc, curr) => acc + curr.random, 0)

offBlocks.map(block => {
offBlocks.map((block) => {
const factor = block.random / sumOfRandom
block.duration = Math.floor(sumOffDurations * factor)
delete block.random
Expand All @@ -101,7 +106,7 @@ function createSchedule ({
return _calcScheduleTimes(schedule, windowBegin)
}

function _combineBlocks (offBlocks, onBlocks, firstBlockType) {
function _combineBlocks(offBlocks, onBlocks, firstBlockType) {
let schedule = []

const greenBlocks = firstBlockType === 'off' ? offBlocks : onBlocks
Expand All @@ -122,8 +127,8 @@ function _combineBlocks (offBlocks, onBlocks, firstBlockType) {
return schedule
}

function _calcScheduleTimes (schedule, start) {
schedule.forEach(block => {
function _calcScheduleTimes(schedule, start) {
schedule.forEach((block) => {
block.beginString = start.format('HH:mm:ss')
let end = start.add(block.duration, 'second')
block.endString = end.format('HH:mm:ss')
Expand All @@ -134,12 +139,12 @@ function _calcScheduleTimes (schedule, start) {
return schedule
}

function stripPastBlocks (blocks) {
function stripPastBlocks(blocks) {
const now = dayjs()
return blocks.filter(block => {
return blocks.filter((block) => {
let end = block.begin.add(block.duration, 'second')
return now.isBefore(end)
})
}

module.exports = { createSchedule, stripPastBlocks }
module.exports = { createSchedule, stripPastBlocks, setDebugFn }
12 changes: 7 additions & 5 deletions presence-faker.js
@@ -1,9 +1,9 @@
const { createSchedule, stripPastBlocks } = require('./pf')
const { createSchedule, stripPastBlocks, setDebugFn } = require('./pf')
const CronJob = require('cron').CronJob
const dayjs = require('dayjs')

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

const node = this
Expand All @@ -18,6 +18,8 @@ module.exports = function (RED) {
}
}

setDebugFn(debug)

const setNodeStatusForBlock = function (block) {
const text = `${block.isOn ? 'ON' : 'OFF'} [${block.beginString} - ${
block.endString
Expand Down Expand Up @@ -112,7 +114,7 @@ module.exports = function (RED) {
}

const scheduleMsgCrons = function (schedule) {
msgCrons = schedule.map(block => {
msgCrons = schedule.map((block) => {
const cron = new CronJob({
cronTime: block.begin.toDate(),
onTick: () => {
Expand Down Expand Up @@ -142,7 +144,7 @@ module.exports = function (RED) {
let deletedCronsCount = 0

if (msgCrons.length > 0) {
msgCrons.forEach(cron => {
msgCrons.forEach((cron) => {
cron.stop()
deletedCronsCount++
})
Expand All @@ -153,7 +155,7 @@ module.exports = function (RED) {
debug(`${deletedCronsCount} message crons deleted`)
}

const executeBlock = block => {
const executeBlock = (block) => {
debug('executing block: ' + JSON.stringify(block))
ejectMsg(block.isOn)
setNodeStatusForBlock(block)
Expand Down

0 comments on commit d82a8d8

Please sign in to comment.