diff --git a/package.json b/package.json index f9a8a17..4223c39 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "author": { "name": "Thomas Bowman Mørch" }, - "version": "0.11.0", + "version": "0.12.0", "engines": { "node": ">=14.0.0" }, diff --git a/src/lib/small-timer-runner.spec.ts b/src/lib/small-timer-runner.spec.ts index a7a796a..707f1c3 100644 --- a/src/lib/small-timer-runner.spec.ts +++ b/src/lib/small-timer-runner.spec.ts @@ -36,6 +36,7 @@ describe('lib/small-timer-runner', () => { wrapMidnight: false, debugEnable: false, minimumOnTime: 0, + sendEmptyPayload: true, id: '', type: '', name: '', @@ -227,6 +228,32 @@ describe('lib/small-timer-runner', () => { ]) }) + + it('should not send message with empty payload', () => { + const stubs = setupTest({ + topic: 'test-topic', + onMsg:'on', + offMsg: '', // empty string should not be sendt + injectOnStartup: true, + debugEnable: true, + sendEmptyPayload: false, + }) + + stubs.stubbedTimeCalc.getTimeToNextStartEvent.returns(0) + stubs.stubbedTimeCalc.getTimeToNextEndEvent.returns(120.6) + stubs.stubbedTimeCalc.getOnState.returns(false) + + const runner = new SmallTimerRunner(stubs.position, stubs.configuration, stubs.node) + + runner.onMessage({payload: 'sync', _msgid: ''}) + sinon.clock.tick(2000) + sinon.assert.calledWith(stubs.send, [ + null, + { debug: 'this is debug', override: 'auto', topic: 'debug' }, + ]) + + }) + it('should stop timer, and not advance anything after cleanup has been called', async () => { const stubs = setupTest({ topic: 'test-topic', diff --git a/src/lib/small-timer-runner.ts b/src/lib/small-timer-runner.ts index 057bada..a34ef72 100644 --- a/src/lib/small-timer-runner.ts +++ b/src/lib/small-timer-runner.ts @@ -49,6 +49,7 @@ export class SmallTimerRunner { private debugMode = false private timer = new Timer() + private sendEmptyPayload: boolean constructor( position: Position, @@ -77,6 +78,7 @@ export class SmallTimerRunner { this.onTimeout = Number(configuration.onTimeout) this.offTimeout = Number(configuration.offTimeout) + this.sendEmptyPayload = configuration.sendEmptyPayload ?? true if (configuration.injectOnStartup) { this.startupTock = setTimeout(this.forceSend.bind(this), 2000) @@ -106,9 +108,9 @@ export class SmallTimerRunner { } private generateMsg(trigger: Trigger): SmallTimerChangeMessage { - const on = this.getCurrentState() + const status = this.getCurrentState() - const payload = on + const payload = status ? util.evaluateNodeProperty(this.onMsg, this.onMsgType, this.node, {}) : util.evaluateNodeProperty(this.offMsg, this.offMsgType, this.node, {}) @@ -126,10 +128,19 @@ export class SmallTimerRunner { } private publishState(trigger: Trigger): void { + const status = this.generateMsg(trigger) + const shouldSendStatus = this.sendEmptyPayload || status.payload !== '' + if (this.debugMode) { - this.node.send([this.generateMsg(trigger), this.generateDebug()]) - } else { - this.node.send(this.generateMsg(trigger)) + this.node.send([ + shouldSendStatus ? status : null, + this.generateDebug(), + ]) + return + } + + if (shouldSendStatus) { + this.node.send(status) } } diff --git a/src/nodes/common.ts b/src/nodes/common.ts index 9f0d97a..c160377 100644 --- a/src/nodes/common.ts +++ b/src/nodes/common.ts @@ -44,4 +44,5 @@ export interface ISmallTimerProperties extends NodeDef { wrapMidnight: boolean, debugEnable: boolean, minimumOnTime: number, + sendEmptyPayload: boolean, } diff --git a/src/nodes/small-timer.html b/src/nodes/small-timer.html index ebc2699..0188712 100644 --- a/src/nodes/small-timer.html +++ b/src/nodes/small-timer.html @@ -108,6 +108,9 @@