-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWH2600Utils.js
615 lines (522 loc) · 17.3 KB
/
WH2600Utils.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
const LIVE_DATA_STRUCT = {
/*x01*/ 1: [decodeTemp, 2, "intemp"],
/*x02*/ 2: [decodeTemp, 2, "outtemp"],
/*x03*/ 3: [decodeTemp, 2, "dewpoint"],
/*x04*/ 4: [decodeTemp, 2, "windchill"],
/*x05*/ 5: [decodeTemp, 2, "heatindex"],
/*x06*/ 6: [decodeHumid, 1, "inhumid"],
/*x07*/ 7: [decodeHumid, 1, "outhumid"],
/*x08*/ 8: [decodePress, 2, "absbarometer"],
/*x09*/ 9: [decodePress, 2, "relbarometer"],
/*x0A*/ 10: [decodeDir, 2, "winddir"],
/*x0B*/ 11: [decodeSpeed, 2, "windspeed"],
/*x0C*/ 12: [decodeSpeed, 2, "gustspeed"],
/*x0D*/ 13: [decodeRain, 2, "rainevent"],
/*x0E*/ 14: [decodeRainRate, 2, "rainrate"],
/*x0F*/ 15: [decodeRain, 2, "rainhour"],
/*x10*/ 16: [decodeRain, 2, "rainday"],
/*x11*/ 17: [decodeRain, 2, "rainweek"],
/*x12*/ 18: [decodeBigRain, 4, "rainmonth"],
/*x13*/ 19: [decodeBigRain, 4, "rainyear"],
/*x14*/ 20: [decodeBigRain, 4, "raintotals"],
/*x15*/ 21: [decodeLight, 4, "light"],
/*x16*/ 22: [decodeUV, 2, "uv"],
/*x17*/ 23: [decodeUVI, 1, "uvi"],
/*x18*/ 24: [decodeDatetime, 6, "datetime"],
/*x19*/ 25: [decodeSpeed, 2, "daymaxwind"],
/*x1A*/ 26: [decodeTemp, 2, "temp1"],
/*x1B*/ 27: [decodeTemp, 2, "temp2"],
/*x1C*/ 28: [decodeTemp, 2, "temp3"],
/*x1D*/ 29: [decodeTemp, 2, "temp4"],
/*x1E*/ 30: [decodeTemp, 2, "temp5"],
/*x1F*/ 31: [decodeTemp, 2, "temp6"],
/*x20*/ 32: [decodeTemp, 2, "temp7"],
/*x21*/ 33: [decodeTemp, 2, "temp8"],
/*x22*/ 34: [decodeHumid, 1, "humid1"],
/*x23*/ 35: [decodeHumid, 1, "humid2"],
/*x24*/ 36: [decodeHumid, 1, "humid3"],
/*x25*/ 37: [decodeHumid, 1, "humid4"],
/*x26*/ 38: [decodeHumid, 1, "humid5"],
/*x27*/ 39: [decodeHumid, 1, "humid6"],
/*x28*/ 40: [decodeHumid, 1, "humid7"],
/*x29*/ 41: [decodeHumid, 1, "humid8"],
/*x2A*/ 42: [decodeAq, 2, "pm251"],
/*x2B*/ 43: [decodeTemp, 2, "soiltemp1"],
/*x2C*/ 44: [decodeMoist, 1, "soilmoist1"],
/*x2D*/ 45: [decodeTemp, 2, "soiltemp2"],
/*x2E*/ 46: [decodeMoist, 1, "soilmoist2"],
/*x2F*/ 47: [decodeTemp, 2, "soiltemp3"],
/*x30*/ 48: [decodeMoist, 1, "soilmoist3"],
/*x31*/ 49: [decodeTemp, 2, "soiltemp4"],
/*x32*/ 50: [decodeMoist, 1, "soilmoist4"],
/*x33*/ 51: [decodeTemp, 2, "soiltemp5"],
/*x34*/ 52: [decodeMoist, 1, "soilmoist5"],
/*x35*/ 53: [decodeTemp, 2, "soiltemp6"],
/*x36*/ 54: [decodeMoist, 1, "soilmoist6"],
/*x37*/ 55: [decodeTemp, 2, "soiltemp7"],
/*x38*/ 56: [decodeMoist, 1, "soilmoist7"],
/*x39*/ 57: [decodeTemp, 2, "soiltemp8"],
/*x3A*/ 58: [decodeMoist, 1, "soilmoist8"],
/*x3B*/ 59: [decodeTemp, 2, "soiltemp9"],
/*x3C*/ 60: [decodeMoist, 1, "soilmoist9"],
/*x3D*/ 61: [decodeTemp, 2, "soiltemp10"],
/*x3E*/ 62: [decodeMoist, 1, "soilmoist10"],
/*x3F*/ 63: [decodeTemp, 2, "soiltemp11"],
/*x40*/ 64: [decodeMoist, 1, "soilmoist11"],
/*x41*/ 65: [decodeTemp, 2, "soiltemp12"],
/*x42*/ 66: [decodeMoist, 1, "soilmoist12"],
/*x43*/ 67: [decodeTemp, 2, "soiltemp13"],
/*x44*/ 68: [decodeMoist, 1, "soilmoist13"],
/*x45*/ 69: [decodeTemp, 2, "soiltemp14"],
/*x46*/ 70: [decodeMoist, 1, "soilmoist14"],
/*x47*/ 71: [decodeTemp, 2, "soiltemp15"],
/*x48*/ 72: [decodeMoist, 1, "soilmoist15"],
/*x49*/ 73: [decodeTemp, 2, "soiltemp16"],
/*x4A*/ 74: [decodeMoist, 1, "soilmoist16"],
/*x4C*/ 76: [decodeBatt, 16, "lowbatt"],
/*x4D*/ 77: [decodeAq, 2, "pm251_24hav"],
/*x4E*/ 78: [decodeAq, 2, "pm252_24hav"],
/*x4F*/ 79: [decodeAq, 2, "pm253_24hav"],
/*x50*/ 80: [decodeAq, 2, "pm254_24hav"],
/*x51*/ 81: [decodeAq, 2, "pm252"],
/*x52*/ 82: [decodeAq, 2, "pm253"],
/*x53*/ 83: [decodeAq, 2, "pm254"],
/*x58*/ 88: [decodeLeak, 1, "leak1"],
/*x59*/ 89: [decodeLeak, 1, "leak2"],
/*x5A*/ 90: [decodeLeak, 1, "leak3"],
/*x5B*/ 91: [decodeLeak, 1, "leak4"],
/*x60*/ 96: [decodeDistance, 1, "lightningdist"],
/*x61*/ 97: [decodeUTC, 4, "lightningdettime"],
/*x62*/ 98: [decodeCount, 4, "lightningcount"],
/*x63*/ 99: [decodeTempBatt, 3, "usertemp1"],
/*x64*/ 100: [decodeTempBatt, 3, "usertemp2"],
/*x65*/ 101: [decodeTempBatt, 3, "usertemp3"],
/*x66*/ 102: [decodeTempBatt, 3, "usertemp4"],
/*x67*/ 103: [decodeTempBatt, 3, "usertemp5"],
/*x68*/ 104: [decodeTempBatt, 3, "usertemp6"],
/*x69*/ 105: [decodeTempBatt, 3, "usertemp7"],
/*x6A*/ 106: [decodeTempBatt, 3, "usertemp8"]
};
const CUSTOMIZED_SERVER_STRUCT = [
["station", "string", null],
["key", "string", null],
["server", "string", null],
["port", "uint16", 2],
["interval", "uint16", 2],
["protocol", "protocol", 1],
["enabled", "bool", 1]
];
const USER_PATH_STRUCT = [
["path_ecowitt", "string", null],
["path_wunderground", "string", null]
];
const RAIN_DATA_STRUCT = [
["rain_event", "rain", 2], // confirm (may not exist)
["rain_rate", "rain", 2], // confirm (may be 4)
["rain_hour", "rain", 2], // confirm (may not exist)
["rain_day", "rain", 2], // confirm (may be 4)
["rain_week", "bigRain", 4], // confirm (may be 2)
["rain_month", "bigRain", 4],
["rain_year", "bigRain", 4]
];
const MULTI_BATT = {
wh40: 4,
wh26: 5,
wh25: 6,
wh65: 7 //aka wh24
};
class WH2600Utils {
constructor(isWH24 = false) {
if (isWH24) {
if (MULTI_BATT.wh24 === undefined) {
MULTI_BATT.wh24 = MULTI_BATT.wh65;
delete MULTI_BATT.wh65;
}
} else {
if (MULTI_BATT.wh65 === undefined) {
MULTI_BATT.wh65 = MULTI_BATT.wh24;
delete MULTI_BATT.wh24;
}
}
this.parseStructStrict = (buffer, struct) => {
let data = {},
idx = 4;
struct.forEach(([field, func, fieldSize]) => {
if (fieldSize == null) {
fieldSize = buffer.readInt8(idx);
let rfunc = PARSE[func];
if (rfunc === undefined) {
console.log(func);
}
data[field] = rfunc(idx + 1, buffer, fieldSize);
idx += fieldSize + 1;
} else {
data[field] = PARSE[func](idx, buffer, fieldSize);
idx += fieldSize;
}
});
return data;
};
this.packStructStrict = (data, struct) => {
let buffer = new Buffer.alloc(256);
let idx = 0;
struct.forEach(([field, func]) => {
const [nidx, nbuffer] = PACK[func](idx, buffer, data[field]);
idx = nidx;
buffer = nbuffer;
});
return Uint8Array.from(buffer.slice(0, idx));
};
}
parseLiveData(buffer, timestamp = null) {
let data = {},
idx = 5;
while (idx < buffer.length - 1) {
let [func, fieldSize, field] = LIVE_DATA_STRUCT[buffer.readInt8(idx++)];
data[field] = func(idx, buffer);
idx += fieldSize;
}
if (!("datetime" in data)) {
data["datetime"] = timestamp != null ? timestamp : Date.now();
}
return data;
}
parseCustomServerInfo(buffer) {
return this.parseStructStrict(buffer, CUSTOMIZED_SERVER_STRUCT);
}
parseUserPathInfo(buffer) {
return this.parseStructStrict(buffer, USER_PATH_STRUCT);
}
parseRainData(buffer) {
return this.parseStructStrict(buffer, RAIN_DATA_STRUCT);
}
packRainData(data) {
return this.packStructStrict(data, RAIN_DATA_STRUCT);
}
static calcChecksum(body) {
return calcChecksum(body);
}
}
const PARSE = {
string: parseString,
int8: parseInt8,
uint16: parseUInt16,
uint32: parseUInt32,
bool: parseBool,
protocol: parseProtocol,
rain: decodeRain,
bigRain: decodeBigRain
};
const PACK = {
string: packString,
int8: packInt8,
uint16: packUInt16,
uint32: packUInt32,
bool: packBool,
protocol: packProtocol,
rain: packRain,
bigRain: packBigRain
};
function parseString(idx, buffer, length) {
return buffer.toString("utf8", idx, idx + length);
}
function parseBool(idx, buffer) {
return buffer.readInt8(idx) === 1;
}
function parseInt8(idx, buffer) {
return buffer.readInt8(idx);
}
function parseUInt16(idx, buffer) {
return buffer.readInt16BE(idx);
}
function parseUInt32(idx, buffer) {
return buffer.readInt32BE(idx);
}
function parseProtocol(idx, buffer) {
return buffer.readInt8(idx) === 0 ? "ecowitt" : "wunderground";
}
function packString(idx, buffer, str) {
buffer.writeInt8(str.length, idx);
idx++;
buffer.write(str, idx, str.length, "utf8");
idx += str.length;
return [idx, buffer];
}
function packBool(idx, buffer, data) {
return packInt8(idx, buffer, data ? 1 : 0);
}
function packInt8(idx, buffer, data) {
buffer.writeInt8(data, idx);
return [idx + 1, buffer];
}
function packUInt16(idx, buffer, data) {
buffer.writeUInt16BE(data, idx);
return [idx + 2, buffer];
}
function packUInt32(idx, buffer, data) {
buffer.writeUInt32BE(data, idx);
return [idx + 4, buffer];
}
function packProtocol(idx, buffer, data) {
return packInt8(idx, buffer, "ecowitt" === data ? 0 : 1);
}
function packRain(idx, buffer, value) {
return packUInt16(idx, buffer, value * 10);
}
function packBigRain(idx, buffer, value) {
return packUInt32(idx, buffer, value * 10);
}
/* Decode temperature data.
Data is contained in a two byte big endian signed integer and represents tenths of a degree celcius.
*/
function decodeTemp(idx, buffer) {
return buffer.readInt16BE(idx) / 10;
}
/* Decode humidity data.
Data is contained in a single unsigned byte and represents whole units (%).
*/
function decodeHumid(idx, buffer) {
return buffer.readInt8(idx);
}
/* Decode pressure data.
Data is contained in a two byte big endian integer and represents tenths of a unit (hpa).
*/
function decodePress(idx, buffer) {
return buffer.readUInt16BE(idx) / 10.0;
}
/* Decode direction data.
Data is contained in a two byte big endian integer and represents whole degrees.
*/
function decodeDir(idx, buffer) {
return buffer.readUInt16BE(idx);
}
/* Decode speed data.
Data is contained in a two byte big endian integer and represents tenths of m/s.
*/
function decodeSpeed(idx, buffer) {
return buffer.readUInt16BE(idx) / 10.0;
}
/* Decode rain data.
Data is contained in a two byte big endian integer and represents tenths of mm.
*/
function decodeRain(idx, buffer) {
return buffer.readUInt16BE(idx) / 10.0;
}
/* Decode rain rate data.
Data is contained in a two byte big endian integer and represents tenths of mm/hr.
*/
function decodeRainRate(idx, buffer) {
return buffer.readUInt16BE(idx) / 10.0;
}
/* Decode 4 byte rain data.
Data is contained in a four byte big endian integer and represents tenths of a unit mm.
*/
function decodeBigRain(idx, buffer) {
return buffer.readUInt32BE(idx) / 10.0;
}
/* Decode 4 byte light data.
Data is contained in a four byte big endian integer and represents tenths of a unit (lux).
*/
function decodeLight(idx, buffer) {
return buffer.readUInt32BE(idx) / 10.0;
}
/* Decode 2 byte UV data.
Data is contained in a two byte big endian integer and represents tenths of a unit (µW/cm²).
*/
function decodeUV(idx, buffer) {
return buffer.readUInt16BE(idx) / 10.0;
}
/* Decode UV Index.
Data is contained in a single unsigned byte and represents whole units (0-15).
*/
function decodeUVI(idx, buffer) {
return buffer.readInt8(idx);
}
/* Decode date-time data.
Unknown format but length is six bytes.
*/
function decodeDatetime(idx, buffer) {
return buffer.slice(idx, idx + 6);
}
/* Decode UTC time.
The WH2600 API claims to provide 'UTC time' as a 4 byte big endian
integer. The 4 byte integer is a unix epoch timestamp; however,
the timestamp is offset by the stations timezone. So for a station
in the +10 hour timezone, the timestamp returned is the present
epoch timestamp plus 10 * 3600 seconds.
When decoded in localtime the decoded date-time is off by the
station time zone, when decoded as GMT the date and time figures
are correct but the timezone is incorrect.
In any case decode the 4 byte big endian integer as is and any
further use of this timestamp needs to take the above time zone
offset into account when using the timestamp.
*/
function decodeUTC(idx, buffer) {
let val = buffer.readUInt32BE(idx);
return val === 0xffffffff ? null : val;
}
/* Decode combined temperature and battery status data.
Data consists of three bytes; bytes 0 and 1 are normal temperature data
and byte 3 is battery status data.
*/
function decodeTempBatt(idx, buffer) {
return {
temp: buffer.readInt16BE(idx),
batt: buffer.readInt8(idx + 2)
};
}
/* Decode lightning distance.
Data is contained in a single byte integer and represents a value from 0 to 40km.
*/
function decodeDistance(idx, buffer) {
let val = buffer.readInt8(idx);
return val <= 40 ? val : null;
}
/* Decode lightning count.
Count is an integer stored in a 4 byte big endian integer.
*/
function decodeCount(idx, buffer) {
return buffer.readUInt32BE(idx);
}
/* Decode moist data.
Data is contained in a single unsigned byte and represents whole units.
*/
function decodeMoist(idx, buffer) {
return buffer.readInt8(idx);
}
/* Decode aq data.
Data is contained in a two byte big endian integer and represents tenths of a unit.
*/
function decodeAq(idx, buffer) {
return buffer.readUInt16BE(idx) / 10.0;
}
/* Decode leak data.
Data is contained in a single unsigned byte and represents whole units.
*/
function decodeLeak(idx, buffer) {
return buffer.readInt8(idx);
}
/* Decode battery status data.
Battery status data is provided in 16 bytes using a variety of
representations. Different representations include:
- use of a single bit to indicate low/OK
- use of a nibble to indicate battery level
- use of a byte to indicate battery voltage
WH24, WH25, WH26(WH32), WH31, WH40, WH41 and WH51
stations/sensors use a single bit per station/sensor to indicate OK or
low battery. WH55 and WH57 sensors use a single byte per sensor to
indicate OK or low battery. WH68 and WS80 sensors use a single byte to
store battery voltage.
The battery status data is allocated as follows
Byte # Sensor Value Comments
byte 1 WH40(b4) 0/1 1=low, 0=normal
WH26(WH32?)(b5) 0/1 1=low, 0=normal
WH25(b6) 0/1 1=low, 0=normal
WH24(b7) 0/1 may be WH65, 1=low, 0=normal
2 WH31 ch1(b0) 0/1 1=low, 0=normal, 8 channels b0..b7
...
ch8(b7) 0/1 1=low, 0=normal
3 WH51 ch1(b0) 0/1 1=low, 0=normal, 16 channels b0..b7 over 2 bytes
...
ch8(b7) 0/1 1=low, 0=normal
4 ch9(b0) 0/1 1=low, 0=normal
...
ch16(b7) 0/1 1=low, 0=normal
5 WH57 0-5 <=1 is low
6 WH68 0.02*value Volts
7 WS80 0.02*value Volts
8 Unused
9 WH41 ch1(b0-b3) 0-5 <=1 is low
ch2(b4-b7) 0-5 <=1 is low
10 ch3(b0-b3) 0-5 <=1 is low
ch4(b4-b7) 0-5 <=1 is low
11 WH55 ch1 0-5 <=1 is low
12 WH55 ch2 0-5 <=1 is low
13 WH55 ch3 0-5 <=1 is low
14 WH55 ch4 0-5 <=1 is low
15 Unused
16 Unused
For stations/sensors using a single bit for battery status 0=OK and
1=low. For stations/sensors using a single byte for battery
status >1=OK and <=1=low. For stations/sensors using a single byte for
battery voltage the voltage is 0.02 * the byte value.
# WH24 F/O THWR sensor station
# WH25 THP sensor
# WH26(WH32) TH sensor
# WH40 rain gauge sensor
*/
function decodeBatt(idx, buffer) {
let status = {};
batt_fields.forEach(([sensor, size, func, format]) => {
let data;
data = buffer.readUIntBE(idx, size);
idx += size;
if (func !== null) {
func(status, sensor, data, size, format);
}
});
return status;
}
const wh41_batt = {
1: { shift: 0, mask: 0x0ffff },
2: { shift: 4, mask: 0x0fff },
3: { shift: 8, mask: 0x0ff },
4: { shift: 12, mask: 0x0f }
};
const wh55_batt = {
1: { shift: 0, mask: 0xff },
2: { shift: 8, mask: 0xff },
3: { shift: 16, mask: 0xff },
4: { shift: 24, mask: 0xff }
};
const batt_fields = [
["multi", 1, battMultiMask, MULTI_BATT],
["wh31", 1, battMaskAll, null],
["wh51", 2, battMaskAll, null],
["wh57", 1, battVal, null],
["wh68", 1, battVolt, null],
["ws80", 1, battVolt, null],
["unused", 1, null, null],
["wh41", 2, battValSplit, wh41_batt],
["wh55", 4, battValSplit, wh55_batt],
["unused", 1, null, null],
["unused", 1, null, null]
];
function battMultiMask(status, multiSensor, data, size, format) {
for (const [sensor, maskBit] of Object.entries(format)) {
status[sensor] = (data & maskBit) === 1;
}
}
function battMaskAll(status, sensor, data, size) {
let sensorStatus = {};
for (let i = 0; i < 8 * size; i++) {
sensorStatus[`ch${i + 1}`] = (data & i) === 1;
}
status[sensor] = sensorStatus;
}
function battVal(status, sensor, data) {
status[sensor] = data >= 0 && data < 2;
}
function battValSplit(status, sensor, data, size, format) {
let sensorStatus = {};
for (const [ch, f] of Object.entries(format)) {
let _data = f.shift != null ? data >> f.shift : data;
if (f.mask != null) {
_data = _data & f.mask;
if (_data === f.mask) {
_data = false;
}
}
sensorStatus[`ch${ch}`] = _data;
}
status[sensor] = sensorStatus;
}
function battVolt(status, sensor, data) {
status[sensor] = data < 256 ? false : data * 0.2;
}
function calcChecksum(body) {
let chksum = 0;
body.forEach((b) => (chksum += b));
return chksum % 256;
}
module.exports = WH2600Utils;