diff --git a/pf.js b/pf.js index 85b35bf..d25332c 100644 --- a/pf.js +++ b/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, @@ -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() @@ -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 @@ -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 @@ -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 @@ -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') @@ -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 } diff --git a/presence-faker.js b/presence-faker.js index fdf2a7c..6edfb92 100644 --- a/presence-faker.js +++ b/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 @@ -18,6 +18,8 @@ module.exports = function (RED) { } } + setDebugFn(debug) + const setNodeStatusForBlock = function (block) { const text = `${block.isOn ? 'ON' : 'OFF'} [${block.beginString} - ${ block.endString @@ -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: () => { @@ -142,7 +144,7 @@ module.exports = function (RED) { let deletedCronsCount = 0 if (msgCrons.length > 0) { - msgCrons.forEach(cron => { + msgCrons.forEach((cron) => { cron.stop() deletedCronsCount++ }) @@ -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)