forked from grelca/homebridge-logic-switch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
199 lines (158 loc) · 5.81 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
const each = require('lodash/each')
const every = require('lodash/every')
const get = require('lodash/get')
const includes = require('lodash/includes')
const map = require('lodash/map')
const pick = require('lodash/pick')
const some = require('lodash/some')
const upperCase = require('lodash/upperCase')
module.exports = function (homebridge) {
homebridge.registerAccessory('homebridge-logic-switch', 'LogicSwitch', LogicSwitch)
}
// TODO: support more logic gates?
const LOGIC_GATES = {
AND: every,
OR: some,
NOT: (inputs, callback) => !some(inputs, callback)
}
class LogicSwitch {
switches = {}
hasLoop = false
constructor (logger, config, homebridge) {
this.logger = logger
this.Service = homebridge.hap.Service
this.Characteristic = homebridge.hap.Characteristic
this.uuid = homebridge.hap.uuid
// TODO: make whether this is stateful or not configurable
const dir = homebridge.user.persistPath();
this.storage = require('node-persist');
this.storage.initSync({ dir, forgiveParseErrors: true });
this.name = config.name
const { conditions } = config
this._initInformationService()
this._configureSwitches(conditions)
this._createServices()
this._detectLoops()
this._initOutputValues()
}
getServices () {
// TODO: make this smarter, disable only the outputs with invalid inputs
if (this.hasLoop) {
return []
}
const services = map(this.switches, 'service')
this.logger.debug('num services', services.length)
if (services.length > 0) {
// don't return information service without any actual accessories
services.unshift(this.informationService)
}
return services
}
_initInformationService () {
this.informationService = new this.Service.AccessoryInformation()
this.informationService.setCharacteristic(this.Characteristic.Manufacturer, 'Logic Switch')
.setCharacteristic(this.Characteristic.Model, 'Logic Switch')
.setCharacteristic(this.Characteristic.FirmwareRevision, require('./package.json').version)
.setCharacteristic(this.Characteristic.SerialNumber, this.uuid.generate(this.name))
}
_configureSwitches (conditions) {
each(conditions, condition => {
const output = get(condition, 'output')
const inputs = get(condition, 'inputs')
this._createSwitches(inputs)
this._createSwitches([output])
const gate = get(condition, 'gate')
this.switches[output].gate = upperCase(gate)
this.switches[output].inputs = inputs
inputs.forEach(input => this.switches[input].outputs.push(output))
})
}
_createSwitches (names) {
each(names, name => {
if (this.switches[name]) {
return
}
const storedValue = !!this.storage.getItemSync(this.name + name)
this.switches[name] = {
name: name,
value: storedValue,
outputs: []
}
})
}
_createServices () {
each(this.switches, s => {
if (s.inputs) {
s.service = this._createOutputService(s.name)
} else {
s.service = this._createInputService(s.name)
}
})
}
_createInputService (name) {
const service = new this.Service.Switch(name, name)
service.getCharacteristic(this.Characteristic.On)
.onGet(async () => this._getValue(name))
.onSet(async (value) => this._setValue(name, value))
return service
}
_createOutputService (name) {
const service = new this.Service.MotionSensor(name, name)
service.getCharacteristic(this.Characteristic.MotionDetected)
.onGet(async () => this._getValue(name))
return service
}
_getValue (name) {
return !!this.switches[name].value
}
_setValue (name, value) {
this.logger.info(`setting ${name} to ${value}`)
this.switches[name].value = value
this.storage.setItemSync(this.name + name, value)
this._updateOutputs(name)
}
_updateOutputs (name) {
const { outputs } = this.switches[name]
outputs.forEach(output => {
const newValue = this._calcOutput(output)
if (!!newValue === !!this._getValue(output)) {
return this.logger.debug(`${output} value not changed`)
}
const { service } = this.switches[output]
service.getCharacteristic(this.Characteristic.MotionDetected).updateValue(newValue)
this._setValue(output, newValue)
})
}
_calcOutput (name) {
const { gate, inputs } = this.switches[name]
// get comparison method, default is AND
const method = get(LOGIC_GATES, gate, every)
return method(
pick(this.switches, inputs),
input => !!input.value
)
}
_detectLoops () {
this.hasLoop = some(this.switches, s => this._hasLoop(s, []))
}
// recursively look for logic loops
_hasLoop (s, inputs) {
this.logger.debug('checking for loops', s.name, inputs)
if (includes(inputs, s.name)) {
this.logger.error('logic loop detected!', s.name, inputs)
return true
}
if (s.outputs.length === 0) {
return false
}
inputs.push(s.name)
return some(s.outputs, output => this._hasLoop(this.switches[output], inputs))
}
// TODO: this could be made more efficient
_initOutputValues () {
if (this.hasLoop) {
return
}
Object.keys(this.switches).forEach(this._updateOutputs.bind(this))
}
}