Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to block sending empty payloads #27

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": {
"name": "Thomas Bowman Mørch"
},
"version": "0.11.0",
"version": "0.12.0",
"engines": {
"node": ">=14.0.0"
},
Expand Down
27 changes: 27 additions & 0 deletions src/lib/small-timer-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('lib/small-timer-runner', () => {
wrapMidnight: false,
debugEnable: false,
minimumOnTime: 0,
sendEmptyPayload: true,
id: '',
type: '',
name: '',
Expand Down Expand Up @@ -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',
Expand Down
21 changes: 16 additions & 5 deletions src/lib/small-timer-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class SmallTimerRunner {
private debugMode = false

private timer = new Timer()
private sendEmptyPayload: boolean

constructor(
position: Position,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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, {})

Expand All @@ -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)
}
}

Expand Down
1 change: 1 addition & 0 deletions src/nodes/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ export interface ISmallTimerProperties extends NodeDef {
wrapMidnight: boolean,
debugEnable: boolean,
minimumOnTime: number,
sendEmptyPayload: boolean,
}
6 changes: 5 additions & 1 deletion src/nodes/small-timer.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ <h3>References</h3>
<label><input type="checkbox" id="node-input-wrapMidnight" > Wrap midnight</label>
<label><input type="checkbox" id="node-input-debugEnable" > Debug enable</label>
</div>
<div>
<label><input type="checkbox" id="node-input-sendEmptyPayload" > Send empty payload</label>
</div>
</div>
</div>

Expand Down Expand Up @@ -381,6 +384,7 @@ <h3>References</h3>
endOffset: { value: 0, required: true, validate: (v) => !isNaN(v || 0) },

topic: { value: '' },
sendEmptyPayload: {value: true, required: true},

onMsg: { value: '1' },
onMsgType: { value: 'str' },
Expand Down Expand Up @@ -416,7 +420,7 @@ <h3>References</h3>

node.onTimeout = node.onTimeout ?? node.timeout
$('#node-input-onTimeout').val(node.onTimeout)

$('#node-input-sendEmptyPayload').prop('checked', node.sendEmptyPayload ?? true)
node.offTimeout = node.offTimeout ?? node.timeout
$('#node-input-offTimeout').val(node.offTimeout)

Expand Down