Skip to content
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.15.0",
"version": "0.15.1",
"engines": {
"node": ">=14.0.0"
},
Expand Down
2 changes: 2 additions & 0 deletions src/lib/small-timer-runner.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ describe('lib/small-timer-runner', () => {
stubs.stubbedTimeCalc.getOnState.returns(false)

new SmallTimerRunner(stubs.position, stubs.configuration, stubs.node)
const initialCallCount = stubs.status.callCount
sinon.clock.tick(60000)

expect(stubs.send.callCount).to.equal(0)
expect(stubs.status.callCount).to.be.greaterThan(initialCallCount)
})

it('should repeat twice in 60 seconds when configured to 30 second repeat interval', () => {
Expand Down
38 changes: 22 additions & 16 deletions src/lib/small-timer-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Position = {

const pad = (n: number) => n < 10 ? `0${n.toFixed(0)}` : `${n.toFixed(0)}`

const SecondsTick = 1000

export class SmallTimerRunner {

private startupTock: ReturnType<typeof setTimeout> | undefined = undefined
Expand Down Expand Up @@ -54,7 +56,7 @@ export class SmallTimerRunner {
private sendEmptyPayload: boolean

// Default to 20 seconds between ticks (update of state / node)
private defaultTickTimer = 20000
private defaultTickTimer = SecondsTick * 20

constructor(
position: Position,
Expand All @@ -79,19 +81,25 @@ export class SmallTimerRunner {
this.offMsgType = configuration.offMsgType
this.rules = configuration.rules
this.repeat = configuration.repeat
this.repeatInterval = Number(configuration.repeatInterval)

this.repeatInterval = this.repeat
? Number(configuration.repeatInterval || 60)
: 60

// default tick timer is 3 times as frequent as repeat timer, but never below 1 second
this.defaultTickTimer = this.repeatInterval * SecondsTick / 3
if (this.defaultTickTimer < SecondsTick) {
this.defaultTickTimer = SecondsTick
}

this.debugMode = configuration.debugEnable

this.onTimeout = Number(configuration.onTimeout)
this.offTimeout = Number(configuration.offTimeout)
this.sendEmptyPayload = configuration.sendEmptyPayload ?? true

// default tick timer is 3 times as frequent as repeat timer, but never below 1 second
this.defaultTickTimer = this.repeatInterval * 1000 / 3
if (this.defaultTickTimer < 1000) { this.defaultTickTimer = 1000 }

if (configuration.injectOnStartup) {
this.startupTock = setTimeout(this.forceSend.bind(this), 2000)
this.startupTock = setTimeout(this.forceSend.bind(this), 2 * SecondsTick)
} else {
this.calcState()
this.updateNodeStatus()
Expand Down Expand Up @@ -194,7 +202,7 @@ export class SmallTimerRunner {
}

private shouldRepeatPublish(): boolean {
const seconds = Date.now() / 1000
const seconds = Date.now() / SecondsTick
if (this.repeat && (seconds - this.lastPublish >= this.repeatInterval)) {
this.lastPublish = seconds
return true
Expand All @@ -211,7 +219,7 @@ export class SmallTimerRunner {
const nextChange = this.updateNodeStatus()

if (nextChange < 60) {
this.startTickTimer(1000)
this.startTickTimer(SecondsTick)
} else {
this.startTickTimer(this.defaultTickTimer)
}
Expand Down Expand Up @@ -412,16 +420,14 @@ export class SmallTimerRunner {
}
}

private startTickTimer(interval: number): void {
if (this.tickTimer && (interval === this.tickTimerInterval)) {
private startTickTimer(newInterval: number): void {
if (this.tickTimer && (newInterval === this.tickTimerInterval)) {
// No need in (re) starting the tick timer, if it running with desired interval already
return
}

if (this.tickTimer) {
this.stopTickTimer()
}
this.tickTimerInterval = interval
this.tickTimer = setInterval(this.timerEvent.bind(this), interval)
this.stopTickTimer()
this.tickTimerInterval = newInterval
this.tickTimer = setInterval(this.timerEvent.bind(this), newInterval)
}
}