-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdownlink_formatter.js
704 lines (688 loc) · 23.6 KB
/
downlink_formatter.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
///////////////////////////////////////////////////////////////////////////////
// downlink_formatter.js
//
// Bresser 868 MHz Weather Sensor Radio Receiver
// based on ESP32 and SX1262/SX1276 -
// sends data to a LoRaWAN network (e.g. The Things Network)
//
// This script allows to send downlink data to the The Things Network using
// parameters encoded in JSON format.
// The FPort must be set to the command.
//
/// Commands:
// ----------
// (see constants for ports below)
// port = CMD_SET_WS_TIMEOUT, {"timeout": <timeout_in_seconds>}
// port = CMD_SET_SLEEP_INTERVAL, {"sleep_interval": <interval_in_seconds>}
// port = CMD_SET_SLEEP_INTERVAL_LONG, {"sleep_interval_long": <interval_in_seconds>}
// port = CMD_GET_DATETIME, {"cmd": "CMD_GET_DATETIME"} / payload = 0x00
// port = CMD_SET_DATETIME, {"epoch": <epoch>}
// port = CMD_RESET_WS_POSTPROC, {"reset_flags": <flags>}
// port = CMD_GET_LW_CONFIG, {"cmd": "CMD_GET_LW_CONFIG"} / payload = 0x00
// port = CMD_GET_WS_TIMEOUT, {"cmd": "CMD_GET_WS_TIMEOUT" / payload = 0x00
// port = CMD_SET_WS_TIMEOUT, {"ws_timeout": <ws_timeout>}
// port = CMD_GET_STATUS_INTERVAL, {"cmd": "CMD_GET_STATUS_INTERVAL"} / payload = 0x00
// port = CMD_SET_STATUS_INTERVAL, {"status_interval": <status_interval>}
// port = CMD_GET_SENSORS_STAT, {"cmd": "CMD_GET_SENSORS_STAT"} / payload = 0x00
// port = CMD_GET_SENSORS_INC, {"cmd": "CMD_GET_SENSORS_INC"} / payload = 0x00
// port = CMD_SET_SENSORS_INC, {"sensors_inc": [<sensors_inc0>, ..., <sensors_incN>]}
// port = CMD_GET_SENSORS_EXC, {"cmd": "CMD_GET_SENSORS_EXC"} / payload = 0x00
// port = CMD_SET_SENSORS_EXC, {"sensors_exc": [<sensors_exc0>, ..., <sensors_excN>]}
// port = CMD_GET_APP_PAYLOAD_CFG, {"cmd": "CMD_GET_APP_PAYLOAD_CFG"} / payload = 0x00
// port = CMD_SET_APP_PAYLOAD_CFG, ["bresser": [<type0>, ... <type15>], "onewire": <onewire>, "analog": <analog>, "digital": <digital>]
// port = CMD_GET_BLE_ADDR, {"cmd": "CMD_GET_BLE_ADDR"} / payload = 0x00
// port = CMD_SET_BLE_ADDR, {"ble_addr": [<ble_addr0>, ..., <ble_addrN>]}
// port = CMD_GET_BLE_CONFIG, {"cmd": "CMD_GET_BLE_CONFIG"} / payload = 0x00
// port = CMD_SET_BLE_CONFIG, {"ble_active": <ble_active>, "ble_scantime": <ble_scantime>}
//
// Responses:
// -----------
// (The response uses the same port as the request.)
// CMD_GET_LW_CONFIG {"sleep_interval": <sleep_interval>,
// "sleep_interval_long": <sleep_interval_long>}
//
// CMD_GET_DATETIME {"epoch": <unix_epoch_time>, "rtc_source": <rtc_source>}
//
// CMD_GET_WS_TIMEOUT {"ws_timeout": <ws_timeout>}
//
// CMD_GET_STATUS_INTERVAL {"status_interval": <status_interval>}
//
// CMD_GET_SENSORS_STAT {"sensor_status": {bresser: [<bresser_stat0>, ..., <bresser_stat15>], "ble_stat": <ble_stat>}}
//
// CMD_GET_SENSORS_INC {"sensors_inc": [<sensors_inc0>, ...]}
//
// CMD_GET_SENSORS_EXC {"sensors_exc"}: [<sensors_exc0>, ...]}
//
// CMD_GET_SENSORS_CFG {"max_sensors": <max_sensors>, "rx_flags": <rx_flags>, "en_decoders": <en_decoders>}
//
// CMD_GET_APP_PAYLOAD_CFG {"bresser": [<type0>, <type1>, ..., <type15>], "onewire": <onewire>, "analog": <analog>, "digital": <digital>}
//
// CMD_GET_BLE_ADDR {"ble_addr": [<ble_addr0>, ...]}
//
// CMD_GET_BLE_CONFIG {"ble_active": <ble_active>, "ble_scantime": <ble_scantime>}
//
// <ws_timeout> : 0...255
// <sleep_interval> : 0...65535
// <sleep_interval> : 0...65535
// <epoch> : unix epoch time, see https://www.epochconverter.com/ (<integer> / "0x....")
// <reset_flags> : 0...15 (1: hourly / 2: daily / 4: weekly / 8: monthly) / "0x0"..."0xF"
// <rtc_source> : 0x00: GPS / 0x01: RTC / 0x02: LORA / 0x03: unsynched / 0x04: set (source unknown)
// <status_interval> : Sensor status message uplink interval in no. of frames (0...255, 0: disabled)
// <sensors_incN> : e.g. "0xDEADBEEF"
// <sensors_excN> : e.g. "0xDEADBEEF"
// <max_sensors> : max. number of Bresser sensors per receive cycle; 1...8
// <rx_flags> : Flags for getData(); see BresserWeatherSensorReceiver
// <en_decoders> : Enabled decoders; see BresserWeatherSensorReceiver
// <ble_active> : BLE scan mode - 0: passive / 1: active
// <ble_scantime> : BLE scan time in seconds (0...255)
// <ble_addrN> : e.g. "DE:AD:BE:EF:12:23"
// <typeN> : Bitmap for enabling Bresser sensors of type N; each bit position corresponds to a channel, e.g. bit 0 controls ch0;
// unused bits can be used to select features
// <onewire> : Bitmap for enabling 1-Wire sensors; each bit position corresponds to an index
// <analog> : Bitmap for enabling analog input channels; each bit positions corresponds to a channel
// <digital> : Bitmap for enabling digital input channels in a broad sense — GPIO, SPI, I2C, UART, ...
//
//
// Based on:
// ---------
// https://www.thethingsindustries.com/docs/integrations/payload-formatters/javascript/downlink/
//
// created: 08/2023
//
//
// MIT License
//
// Copyright (c) 2023 Matthias Prinke
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
//
// History:
// 20230821 Created
// 20240420 Updated for BresserWeatherSensorLW,
// renamed from downlink_formatter.js
// 20240427 Added BLE configuration
// 20240507 Added CMD_GET_SENSORS_CFG/CMD_SET_SENSORS_CFG
// 20240508 Fixed decoding of raw data
// Added en_decoders to CMD_GET_SENSORS_CFG/CMD_SET_SENSORS_CFG
// 20240519 Added CMD_GET_APP_PAYLOAD_CFG/CMD_SET_APP_PAYLOAD_CFG
// 20240530 Added decoding of CMD_GET_APP_PAYLOAD_CFG/CMD_SET_APP_PAYLOAD_CFG
// 20240603 Added CMD_GET_SENSORS_STAT
// 20240607 Added CMD_GET_STATUS_INTERVAL/CMD_SET_STATUS_INTERVAL
// 20240608 Added CMD_GET_LW_STATUS
// 20240609 Refactored command encoding
// 20240614 Renamed CMD_RESET_RAINGAUGE to CMD_RESET_WS_POSTPROC
// 20240615 Added CMD_SCAN_SENSORS
// 20240722 Added CMD_SET_LW_STATUS_INTERVAL,
// renamed CMD_SET_STATUS_INTERVAL to CMD_SET_APP_STATUS_INTERVAL
//
// ToDo:
// -
//
///////////////////////////////////////////////////////////////////////////////
// Commands
const CMD_GET_DATETIME = 0x20;
const CMD_SET_DATETIME = 0x21;
const CMD_SET_SLEEP_INTERVAL = 0x31;
const CMD_SET_SLEEP_INTERVAL_LONG = 0x33;
const CMD_SET_LW_STATUS_INTERVAL = 0x35;
const CMD_GET_LW_CONFIG = 0x36;
const CMD_GET_LW_STATUS = 0x38;
const CMD_GET_APP_STATUS_INTERVAL = 0x40;
const CMD_SET_APP_STATUS_INTERVAL = 0x41;
const CMD_GET_SENSORS_STAT = 0x42;
const CMD_GET_APP_PAYLOAD_CFG = 0x46;
const CMD_SET_APP_PAYLOAD_CFG = 0x47;
const CMD_GET_WS_TIMEOUT = 0xC0;
const CMD_SET_WS_TIMEOUT = 0xC1;
const CMD_RESET_WS_POSTPROC = 0xC3;
const CMD_SCAN_SENSORS = 0xC4;
const CMD_GET_SENSORS_INC = 0xC6;
const CMD_SET_SENSORS_INC = 0xC7;
const CMD_GET_SENSORS_EXC = 0xC8;
const CMD_SET_SENSORS_EXC = 0xC9;
const CMD_GET_SENSORS_CFG = 0xCA;
const CMD_SET_SENSORS_CFG = 0xCB;
const CMD_GET_BLE_CONFIG = 0xD0;
const CMD_SET_BLE_CONFIG = 0xD1;
const CMD_GET_BLE_ADDR = 0xD2;
const CMD_SET_BLE_ADDR = 0xD3;
// Source of Real Time Clock setting
var rtc_source_code = {
0x00: "GPS",
0x01: "RTC",
0x02: "LORA",
0x03: "unsynched",
0x04: "set (source unknown)"
};
function bytesToInt(bytes) {
var i = 0;
for (var x = 0; x < bytes.length; x++) {
i |= +(bytes[x] << (x * 8));
}
return i;
}
// Big Endian
function bytesToIntBE(bytes) {
var i = 0;
for (var x = 0; x < bytes.length; x++) {
i |= +(bytes[x] << ((bytes.length - 1 - x) * 8));
}
return i;
}
function uint8(bytes) {
if (bytes.length !== 1) {
throw new Error('uint8 must have exactly 1 byte');
}
return bytesToInt(bytes);
}
function uint16BE(bytes) {
if (bytes.length !== 2) {
throw new Error('uint16BE must have exactly 2 bytes');
}
return bytesToIntBE(bytes);
}
function uint32BE(bytes) {
if (bytes.length !== 4) {
throw new Error('uint32BE must have exactly 4 bytes');
}
return bytesToIntBE(bytes);
}
function hex16(bytes) {
let res = "0x" + byte2hex(bytes[0]) + byte2hex(bytes[1]);
return res;
}
function hex32(bytes) {
let res = "0x" + byte2hex(bytes[0]) + byte2hex(bytes[1]) + byte2hex(bytes[2]) + byte2hex(bytes[3]);
return res;
}
function byte2hex(byte) {
return ('0' + byte.toString(16)).slice(-2);
}
function mac48(bytes) {
var res = [];
var j = 0;
for (var i = 0; i < bytes.length; i += 6) {
res[j++] = byte2hex(bytes[i]) + ":" + byte2hex(bytes[i + 1]) + ":" + byte2hex(bytes[i + 2]) + ":" +
byte2hex(bytes[i + 3]) + ":" + byte2hex(bytes[i + 4]) + ":" + byte2hex(bytes[i + 5]);
}
return res;
}
function id32(bytes) {
var res = [];
var j = 0;
for (var i = 0; i < bytes.length; i += 4) {
res[j++] = "0x" + byte2hex(bytes[i]) + byte2hex(bytes[i + 1]) + byte2hex(bytes[i + 2]) + byte2hex(bytes[i + 3]);
}
return res;
}
function bresser_bitmaps(bytes) {
let res = [];
res[0] = "0x" + byte2hex(bytes[0]);
for (var i = 1; i < 16; i++) {
res[i] = "0x" + byte2hex(bytes[i]);
}
return res;
}
// Encode Downlink from JSON to bytes
function encodeDownlink(input) {
var i;
var j;
var k;
var output = [];
var value;
if (input.data.hasOwnProperty('cmd')) {
if (input.data.cmd == "CMD_GET_DATETIME") {
return {
bytes: [0],
fPort: CMD_GET_DATETIME,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_LW_CONFIG") {
return {
bytes: [0],
fPort: CMD_GET_LW_CONFIG,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_LW_STATUS") {
return {
bytes: [0],
fPort: CMD_GET_LW_STATUS,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_WS_TIMEOUT") {
return {
bytes: [0],
fPort: CMD_GET_WS_TIMEOUT,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_APP_STATUS_INTERVAL") {
return {
bytes: [0],
fPort: CMD_GET_APP_STATUS_INTERVAL,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_STAT") {
return {
bytes: [0],
fPort: CMD_GET_SENSORS_STAT,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_INC") {
return {
bytes: [0],
fPort: CMD_GET_SENSORS_INC,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_EXC") {
return {
bytes: [0],
fPort: CMD_GET_SENSORS_EXC,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_SENSORS_CFG") {
return {
bytes: [0],
fPort: CMD_GET_SENSORS_CFG,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_APP_PAYLOAD_CFG") {
return {
bytes: [0],
fPort: CMD_GET_APP_PAYLOAD_CFG,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_BLE_ADDR") {
return {
bytes: [0],
fPort: CMD_GET_BLE_ADDR,
warnings: [],
errors: []
};
}
else if (input.data.cmd == "CMD_GET_BLE_CONFIG") {
return {
bytes: [0],
fPort: CMD_GET_BLE_CONFIG,
warnings: [],
errors: []
};
}
}
if (input.data.hasOwnProperty('sleep_interval')) {
return {
bytes: [
input.data.sleep_interval >> 8,
input.data.sleep_interval & 0xFF
],
fPort: CMD_SET_SLEEP_INTERVAL,
warnings: [],
errors: []
};
}
if (input.data.hasOwnProperty('sleep_interval_long')) {
return {
bytes: [
input.data.sleep_interval_long >> 8,
input.data.sleep_interval_long & 0xFF
],
fPort: CMD_SET_SLEEP_INTERVAL_LONG,
warnings: [],
errors: []
};
}
else if (input.data.hasOwnProperty('lw_status_interval')) {
return {
bytes: [input.data.lw_status_interval],
fPort: CMD_SET_LW_STATUS_INTERVAL,
warnings: [],
errors: []
};
}
else if (input.data.hasOwnProperty('epoch')) {
if (typeof input.data.epoch == "string") {
if (input.data.epoch.substr(0, 2) == "0x") {
value = parseInt(input.data.epoch.substr(2), 16);
} else {
return {
bytes: [],
warnings: [],
errors: ["Invalid hex value"]
};
}
} else {
value = input.data.epoch;
}
return {
bytes: [
value >> 24,
(value >> 16) & 0xFF,
(value >> 8) & 0xFF,
(value & 0xFF)],
fPort: CMD_SET_DATETIME,
warnings: [],
errors: []
};
}
else if (input.data.hasOwnProperty('ws_timeout')) {
return {
bytes: [input.data.ws_timeout],
fPort: CMD_SET_WS_TIMEOUT,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('reset_flags')) {
if (typeof input.data.epoch == "string") {
if (input.data.reset_flags.substr(0, 2) == "0x") {
value = parseInt(input.data.reset_flags.substr(2), 16);
} else {
return {
bytes: [],
warnings: [],
errors: ["Invalid hex value"]
};
}
} else {
value = input.data.reset_flags;
}
return {
bytes: [value],
fPort: CMD_RESET_WS_POSTPROC,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('ws_scantime')) {
return {
bytes: [input.data.ws_scantime],
fPort: CMD_SCAN_SENSORS,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('app_status_interval')) {
return {
bytes: [input.data.app_status_interval],
fPort: CMD_SET_APP_STATUS_INTERVAL,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('sensors_inc')) {
k = 0;
for (i = 0; i < input.data.sensors_inc.length; i++) {
for (j = 2; j < 10; j += 2) {
output[k++] = parseInt(input.data.sensors_inc[i].substr(j, 2), 16);
}
}
return {
bytes: output,
fPort: CMD_SET_SENSORS_INC,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('sensors_exc')) {
k = 0;
for (i = 0; i < input.data.sensors_exc.length; i++) {
for (j = 2; j < 10; j += 2) {
output[k++] = parseInt(input.data.sensors_exc[i].substr(j, 2), 16);
}
}
return {
bytes: output,
fPort: CMD_SET_SENSORS_EXC,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('max_sensors') &&
input.data.hasOwnProperty('rx_flags') &&
input.data.hasOwnProperty('en_decoders')) {
return {
bytes: [input.data.max_sensors, input.data.rx_flags, input.data.en_decoders],
fPort: CMD_SET_SENSORS_CFG,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('bresser') &&
input.data.hasOwnProperty('onewire') &&
input.data.hasOwnProperty('analog') &&
input.data.hasOwnProperty('digital')) {
if (input.data.bresser.length != 16) {
return {
bytes: [],
warnings: [],
errors: ["<bresser>: expected 16 bytes, got " + input.data.bresser.length]
};
}
for (i = 0; i < input.data.bresser.length; i++) {
if (input.data.bresser[i].substr(0, 2) == "0x") {
value = parseInt(input.data.bresser[i].substr(2), 16);
output[i] = value;
} else {
return {
bytes: [],
warnings: [],
errors: ["'bresser': Invalid hex value"]
};
}
}
if (input.data.onewire.substr(0, 2) == "0x") {
output[16] = parseInt(input.data.onewire.substr(2, 2), 16);
output[17] = parseInt(input.data.onewire.substr(4, 2), 16);
} else {
return {
bytes: [],
warnings: [],
errors: ["'onewire': Invalid hex value"]
};
}
if (input.data.analog.substr(0, 2) == "0x") {
output[18] = parseInt(input.data.analog.substr(2, 2), 16);
output[19] = parseInt(input.data.analog.substr(4, 2), 16);
} else {
return {
bytes: [],
warnings: [],
errors: ["'analog': Invalid hex value"]
};
}
if (input.data.digital.substr(0, 2) == "0x") {
output[20] = parseInt(input.data.digital.substr(2, 2), 16);
output[21] = parseInt(input.data.digital.substr(4, 2), 16);
output[22] = parseInt(input.data.digital.substr(6, 2), 16);
output[23] = parseInt(input.data.digital.substr(8, 2), 16);
} else {
return {
bytes: [],
warnings: [],
errors: ["'digital': Invalid hex value"]
};
}
return {
bytes: output,
fPort: CMD_SET_APP_PAYLOAD_CFG,
warnings: [],
errors: []
};
} else if (input.data.hasOwnProperty('ble_addr')) {
output = [];
k = 0;
for (i = 0; i < input.data.ble_addr.length; i++) {
var tmp = input.data.ble_addr[i].replace(/([^:]*):/g, '$1');
for (j = 0; j < 12; j += 2) {
output[k++] = parseInt(tmp.substr(j, 2), 16);
}
}
return {
bytes: output,
fPort: CMD_SET_BLE_ADDR,
warnings: [],
errors: []
};
}
else if (input.data.hasOwnProperty('ble_active') && input.data.hasOwnProperty('ble_scantime')) {
return {
bytes: [input.data.ble_active, input.data.ble_scantime],
fPort: CMD_SET_BLE_CONFIG,
warnings: [],
errors: []
};
}
else {
return {
bytes: [],
errors: ["Unknown command"],
fPort: 1,
warnings: []
};
}
}
// Decode Downlink from bytes to JSON
function decodeDownlink(input) {
switch (input.fPort) {
case CMD_GET_DATETIME:
case CMD_GET_LW_CONFIG:
case CMD_GET_LW_STATUS:
case CMD_GET_WS_TIMEOUT:
case CMD_GET_APP_STATUS_INTERVAL:
case CMD_GET_SENSORS_STAT:
case CMD_GET_SENSORS_INC:
case CMD_GET_SENSORS_EXC:
case CMD_GET_SENSORS_CFG:
case CMD_GET_APP_PAYLOAD_CFG:
case CMD_GET_BLE_ADDR:
case CMD_GET_BLE_CONFIG:
return {
data: [0],
warnings: [],
errors: []
};
case CMD_SET_DATETIME:
return {
data: {
unixtime: "0x" + uint32BE(input.bytes.slice(0, 4)).toString(16)
}
};
case CMD_SET_SLEEP_INTERVAL:
return {
data: {
sleep_interval: uint16BE(input.bytes)
}
};
case CMD_SET_SLEEP_INTERVAL_LONG:
return {
data: {
sleep_interval_long: uint16BE(input.bytes)
}
};
case CMD_SET_LW_STATUS_INTERVAL:
return {
data: {
lw_status_interval: uint8(input.bytes)
}
};
case CMD_SET_WS_TIMEOUT:
return {
data: {
ws_timeout: uint8(input.bytes)
}
};
case CMD_RESET_WS_POSTPROC:
return {
data: {
reset_flags: "0x" + uint8(input.bytes).toString(16)
}
};
case CMD_SCAN_SENSORS:
return {
data: {
ws_scantime: uint8(input.bytes)
}
};
case CMD_SET_APP_STATUS_INTERVAL:
return {
data: {
status_interval: uint8(input.bytes)
}
};
case CMD_SET_SENSORS_INC:
return {
data: {
sensors_inc: id32(input.bytes)
}
};
case CMD_SET_SENSORS_EXC:
return {
data: {
sensors_exc: id32(input.bytes)
}
};
case CMD_SET_SENSORS_CFG:
return {
data: {
max_sensors: uint8(input.bytes.slice(0, 1)),
rx_flags: uint8(input.bytes.slice(1, 2)),
en_decoders: uint8(input.bytes.slice(2, 3))
}
};
case CMD_SET_APP_PAYLOAD_CFG:
return {
data: {
bresser: bresser_bitmaps(input.bytes.slice(0, 16)),
onewire: hex16(input.bytes.slice(16, 18)),
analog: hex16(input.bytes.slice(18, 20)),
digital: hex32(input.bytes.slice(20, 24))
}
};
case CMD_SET_BLE_ADDR:
return {
data: {
ble_addr: mac48(input.bytes)
}
};
case CMD_SET_BLE_CONFIG:
return {
data: {
ble_active: uint8(input.bytes.slice(0, 1)),
ble_timeout: uint8(input.bytes.slice(1, 2))
}
};
default:
return {
errors: ["unknown FPort"]
};
}
}