This repository has been archived by the owner on Feb 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
527 lines (500 loc) · 21.5 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
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/*
homebridge-piazzetta-stove-simple
Homebridge plugin to control Piazzetta pellets stove via Micronova's WiFi module.
Licensed under AGPL-3.0-only License [https://www.gnu.org/licenses/agpl-3.0.en.html].
Copyright (C) 2020, @securechicken
*/
const PLUGIN_NAME = "homebridge-piazzetta-stove-simple";
const PLUGIN_AUTHOR = "@securechicken";
const PLUGIN_VERSION = "1.0.0";
const PLUGIN_DEVICE_MANUFACTURER = "Piazzetta";
const PLUGIN_DEVICE_MODEL = "Piazzetta Stove";
const ACCESSORY_PLUGIN_NAME = "HeaterCoolerPiazzettaStoveSimple";
const fetch = require("node-fetch");
const https = require("https");
module.exports = (api) => {
api.registerAccessory(ACCESSORY_PLUGIN_NAME, HeaterCoolerPiazzettaStoveSimple);
};
const HTTP_TIMEOUT = 10000; // The web service is unfortunately very laggy
const HTTP_REQ_COOKIE_HEADER = "Cookie";
const HTTP_REQ_UA_HEADER = "User-Agent";
const HTTP_REP_COOKIE_HEADER = "set-cookie";
const HTTP_UA = "homebridge-piazzetta-stove-simple/" + PLUGIN_VERSION;
const API_PROTOCOL = "https://";
const API_HOSTNAME = "piazzetta.efesto.web2app.it";
const API_LOGIN = "/en/login/";
const API_LOGIN_PARAM_LOGIN = "login[username]=";
const API_LOGIN_PARAM_PASSWORD = "login[password]=";
const API_LOGIN_TOKEN_NAME = "remember=";
const API_LOGIN_AUTOLOGIN_DELAY = 86400000; // 24h in millisec
const API_ENDPOINT_FRONTEND = "/en/ajax/action/frontend/response/ajax/";
const API_METHOD_PARAM_TOKEN = API_LOGIN_TOKEN_NAME;
const API_METHOD_PARAM_DEVICE = "device=";
const API_METHOD_PARAM_METHOD = "method=";
const API_METHOD_PARAM_PARAMS = "params=";
const API_METHOD_GET_STATE = "get-state";
const API_METHOD_POWER_ON = "heater-on";
const API_METHOD_POWER_OFF = "heater-off";
const POWER_SWING_PROTECTION_DELAY = 3600000; // 60min in millisec
const API_METHOD_SET_VAL = "write-parameters-queue";
const API_METHOD_TEMP_PARAM = "set-air-temperature=";
const API_METHOD_POWER_PARAM = "set-power=";
const API_RESP_STATUS = "status";
const API_RESP_MESSAGE = "message";
const STOVE_CONNECTION_STATUS_OK = 0;
const STOVE_ALARM_STATUS_OK = 0;
const STOVE_ALARM_AWAITING_FLAME = 32;
const STOVE_MIN_TEMP = 0;
const STOVE_MAX_TEMP = 50;
const STOVE_MIN_TEMP_THRESHOLD = 7;
const STOVE_MAX_TEMP_THRESHOLD = 30;
const STOVE_TEMP_DELTA = 1;
const STOVE_MIN_POWER = 1;
const STOVE_MAX_POWER = 4;
const STOVE_POWER_DELTA = 1;
const STOVE_STATUS_STATE = "deviceStatus";
const STOVE_STATUS_CURRENT_TEMP = "airTemperature";
const STOVE_STATUS_SET_TEMP = "lastSetAirTemperature";
const STOVE_STATUS_CURRENT_POWER = "realPower";
const STOVE_STATUS_SET_POWER = "lastSetPower";
const STOVE_STATUS_TIMESTAMP = "lastCheckTimestamp";
const STOVE_STATUS_ALARM = "isDeviceInAlarm";
const STOVE_STATUS_CONNECTION = "contactStatus";
const STOVE_STATUS_CACHE_KEEP = 10000;
class HeaterCoolerPiazzettaStoveSimple {
constructor(log, config, api) {
this.log = log;
this.config = config;
this.api = api;
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;
this.log.debug(ACCESSORY_PLUGIN_NAME + "init, config: " + JSON.stringify(this.config));
this.isAuth = false;
this.authToken = null;
// Required to allow non verified HTTPS certs (Efesto API calls are non HTTPS by
// default if official apps... and their cert is auto-signed)
this.httpsAgent = new https.Agent({ rejectUnauthorized: false });
this.httpHeaders = {};
// Auth to API is done with a mere cookie
this.httpHeaders[HTTP_REQ_UA_HEADER] = HTTP_UA;
this.httpHeaders[HTTP_REQ_COOKIE_HEADER] = this.authToken;
// Stove status cache, as returned by API
this.status = {};
this.status[STOVE_STATUS_STATE] = 0;
this.status[STOVE_STATUS_CURRENT_TEMP] = STOVE_MIN_TEMP;
this.status[STOVE_STATUS_SET_TEMP] = STOVE_MIN_TEMP_THRESHOLD;
this.status[STOVE_STATUS_CURRENT_POWER] = STOVE_MIN_POWER;
this.status[STOVE_STATUS_SET_POWER] = STOVE_MIN_POWER;
this.status[STOVE_STATUS_ALARM] = STOVE_ALARM_STATUS_OK;
this.status[STOVE_STATUS_CONNECTION] = STOVE_CONNECTION_STATUS_OK;
this.status[STOVE_STATUS_TIMESTAMP] = null;
// Mappings between HomeKit states and API returned one.
this.statusStateMap = new Map([
[0, this.Characteristic.CurrentHeaterCoolerState.INACTIVE], // OFF, OFF E
[1, this.Characteristic.CurrentHeaterCoolerState.IDLE], // TURNING OFF, AWAITING FLAME (+ ERROR 32)
[2, this.Characteristic.CurrentHeaterCoolerState.IDLE],
[3, this.Characteristic.CurrentHeaterCoolerState.IDLE], // LIGHTING
[4, this.Characteristic.CurrentHeaterCoolerState.HEATING], // WORRKING
[5, this.Characteristic.CurrentHeaterCoolerState.IDLE],
[6, this.Characteristic.CurrentHeaterCoolerState.IDLE], // FINAL CLEANING
[7, this.Characteristic.CurrentHeaterCoolerState.IDLE] // STANDBY
]);
this.statusActiveMap = new Map([
[0, this.Characteristic.Active.INACTIVE],
[1, this.Characteristic.Active.ACTIVE],
[2, this.Characteristic.Active.ACTIVE],
[3, this.Characteristic.Active.ACTIVE],
[4, this.Characteristic.Active.ACTIVE],
[5, this.Characteristic.Active.ACTIVE],
[6, this.Characteristic.Active.ACTIVE],
[7, this.Characteristic.Active.ACTIVE]
]);
// Anti power swinging protection
this.lastPowerChange = null;
// Auto login to API (at start, then every 24h)
this._autoLoginWrapper(true);
setInterval( this._autoLoginWrapper.bind(this), API_LOGIN_AUTOLOGIN_DELAY, false);
// Device infos
this.infoService = new this.Service.AccessoryInformation();
this.infoService
.setCharacteristic(this.Characteristic.Manufacturer, PLUGIN_DEVICE_MANUFACTURER)
.setCharacteristic(this.Characteristic.Model, PLUGIN_DEVICE_MODEL)
.setCharacteristic(this.Characteristic.Name, this.config.name)
.setCharacteristic(this.Characteristic.SerialNumber, this.config.id)
.setCharacteristic(this.Characteristic.SoftwareRevision, PLUGIN_VERSION)
.setCharacteristic(this.Characteristic.FirmwareRevision, PLUGIN_NAME)
.setCharacteristic(this.Characteristic.HardwareRevision, PLUGIN_AUTHOR);
// Heater Cooler service
const sname = this.config.name || ACCESSORY_PLUGIN_NAME;
this.service = new this.Service.HeaterCooler(sname);
// Set characteristics properties boundaries and valid values
// Setting CurrentHeaterCoolerState and TargetHeaterCoolerState allows to
// lock device to heater mode only
this.service.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState)
.setProps({
minValue: this.Characteristic.CurrentHeaterCoolerState.INACTIVE,
maxValue: this.Characteristic.CurrentHeaterCoolerState.HEATING,
validValues: [this.Characteristic.CurrentHeaterCoolerState.INACTIVE, this.Characteristic.CurrentHeaterCoolerState.IDLE, this.Characteristic.CurrentHeaterCoolerState.HEATING]
});
this.service.getCharacteristic(this.Characteristic.TargetHeaterCoolerState)
.setProps({
minValue: this.Characteristic.TargetHeaterCoolerState.HEAT,
maxValue: this.Characteristic.TargetHeaterCoolerState.HEAT,
validValues: [this.Characteristic.TargetHeaterCoolerState.HEAT]
});
this.service.getCharacteristic(this.Characteristic.CurrentTemperature)
.setProps({minValue: STOVE_MIN_TEMP, maxValue: STOVE_MAX_TEMP, minStep: STOVE_TEMP_DELTA});
this.service.getCharacteristic(this.Characteristic.HeatingThresholdTemperature)
.setProps({minValue: STOVE_MIN_TEMP_THRESHOLD, maxValue: STOVE_MAX_TEMP_THRESHOLD, minStep: STOVE_TEMP_DELTA});
this.service.getCharacteristic(this.Characteristic.RotationSpeed)
.setProps({minValue: STOVE_MIN_POWER, maxValue: STOVE_MAX_POWER, minStep: STOVE_POWER_DELTA});
this.service.getCharacteristic(this.Characteristic.LockPhysicalControls)
.setProps({
minValue: this.Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED,
maxValue: this.Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED,
validValues: [this.Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED]
});
this.service.getCharacteristic(this.Characteristic.SwingMode)
.setProps({
minValue: this.Characteristic.SwingMode.SWING_DISABLED,
maxValue: this.Characteristic.SwingMode.SWING_DISABLED,
validValues: [this.Characteristic.SwingMode.SWING_DISABLED]
});
this.service.getCharacteristic(this.Characteristic.TemperatureDisplayUnits)
.setProps({
minValue: this.Characteristic.TemperatureDisplayUnits.CELSIUS,
maxValue: this.Characteristic.TemperatureDisplayUnits.CELSIUS,
validValues: [this.Characteristic.TemperatureDisplayUnits.CELSIUS]
});
// Forced initial arbitrary states
this.service.setCharacteristic(this.Characteristic.Name, this.config.name);
this.service.setCharacteristic(this.Characteristic.Active, this.Characteristic.Active.INACTIVE);
this.service.setCharacteristic(this.Characteristic.CurrentHeaterCoolerState, this.Characteristic.CurrentHeaterCoolerState.INACTIVE);
this.service.setCharacteristic(this.Characteristic.TargetHeaterCoolerState, this.Characteristic.TargetHeaterCoolerState.HEAT);
this.service.setCharacteristic(this.Characteristic.TemperatureDisplayUnits, this.Characteristic.TemperatureDisplayUnits.CELSIUS);
this.service.setCharacteristic(this.Characteristic.CurrentTemperature, STOVE_MIN_TEMP);
this.service.setCharacteristic(this.Characteristic.HeatingThresholdTemperature, STOVE_MIN_TEMP_THRESHOLD);
this.service.setCharacteristic(this.Characteristic.LockPhysicalControls, this.Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED);
this.service.setCharacteristic(this.Characteristic.SwingMode, this.Characteristic.SwingMode.SWING_DISABLED);
this.service.setCharacteristic(this.Characteristic.RotationSpeed, STOVE_MIN_POWER);
// Services methods and events handling
this.service.getCharacteristic(this.Characteristic.Active)
.on("get", this.getStoveActive.bind(this))
.on("set", this.setStoveActive.bind(this));
this.service.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState)
.on("get", this.getStoveState.bind(this));
this.service.getCharacteristic(this.Characteristic.CurrentTemperature)
.on("get", this.getStoveCurrentTemp.bind(this));
this.service.getCharacteristic(this.Characteristic.HeatingThresholdTemperature)
.on("get", this.getStoveSetTemp.bind(this))
.on("set", this.setStoveTemp.bind(this));
this.service.getCharacteristic(this.Characteristic.RotationSpeed)
.on("get", this.getStovePower.bind(this))
.on("set", this.setStovePower.bind(this));
this.log.debug("Finished loading, status: " + JSON.stringify(this.status));
}
// Mandatory services export method
getServices() {
return [this.infoService, this.service];
}
// API login helper
_sendAPILogin(callback) {
this.httpHeaders[HTTP_REQ_COOKIE_HEADER] = null;
this.isAuth = false;
let url = API_LOGIN + "?" + API_LOGIN_PARAM_LOGIN + this.config.login + "&" + API_LOGIN_PARAM_PASSWORD + this.config.password;
fetch(API_PROTOCOL + API_HOSTNAME + url, {timeout: HTTP_TIMEOUT, agent: this.httpsAgent, headers: this.httpHeaders, redirect: "manual"})
.then( (resp) => {
if (resp.headers) {
this.log.debug("_sendAPILogin got a response with headers");
return resp.headers.raw()[HTTP_REP_COOKIE_HEADER];
} else {
this.log.debug("_sendAPILogin got a response with NON-OK status");
this.authToken = null;
throw new Error("Authentication rejected by API:" + resp.status);
}
})
.then( (cookies) => {
let token = null;
if (cookies && Array.isArray(cookies) && cookies.length >= 1) {
for (const cookie of cookies) {
if (cookie.startsWith(API_LOGIN_TOKEN_NAME)) {
let commaSplit = cookie.split(";");
let equalSplit = commaSplit[0].split("=");
if (API_LOGIN_TOKEN_NAME.startsWith(equalSplit[0]) && equalSplit.length >= 1) {
token = equalSplit[1];
this.log.debug("_sendAPILogin retrieved a token: " + token);
}
}
}
}
if (token) {
this.isAuth = true;
this.authToken = token;
callback(null, token);
} else {
throw new Error("API did not send a token back: " + JSON.stringify(cookies));
}
})
.catch( (err) => {
this.authToken = null;
callback(err.message, null);
});
}
// API auto-login wrapper that can be called regularly
_autoLoginWrapper(init) {
if (init) {
this.log.info("First log-in");
} else {
this.log.info("Attempting auto log-in");
}
this._sendAPILogin( (err, token) => {
if (token || !err) {
if (init) {
this.log.info("Successfully logged-in: " + token);
} else {
this.log.info("Successfully logged-in automatically after set delay: " + token);
}
} else {
this.log.error("Could not log-in with login '" + this.config.login + "': " + err);
}
} );
}
// Send stove command to API (Efesto)
_sendAPIRequest(url, callback) {
if (this.isAuth) {
this.httpHeaders[HTTP_REQ_COOKIE_HEADER] = API_METHOD_PARAM_TOKEN + this.authToken;
this.log.debug("_sendAPIRequest will send to: " + url + ", with headers: " + JSON.stringify(this.httpHeaders));
fetch(API_PROTOCOL + API_HOSTNAME + url, {timeout: HTTP_TIMEOUT, agent: this.httpsAgent, headers: this.httpHeaders})
.then( (resp) => {
if (resp.ok || resp.status === 302) {
this.log.debug("_sendAPIRequest got a response with OK status");
return resp;
} else {
this.log.debug("_sendAPIRequest got a response with NON-OK status");
throw new Error("Non-OK HTTP Response Status received:" + resp.status);
}
})
.then( resp => resp.json() )
.then( (json) => {
if (json && json[API_RESP_STATUS] === 0 && json[API_RESP_MESSAGE]) {
this.log.debug("_sendAPIRequest got a JSON response with OK status");
callback(null, json[API_RESP_MESSAGE]);
} else if (json && json[API_RESP_STATUS] === 1 && json[API_RESP_MESSAGE]) {
this.log.debug("_sendAPIRequest got a JSON response with NON-OK status");
throw new Error("API Error: " + json[API_RESP_MESSAGE]);
} else {
throw new Error("Unspecified Error: " + JSON.stringify(json));
}
})
.catch( err => callback(err.message, null) );
} else {
callback("Not logged-in...", null);
}
}
// Wrapper to send an API request from command name (method) and params only
_sendAPIMethod(method, params, callback) {
let url = API_ENDPOINT_FRONTEND + "?" + API_METHOD_PARAM_DEVICE + this.config.id + "&" + API_METHOD_PARAM_METHOD + method;
if (params) {
url += "&" + API_METHOD_PARAM_PARAMS + params;
}
this.log.debug("_sendAPIMethod will send " + method + ", " + params);
this._sendAPIRequest(url, (err, res) => {
if (res || !err) {
this.log.debug("_sendAPIMethod got a response: " + JSON.stringify(res));
callback(null, res);
} else {
this.log.debug("_sendAPIMethod failed to run '" + method + "': " + err);
callback(err, null);
}
});
}
// Status cache filling helper
_fillStatusCache(status, callback) {
try {
this.status[STOVE_STATUS_STATE] = status[STOVE_STATUS_STATE];
this.status[STOVE_STATUS_CURRENT_TEMP] = status[STOVE_STATUS_CURRENT_TEMP];
this.status[STOVE_STATUS_SET_TEMP] = status[STOVE_STATUS_SET_TEMP];
this.status[STOVE_STATUS_CURRENT_POWER] = status[STOVE_STATUS_CURRENT_POWER];
this.status[STOVE_STATUS_SET_POWER] = status[STOVE_STATUS_SET_POWER];
this.status[STOVE_STATUS_ALARM] = status[STOVE_STATUS_ALARM];
if ( (status[STOVE_STATUS_ALARM] !== STOVE_ALARM_STATUS_OK) && (status[STOVE_STATUS_ALARM] !== STOVE_ALARM_AWAITING_FLAME)) {
this.log.warn("Stove alarm is set: " + status[STOVE_STATUS_ALARM]);
}
this.status[STOVE_STATUS_CONNECTION] = status[STOVE_STATUS_CONNECTION];
if (status[STOVE_STATUS_CONNECTION] !== STOVE_CONNECTION_STATUS_OK) {
this.log.warn("Possible stove WiFi module connection error: " + status[STOVE_STATUS_CONNECTION]);
}
this.status[STOVE_STATUS_TIMESTAMP] = Date.now();
this.log.info("Stove status updated: " + JSON.stringify(this.status));
callback(null, true);
} catch (ex) {
this.log.error("Failed to parse stove status: " + ex.message);
callback(ex, null);
}
}
// Get current stove status if cache expired
_getStoveStatus(callback) {
if( (this.status[STOVE_STATUS_TIMESTAMP] + STOVE_STATUS_CACHE_KEEP) >= Date.now() ) {
this.log.debug("Stove status served from cache: " + JSON.stringify(this.status));
callback(null, true);
} else {
this._sendAPIMethod(API_METHOD_GET_STATE, null, (err, status) => {
if (status || !err) {
this._fillStatusCache(status, callback);
} else {
this.log.error("Failed to get stove status: " + err);
callback(err, null);
}
});
}
}
// Get ON/OFF state
getStoveActive(callback) {
let active = this.Characteristic.Active.INACTIVE;
this._getStoveStatus(
(err, ok) => {
if (ok || !err) {
if ( (this.status[STOVE_STATUS_CONNECTION] === STOVE_CONNECTION_STATUS_OK) && ((this.status[STOVE_STATUS_ALARM] === STOVE_ALARM_STATUS_OK) || (this.status[STOVE_STATUS_ALARM] === STOVE_ALARM_AWAITING_FLAME)) ) {
active = this.statusActiveMap.get(this.status[STOVE_STATUS_STATE]);
}
this.log.debug("getStoveActive: " + this.status[STOVE_STATUS_STATE] + ", " + this.status[STOVE_STATUS_CONNECTION] + ", " + this.status[STOVE_STATUS_ALARM] + " => " + active);
} else {
this.log.error("getStoveActive failed: " + err);
}
callback(err, active);
});
}
// Set ON/OFF
setStoveActive(state, callback) {
let method = API_METHOD_POWER_OFF;
if (state == this.Characteristic.Active.ACTIVE) {
method = API_METHOD_POWER_ON;
}
let dn = Date.now();
if ( this.statusActiveMap.get(this.status[STOVE_STATUS_STATE]) == state ) {
this.log.debug("Stove power swing protection: stove already at target state: " + state + " => " + this.statusActiveMap.get(this.status[STOVE_STATUS_STATE]));
callback(null);
} else if ( (dn - this.lastPowerChange) <= POWER_SWING_PROTECTION_DELAY ) {
let msg = "Stove power swing protection: last power change is too close in time (now " + dn + " vs. last change " + this.lastPowerChange + ")";
this.log.warn(msg);
callback(msg);
} else {
this._sendAPIMethod(method, null, (err, message) => {
if (message || !err) {
this.service.updateCharacteristic(this.Characteristic.Active, state);
this.lastPowerChange = Date.now();
this.log.info("Set stove to power " + state + ": " + JSON.stringify(message));
this._fillStatusCache(message, (ex, ok) => {
if (ok || !ex) {
this.log.debug("Filled stove status from setStoveActive result");
}
});
callback(null);
} else {
this.log.error("Failed to set stove power: " + err);
callback(err);
}
});
}
}
// Get running state (more precise than ON/OFF)
getStoveState(callback) {
let state = this.Characteristic.CurrentHeaterCoolerState.INACTIVE;
this._getStoveStatus(
(err, ok) => {
if (ok || !err) {
if (this.status[STOVE_STATUS_CONNECTION] === STOVE_CONNECTION_STATUS_OK) {
state = this.statusStateMap.get(this.status[STOVE_STATUS_STATE]);
}
this.log.debug("getStoveState: " + this.status[STOVE_STATUS_STATE] + ", " + this.status[STOVE_STATUS_CONNECTION] + ", " + this.status[STOVE_STATUS_ALARM] + " => " + state);
} else {
this.log.error("getStoveState failed: " + err);
}
callback(err, state);
});
}
// Get stove measured air temp
getStoveCurrentTemp(callback) {
let temp = STOVE_MIN_TEMP;
this._getStoveStatus(
(err, ok) => {
if (ok || !err) {
temp = this.status[STOVE_STATUS_CURRENT_TEMP];
this.log.debug("getStoveCurrentTemp: " + temp);
} else {
this.log.error("getStoveCurrentTemp failed: " + err);
}
callback(err, temp);
});
}
// Get threshold temperature from which to power on heating
getStoveSetTemp(callback) {
let temp = STOVE_MIN_TEMP_THRESHOLD;
this._getStoveStatus(
(err, ok) => {
if (ok || !err) {
temp = this.status[STOVE_STATUS_SET_TEMP];
this.log.debug("getStoveSetTemp: " + temp);
} else {
this.log.error("getStoveSetTemp failed: " + err);
}
callback(err, temp);
});
}
// Set threshold temperature from which to power on heating
setStoveTemp(temp, callback) {
let correctedtemp = temp;
if (temp > STOVE_MAX_TEMP_THRESHOLD) {
correctedtemp = STOVE_MAX_TEMP_THRESHOLD;
}
if (temp < STOVE_MIN_TEMP_THRESHOLD) {
correctedtemp = STOVE_MIN_TEMP_THRESHOLD;
}
this.log.debug("setStoveTemp: " + temp + " => " + correctedtemp);
this._sendAPIMethod(API_METHOD_SET_VAL, API_METHOD_TEMP_PARAM + correctedtemp, (err, message) => {
if (message || !err) {
this.service.updateCharacteristic(this.Characteristic.HeatingThresholdTemperature, correctedtemp);
this.log.info("Set stove heating temp to " + correctedtemp + ": " + JSON.stringify(message));
callback(null);
} else {
this.log.error("Failed to set stove heating temp: " + err);
callback(err);
}
});
}
// Get stove current running power
getStovePower(callback) {
let power = STOVE_MIN_POWER;
this._getStoveStatus(
(err, ok) => {
if (ok || !err) {
power = this.status[STOVE_STATUS_SET_POWER];
this.log.debug("getStovePower: " + power);
} else {
this.log.error("getStovePower failed: " + err);
}
callback(err, power);
});
}
// Set stove running power
setStovePower(power, callback) {
let correctedpower = power;
if (power > STOVE_MAX_POWER) {
correctedpower = STOVE_MAX_POWER;
}
if (power < STOVE_MIN_POWER) {
correctedpower = STOVE_MIN_POWER;
}
this.log.debug("setStovePower: " + power + " => " + correctedpower);
this._sendAPIMethod(API_METHOD_SET_VAL, API_METHOD_POWER_PARAM + correctedpower, (err, message) => {
if (message || !err) {
this.service.updateCharacteristic(this.Characteristic.RotationSpeed, correctedpower);
this.log.info("Set stove power to " + correctedpower + ": " + JSON.stringify(message));
callback(null);
} else {
this.log.error("Failed to set stove power: " + err);
callback(err);
}
});
}
}