-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstatus.js
93 lines (71 loc) · 1.92 KB
/
status.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const events = require('events')
const eventEmitter = new events.EventEmitter()
const status = {
link: undefined,
armed: undefined,
abort: undefined,
intruder: undefined,
panic: undefined,
autoArm: undefined,
autoDisarm: undefined,
autoDays: [false, false, false, false, false, false, false],
airQuality: undefined
}
const update = (prop, newState) => {
const prevState = status[prop]
status[prop] = newState
const stateChanged = prevState !== undefined && newState !== prevState
if (stateChanged) {
eventEmitter.emit(prop, newState)
}
}
const setLink = state =>
update('link', state)
const getLink = () =>
status.link
const setArmed = state =>
update('armed', state)
const getArmed = () =>
status.armed
const setAbort = state =>
update('abort', state)
const getAbort = () =>
status.abort
const setIntruder = state =>
update('intruder', state)
const getIntruder = () =>
status.intruder
const setPanic = state =>
update('panic', state)
const getPanic = () =>
status.panic
const setAutoArm = hour =>
update('autoArm', hour)
const getAutoArm = () =>
status.autoArm
const setAutoDisarm = hour =>
update('autoDisarm', hour)
const getAutoDisarm = () =>
status.autoDisarm
const toggleAutoDay = day =>
status.autoDays[day] = !status.autoDays[day]
const getAutoDays = () =>
status.autoDays.map(day => day ? '1' : '0').join('')
const isAutoDay = day =>
status.autoDays[day]
const setAirQuality = value =>
update('airQuality', value)
const getAirQuality = () =>
status.airQuality
module.exports = {
setLink, getLink,
setArmed, getArmed,
setAbort, getAbort,
setIntruder, getIntruder,
setPanic, getPanic,
setAutoArm, getAutoArm,
setAutoDisarm, getAutoDisarm,
toggleAutoDay, getAutoDays, isAutoDay,
setAirQuality, getAirQuality,
on: (...args) => eventEmitter.on(...args)
}