This repository has been archived by the owner on Jan 7, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
1234 lines (1188 loc) · 56.7 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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
homebridge-micronova-agua-iot-stove
Homebridge plugin to manage a Micronova's Agua IOT WiFi module controlled stove.
Licensed under AGPL-3.0-only License [https://www.gnu.org/licenses/agpl-3.0.en.html].
Copyright (C) 2021, @securechicken
*/
const PLUGIN_NAME = "homebridge-micronova-agua-iot-stove";
const PLUGIN_AUTHOR = "@securechicken";
const PLUGIN_VERSION = "1.1.0";
const PLUGIN_DEVICE_MANUFACTURER = "Micronova Agua IOT";
const ACCESSORY_PLUGIN_NAME = "HeaterCoolerMicronovaAguaIOTStove";
const fetch = require("node-fetch");
const jwt = require("jsonwebtoken");
module.exports = (api) => {
api.registerAccessory(ACCESSORY_PLUGIN_NAME, HeaterCoolerMicronovaAguaIOTStove);
};
// Mapping of supported brands and associated settings.
const MAP_SUPPORTED_BRANDS = new Map([
["piazzetta", ["458632", "https://piazzetta.agua-iot.com/", "https://piazzetta.iot.web2app.it/api/bridge/endpoint/"]],
["evastampaggi", ["635987", "https://evastampaggi.agua-iot.com/"]],
["nordicfire", ["132678", "https://nordicfire.agua-iot.com/"]],
["alphaplam", ["862148", "https://alfaplam.agua-iot.com/"]],
["elfire", ["402762", "https://elfire.agua-iot.com/"]],
["karmekone", ["403873", "https://karmekone.agua-iot.com/"]],
["mcz1", ["354924", "https://remote.mcz.it/"]],
["mcz2", ["746318", "https://remote.mcz.it/"]],
["mcz3", ["354925", "https://remote.mcz.it/"]],
["lorflam", ["121567", "https://lorflam.agua-iot.com/"]],
["laminox", ["352678", "https://laminox.agua-iot.com/"]],
["boreal", ["173118", "https://boreal.agua-iot.com/"]],
["bronpi", ["164873", "https://bronpi.agua-iot.com/"]],
["solartecnik", ["326495", "https://solartecnik.agua-iot.com/"]],
["jollymec", ["732584", "https://jollymec.agua-iot.com/"]],
["globefire", ["634876", "https://globefire.agua-iot.com/"]],
["timsistem", ["046629", "https://timsistem.agua-iot.com/"]],
["stufepelletitalia", ["015142", "https://stufepelletitalia.agua-iot.com/"]],
["mycorisit", ["101427", "https://mycorisit.agua-iot.com/"]],
["fonteflame", ["848324", "https://fonteflame.agua-iot.com/"]],
["klover", ["143789", "https://klover.agua-iot.com/"]],
["amg", ["859435", "https://amg.agua-iot.com/"]],
["lineavz", ["521228", "https://lineavz.agua-iot.com/"]],
["thermoflux", ["391278", "https://thermoflux.agua-iot.com/"]],
["cola", ["475219", "https://cola.agua-iot.com/"]],
["moretti", ["624813", "https://moretti.agua-iot.com/"]],
["fontanaforni", ["505912", "https://fontanaforni.agua-iot.com/"]],
["nina", ["999999", "https://micronova.agua-iot.com/"]]
]);
// Stove status registers (data) constants
const REGISTER_KEY_ID = "reg_key";
const REGISTER_KEY_OFFSET = "offset";
const REGISTER_KEY_TYPE = "reg_type";
const REGISTER_KEY_FORMULA = "formula";
const REGISTER_KEY_FORMULAREV = "formula_inverse";
const REGISTER_KEY_FORMAT = "format_string";
const REGISTER_KEY_MIN = "set_min";
const REGISTER_KEY_MAX = "set_max";
const REGISTER_KEY_STEP = "step";
const REGISTER_KEY_MASK = "mask";
const REGISTER_KEY_ENCVAL = "enc_val";
const REGISTER_ENCVAL_KEY_LANG = "lang";
const REGISTER_ENCVAL_KEY_DESC = "description";
const REGISTER_ENCVAL_KEY_VAL = "value";
const REGISTER_INTERNAL_KEY_VALUE = "_v";
const REGISTER_INTERNAL_KEY_VALUEON = "_onv";
const REGISTER_INTERNAL_KEY_VALUEOFF = "_offv";
const REGISTER_VALUE_ENCVAL_LANG = "ENG";
const REGISTER_VALUE_ENCVAL_DESC_ON = "ON";
const REGISTER_VALUE_ENCVAL_DESC_OFF = "OFF";
const REGISTER_VALUE_FORMULA_VALPH = "#";
const REGISTER_VALUE_FORMULA_REGEX = /^([()\d][+\-*/]?)+$/;
const REGISTER_VALUE_STRING_VALPH = "{0}";
// API related constants
// Doc: http://<brand>.agua-iot.com:3001/api-docs/
const HTTP_TIMEOUT = 7000; // 7s in ms, web API can be laggy
const HTTP_RETRY_DELAY = 10000; // 10s in ms
const HTTP_STATUS_UNAUTH = 401;
const HTTP_REQ_ACCEPT_HEADER = "Accept";
const HTTP_REQ_CONTENT_HEADER = "Content-Type";
const HTTP_REQ_ORIGIN_HEADER = "Origin";
const HTTP_REQ_ID_BRAND_HEADER = "id_brand";
const HTTP_REQ_CUSTOMER_CODE_HEADER = "customer_code";
const HTTP_REQ_UA_HEADER = "User-Agent";
const HTTP_REQ_LOCAL_HEADER = "local";
const HTTP_REQ_AUTH_HEADER = "Authorization";
const HTTP_REQ_PIAZZETTA_URL = "url";
const HTTP_REQ_PIAZZETTA_APP_VERSION = "applicationversion";
const HTTP_REQ_PIAZZETTA_APP_VERSION_VALUE = "1.8.5";
const HTTP_ACCEPT = "application/json, text/javascript, */*; q=0.01";
const HTTP_CONTENT = "application/json";
const HTTP_ORIGIN = "file://";
const HTTP_ID_BRAND = "1";
const HTTP_UA = "homebridge-micronova-agua-iot-stove/" + PLUGIN_VERSION;
const API_ALIENS = ["piazzetta"];
const API_APPSIGNUP = "appSignup";
const POST_API_APPSIGNUP_KEY_TYPE = "phone_type";
const POST_API_APPSIGNUP_KEY_ID = "phone_id";
const POST_API_APPSIGNUP_KEY_VERSION = "phone_version";
const POST_API_APPSIGNUP_KEY_LANG = "language";
const POST_API_APPSIGNUP_KEY_IDAPP = "id_app";
const POST_API_APPSIGNUP_KEY_PUSHTOKEN = "push_notification_token";
const POST_API_APPSIGNUP_KEY_PUSHACTIVE = "push_notification_active";
const POST_API_APPSIGNUP_VALUE_TYPE = "Android";
const POST_API_APPSIGNUP_VALUE_LANG = "en";
const POST_API_APPSIGNUP_VALUE_PUSHACTIVE = false;
const API_LOGIN = "userLogin";
const POST_API_LOGIN_KEY_LOGIN = "email";
const POST_API_LOGIN_KEY_PASSWORD = "password";
const RESP_API_LOGIN_KEY_TOKEN = "token";
const RESP_API_LOGIN_KEY_REFRESHTOKEN = "refresh_token";
const API_REFRESHTOKEN = "refreshToken";
const POST_API_REFRESHTOKEN_KEY_REFRESHTOKEN = RESP_API_LOGIN_KEY_REFRESHTOKEN;
const API_DEVICESLIST = "deviceList";
const RESP_API_DEVICESLIST_KEY_DEVICES = "device";
const RESP_API_DEVICESLIST_KEY_DEVICE_ID = "id_device";
const RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT = "id_product";
const RESP_API_DEVICESLIST_KEY_DEVICE_NAME = "name";
const API_DEVICEINFO = "deviceGetInfo";
const POST_API_DEVICEINFO_KEY_ID = RESP_API_DEVICESLIST_KEY_DEVICE_ID;
const POST_API_DEVICEINFO_KEY_PRODUCT = RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT;
const RESP_API_DEVICEINFO_KEY_INFO = "device_info";
const RESP_API_DEVICEINFO_KEY_REGISTERSMAP_ID = "id_registers_map";
const API_DEVICEREGISTERSMAP = "deviceGetRegistersMap";
const POST_API_DEVICEREGISTERSMAP_KEY_ID = RESP_API_DEVICESLIST_KEY_DEVICE_ID;
const POST_API_DEVICEREGISTERSMAP_KEY_PRODUCT = RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT;
const POST_API_DEVICEREGISTERSMAP_KEY_LAST_UPDATE = "last_update";
const RESP_API_DEVICEREGISTERSMAP_KEY_L1 = "device_registers_map";
const RESP_API_DEVICEREGISTERSMAP_KEY_L2 = "registers_map";
const RESP_API_DEVICEREGISTERSMAP_KEY_ID = "id";
const RESP_API_DEVICEREGISTERSMAP_KEY_REGISTERS = "registers";
const RESP_API_DEVICEREGISTERSMAP_REGISTER_KEYS = [REGISTER_KEY_TYPE, REGISTER_KEY_OFFSET, REGISTER_KEY_FORMULA, REGISTER_KEY_FORMULAREV, REGISTER_KEY_FORMAT, REGISTER_KEY_MIN, REGISTER_KEY_MAX, REGISTER_KEY_STEP, REGISTER_KEY_MASK];
const API_DEVICEREAD = "deviceRequestReading";
const POST_API_DEVICEREAD_KEY_ID = RESP_API_DEVICESLIST_KEY_DEVICE_ID;
const POST_API_DEVICEREAD_KEY_PRODUCT = RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT;
const POST_API_DEVICEREAD_KEY_PROTO = "Protocol";
const POST_API_DEVICEREAD_KEY_BITDATA = "BitData";
const POST_API_DEVICEREAD_KEY_ENDIANESS = "Endianess";
const POST_API_DEVICEREAD_KEY_FREQ = "Freq";
const POST_API_DEVICEREAD_KEY_ITEMS = "Items";
const POST_API_DEVICEREAD_KEY_MASKS = "Masks";
const POST_API_DEVICEREAD_KEY_VALUES = "Values";
const POST_API_DEVICEREAD_VALUE_PROTO = "RWMSmaster";
const POST_API_DEVICEREAD_VALUE_BITDATA = 8;
const POST_API_DEVICEREAD_VALUE_ENDIANESS = "L";
const POST_API_DEVICEREAD_VALUE_FREQ = 0;
const RESP_API_DEVICEREAD_KEY_JOBID = "idRequest";
const API_DEVICEWRITE = "deviceRequestWriting";
const POST_API_DEVICEWRITE_KEY_ID = RESP_API_DEVICESLIST_KEY_DEVICE_ID;
const POST_API_DEVICEWRITE_KEY_PRODUCT = RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT;
const POST_API_DEVICEWRITE_KEY_PROTO = POST_API_DEVICEREAD_KEY_PROTO;
const POST_API_DEVICEWRITE_KEY_BITDATA = POST_API_DEVICEREAD_KEY_BITDATA;
const POST_API_DEVICEWRITE_KEY_ENDIANESS = POST_API_DEVICEREAD_KEY_ENDIANESS;
const POST_API_DEVICEWRITE_KEY_ITEMS = POST_API_DEVICEREAD_KEY_ITEMS;
const POST_API_DEVICEWRITE_KEY_MASKS = POST_API_DEVICEREAD_KEY_MASKS;
const POST_API_DEVICEWRITE_KEY_VALUES = POST_API_DEVICEREAD_KEY_VALUES;
const POST_API_DEVICEWRITE_VALUE_PROTO = POST_API_DEVICEREAD_VALUE_PROTO;
const POST_API_DEVICEWRITE_VALUE_BITDATA = POST_API_DEVICEREAD_VALUE_BITDATA;
const POST_API_DEVICEWRITE_VALUE_ENDIANESS = POST_API_DEVICEREAD_VALUE_ENDIANESS;
const RESP_API_DEVICEWRITE_KEY_JOBID = RESP_API_DEVICEREAD_KEY_JOBID;
const API_DEVICEJOBSTATUS = "deviceJobStatus";
const API_DEVICEJOBSTATUS_MAX_RETRIES = 15;
const API_DEVICEJOBSTATUS_DELAY_RETRY = 1000; // 1000 ms
const RESP_API_DEVICEJOBSTATUS_KEY_STATUS = "jobAnswerStatus";
const RESP_API_DEVICEJOBSTATUS_KEY_RESULT = "jobAnswerData";
const RESP_API_DEVICEJOBSTATUS_RESULT_KEY_ITEMS = POST_API_DEVICEWRITE_KEY_ITEMS;
const RESP_API_DEVICEJOBSTATUS_RESULT_KEY_VALUES = POST_API_DEVICEWRITE_KEY_VALUES;
const RESP_API_DEVICEJOBSTATUS_RESULT_WRITE_KEY_ERRCODE = "NackErrCode";
const RESP_API_DEVICEJOBSTATUS_VALUE_STATUS_OK = "completed";
const RESP_API_DEVICEJOBSTATUS_VALUE_STATUS_NTD = "terminated";
const DATE_NEVER = "1970-01-01T00:00:00.000Z";
const API_AUTH_REFRESH_DELAY = 14400000; // 4h in ms
const API_UPDATE_VALUES_DELAY = 3600000; // 1h in ms
// Module stove management constants
const POWER_SWING_PROTECTION_DELAY = 3600000; // 1h in ms
const STOVE_ALARM_REGISTER = "alarms_get";
const STOVE_ALARM_IGNORE_VALUES = [0]; // 0 is no alarm
const STOVE_POWER_STATE_INFO_REGISTER = "status_managed_get";
const STOVE_POWER_STATE_SET_ON_REGISTER = STOVE_POWER_STATE_INFO_REGISTER;
const STOVE_POWER_STATE_SET_OFF_REGISTER = STOVE_POWER_STATE_INFO_REGISTER;
const STOVE_STATE_REGISTER = "status_get";
const STOVE_CURRENT_TEMP_REGISTER = "temp_air_get";
const STOVE_SET_TEMP_REGISTER = "temp_air_set";
const STOVE_CURRENT_POWER_REGISTER = "power_set"; // real_power_get for applied power
const STOVE_SET_POWER_REGISTER = "power_set";
const STOVE_REGISTERS_CACHE_KEEP = 10000; // 10s in ms
const STOVE_READ_REGISTERS = [STOVE_ALARM_REGISTER, STOVE_POWER_STATE_INFO_REGISTER, STOVE_STATE_REGISTER, STOVE_CURRENT_TEMP_REGISTER, STOVE_SET_TEMP_REGISTER, STOVE_CURRENT_POWER_REGISTER];
class HeaterCoolerMicronovaAguaIOTStove {
constructor(log, config, api) {
// Intro common module init
this.config = config;
this.api = api;
this.log = log;
this.Service = this.api.hap.Service;
this.Characteristic = this.api.hap.Characteristic;
if (!( (this.config) && (this.config.brand) && (this.config.login) && (this.config.password) )) {
this.log.error("Plugin configuration is not valid: every value must be set");
return;
}
this._debug("init config for " + this.config.brand + " brand with login: " + this.config.login);
// Mappings between HomeKit states and API returned one.
this.defaultStatePair = [this.Characteristic.Active.ACTIVE, this.Characteristic.CurrentHeaterCoolerState.IDLE];
this.stateMap = new Map([
[0, [this.Characteristic.Active.INACTIVE, this.Characteristic.CurrentHeaterCoolerState.INACTIVE]], // OFF, OFF E
[1, [this.Characteristic.Active.ACTIVE, this.Characteristic.CurrentHeaterCoolerState.HEATING]], // LOAD PELLETS
[2, [this.Characteristic.Active.ACTIVE, this.Characteristic.CurrentHeaterCoolerState.HEATING]], // AWAITING FLAME
[3, [this.Characteristic.Active.ACTIVE, this.Characteristic.CurrentHeaterCoolerState.HEATING]], // LIGHTING
[4, [this.Characteristic.Active.ACTIVE, this.Characteristic.CurrentHeaterCoolerState.HEATING]], // WORRKING
[5, [this.Characteristic.Active.ACTIVE, this.Characteristic.CurrentHeaterCoolerState.HEATING]], // ASHPAN CLEANING
[6, this.defaultStatePair], // FINAL CLEANING
[7, this.defaultStatePair], // STANDBY
[8, this.defaultStatePair], // ALARM
[9, this.defaultStatePair], // ALARM MEMORY
[10, this.defaultStatePair], // JOLLY MEC = ?
[11, this.defaultStatePair], // JOLLY MEC = ?
[12, this.defaultStatePair], // ?
]);
// Authentication and API root URL infos
this.apiRoot = MAP_SUPPORTED_BRANDS.get(this.config.brand)[1];
this.apiCustomerCode = MAP_SUPPORTED_BRANDS.get(this.config.brand)[0];
this.apiAppUUID = this.api.hap.uuid.generate(PLUGIN_NAME);
this.apiPhoneUUID = this.api.hap.uuid.generate(this.config.login);
this.apiIsAuth = false;
this.apiAuthToken = null;
this.apiAuthRefreshToken = null;
this.jobAutoLogin = null;
this.apiHTTPHeaders = {};
this.apiHTTPHeaders[HTTP_REQ_ACCEPT_HEADER] = HTTP_ACCEPT;
this.apiHTTPHeaders[HTTP_REQ_CONTENT_HEADER] = HTTP_CONTENT;
this.apiHTTPHeaders[HTTP_REQ_ORIGIN_HEADER] = HTTP_ORIGIN;
this.apiHTTPHeaders[HTTP_REQ_ID_BRAND_HEADER] = HTTP_ID_BRAND;
this.apiHTTPHeaders[HTTP_REQ_CUSTOMER_CODE_HEADER] = this.apiCustomerCode;
this.apiHTTPHeaders[HTTP_REQ_UA_HEADER] = HTTP_UA;
// Stove device
this.apiStoveDeviceID = null;
this.apiStoveDeviceProduct = null;
// Stove registers and associated data
this.apiStoveRegisters = null; // { registername: {register_key: value, ...}, ...}
this.apiStoveOffsetsRegistersMap = new Map(); // { offset: [registername, ...], ...}
this.apiStoveAlarmsMap = new Map(); // { value: "error code", ...}
this.apiStoveRegistersSet = false; // Registers map initialized from API
this.lastStoveRegistersUpdate = null; // Last update of registers data from API, to enable cache
// Magic object value to ensure an ongoing API read job is completed before any
// other one is scheduled
this.apiPendingReadJob = false;
// Anti power swinging protection
this.lastStovePowerChange = null;
// Default characteristics value when nothing is available yet
this.stoveCharDefaultActive = this.Characteristic.Active.INACTIVE;
this.stoveCharDefaultState = this.Characteristic.CurrentHeaterCoolerState.INACTIVE;
this.stoveCharDefaultTemp = 0;
this.stoveCharDefaultSetTemp = 0;
this.stoveCharDefaultPower = 0;
// Heater Cooler service
const sname = this.config.name || ACCESSORY_PLUGIN_NAME;
this.stoveService = new this.Service.HeaterCooler(sname);
// Device infos
this.infoService = new this.Service.AccessoryInformation()
.setCharacteristic(this.Characteristic.Name, sname)
.setCharacteristic(this.Characteristic.Manufacturer, PLUGIN_DEVICE_MANUFACTURER)
.setCharacteristic(this.Characteristic.Model, this.config.brand)
.setCharacteristic(this.Characteristic.SerialNumber, this.apiPhoneUUID)
.setCharacteristic(this.Characteristic.SoftwareRevision, PLUGIN_NAME)
.setCharacteristic(this.Characteristic.FirmwareRevision, PLUGIN_VERSION)
.setCharacteristic(this.Characteristic.HardwareRevision, PLUGIN_AUTHOR);
// Register app at start, then login, then get a stove device, and associated values
this._initPluginFromAPI();
// Set characteristics properties boundaries and valid values
// Setting CurrentHeaterCoolerState and TargetHeaterCoolerState allows to
// lock device to heater mode only
this.stoveService.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.stoveService.getCharacteristic(this.Characteristic.TargetHeaterCoolerState)
.setProps({
minValue: this.Characteristic.TargetHeaterCoolerState.HEAT,
maxValue: this.Characteristic.TargetHeaterCoolerState.HEAT,
validValues: [this.Characteristic.TargetHeaterCoolerState.HEAT]
});
this.stoveService.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.stoveService.getCharacteristic(this.Characteristic.SwingMode)
.setProps({
minValue: this.Characteristic.SwingMode.SWING_DISABLED,
maxValue: this.Characteristic.SwingMode.SWING_DISABLED,
validValues: [this.Characteristic.SwingMode.SWING_DISABLED]
});
this.stoveService.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.stoveService.setCharacteristic(this.Characteristic.Active, this.stoveCharDefaultActive);
this.stoveService.setCharacteristic(this.Characteristic.CurrentHeaterCoolerState, this.stoveCharDefaultState);
this.stoveService.setCharacteristic(this.Characteristic.TargetHeaterCoolerState, this.stoveCharDefaultState);
this.stoveService.setCharacteristic(this.Characteristic.TemperatureDisplayUnits, this.Characteristic.TemperatureDisplayUnits.CELSIUS);
this.stoveService.setCharacteristic(this.Characteristic.LockPhysicalControls, this.Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED);
this.stoveService.setCharacteristic(this.Characteristic.SwingMode, this.Characteristic.SwingMode.SWING_DISABLED);
}
// Mandatory services export method
getServices() {
return [this.infoService, this.stoveService];
}
// Debug logs output
_debug(message) {
if (this.config.debug) {
this.log.info("[DEBUG] " + message);
} else {
this.log.debug(message);
}
}
// Init plugin from API required data
_initPluginFromAPI() {
this._registerAPIApp( (err, appok) => {
if (appok || !err) {
this._setAPILogin(false, (err, tokok) => {
if (tokok || !err) {
this._getAPIStoveDevice( (err, okmap) => {
if (okmap || !err) {
// Set API provided characteristics limits
this._getStoveRegisterBoundaries(STOVE_CURRENT_TEMP_REGISTER, (err, boundaries) => {
if (boundaries || !err) {
this.stoveService.getCharacteristic(this.Characteristic.CurrentTemperature)
.setProps({minValue: boundaries[0], maxValue: boundaries[1], minStep: boundaries[2]});
this.stoveCharDefaultTemp = boundaries[0];
} else {
this.log.error("init could not get stove current temperature boundaries: " + err);
}
});
this._getStoveRegisterBoundaries(STOVE_SET_TEMP_REGISTER, (err, boundaries) => {
if (boundaries || !err) {
this.stoveService.getCharacteristic(this.Characteristic.HeatingThresholdTemperature)
.setProps({minValue: boundaries[0], maxValue: boundaries[1], minStep: boundaries[2]});
this.stoveCharDefaultSetTemp = boundaries[0];
} else {
this.log.error("init could not get stove temperature threshold boundaries: " + err);
}
});
this._getStoveRegisterBoundaries(STOVE_SET_POWER_REGISTER, (err, boundaries) => {
if (boundaries || !err) {
this.stoveService.getCharacteristic(this.Characteristic.RotationSpeed)
.setProps({minValue: boundaries[0], maxValue: boundaries[1], minStep: boundaries[2]});
this.stoveCharDefaultPower = boundaries[0];
} else {
this.log.error("init could not get stove power boundaries: " + err);
}
});
// Ask for registers data and Homebridge chracteristics update, now and then
// regularly
this._updateCharacteristicsValues();
setInterval(this._updateCharacteristicsValues.bind(this), API_UPDATE_VALUES_DELAY);
// Services methods and events handling
// Set them in API callback so they do not trigger events while API provided infos are not yet retrieved
this.stoveService.getCharacteristic(this.Characteristic.Active)
.on("get", this.getStoveActive.bind(this))
.on("set", this.setStoveActive.bind(this));
this.stoveService.getCharacteristic(this.Characteristic.CurrentHeaterCoolerState)
.on("get", this.getStoveState.bind(this));
this.stoveService.getCharacteristic(this.Characteristic.CurrentTemperature)
.on("get", this.getStoveCurrentTemp.bind(this));
this.stoveService.getCharacteristic(this.Characteristic.HeatingThresholdTemperature)
.on("get", this.getStoveSetTemp.bind(this))
.on("set", this.setStoveTemp.bind(this));
this.stoveService.getCharacteristic(this.Characteristic.RotationSpeed)
.on("get", this.getStovePower.bind(this))
.on("set", this.setStovePower.bind(this));
} else {
this.log.error("init could not retrieve required stove info from API: " + err);
}
});
} else {
this.log.error("init could not login once with API: " + err);
}
});
} else {
this.log.error("init could not register app with API: " + err);
}
});
}
// API app registering helper
_registerAPIApp(callback) {
const url = this.apiRoot + API_APPSIGNUP;
let postdata = {};
postdata[POST_API_APPSIGNUP_KEY_TYPE] = POST_API_APPSIGNUP_VALUE_TYPE;
postdata[POST_API_APPSIGNUP_KEY_ID] = this.apiPhoneUUID;
postdata[POST_API_APPSIGNUP_KEY_VERSION] = PLUGIN_VERSION;
postdata[POST_API_APPSIGNUP_KEY_LANG] = POST_API_APPSIGNUP_VALUE_LANG;
postdata[POST_API_APPSIGNUP_KEY_IDAPP] = this.apiAppUUID;
postdata[POST_API_APPSIGNUP_KEY_PUSHTOKEN] = this.apiPhoneUUID;
postdata[POST_API_APPSIGNUP_KEY_PUSHACTIVE] = POST_API_APPSIGNUP_VALUE_PUSHACTIVE;
fetch(url, {method: "POST", body: JSON.stringify(postdata), timeout: HTTP_TIMEOUT, headers: this.apiHTTPHeaders, redirect: "error"})
.then( (resp) => {
if (resp.ok) {
this._debug("_registerAPIApp got a response with OK status");
callback(null, true);
} else {
throw new Error("_registerAPIApp could not register an app to API (wrong brand in config), got status: " + resp.status);
}
})
.catch( (err) => {
this.log.error("_registerAPIApp HTTP request failed. Retrying... Reason: " + err.message);
setTimeout(this._registerAPIApp.bind(this), HTTP_RETRY_DELAY, callback);
//callback(err.message, null);
});
}
// API login and auth auto-refresh helper
_setAPILogin(refresh, callback) {
if (!refresh) {
this._debug("_setAPILogin attempting log-in");
} else {
this._debug("_setAPILogin refreshing authentication");
}
this.apiIsAuth = false;
let url = this.apiRoot;
if (!refresh) {
// Specific case for Piazzetta
// The user login has to be done at Piazzetta frontends mobile app server
// with additional headers, but the response is a standard JWT token then and
// can be used with Agua IOT API as before.
if (API_ALIENS.includes(this.config.brand)) {
url = MAP_SUPPORTED_BRANDS.get(this.config.brand)[2];
} else {
url += API_LOGIN;
}
} else {
url += API_REFRESHTOKEN;
}
let loginheaders = {...this.apiHTTPHeaders};
if (!refresh) {
loginheaders[HTTP_REQ_LOCAL_HEADER] = true;
loginheaders[HTTP_REQ_AUTH_HEADER] = this.apiAppUUID;
// Specific case for Piazzetta
if (API_ALIENS.includes(this.config.brand)) {
loginheaders[HTTP_REQ_PIAZZETTA_URL] = API_LOGIN;
loginheaders[HTTP_REQ_PIAZZETTA_APP_VERSION] = HTTP_REQ_PIAZZETTA_APP_VERSION_VALUE;
}
}
let postdata = {};
if (!refresh) {
postdata[POST_API_LOGIN_KEY_LOGIN] = this.config.login;
postdata[POST_API_LOGIN_KEY_PASSWORD] = this.config.password;
} else {
postdata[POST_API_REFRESHTOKEN_KEY_REFRESHTOKEN] = this.apiAuthRefreshToken;
}
fetch(url, {method: "POST", body: JSON.stringify(postdata), timeout: HTTP_TIMEOUT, headers: loginheaders, redirect: "error"})
.then( (resp) => {
if (resp.ok) {
return resp.json();
} else {
throw new Error("_setAPILogin authentication rejected by API, got status: " + resp.status);
}
})
.then( (jsonresp) => {
this._debug("_setAPILogin got a OK response");
let jwset = null;
let jwref = null;
if (jsonresp && (RESP_API_LOGIN_KEY_TOKEN in jsonresp)) {
jwset = jwt.decode(jsonresp[RESP_API_LOGIN_KEY_TOKEN]);
this._debug("_setAPILogin got JWT token decoded as: " + JSON.stringify(jwset));
if (RESP_API_LOGIN_KEY_REFRESHTOKEN in jsonresp) {
jwref = jwt.decode(jsonresp[RESP_API_LOGIN_KEY_REFRESHTOKEN]);
this._debug("_setAPILogin got JWT refresh token decoded as: " + JSON.stringify(jwref));
}
}
if (jwset) {
this.apiAuthToken = jsonresp[RESP_API_LOGIN_KEY_TOKEN];
this.apiIsAuth = true;
if (jwref && !refresh) {
this.apiAuthRefreshToken = jsonresp[RESP_API_LOGIN_KEY_REFRESHTOKEN];
this.log.info("_setAPILogin successfully logged-in, setting auto-login refresh job every " + (API_AUTH_REFRESH_DELAY / 1000 / 60 / 60) + " h");
if (this.jobAutoLogin !== null) {
clearInterval(this.jobAutoLogin);
this.jobAutoLogin = null;
}
this.jobAutoLogin = setInterval(this._setAPILogin.bind(this), API_AUTH_REFRESH_DELAY, true, (err, ok) => {
if (err || !ok) {
this._debug("_setAPILogin failed to auto-refresh token, rebooting to a regular auth: " + err);
clearInterval(this.jobAutoLogin);
this._setAPILogin(false, (err, ok) => {
if (err || !ok) {
this.log.error("_setAPILogin shitstorm (in autologin job failover): " + err);
}
});
}
});
} else {
this.log.info("_setAPILogin successfully refreshed authentication");
}
callback(null, true);
} else {
throw new Error("_setAPILogin did not get the expected JWT token back from API: " + JSON.stringify(jsonresp));
}
})
.catch( (err) => {
this.apiIsAuth = false;
this.apiAuthToken = null;
this.apiAuthRefreshToken = null;
// Try forever on regular login
if( !refresh ) {
this.log.error("_setAPILogin HTTP login request failed. Retrying... Reason: " + err.message);
setTimeout(this._setAPILogin.bind(this), HTTP_RETRY_DELAY, false, callback);
} else {
this._debug("_setAPILogin HTTP refresh request failed. Reason: " + err.message);
callback(err.message, null);
}
});
}
// Send an authenticated request to API
_sendAPIRequest(endpoint, httpmethod, postdata, callback) {
if (this.apiIsAuth) {
const url = this.apiRoot + endpoint;
let requestheaders = {...this.apiHTTPHeaders};
requestheaders[HTTP_REQ_LOCAL_HEADER] = false;
requestheaders[HTTP_REQ_AUTH_HEADER] = this.apiAuthToken;
this._debug("_sendAPIRequest " + httpmethod + " to: " + url + ", DATA = " + postdata);
fetch(url, {method: httpmethod, body: postdata, timeout: HTTP_TIMEOUT, headers: requestheaders})
.then( (resp) => {
if (resp.ok) {
this.counter += 1;
return resp.json();
} else if (resp.status === HTTP_STATUS_UNAUTH) {
this.counter = 0;
this.apiIsAuth = false;
this._setAPILogin(false, (inerr, tokok) => {
if (tokok && !inerr) {
this._debug("_sendAPIRequest has renewed login because of 401 error");
} else {
this._debug("_sendAPIRequest could not renew login after a 401 error: " + inerr);
}
});
throw new Error("_sendAPIRequest got 401 status, not logged-in or token expired");
} else {
throw new Error("_sendAPIRequest got non-OK non-401 HTTP response status: " + resp.status);
}
})
.then( (jsonresp) => {
this._debug("_sendAPIRequest got a response with OK status");
callback(null, jsonresp);
})
.catch( (err) => {
this.log.error("_sendAPIRequest HTTP request failed. Retrying... Reason: " + err.message);
setTimeout(this._sendAPIRequest.bind(this), HTTP_RETRY_DELAY, endpoint, httpmethod, postdata, callback);
});
} else {
callback("_sendAPIRequest could not send API request: not logged-in...", null);
}
}
// Get a job result from API, or fail if max attempts reached
_getAPIJobResult(jobid, attempt, callback) {
if (this.apiStoveRegistersSet) {
this._debug("_getAPIJobResult " + jobid + " attempt " + attempt + " start");
if (attempt < API_DEVICEJOBSTATUS_MAX_RETRIES) {
const url = API_DEVICEJOBSTATUS + "/" + jobid;
this._sendAPIRequest(url, "GET", null, (err, json) => {
if (json || !err) {
if ((RESP_API_DEVICEJOBSTATUS_KEY_STATUS in json)) {
this._debug("_getAPIJobResult " + jobid + " attempt " + attempt + " result: " + json[RESP_API_DEVICEJOBSTATUS_KEY_STATUS]);
if (json[RESP_API_DEVICEJOBSTATUS_KEY_STATUS] === RESP_API_DEVICEJOBSTATUS_VALUE_STATUS_OK) {
if (RESP_API_DEVICEJOBSTATUS_KEY_RESULT in json) {
callback(null, json[RESP_API_DEVICEJOBSTATUS_KEY_RESULT]);
} else {
callback("_getAPIJobResult got job result without data: " + JSON.stringify(json), null);
}
} else if (json[RESP_API_DEVICEJOBSTATUS_KEY_STATUS] === RESP_API_DEVICEJOBSTATUS_VALUE_STATUS_NTD) {
this._debug("_getAPIJobResult " + jobid + " finished successfully without returning data");
callback(null, null);
} else {
this._debug("_getAPIJobResult " + jobid + " attempt " + attempt + " needs to schedule another attempt");
setTimeout(this._getAPIJobResult.bind(this), API_DEVICEJOBSTATUS_DELAY_RETRY, jobid, attempt + 1, callback);
}
} else {
callback("_getAPIJobResult did not get expected job result from API: " + JSON.stringify(json), null);
}
} else {
callback("_getAPIJobResult API request failed: " + err, null);
}
});
} else {
callback("_getAPIJobResult did not complete in " + API_DEVICEJOBSTATUS_MAX_RETRIES + " requests", null);
}
} else {
callback("_getAPIJobResult cannot query job result: stove registers map is not set", null);
}
}
// Get the list of stove devices known for the config given account from API, and select first
_getAPIStoveDevicesList(callback) {
this._sendAPIRequest(API_DEVICESLIST, "POST", null, (err, json) => {
if (json || !err) {
if ((RESP_API_DEVICESLIST_KEY_DEVICES in json) && (json[RESP_API_DEVICESLIST_KEY_DEVICES].length > 0)) {
let founddev = null;
let errmess = null;
for (const device of json[RESP_API_DEVICESLIST_KEY_DEVICES]) {
this._debug("_getAPIStoveDevicesList checking device from API: " + JSON.stringify(device));
if ((RESP_API_DEVICESLIST_KEY_DEVICE_ID in device) && (RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT in device) &&
(RESP_API_DEVICESLIST_KEY_DEVICE_NAME in device)) {
const devicename = device[RESP_API_DEVICESLIST_KEY_DEVICE_NAME];
if (devicename === this.config.name) {
founddev = true;
this.apiStoveDeviceID = device[RESP_API_DEVICESLIST_KEY_DEVICE_ID];
this.apiStoveDeviceProduct = device[RESP_API_DEVICESLIST_KEY_DEVICE_PRODUCT];
this._debug("_getAPIStoveDevicesList found config device: " + devicename);
}
} else {
errmess = "_getAPIStoveDevicesList did not get expected result for a device from API: " + JSON.stringify(device);
break;
}
}
if (!founddev) {
errmess = "_getAPIStoveDevicesList did not find stove name in API - PLEASE FIX THIS PLUGIN CONFIG VALUE & RESTART: " + this.config.name;
}
callback(errmess, founddev);
} else {
callback("_getAPIStoveDevicesList did not get expected result from API: " + JSON.stringify(json), null);
}
} else {
callback("_getAPIStoveDevicesList failed: " + err, null);
}
});
}
// Get the list of stove devices from API, select first one for further use, and get its registers map
_getAPIStoveDevice(callback) {
this._getAPIStoveDevicesList((err, founddev) => {
if (founddev || !err) {
this._getAPIStoveDeviceInfo((err, registersmapid) => {
if (registersmapid || !err) {
this._getAPIStoveRegistersMap(registersmapid, callback);
} else {
callback("_getAPIStoveDevice could not retrieve device registers map ID from API: " + err, null);
}
});
} else {
callback("_getAPIStoveDevice could not retrieve selected device (" + this.config.name + ") from API devices list: " + err, null);
}
});
}
// Get device info for previously retrieved device ID/product, from API
_getAPIStoveDeviceInfo(callback) {
let devicepostdata = {};
devicepostdata[POST_API_DEVICEINFO_KEY_ID] = this.apiStoveDeviceID;
devicepostdata[POST_API_DEVICEINFO_KEY_PRODUCT] = this.apiStoveDeviceProduct;
this._sendAPIRequest(API_DEVICEINFO, "POST", JSON.stringify(devicepostdata), (err, json) => {
if (json || !err) {
if ((RESP_API_DEVICEINFO_KEY_INFO in json) && (json[RESP_API_DEVICEINFO_KEY_INFO].length > 0) &&
(RESP_API_DEVICEINFO_KEY_REGISTERSMAP_ID in json[RESP_API_DEVICEINFO_KEY_INFO][0])) {
this._debug("_getAPIStoveDeviceInfo got infos " + JSON.stringify(json));
callback(null, json[RESP_API_DEVICEINFO_KEY_INFO][0][RESP_API_DEVICEINFO_KEY_REGISTERSMAP_ID]);
} else {
callback("_getAPIStoveDeviceInfo did not get expected result (map ID) from API: " + JSON.stringify(json), null);
}
} else {
callback("_getAPIStoveDeviceInfo failed: " + err, null);
}
});
}
// Get device registers map for previously retrieved device ID/product and given registers map ID, from API
_getAPIStoveRegistersMap(registersmapid, callback) {
let regmappostdata = {};
regmappostdata[POST_API_DEVICEREGISTERSMAP_KEY_ID] = this.apiStoveDeviceID;
regmappostdata[POST_API_DEVICEREGISTERSMAP_KEY_PRODUCT] = this.apiStoveDeviceProduct;
regmappostdata[POST_API_DEVICEREGISTERSMAP_KEY_LAST_UPDATE] = DATE_NEVER;
this._sendAPIRequest(API_DEVICEREGISTERSMAP, "POST", JSON.stringify(regmappostdata), (err, json) => {
if (json || !err) {
if ((RESP_API_DEVICEREGISTERSMAP_KEY_L1 in json) && (RESP_API_DEVICEREGISTERSMAP_KEY_L2 in json[RESP_API_DEVICEREGISTERSMAP_KEY_L1])) {
const registersmaps = json[RESP_API_DEVICEREGISTERSMAP_KEY_L1][RESP_API_DEVICEREGISTERSMAP_KEY_L2];
this.apiStoveRegisters = {};
let brokein = false;
let errmess = null;
for (const registersmap of registersmaps) {
if (!brokein && (RESP_API_DEVICEREGISTERSMAP_KEY_ID in registersmap) && (RESP_API_DEVICEREGISTERSMAP_KEY_REGISTERS in registersmap)) {
if (registersmap[RESP_API_DEVICEREGISTERSMAP_KEY_ID] === registersmapid) {
for (const register of registersmap[RESP_API_DEVICEREGISTERSMAP_KEY_REGISTERS]) {
if (!brokein && (REGISTER_KEY_ID in register) && (REGISTER_KEY_OFFSET in register)) {
const regid = register[REGISTER_KEY_ID];
const regoffset = register[REGISTER_KEY_OFFSET];
this.apiStoveRegisters[regid] = {};
this.apiStoveRegisters[regid][REGISTER_INTERNAL_KEY_VALUE] = null;
if (this.apiStoveOffsetsRegistersMap && this.apiStoveOffsetsRegistersMap.has(regoffset)) {
let sameoffsetregisters = this.apiStoveOffsetsRegistersMap.get(regoffset);
sameoffsetregisters.push(regid);
this.apiStoveOffsetsRegistersMap.set(regoffset, sameoffsetregisters);
} else {
this.apiStoveOffsetsRegistersMap.set(regoffset, [regid]);
}
for (const reqregisterkey of RESP_API_DEVICEREGISTERSMAP_REGISTER_KEYS) {
if ((reqregisterkey in register)) {
this.apiStoveRegisters[regid][reqregisterkey] = register[reqregisterkey];
} else {
errmess = "_getAPIStoveRegistersMap got an unexpected JSON register from API: " + JSON.stringify(register);
brokein = true;
break;
}
}
// Get ON/OFF values according to value encoding tables of registers if any, as well as alarm error codes
if (!brokein && (REGISTER_KEY_ENCVAL in register)) {
for (const encval of register[REGISTER_KEY_ENCVAL]) {
if ((REGISTER_ENCVAL_KEY_LANG in encval) && (REGISTER_ENCVAL_KEY_DESC in encval) &&
encval[REGISTER_ENCVAL_KEY_LANG] === REGISTER_VALUE_ENCVAL_LANG) {
if (encval[REGISTER_ENCVAL_KEY_DESC] === REGISTER_VALUE_ENCVAL_DESC_ON) {
this.apiStoveRegisters[regid][REGISTER_INTERNAL_KEY_VALUEON] = encval[REGISTER_ENCVAL_KEY_VAL];
} else if (encval[REGISTER_ENCVAL_KEY_DESC] === REGISTER_VALUE_ENCVAL_DESC_OFF) {
this.apiStoveRegisters[regid][REGISTER_INTERNAL_KEY_VALUEOFF] = encval[REGISTER_ENCVAL_KEY_VAL];
} else if (regid === STOVE_ALARM_REGISTER) {
this.apiStoveAlarmsMap.set(encval[REGISTER_ENCVAL_KEY_VAL], encval[REGISTER_ENCVAL_KEY_DESC]);
}
}
}
}
} else {
if (!brokein) {
errmess = "_getAPIStoveRegistersMap got an unexpected JSON register from API: " + JSON.stringify(register);
brokein = true;
}
break;
}
}
}
} else {
if (!brokein) {
errmess = "_getAPIStoveRegistersMap got unexpected registers map JSON from API: " + Object.keys(registersmaps);
brokein = true;
}
break;
}
}
if (!brokein) {
this.apiStoveRegistersSet = true;
this._debug("_getAPIStoveRegistersMap retrieved registers map");
for (const offnamecouple of this.apiStoveOffsetsRegistersMap.entries()) {
this._debug(offnamecouple[0] + " => " + offnamecouple[1]);
}
callback(null, true);
} else {
callback(errmess, null);
}
} else {
callback("_getAPIStoveRegistersMap did not get expected results from API: " + JSON.stringify(Object.keys(json)), null);
}
} else {
callback("_getAPIStoveRegistersMap failed: " + err, null);
}
});
}
// Read required stove registers data from API
_readAPIStoveRegisters(callback) {
let readitems = [];
let readmasks = [];
function gsrCb(err, register) {
if (register || !err) {
readitems.push(register[REGISTER_KEY_OFFSET]);
readmasks.push(register[REGISTER_KEY_MASK]);
}
}
for (let registername of STOVE_READ_REGISTERS) {
this._getStoveRegister(registername, gsrCb);
}
if (readitems.length === STOVE_READ_REGISTERS.length) {
this._debug("_readAPIStoveRegisters asked to read");
let regreadpostdata = {};
regreadpostdata[POST_API_DEVICEREAD_KEY_ID] = this.apiStoveDeviceID;
regreadpostdata[POST_API_DEVICEREAD_KEY_PRODUCT] = this.apiStoveDeviceProduct;
regreadpostdata[POST_API_DEVICEREAD_KEY_PROTO] = POST_API_DEVICEREAD_VALUE_PROTO;
regreadpostdata[POST_API_DEVICEREAD_KEY_BITDATA] = POST_API_DEVICEREAD_VALUE_BITDATA;
regreadpostdata[POST_API_DEVICEREAD_KEY_ENDIANESS] = POST_API_DEVICEREAD_VALUE_ENDIANESS;
regreadpostdata[POST_API_DEVICEREAD_KEY_FREQ] = POST_API_DEVICEREAD_VALUE_FREQ;
regreadpostdata[POST_API_DEVICEREAD_KEY_ITEMS] = readitems;
regreadpostdata[POST_API_DEVICEREAD_KEY_MASKS] = readmasks;
this._sendAPIRequest(API_DEVICEREAD, "POST", JSON.stringify(regreadpostdata), (err, json) => {
if (json || !err) {
if ((RESP_API_DEVICEREAD_KEY_JOBID in json)) {
this._waitForRegistersDataReadJobResult(json[RESP_API_DEVICEREAD_KEY_JOBID], (err, registersok) => {
callback(err, registersok);
});
} else {
callback("_readAPIStoveRegisters did not get expected result from API: " + JSON.stringify(json));
}
} else {
callback("_readAPIStoveRegisters failed to request registers read with API: " + err, null);
}
});
} else {
callback("_readAPIStoveRegisters failed to get stove registers before trying to read them", null);
}
}
// Write a stove register to API
_writeAPIStoveRegister(registername, value, callback) {
this._getStoveRegister(registername, (err, register) => {
if (register || !err) {
if (value >= register[REGISTER_KEY_MIN] && value <= register[REGISTER_KEY_MAX]) {
const calcedval = this._calculateStoveValue(register, false, true, value);
if (calcedval) {
this._debug("_writeAPIStoveRegister asked to write " + registername + "=" + value + " => " + register[REGISTER_KEY_OFFSET] + "=" + calcedval);
let regwritepostdata = {};
regwritepostdata[POST_API_DEVICEWRITE_KEY_ID] = this.apiStoveDeviceID;
regwritepostdata[POST_API_DEVICEWRITE_KEY_PRODUCT] = this.apiStoveDeviceProduct;
regwritepostdata[POST_API_DEVICEWRITE_KEY_PROTO] = POST_API_DEVICEWRITE_VALUE_PROTO;
regwritepostdata[POST_API_DEVICEWRITE_KEY_BITDATA] = POST_API_DEVICEWRITE_VALUE_BITDATA;
regwritepostdata[POST_API_DEVICEWRITE_KEY_ENDIANESS] = POST_API_DEVICEWRITE_VALUE_ENDIANESS;
regwritepostdata[POST_API_DEVICEWRITE_KEY_ITEMS] = [register[REGISTER_KEY_OFFSET]];
regwritepostdata[POST_API_DEVICEWRITE_KEY_MASKS] = [register[REGISTER_KEY_MASK]];
regwritepostdata[POST_API_DEVICEWRITE_KEY_VALUES] = [calcedval];
this._sendAPIRequest(API_DEVICEWRITE, "POST", JSON.stringify(regwritepostdata), (err, json) => {
if (json || !err) {
if ((RESP_API_DEVICEWRITE_KEY_JOBID in json)) {
this._getAPIJobResult(json[RESP_API_DEVICEWRITE_KEY_JOBID], 0, (err, res) => {
if (res || !err) {
if (RESP_API_DEVICEJOBSTATUS_RESULT_WRITE_KEY_ERRCODE in res) {
callback("_writeAPIStoveRegister API job returned an error: " + res[RESP_API_DEVICEJOBSTATUS_RESULT_WRITE_KEY_ERRCODE], null);
} else {
register[REGISTER_INTERNAL_KEY_VALUE] = calcedval;
this._debug("_writeAPIStoveRegister wrote registers in API");
callback(null, true);
}
} else {
callback("_writeAPIStoveRegister API job failed: " + err, null);
}
});
} else {
callback("_writeAPIStoveRegister did not get expected result from API: " + JSON.stringify(json));
}
} else {
callback("_writeAPIStoveRegister failed to request registers write with API: " + err, null);
}
});
} else {
callback("_writeAPIStoveRegister could not calculate register value for: " + registername, null);
}
} else {
callback("_writeAPIStoveRegister wanted to write an out ot bound value for " + registername + ": " + value, null);
}
} else {
callback("_writeAPIStoveRegister failed to get stove register before trying to write it: " + err, null);
}
});
}
// Wait for read registers data job results, and parse it when done
_waitForRegistersDataReadJobResult(jobid, callback) {
this._debug("_waitForRegistersDataReadJobResult called for job " + jobid);
this._getAPIJobResult(jobid, 0, (err, res) => {
if ((res === null) && (err === null)) {
this._debug("_waitForRegistersDataReadJobResult got nothing to update");
callback(null, false);
} else if (res || !err) {
if( (RESP_API_DEVICEJOBSTATUS_RESULT_KEY_ITEMS in res) && (RESP_API_DEVICEJOBSTATUS_RESULT_KEY_VALUES in res)) {
this.lastStoveRegistersUpdate = Date.now();
let itemindex = 0;
for (const offset of res[RESP_API_DEVICEJOBSTATUS_RESULT_KEY_ITEMS]) {
if ((res[RESP_API_DEVICEJOBSTATUS_RESULT_KEY_VALUES].length > itemindex) && this.apiStoveOffsetsRegistersMap.has(offset)) {
for (const registername of this.apiStoveOffsetsRegistersMap.get(offset)) {
this.apiStoveRegisters[registername][REGISTER_INTERNAL_KEY_VALUE] = res[RESP_API_DEVICEJOBSTATUS_RESULT_KEY_VALUES][itemindex];
this._debug("_waitForRegistersDataReadJobResult setting raw value: " + registername + "(" + offset + ")=" + this.apiStoveRegisters[registername][REGISTER_INTERNAL_KEY_VALUE]);
}
itemindex++;
} else {
this.log.warn("_waitForRegistersDataReadJobResult got unknown register offset, or offset without value: " + offset);
}
}
if (STOVE_ALARM_REGISTER in this.apiStoveRegisters) {
const alarmval = this.apiStoveRegisters[STOVE_ALARM_REGISTER][REGISTER_INTERNAL_KEY_VALUE];
if (!STOVE_ALARM_IGNORE_VALUES.includes(alarmval) && this.apiStoveAlarmsMap.has(alarmval)) {
this.log.warn("Stove alarm seems to be set: " + this.apiStoveAlarmsMap.get(alarmval));
}
}
this._debug("_waitForRegistersDataReadJobResult updated stove registers from API");
callback(null, true);
} else {
this.log.error("_waitForRegistersDataReadJobResult did not get expected result from API: " + JSON.stringify(res));
}
} else {
callback("_waitForRegistersDataReadJobResult API job failed: " + err, null);
}
});
}
// Update registers data cache from API values if necessary
_updateAPIRegistersData(callback) {
this._debug("_updateAPIRegistersData called, apiStoveRegistersSet=" + this.apiStoveRegistersSet + ", apiPendingReadJob=" + this.apiPendingReadJob);
if (this.apiStoveRegistersSet) {
// If an API read job is already pending, or cache did not expire, do nothing
if (this.apiPendingReadJob) {
this._debug("_updateAPIRegistersData will do nothing, a job is pending: " + this.apiPendingReadJob);
callback(null, false);
} else if ((this.lastStoveRegistersUpdate + STOVE_REGISTERS_CACHE_KEEP) >= Date.now()) {
this._debug("_updateAPIRegistersData will do nothing, cache is up to date");
callback(null, false);
// Otherwise, schedule a real API data read job to fill the cache
} else {
// This var is doing the magic on knowing if a read job is already scheduled
this.apiPendingReadJob = true;
this._readAPIStoveRegisters((err, registersok) => {
this.apiPendingReadJob = false;
callback(err, registersok);
});
}
} else {
callback("_updateAPIRegistersData cannot set registers: registers map not set yet", null);
}
}
// Get a single register structure
_getStoveRegister(registername, callback) {
if (this.apiStoveRegistersSet) {
if (registername in this.apiStoveRegisters) {
const register = this.apiStoveRegisters[registername];
this._debug("_getStoveRegister " + registername + ": " + JSON.stringify(register));
callback(null, register);
} else {
callback("_getStoveRegister register name not in registers: " + registername, null);
}
} else {
callback("_getStoveRegister cannot get a register: map not set yet", null);
}
}
// Calculate human value from register value, or the opposite
_calculateStoveValue(register, tostring, reverse, inputvalue) {
let result = null;
let rawval = null;
if (reverse || (inputvalue !== null)) {
rawval = inputvalue;
} else {
rawval = register[REGISTER_INTERNAL_KEY_VALUE];
}
if (rawval !== null) {
let formula = null;
if (reverse) {
formula = register[REGISTER_KEY_FORMULAREV];
} else {
formula = register[REGISTER_KEY_FORMULA];
}
if (formula === REGISTER_VALUE_FORMULA_VALPH) {
result = rawval;
} else {
formula = formula.replace(REGISTER_VALUE_FORMULA_VALPH, rawval);
// This is insanely dangerous, but it seems it is designed to be done
// with such things like eval.
// Using a REGEX to try to limit malicious opportunities.
if (formula.match(REGISTER_VALUE_FORMULA_REGEX)) {
// jshint -W061
let calcedval = eval(formula);
// jshint +W061
if (!reverse && tostring && (REGISTER_KEY_FORMAT in register) && register[REGISTER_KEY_FORMAT].contains(REGISTER_VALUE_STRING_VALPH)) {
calcedval = register[REGISTER_KEY_FORMAT].replace(REGISTER_VALUE_STRING_VALPH, calcedval);
}
result = calcedval;
} else {
this.log.error("_calculateStoveValue refusing to eval, because of dangerous register value calculation: " + formula, null);
}
}
}
return result;
}
// Get a register min and max boundaries as [min, max] array
_getStoveRegisterBoundaries(registername, callback) {
this._getStoveRegister(registername, (err, register) => {
if (register || !err) {
if ( (REGISTER_KEY_MIN in register) && (REGISTER_KEY_MAX in register) ) {
const bmin = register[REGISTER_KEY_MIN];
const bmax = register[REGISTER_KEY_MAX];
const bstep = register[REGISTER_KEY_STEP];
this._debug("_getStoveRegisterBoundaries " + registername + " => [" + bmin + ", " + bmax + "] step " + bstep);
callback(null, [bmin, bmax, bstep]);
} else {
callback("_getStoveRegisterBoundaries could not get boundaries from register for: " + registername, null);
}