-
Notifications
You must be signed in to change notification settings - Fork 0
/
SalusCloud.lua
442 lines (422 loc) · 13.5 KB
/
SalusCloud.lua
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
--[[
Salus IT600 Cloud SDK
@author ikubicki
]]
class 'SalusCloud'
function SalusCloud:new(config)
self.user = config:getUser()
self.pass = config:getPassword()
self.device_id = config:getDeviceID()
self.token = Globals:get('salus_token', '')
self.token_time = tonumber(Globals:get('salus_token_time', 0))
self.http = HTTPClient:new({
baseUrl = 'https://eu.salusconnect.io'
})
return self
end
function SalusCloud:getProperties(callback, failCallback)
local properties = {}
local batteryLevelCallback = function(response)
properties["battery"] = response.value
callback(properties)
end
local holdtypeCallback = function(response)
properties["holdtype"] = response.value
SalusCloud:batteryLevel(batteryLevelCallback, failCallback)
end
local runningCallback = function(response)
properties["running"] = response.value
SalusCloud:holdtype(holdtypeCallback, failCallback)
end
local humidityCallback = function(response)
properties["humidity"] = response.value
SalusCloud:running(runningCallback, failCallback)
end
local heatingSetpointCallback = function(response)
properties["heatingSetpoint"] = response.value / 100
SalusCloud:humidity(humidityCallback, failCallback)
end
local temperatureCallback = function(response)
properties["temperature"] = response.value / 100
SalusCloud:heatingSetpoint(heatingSetpointCallback, failCallback)
end
local authCallback = function(response)
SalusCloud:temperature(temperatureCallback, failCallback)
end
SalusCloud:auth(authCallback, failCallback)
end
function SalusCloud:searchDevices(callback)
local buildGateway = function(data)
return {
id = data.dsn,
name = data.product_name,
ip = data.lan_ip,
devices = {}
}
end
local buildDevice = function(data)
return {
id = data.dsn,
name = data.product_name,
model = data.oem_model,
}
end
local listDevicesCallback = function(response)
QuickApp:debug('OK');
local gateways = {}
-- gateways
for _, d in ipairs(response) do
if d.device.device_type == 'Gateway' then
gateways[d.device.dsn] = buildGateway(d.device)
end
end
-- devices
for _, d in ipairs(response) do
if d.device.dsn ~= d.device.product_name and d.device.device_type == 'Node' and gateways[d.device.gateway_dsn] ~= nil then
table.insert(gateways[d.device.gateway_dsn].devices, buildDevice(d.device))
end
end
callback(gateways)
end
local authCallback = function(response)
SalusCloud:listDevices(listDevicesCallback)
end
SalusCloud:auth(authCallback)
end
function SalusCloud:batteryLevel(callback, failCallback, attempt)
if attempt == nil then
attempt = 1
end
local fail = function(response)
if response.status == 404 then
return callback({})
end
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull battery level')
SalusCloud:setToken('')
-- QuickApp:debug(json.encode(response))
if attempt < 2 then
attempt = attempt + 1
fibaro.setTimeout(3000, function()
QuickApp:debug('SalusCloud:batteryLevel - Retry attempt #' .. attempt)
local authCallback = function(response)
self:batteryLevel(callback, failCallback, attempt)
end
SalusCloud:auth(authCallback, failCallback)
end)
end
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:BatteryLevel.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:temperature(callback, failCallback, attempt)
if attempt == nil then
attempt = 1
end
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull temperature')
SalusCloud:setToken('')
--QuickApp:debug(json.encode(response))
if attempt < 2 then
attempt = attempt + 1
fibaro.setTimeout(3000, function()
QuickApp:debug('SalusCloud:temperature - Retry attempt #' .. attempt)
local authCallback = function(response)
self:temperature(callback, failCallback, attempt)
end
SalusCloud:auth(authCallback, failCallback)
end)
end
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:LocalTemperature_x100.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:heatingSetpoint(callback, failCallback)
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull heating setpoint')
SalusCloud:setToken('')
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:HeatingSetpoint_x100.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:setHeatingSetpoint(heatingSetpoint, callback, failCallback)
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to update heatingSetpoint')
SalusCloud:setToken('')
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:SetHeatingSetpoint_x100/datapoints.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken(),
["Content-Type"] = "application/json",
}
local data = {
datapoint = {
value = heatingSetpoint * 100
}
}
self.http:post(url, data, success, fail, headers)
end
function SalusCloud:humidity(callback, failCallback)
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull humidity')
SalusCloud:setToken('')
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:SunnySetpoint_x100.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:running(callback, failCallback)
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull mode')
SalusCloud:setToken('')
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:RunningState.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:holdtype(callback, failCallback)
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull mode')
SalusCloud:setToken('')
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:HoldType.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:setHoldtype(holdtype, callback, failCallback)
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to update holdtype')
SalusCloud:setToken('')
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data.property)
end
end
local url = "/apiv1/dsns/" .. self.device_id .. "/properties/ep_9:sIT600TH:SetHoldType/datapoints.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken(),
["Content-Type"] = "application/json",
}
local data = {
datapoint = {
value = holdtype
}
}
self.http:post(url, data, success, fail, headers)
end
function SalusCloud:listDevices(callback, fail, attempt)
if attempt == nil then
attempt = 1
end
if fail == nil then
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to pull devices')
SalusCloud:setToken('')
if attempt < 2 then
attempt = attempt + 1
fibaro.setTimeout(3000, function()
QuickApp:debug('SalusCloud:listDevices - Retry attempt #' .. attempt)
local authCallback = function(response)
self:listDevices(callback, nil, attempt)
end
SalusCloud:auth(authCallback)
end)
end
end
end
local success = function(response)
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
if callback ~= nil then
callback(data)
end
end
local url = "/apiv1/devices.json"
local headers = {
Authorization = "Bearer " .. SalusCloud:getToken()
}
self.http:get(url, success, fail, headers)
end
function SalusCloud:auth(callback, failCallback)
if string.len(self.token) > 1 then
-- QuickApp:debug('Already authenticated')
if callback ~= nil then
callback({})
end
return
end
local fail = function(response)
if failCallback then
failCallback(json.encode(response))
end
QuickApp:error('Unable to authenticate')
SalusCloud:setToken('')
end
local success = function(response)
-- QuickApp:debug(json.encode(response))
if response.status > 299 then
fail(response)
return
end
local data = json.decode(response.data)
SalusCloud:setToken(data.access_token)
if callback ~= nil then
callback(data)
end
end
local url = "/users/sign_in.json"
local headers = {
["Content-Type"] = "application/json"
}
local data = {
user = {
email = self.user,
password = self.pass,
}
}
self.http:post(url, data, success, fail, headers)
end
function SalusCloud:setToken(token)
self.token = token
self.token_time = os.time(os.date("!*t"))
Globals:set('salus_token', token)
Globals:set('salus_token_time', self.token_time)
end
function SalusCloud:getToken()
if not self:checkTokenTime() then
self:setToken('')
return ''
end
if string.len(self.token) > 10 then
return self.token
elseif string.len(Globals:get('salus_token', '')) > 10 then
return Globals:get('salus_token', '')
end
return ''
end
function SalusCloud:checkTokenTime()
if self.token_time < 1 then
self.token_time = tonumber(Globals:get('salus_token_time', 0))
end
return self.token_time > 0 and os.time(os.date("!*t")) - self.token_time < 43200
end