Skip to content

Commit b1b0549

Browse files
tbowmoThomas Bowman Mørch
andauthored
Trigger origin (#25)
* Adding trigger property to output * Bump version --------- Co-authored-by: Thomas Bowman Mørch <thomas.git@bowmo.dk>
1 parent d13b1f0 commit b1b0549

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ The rules is read and executed one by one, it is the outcome after the last rule
7373
By default the node has a single output, which provides the following object:
7474
```ts
7575
{
76+
trigger: 'input' | 'timer',
7677
autoState: boolean,
7778
timeout: number,
7879
temporaryManual: boolean,
@@ -87,6 +88,7 @@ By default the node has a single output, which provides the following object:
8788

8889
| property | description |
8990
|----------|-------------|
91+
| trigger | if the output is triggered by an input message, then this property is set to "input" otherwise it is set to "timer"|
9092
| autoState|will be true whenever the node is running in auto mode
9193
|timeout | indicates how long there is until the node will return to auto state, when in temporary override|
9294
|temporaryManual| Will be true when the node is in temporary override|

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"author": {
44
"name": "Thomas Bowman Mørch"
55
},
6-
"version": "0.9.1",
6+
"version": "0.10.0",
77
"engines": {
88
"node": ">=14.0.0"
99
},

src/lib/interfaces.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { NodeMessage, NodeMessageInFlow } from 'node-red'
22

3+
export type State = 'auto' | 'tempOn' | 'tempOff'
4+
export type Trigger = 'input' | 'timer'
5+
36
export interface ISmallTimerMessage extends NodeMessageInFlow {
47
reset?: boolean,
58
timeout?: number,
@@ -11,5 +14,6 @@ export type SmallTimerChangeMessage = NodeMessage & {
1114
temporaryManual: boolean,
1215
duration: number,
1316
stamp: number,
14-
state: string
17+
state: State,
18+
trigger: Trigger
1519
}

src/lib/small-timer-runner.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ describe('lib/small-timer-runner', () => {
172172
timeout: 0,
173173
payload: '0',
174174
topic: 'test-topic',
175+
trigger: 'timer',
175176
})
176177
stubs.stubbedTimeCalc.getOnState.returns(true)
177178
sinon.clock.tick(60000)
@@ -189,6 +190,7 @@ describe('lib/small-timer-runner', () => {
189190
timeout: 0,
190191
payload: 'on-msg',
191192
topic: 'test-topic',
193+
trigger: 'timer',
192194
})
193195
})
194196

@@ -219,6 +221,7 @@ describe('lib/small-timer-runner', () => {
219221
timeout: 0,
220222
payload: 'off',
221223
topic: 'test-topic',
224+
trigger: 'timer',
222225
},
223226
{ debug: 'this is debug', override: 'auto', topic: 'debug' },
224227
])
@@ -250,6 +253,7 @@ describe('lib/small-timer-runner', () => {
250253
timeout: 0,
251254
payload: '0',
252255
topic: 'test-topic',
256+
trigger: 'timer',
253257
})
254258
stubs.stubbedTimeCalc.getOnState.returns(true)
255259
sinon.clock.tick(1200000)
@@ -285,6 +289,7 @@ describe('lib/small-timer-runner', () => {
285289
timeout: 0,
286290
payload: '0',
287291
topic: 'test-topic',
292+
trigger: 'timer',
288293
})
289294

290295
runner.onMessage({ payload: 'toggle', _msgid: 'some-msg' })
@@ -303,6 +308,7 @@ describe('lib/small-timer-runner', () => {
303308
timeout: 0,
304309
payload: 'on-msg',
305310
topic: 'test-topic',
311+
trigger: 'input',
306312
})
307313

308314
runner.onMessage({ payload: 'toggle', _msgid: 'some-msg' })
@@ -320,6 +326,7 @@ describe('lib/small-timer-runner', () => {
320326
timeout: 0,
321327
payload: '0',
322328
topic: 'test-topic',
329+
trigger: 'input',
323330
})
324331
})
325332

@@ -348,6 +355,7 @@ describe('lib/small-timer-runner', () => {
348355
timeout: 0,
349356
payload: '0',
350357
topic: 'test-topic',
358+
trigger: 'timer',
351359
})
352360

353361
runner.onMessage({ payload: 'sync', _msgid: 'some-id' })
@@ -365,6 +373,7 @@ describe('lib/small-timer-runner', () => {
365373
timeout: 0,
366374
payload: '0',
367375
topic: 'test-topic',
376+
trigger: 'input',
368377
}])
369378
})
370379

@@ -393,6 +402,7 @@ describe('lib/small-timer-runner', () => {
393402
timeout: 0,
394403
payload: '0',
395404
topic: 'test-topic',
405+
trigger: 'timer',
396406
})
397407

398408
runner.onMessage({ payload: 'toggle', timeout: 10, _msgid: 'some-msg' })

src/lib/small-timer-runner.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@ import {
77
} from 'node-red'
88
import { util } from '@node-red/util'
99
import { ISmallTimerProperties, Rule } from '../nodes/common'
10-
import { SmallTimerChangeMessage, ISmallTimerMessage } from './interfaces'
10+
import {
11+
SmallTimerChangeMessage,
12+
ISmallTimerMessage,
13+
Trigger,
14+
State,
15+
} from './interfaces'
1116
import { TimeCalc } from './time-calculation'
1217
import { Timer } from './timer'
1318

14-
type Override = 'auto' | 'tempOn' | 'tempOff'
15-
1619
type NodeFunctions = Node
1720

1821
type Position = {
@@ -29,7 +32,7 @@ export class SmallTimerRunner {
2932
// Timing variables
3033
private tickTimer: ReturnType<typeof setInterval> | undefined = undefined
3134

32-
private override: Override = 'auto'
35+
private override: State = 'auto'
3336
private currentState = false
3437

3538
private topic: string
@@ -102,7 +105,7 @@ export class SmallTimerRunner {
102105
} as NodeMessage // we cheat a bit to escape type checking in typescript
103106
}
104107

105-
private generateMsg(): SmallTimerChangeMessage {
108+
private generateMsg(trigger: Trigger): SmallTimerChangeMessage {
106109
const on = this.getCurrentState()
107110

108111
const payload = on
@@ -118,14 +121,15 @@ export class SmallTimerRunner {
118121
timeout: this.timer.timeLeft(),
119122
payload: payload,
120123
topic: this.topic,
124+
trigger,
121125
}
122126
}
123127

124-
private publishState(): void {
128+
private publishState(trigger: Trigger): void {
125129
if (this.debugMode) {
126-
this.node.send([this.generateMsg(), this.generateDebug()])
130+
this.node.send([this.generateMsg(trigger), this.generateDebug()])
127131
} else {
128-
this.node.send(this.generateMsg())
132+
this.node.send(this.generateMsg(trigger))
129133
}
130134
}
131135

@@ -177,14 +181,14 @@ export class SmallTimerRunner {
177181
this.updateNodeStatus()
178182

179183
if (change || this.repeat) {
180-
this.publishState()
184+
this.publishState('timer')
181185
}
182186
}
183187

184-
private forceSend(): void {
188+
private forceSend(trigger: Trigger = 'timer'): void {
185189
this.calcState()
186190
this.updateNodeStatus()
187-
this.publishState()
191+
this.publishState(trigger)
188192
}
189193

190194
/**
@@ -269,7 +273,7 @@ export class SmallTimerRunner {
269273
this.node.status(status)
270274
}
271275

272-
private doOverride(override: Override, timeout?: number): void {
276+
private doOverride(override: State, timeout?: number): void {
273277
this.override = override
274278
if (override === 'auto') {
275279
this.timer.stop()
@@ -309,7 +313,7 @@ export class SmallTimerRunner {
309313
// eslint-disable-next-line no-extra-boolean-cast
310314
if (incomingMsg.reset !== undefined) {
311315
this.doOverride('auto')
312-
this.forceSend()
316+
this.forceSend('input')
313317
return
314318
}
315319

@@ -348,7 +352,7 @@ export class SmallTimerRunner {
348352
default:
349353
throw new Error(`Did not understand the command '${incomingMsg.payload}' supplied in payload`)
350354
}
351-
this.forceSend()
355+
this.forceSend('input')
352356
}
353357

354358
/**

0 commit comments

Comments
 (0)