forked from Fjuxx/pimatic-otgw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
otgw.coffee
331 lines (290 loc) · 10.9 KB
/
otgw.coffee
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
module.exports = (env) ->
Promise = env.require 'bluebird'
assert = env.require 'cassert'
_ = env.require 'lodash'
OTGWConnection = require 'OTGW-Control'
Promise.promisifyAll(OTGWConnection.prototype)
M = env.matcher
settled = (promise) -> Promise.settle([promise])
class OTGWThermostatPlugin extends env.plugins.Plugin
init: (app, @framework, @config) =>
# Promise that is resolved when the connection is established
@_lastAction = new Promise( (resolve, reject) =>
@otgw = new OTGWConnection(@config.host, @config.port)
@otgw.once("connected", resolve)
@otgw.once('error', reject)
return
).timeout(60000).catch( (error) ->
env.logger.error "Error on connecting to OTGW relay: #{error.message}"
env.logger.debug error.stack
return
)
@otgw.on('error', (error) =>
env.logger.error "connection error: #{error}"
env.logger.debug error.stack
)
@otgw.on("room_temperature", (data) =>
env.logger.debug "got room_temperature: ", data
)
@otgw.on("remote_override_setpoint", (data) =>
env.logger.debug "got remote_override_setpoint: ", data
)
@otgw.on("flame_status" , (data) =>
env.logger.debug "Flame status: ", data
)
deviceConfigDef = require("./device-config-schema")
@framework.deviceManager.registerDeviceClass("OTGWHeatingThermostat", {
configDef: deviceConfigDef.OTGWHeatingThermostat,
createCallback: (config, lastState) -> new OTGWHeatingThermostat(config, lastState)
})
@framework.deviceManager.registerDeviceClass("OTGWMainThermostat", {
configDef: deviceConfigDef.OTGWMainThermostat,
createCallback: (config, lastState) -> new OTGWMainThermostat(config, lastState)
})
@framework.deviceManager.registerDeviceClass("OTGWThermostat", {
configDef: deviceConfigDef.OTGWThermostat,
createCallback: (config) -> new OTGWThermostat(config)
})
# @framework.deviceManager.registerDeviceClass("MaxContactSensor", {
# configDef: deviceConfigDef.MaxContactSensor,
# createCallback: (config, lastState) -> new MaxContactSensor(config, lastState)
# })
# @framework.deviceManager.registerDeviceClass("MaxCube", {
# configDef: deviceConfigDef.MaxCube,
# createCallback: (config, lastState) -> new MaxCube(config, lastState)
# })
#setTemperatureSetpoint: (rfAddress, mode, value) ->
# setTemperatureSetpoint(mode,value)
setTemperatureSetpoint: (mode, value) ->
@_lastAction = settled(@_lastAction).then( =>
@otgw.setTemperatureAsync(mode, value)
)
return @_lastAction
plugin = new OTGWThermostatPlugin
class OTGWThermostat extends env.devices.Device
_fault = false
_chmode = false
_dhwmode = false
_flame = false
_coolingstatus = false
_ch2mode = false
_diag = false
_chenable = false
_dhwenable = false
_coolingenable = false
_otcstate = false
_ch2enable = false;
attributes:
Flame:
description: "Flame status"
type: "boolean"
CentralHeatingMode:
description: "Central Heating Mode"
type: "boolean"
Fault:
description: "Fault indication"
type: "boolean"
DomesticHotWaterMode:
description: "Domestic Hot Water Mode"
type: "boolean"
CoolingStatus:
description: "Cooling Status"
type: "boolean"
CentralHeating2Mode:
description: "Central Heating 2 Mode"
type: "boolean"
Diagnostics:
description: "Diagnostics"
type: "boolean"
CentralHeatingEnable:
description: "Central Heating Enable"
type: "boolean"
DomesticHotWaterEnable:
description: "Domestic Hot Water Enable"
type: "boolean"
CoolingEnable:
description: "Cooling Enable"
type: "boolean"
OTCState:
description: "OTC State"
type: "boolean"
CentralHeating2Enable:
description: "Central Heating 2 Enable"
type: "boolean"
constructor: (@config) ->
@id = @config.id
@name = @config.name
plugin.otgw.on("flame_status" , (data) =>
if data.length = 16
@_setFault(@_bitToBool(data.slice(15,16)))
@_setCHMode(@_bitToBool(data.slice(14,15)))
@_setDHWMode(@_bitToBool(data.slice(13,14)))
@_setFlame(@_bitToBool(data.slice(12,13)))
@_setCoolingStatus(@_bitToBool(data.slice(11,12)))
@_setCH2Mode(@_bitToBool(data.slice(10,11)))
@_setDiagnostics(@_bitToBool(data.slice(9,10)))
@_setCHEnable(@_bitToBool(data.slice(8,9)))
@_setDHWEnable(@_bitToBool(data.slice(7,8)))
@_setCoolingEnable(@_bitToBool(data.slice(6,7)))
@_setOTCState(@_bitToBool(data.slice(5,6)))
@_setCH2Enable(@_bitToBool(data.slice(4,5)))
)
super()
#Getters
getFlame: () -> return Promise.resolve @_flame
getCentralHeatingMode: () -> return Promise.resolve @_chmode
getFault: () -> return Promise.resolve @_fault
getDomesticHotWaterMode: () -> return Promise.resolve @_dhwmode
getCoolingStatus: () -> return Promise.resolve @_coolingstatus
getCentralHeating2Mode: () -> return Promise.resolve @_ch2mode
getDiagnostics: () -> return Promise.resolve @_diag
getCentralHeatingEnable: () -> return Promise.resolve @_chenable
getDomesticHotWaterEnable: () -> return Promise.resolve @_dhwenable
getCoolingEnable: () -> return Promise.resolve @_coolingenable
getOTCState: () -> return Promise.resolve @_otcstate
getCentralHeating2Enable: () -> return Promise.resolve @_ch2enable
#Setters
_setFlame: (state) ->
if @_flame isnt state
@_flame = state
@emit 'Flame', state
_setCHMode: (state) ->
if @_chmode isnt state
@_chmode = state
@emit 'CentralHeatingMode', state
_setFault: (state) ->
if @_fault isnt state
@_fault = state
@emit 'Fault', state
_setDHWMode: (state) ->
if @_dhwmode isnt state
@_dhwmode = state
@emit 'DomesticHotWaterMode', state
_setCoolingStatus: (state) ->
if @_coolingstatus isnt state
@_coolingstatus = state
@emit 'CoolingStatus', state
_setCH2Mode: (state) ->
if @_ch2mode isnt state
@_ch2mode = state
@emit 'CentralHeating2Mode', state
_setDiagnostics: (state) ->
if @_diag isnt state
@_diag = state
@emit 'Diagnostics', state
_setCHEnable: (state) ->
if @_chenable isnt state
@_chenable = state
@emit 'CentralHeatingEnable', state
_setDHWEnable: (state) ->
if @_dhwenable isnt state
@_dhwenable = state
@emit 'DomesticHotWaterEnable', state
_setCoolingEnable: (state) ->
if @_coolingenable isnt state
@_coolingenable = state
@emit 'CoolingEnable', state
_setOTCState: (state) ->
if @_otcstate isnt state
@_otcstate = state
@emit 'OTCState', state
_setCH2Enable: (state) ->
if @_ch2enable isnt state
@_ch2enable = state
@emit 'CentralHeating2Enable', state
#Functions
_bitToBool: (value) ->
return (value is "1")
class OTGWHeatingThermostat extends env.devices.HeatingThermostat
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_temperatureSetpoint = lastState?.temperatureSetpoint?.value
@_mode = lastState?.mode?.value or "auto"
@_battery = lastState?.battery?.value or "ok"
@_lastSendTime = 0
plugin.otgw.on("room_setpoint", (data) =>
if data?
data = Number(data)
now = new Date().getTime()
###
Give the gateway some time to handle the changes. If we send new values to the cube
we set _lastSendTime to the current time. We consider the values as succesfull set, when
the command was not rejected.
In the case that the gateway did not react to our the send commands, the values will be
overwritten with the internal state (old ones) of the gateway after 30 seconds.
###
if @_mode is "auto"
@_setSetpoint(data) #override from gateway
@_setSynced(true)
###
if now - @_lastSendTime < 30*1000
# only if values match, we are synced
if data is @_temperatureSetpoint
@_setSynced(true)
else
# more then 30 seconds passed, set the values anyway
@_setSetpoint(data) #override from gateway
@_setSynced(true)
###
return
)
plugin.otgw.on("remote_override_setpoint", (data) =>
if data?
data = Number(data)
now = new Date().getTime()
###
Give the gateway some time to handle the changes. If we send new values to the cube
we set _lastSendTime to the current time. We consider the values as succesfull set, when
the command was not rejected.
In the case that the gateway did not react to our the send commands, the values will be
overwritten with the internal state (old ones) of the gateway after 30 seconds.
###
if @_mode is "manu"
if data < 1
env.logger.debug "setting to auto"
@_setMode("auto")
else
if now - @_lastSendTime < 30*1000
# only if values match, we are synced
if data is @_temperatureSetpoint
@_setSynced(true)
else
# more then 30 seconds passed, set the values anyway
@_setSetpoint(data)
@_setSynced(true)
if @_mode is "auto" and data > 0.00
@_setMode("manu")
return
)
super()
changeModeTo: (mode) ->
temp = @_temperatureSetpoint
if mode is "auto"
temp = null
return plugin.setTemperatureSetpoint(mode, temp).then( =>
@_lastSendTime = new Date().getTime()
@_setSynced(false)
@_setMode(mode)
)
changeTemperatureTo: (temperatureSetpoint) ->
if @temperatureSetpoint is temperatureSetpoint then return
return plugin.setTemperatureSetpoint(@_mode, temperatureSetpoint).then( =>
@_lastSendTime = new Date().getTime()
@_setSynced(false)
@_setSetpoint(temperatureSetpoint)
)
class OTGWMainThermostat extends env.devices.TemperatureSensor
_temperature: null
constructor: (@config, lastState) ->
@id = @config.id
@name = @config.name
@_temperature = lastState?.temperature?.value
super()
plugin.otgw.on("room_temperature", (data) =>
if data?
@_temperature = Number(data)
@emit 'temperature', @_temperature
)
getTemperature: -> Promise.resolve(@_temperature)
return plugin