forked from tagyoureit/nodejs-Pentair
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
927 lines (766 loc) · 40.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
var serialport = require("serialport");
var SerialPort = serialport.SerialPort;
var sp = new SerialPort("/dev/ttyUSB0", {
baudrate: 9600,
databits: 8,
parity: 'none',
stopBits: 1,
flowControl: false,
parser: serialport.parsers.raw
});
var data2; //variable to hold all serialport.open data; appends to this with each read
var loglevel = 0; //1=more, 0=less
var currentStatus; // persistent object to hold pool equipment status.
var currentStatusBytes; //persistent variable to hold full bytes of pool status
var instruction = ''; //var to hold potential chatter instructions
var processingBuffer = 0; //flag to tell us if we are processing the buffer currently
var counter = 0; //log counter to help match messages with buffer in log
var pumpMessages = 0; //variable if we want to output pump messages or not
var duplicateMessages = 0; //variable if we want to output duplicate broadcast messages
var showConsoleNotDecoded = 0; //variable to hide any unknown messages
const state = {
OFF: 0,
ON: 1,
}
const stateStr = {
'off': state.OFF,
'on': state.ON
}
const strState = {
0: "Off",
1: "On"
}
// offset from start of packet info
// this packet definition is only good if heat command on pool is one of Heater/Solar Pref/Solar Only (off is different)
const packetFields = {
DEST: 0,
FROM: 1,
ACTION: 2,
DATASIZE: 3,
HOUR: 4,
MIN: 5,
EQUIP1: 6,
EQUIP2: 7,
EQUIP3: 8,
WATER_TEMP: 18,
TEMP_2: 19,
AIR_TEMP: 22
}
const pumpPacketFields = {
DEST: 0,
FROM: 1,
ACTION: 2,
LENGTH: 3,
CMD: 4, //
MODE: 5, //?? Mode in pump status. Means something else in pump write/response
DRIVESTATE: 6, //?? Drivestate in pump status. Means something else in pump write/response
WATTSH: 7,
WATTSL: 8,
RPMH: 9,
RPML: 10,
PPC: 11, //??
//12 Unknown
ERR: 13,
//14 Unknown
TIMER: 14, //Have to explore
//15, 16 Unknown
HOUR: 17, //Hours
MIN: 18 //Mins
}
const pumpAction = {
1: 'WRITE', //Write commands to pump
4: 'REMOTE', //Turn on/off pump control panel
5: 'MODE', //Set pump mode
6: 'RUN', //Set run mode
7: 'STATUS' //Request status
}
const heatMode = {
//Pentair controller sends the pool and spa heat status as a 4 digit binary byte from 0000 (0) to 1111 (15). The left two (xx__) is for the spa and the right two (__xx) are for the pool. EG 1001 (9) would mean 10xx = 2 (Spa mode Solar Pref) and xx01 = 1 (Pool mode Heater)
0: 'Off',
1: 'Heater',
2: 'Solar Pref',
3: 'Solar Only'
}
const ctrl = {
BROADCAST: 15,
MAIN: 16,
REMOTE: 32,
WIRELESS: 34, //Looks like this is any communications through the wireless link (ScreenLogic on computer, iPhone...)
PUMP1: 96,
PUMP2: 97
}
const ctrlString = {
15: 'Broadcast',
16: 'Main',
32: 'Remote',
34: 'Wireless', //GUESS
96: 'Pump1',
97: 'Pump2'
}
var winston = require('winston');
var winCount = 0;
var logger = new(winston.Logger)({
transports: [
new(winston.transports.File)({
name: 'info-file',
filename: 'pentair_info.log',
level: 'silly'
})
]
});
var fulllogger = new(winston.Logger)({
transports: [
new(winston.transports.File)({
name: 'full-dump-file',
filename: 'pentair_full_dump.log',
level: 'silly'
})
]
});
var configurationFile = 'config.json';
fs = require('fs');
var poolConfig = JSON.parse(fs.readFileSync(configurationFile));
//-----array format
var j = 0;
var circuitArr = [
[], //Circuits 0-7
[], //Circuits 8-15
[] //Circuits 16-?
];
for (var key in poolConfig.Pentair) {
if (poolConfig.Pentair.hasOwnProperty(key)) {
if (j < 8) {
myEQ = 0; //8 bits for first mode byte
} else if (j >= 8 && j < 16) {
(myEQ = 1) //8 bits for 2nd mode byte
} else(myEQ = 2); //8 bits for 3rd mode byte
circuitArr[myEQ].push(poolConfig.Pentair[key]);
j++;
}
}
//return a given equipment name given the circuit # 0-16++
function circuitArrStr(equip) {
equip = equip - 1; //because equip starts at 1 but array starts at 0
if (equip < 8) {
return circuitArr[0][equip]
} else if (equip >= 8 && equip < 16) {
return circuitArr[1][equip - 8]
} else {
return circuitArr[2][equip - 16]
}
return 'Error';
}
//Used to count all items in array
//countObjects(circuitArr) will count all items provided in config.json
function countObjects(obj) {
var count = 0;
// iterate over properties, increment if a non-prototype property
for (var key in obj) {
if (obj.hasOwnProperty(key))
for (var key2 in obj[key]) {
if (obj[key].hasOwnProperty(key2))
count++
}
}
return count;
}
var equipmentCount = countObjects(circuitArr)
if (loglevel) {
logger.debug('total # equipment: ', equipmentCount) //17
logger.debug('equipLen: ', circuitArr.length, '0 array: ', circuitArr[0].length) //3, 8
}
sp.on('open', function () {
console.log('open');
sp.on('data', function (data) {
var brokeBufferLoop = false; //flag to see if there was a message at the end of the buffer
if (typeof data2 === "undefined") {
data2 = data.slice(0);
if (loglevel) {
logger.debug('assigning data2=data');
}
} else {
data2 = Buffer.concat([data2, data]);
}
//put this in a static variable as the length might grow as we process more serial bus data!
var currBufferLen = data2.length;
//start to parse message at 250 bytes. Is there a better time or way to know when the buffer has a full message or to start processing?
if (currBufferLen > 50 && !processingBuffer) {
//Data is being pushed to the buffer from the serial port faster than we are processing it. Maybe just dump some data at some point? But we would read >250 characters before this part of the code would finish so it was being called multiple times on the same (ever increasing) data2 buffer. This flag should at least only let us process one part at a time.
processingBuffer = 1;
var chatter; //a {potential} message we have found on the bus
var b = data2.toJSON();
var i = 0;
//-9 because the 8th byte is the length byte and we check for a full message below.
loop1: {
for (i; i < currBufferLen - 9; i++) {
if (b.data[i] == 0) {
continue;
} else {
//look for Pentair preamble 255,0,255,165
if (b.data[i] == 255 && b.data[i + 1] == 0 && b.data[i + 2] == 255 && b.data[i + 3] == 165) {
//NEED MORE CHECKS FOR VARIOUS TYPES OF MESSAGES! :-)
var chatterlen = b.data[i + 8] + 6 + 2; //chatterlen is length of following message not including checksum (need to add 6 for start of chatter (165,07,Dest,Src,02,chatterlen) and 2 for checksum)
//if we don't have the length bit in the buffer or the length of the message is less than the remaining buffer bytes
fulllogger.log('silly', 'Msg# %s BUFFER IN MSG CHECK: data2.len %s, chatterlen %s, i %s: TOTAL: %s True? %s ', counter, data2.length, chatterlen, i, data2.length - i - 1 - chatterlen, (data2.length - i - 2 - chatterlen) <= 0)
if (chatterlen == undefined || (data2.length - i - 2 - chatterlen) <= 0) {
//reset the buffer starting with the current partial message
fulllogger.debug('Msg# %s Incomplete message at end of buffer. Prepending message to empty buffer string.');
brokeBufferLoop = true;
data2 = data2.slice(i - 2)
break loop1;
}
counter += 1;
fulllogger.info('Msg# %s Full buffer where message found: %s', counter, b.data.toString())
i += 3; //jump ahead to start of payload
fulllogger.silly('Msg# %s Length should be: %s at position: %s ', counter, chatterlen, i)
//iterate through the JSON array to pull out a valid message
loop2: {
for (j = 0; j < chatterlen; j++) {
if (j == 0) {
var output = " Found chatter (text): " //for logging, remove later
chatter = new Array(chatterlen);
}
output += b.data[i + j];
output += " ";
chatter[j] = b.data[i + j];
if (j == chatterlen - 1) {
fulllogger.silly('Msg# %s Extracting chatter from buffer: (length of chatter %s, position in buffer %s, start position of chatter in buffer %s) %s', counter, chatterlen, i, j, output)
//This may be unnecessary; fixed code so we should get correct messages but will leave it for now
if (chatter[j] == undefined || chatter[j - 1] == undefined || chatter[j - 1] == undefined) {
fulllogger.warn('Msg# %s Chatter length MISMATCH. len %s, i %s currBufferLen %s', counter, chatterlen, i, currBufferLen)
}
checksum(chatter, decode, counter);
//skip ahead in the buffer for loop to the end of this message.
i += chatterlen;
break loop1;
}
}
}
}
//this isn't working <-- CHECK
else if (b.data[i + 8] == 6) {
var str;
console.log('------->FOUND SOMETHING???')
console.log('data: ', JSON.stringify(b.data))
i += 3;
chatterlen = 8;
for (j = 0; j < chatterlen; j++) {
if (j == 0) {
var output = " Found SOMETHING chatter (text): " //for logging, remove later
chatter = new Array(chatterlen);
}
output += b.data[i + j];
output += " ";
console.log('SOMETHING in chatter: (len %s, i %s, j %s) %s', chatterlen, i, j, output)
chatter[j] = b.data[i + j];
if (j == chatterlen - 1) {
if (chatter[j] == undefined || chatter[j - 1] == undefined || chatter[j - 1] == undefined) {
console.log('Chatter SOMETHING length MISMATCH. len %s, i %s currBufferLen %s', chatterlen, i, currBufferLen)
}
//console.log(output + '\n');
//console.log('processingBuffer:' + processingBuffer)
//console.log('OR HERE????:' + chatter.toString())
//logger.log('info', chatter.toString())
checksum(chatter, decode);
//skip ahead in the buffer for loop to the end of this message.
i += chatterlen;
console.log('<-----FOUND SOMETHING???')
break loop1;
}
}
}
}
}
}
//slice the buffer from currBufferLen (what we have processed) to the end of the buffer
if (brokeBufferLoop) {
//we are here if we broke out of the buffer. This means there is the start of a message in the last 9+/- bytes
//We do this above! Don't need it here. data2 = data2.slice(currBufferLen - 9);
fulllogger.debug('Incomplete message at end of buffer. Sliced buffer so message is at beginning of buffer (sliced by %s) ', currBufferLen - 9);
} else {
//We should get here after every message. Slice the buffer to a new message
data2 = data2.slice(i);
fulllogger.debug('At end of message. Sliced off %s from remaining buffer.', currBufferLen);
}
processingBuffer = 0;
};
});
});
//Validate the checksum on the chatter
function checksum(chatterdata, callback, counter) {
//make a copy so when we callback the decode method it isn't changing our log output in Winston
fulllogger.silly("Msg# %s Checking checksum on chatter: ", chatterdata);
var chatterCopy = chatterdata.slice(0);
len = chatterCopy.length;
//checksum is calculated by 256*2nd to last bit + last bit
var chatterdatachecksum = (chatterCopy[len - 2] * 256) + chatterCopy[len - 1];
databytes = 0;
// add up the data in the payload
for (i = 0; i < len - 2; i++) {
databytes += chatterCopy[i];
}
validChatter = (chatterdatachecksum == databytes);
if (!validChatter) {
fulllogger.warn('Msg# %s Mismatch on checksum: %s!=%s %s', counter, chatterdatachecksum, databytes, chatterCopy)
console.log('Msg# %s Mismatch on checksum: %s!=%s %s', counter, chatterdatachecksum, databytes, chatterCopy)
} else {
logger.info('Msg# %s Match on Checksum: %s==%s %s', counter, chatterdatachecksum, databytes, chatterCopy)
}
//Go back to working on the original, not the copy
//now that we calculated checksum, strip leading 165 and 10 as it is extraneous
chatterCopy = chatterCopy.splice(2);
//console.log("NewCD: ", newcd);
fulllogger.silly("Msg# %s Chatterdata splice: %s --> %s ", counter, chatterdata, chatterCopy)
//call new function to process message; if it isn't valid, we noted above so just don't continue
if (validChatter) callback(chatterCopy, counter);
};
function dec2bin(dec) {
return (dec >>> 0).toString(2);
}
function printStatus(data1, data2) {
str1 = clone(data1);
str2 = clone(data2);
spacepadding = '';
spacepaddingNum = 19;
for (i = 0; i <= spacepaddingNum; i++) {
spacepadding += ' ';
}
header = '';
header += (spacepadding + ' S L W A \n');
header += (spacepadding + ' O E M M M T I \n');
header += (spacepadding + ' D U N H O O O R T R C C\n');
header += (spacepadding + ' E R G O M D D D T M T H H\n');
header += (spacepadding + ' S C T U I E E E M P M K K\n');
header += (spacepadding + ' T E H R N 1 2 3 P 2 P H L\n');
// e.g. 15, 16, 2, 29, 11, 33, 32, 0, 0, 0, 0, 0, 0, 0, 51, 0, 64, 4, 79, 79, 32, 0, 69,102, 0, 0, 7, 0, 0,182,215, 0, 13, 4,186
//format status1 so numbers are three digits
for (i = 0; i < str1.length - 1; i++) {
str1[i] = pad(str1[i], 3);
}
str1 = 'Orig: ' + spacepadding.substr(6) + str1 + '\n';
//format status2 the same
if (data2 != null || data2 != undefined) {
for (i = 0; i < str2.length - 1; i++) {
str2[i] = pad(str2[i], 3);
}
str2 = ' New: ' + spacepadding.substr(6) + str2 + '\n'
} else {
str2 = ''
}
str = header + str1 + str2;
return (str);
}
function pad(num, size) {
//makes any digit returned as a string of length size (for outputting formatted byte text)
var s = " " + num;
return s.substr(s.length - size);
}
function decode(data, counter) {
var decoded = false;
//uncomment the below line if you think the 'parser' is missing any messages. It will output every message sent here.
//console.log('Msg# %s is %s', counter, JSON.stringify(data));
//this payload is good if heat command on pool is one of Heater/Solar Pref/Solar Only
if (data.length == 35 && data[packetFields.FROM] == ctrl.MAIN && data[packetFields.DEST] == ctrl.BROADCAST) {
//status will be our return object
var status = {};
//time returned in HH:MM (24 hour) <-- need to clean this up so we don't get times like 5:3
status.time = data[packetFields.HOUR] + ':' + data[packetFields.MIN];
status.waterTemp = data[packetFields.WATER_TEMP];
status.temp2 = data[packetFields.TEMP_2];
status.airTemp = data[packetFields.AIR_TEMP];
//Loop through the three bits that start at 3rd (and 4th/5th) bit in the data payload
for (i = 0; i < circuitArr.length; i++) {
//loop through all physical circuits within each of the bits
for (j = 0; j < circuitArr[i].length; j++) {
tempFeature = circuitArr[i][j]; //name of circuit
equip = data[packetFields.EQUIP1 + i]
status[tempFeature] = (equip & (1 << (j))) >> j ? "on" : "off"
}
}
//Initialize static variable (currentStatus) if not defined, and log it.
if (currentStatus == null || currentStatus == undefined) {
currentStatus = clone(status);
currentStatusBytes = data.slice(0);
console.log('-->EQUIPMENT Msg# %s \n Equipment Status: %O', counter, status)
logger.info('Msg# %s Discovered initial pool settings: %s', counter, JSON.stringify(currentStatus))
console.log(printStatus(data));
console.log('\n <-- EQUIPMENT \n');
decoded = true;
} else {
//Check if we have the same data
//This should also significantly limit the amount of duplicate broadcast/chatter. At a minimum, the packet should be different every minute (due to time). If it is more than that, we need to explore further.
if (!status.equals(currentStatus)) {
//we are only checking our KNOWN objects. There may be other differences and we'll recode for that shortly.
console.log('-->EQUIPMENT Msg# %s \n Equipment Status: ', counter, currentStatus)
console.log('Msg# %s What\'s Different?: %s', counter, currentStatus.whatsDifferent(status))
console.log(printStatus(currentStatusBytes, data));
console.log('\n <-- EQUIPMENT \n');
currentStatus = clone(status);
currentStatusBytes = data.slice(0);
decoded = true;
} else {
//let's see if it is the exact same packet or if there are variations in the data we have not interpreted yet
if (!data.equals(currentStatusBytes)) {
console.log('-->Variation in unknown status bytes Msg# ', counter)
console.log(printStatus(currentStatusBytes, data));
console.log('<--Variation \n')
currentStatusBytes = data.slice(0);
decoded = true;
} else {
if (duplicateMessages) console.log('Msg# %s Duplicate broadcast.', counter)
decoded = true;
}
}
}
// if the time field is whacked, don't send any data. <-- leftover code. can probable eliminate
d = status.time.split(":")
if (parseInt(d[0]) < 24 && parseInt(d[1]) < 60) {
if (loglevel) console.log('-->EQUIPMENT Msg# ', counter, '\n Equipment Status verbose: ', JSON.stringify(status), '\n', parseChatter(data), '\n <-- EQUIPMENT \n');
}
} else if (((data[packetFields.FROM] == ctrl.PUMP1 || data[packetFields.FROM] == ctrl.PUMP2) && data[packetFields.DEST] == ctrl.MAIN) || ((data[packetFields.DEST] == ctrl.PUMP1 || data[packetFields.DEST] == ctrl.PUMP2) && data[packetFields.FROM] == ctrl.MAIN))
// --> Could be from/to control and from/to pump. Check all???? Or is data length 8 sufficient
//&& data[packetFields.FROM] == ctrl.MAIN && (data[packetFields.DEST] == ctrl.PUMP1 || data[packetFields.DEST] == ctrl.PUMP2)
{
if (instruction == null || instruction == undefined || instruction == '') {
instruction = data;
console.log('Msg# %s Setting initial chatter as instruction: %s', counter, instruction)
}
var isResponse = data.isResponse(instruction);
var ctrlType = data2;
//Send request/response for pump status
if (data[pumpPacketFields.ACTION] == 7) {
if (data[pumpPacketFields.CMD] == 1) //Request pump status
{
if (pumpMessages) console.log('Msg# %s Main asking %s for status: %s', counter, ctrlString[data[pumpPacketFields.DEST]], JSON.stringify(data));
decoded = true;
} else //Response to request for status
{
var status = {
pump: null,
run: null,
mode: null,
drivestate: null,
watts: null,
rpm: null,
ppc: null,
err: null,
timer: null,
time: null
}
var pumpnum = (data[pumpPacketFields.FROM]).toString();
//time returned in HH:MM (24 hour) <-- need to clean this up so we don't get times like 5:3
status.time = data[pumpPacketFields.HOUR] + ':' + data[pumpPacketFields.MIN];
status.run = data[pumpPacketFields.CMD]
status.pump = ctrlString[pumpnum];
status.mode = data[pumpPacketFields.MODE]
status.drivestate = data[pumpPacketFields.DRIVESTATE]
status.watts = (data[pumpPacketFields.WATTSH] * 256) + data[pumpPacketFields.WATTSL]
status.rpm = (data[pumpPacketFields.RPMH] * 256) + data[pumpPacketFields.RPML]
status.ppc = data[pumpPacketFields.PPC]
status.err = data[pumpPacketFields.ERR]
status.timer = data[pumpPacketFields.TIMER]
if (pumpMessages) console.log('--> PUMP Msg# ', counter, '\n', ctrlString[pumpnum], '\n Pump Status: ', JSON.stringify(status), '\n', 'Full Payload: ', JSON.stringify(data), '\n<-- PUMP ', ctrlString[pumpnum], '\n');
decoded = true;
}
} else
if (data[pumpPacketFields.ACTION] == 4) //Pump control panel on/off
{
if (data[pumpPacketFields.CMD] == 255) //Set pump control panel off (Main panel control only)
{
if (!isResponse) {
if (pumpMessages) console.log('Msg# %s %s asking %s for remote control (turn off pump control panel): %s', counter, ctrlString[data[pumpPacketFields.FROM]], ctrlString[data[pumpPacketFields.DEST]], JSON.stringify(data));
decoded = true;
} else {
if (pumpMessages) console.log('Msg# %s %s confirming it is in remote control: %s', counter, ctrlString[data[pumpPacketFields.FROM]], JSON.stringify(data))
decoded = true;
}
}
if (data[pumpPacketFields.CMD] == 0) //Set pump control panel on
{
if (!isResponse) {
if (pumpMessages) console.log('Msg# %s %s asking %s for local control (turn on pump control panel): %s', counter, ctrlString[data[pumpPacketFields.FROM]], ctrlString[data[pumpPacketFields.DEST]], JSON.stringify(data))
decoded = true;
} else {
if (pumpMessages) console.log('Msg# %s %s confirming it is in local control: %s', counter, ctrlString[data[pumpPacketFields.FROM]], JSON.stringify(data))
decoded = true;
}
}
} else if (data[pumpPacketFields.ACTION] == 1) //Write command to pump
{
if (data[pumpPacketFields.LENGTH] == 4) //This might be the only packet where isResponse() won't work because the pump sends back a validation command
{
var pumpCommand = '';
for (i = 1; i < data[pumpPacketFields.LENGTH]; i++) {
pumpCommand += data[i + pumpPacketFields.LENGTH] //Not sure if it is a coincidence that pumpPacketFields.LENGTH ==4, but the 4th byte is the start of the message.
pumpCommand += ', '
}
if (pumpMessages) console.log('Msg# %s %s asking %s to write a _%s_ command: %s', counter, ctrlString[data[pumpPacketFields.FROM]], ctrlString[data[pumpPacketFields.DEST]], pumpCommand, JSON.stringify(data));
decoded = true;
} else {
var pumpResponse = ''
pumpResponse += data[pumpPacketFields.LENGTH + 1] + ', ' + data[pumpPacketFields.LENGTH + 2]
if (pumpMessages) console.log('Msg# %s %s sent response _%s_ to write command: %s', counter, ctrlString[data[pumpPacketFields.FROM]], pumpResponse, JSON.stringify(data));
decoded = true;
}
} else if (data[pumpPacketFields.ACTION] == 5) //Set pump mode
{
if (!isResponse) {
if (pumpMessages) console.log('Msg# %s %s asking %s to set pump mode to _%s_: %s', counter, ctrlString[data[pumpPacketFields.FROM]], ctrlString[data[pumpPacketFields.DEST]], data[pumpPacketFields.CMD], JSON.stringify(data));
decoded = true;
} else {
if (pumpMessages) console.log('Msg# %s %s confirming it is in mode _%s_: %s', counter, ctrlString[data[pumpPacketFields.FROM]], data[pumpPacketFields.CMD], JSON.stringify(data));
decoded = true;
}
} else if (data[pumpPacketFields.ACTION] == 6) //Set run mode
{
if (!isResponse) {
if (pumpMessages) console.log('Msg# %s %s asking %s to set run to _%s_: %s', counter, ctrlString[data[pumpPacketFields.FROM]], ctrlString[data[pumpPacketFields.DEST]], data[pumpPacketFields.CMD], JSON.stringify(data));
decoded = true;
} else {
if (pumpMessages) console.log('Msg# %s %s confirming it is in run _%s_: %s', counter, ctrlString[data[pumpPacketFields.FROM]], data[pumpPacketFields.CMD], JSON.stringify(data));
decoded = true;
}
} else {
if (pumpMessages) console.log('Msg# %s is %s', counter, JSON.stringify(data));
decoded = true;
}
instruction = data.slice();
} else if (((data[packetFields.FROM] == ctrl.REMOTE || data[packetFields.FROM] == ctrl.WIRELESS) && data[packetFields.DEST] == ctrl.MAIN) || ((data[packetFields.DEST] == ctrl.REMOTE || data[packetFields.DEST] == ctrl.WIRELESS) && data[packetFields.FROM] == ctrl.MAIN)) {
if (data[packetFields.DEST] == 16) {
var status = {
source: null,
destination: null,
b3: null,
CMD: null,
sFeature: null,
ACTION: null,
b7: null
}
status.source = data[packetFields.FROM]
status.destination = data[packetFields.DEST]
status.b3 = data[2] //134... always?
status.CMD = data[3] == 4 ? 'pool temp' : 'feature'; // either 4=pool temp or 2=feature
if (data[3] == 2) {
status.sFeature = circuitArrStr(data[4])
if (data[5] == 0) {
status.ACTION = "off"
} else if (data[5] == 1) {
status.ACTION = "on"
}
console.log('Msg# %s %s asking %s to change _%s %s to %s_ : %s', counter, ctrlString[data[packetFields.FROM]], ctrlString[data[packetFields.DEST]], status.CMD, status.sFeature, status.ACTION, JSON.stringify(data));
decoded = true;
} else if (data[3] == 4) {
status.POOLSETPOINT = data[4];
status.SPASETPOINT = data[5];
status.POOLHEATMODE = heatMode[data[6]&3]; //mask the data[6] with 0011
status.SPAHEATMODE = heatMode[(data[6]&12)>>2]; //mask the data[6] with 1100 and shift right two places
console.log('Msg# %s %s asking %s to change pool heat mode to %s (@ %s degrees) % spa heat mode to %s (at %s degrees): %s', counter, ctrlString[data[packetFields.FROM]], ctrlString[data[packetFields.DEST]], status.POOLHEATMODE, status.POOLSETPOINT, status.SPAHEATMODE, status.SPASETPOINT, JSON.stringify(data));
decoded = true;
} else if (data[3] == 16) {
//165, 10, 15, 16, 10, 12, 0, 87, 116, 114, 70, 97, 108, 108, 32, 49, 0, 251, 4, 236
console.log('12! else: %s', JSON.stringify(data))
var myString = '';
for (i = 0; i < data.length; i++) {
myString += String.fromCharCode(data[i])
}
console.log('Msg# %s %s sending %s _%s_ : %s', counter, ctrlString[data[packetFields.FROM]], ctrlString[data[packetFields.DEST]], myString, JSON.stringify(data));
}
}
}
//in case we get here and the first message has not already been set as the instruction command
if (instruction == null || instruction == undefined) {
instruction = data;
}
if (!decoded) {
fulllogger.debug('Msg# %s Starting to decode message.', counter)
if (showConsoleNotDecoded) console.log('Msg# %s is %s', counter, JSON.stringify(data));
} else(decoded = false)
return true; //fix this; turn into callback(?) What do we want to do with it?
}
//This function just for visual/log usage.
function parseChatter(parsedata) {
//make copy so we don't change the original by reference
parsedataCopy = parsedata.slice(0);
console.log(JSON.stringify(parsedataCopy));
len = parsedataCopy.length;
if (len == 35) { //Broadcast packet
if (loglevel) {
_equip1 = parsedataCopy[packetFields.EQUIP1]
_equip2 = parsedataCopy[packetFields.EQUIP2]
_equip3 = parsedataCopy[packetFields.EQUIP3]
console.log('Equip 1')
console.log('circuit1 (Spa) : ', _equip1 & 1)
console.log('bit2 (Jets) : ', (_equip1 & 2) >> 1)
console.log('bit3 (Air Blower ): ', (_equip1 & 4) >> 2)
console.log('bit4 (Cleaner) : ', (_equip1 & 8) >> 3)
console.log('bit5 (WtrFall1.5) : ', (_equip1 & 16) >> 4)
console.log('bit6 (Pool) : ', (_equip1 & 32) >> 5)
console.log('bit7 (Spa Lights?): ', (_equip1 & 64) >> 6)
console.log('bit8 (Pool Lights): ', (_equip1 & 128) >> 7)
console.log('Equip2')
console.log('bit1 (Path Lights): ', _equip2 & 1)
console.log('bit2 (?) : ', (_equip2 & 2) >> 1)
console.log('bit3 (Spillway) : ', (_equip2 & 4) >> 2)
console.log('bit4 (WtrFall 1 ) : ', (_equip2 & 8) >> 3)
console.log('bit5 (WtrFall 2 ) : ', (_equip2 & 16) >> 4)
console.log('bit6 (WtrFall 3 ) : ', (_equip2 & 32) >> 5)
console.log('bit7 (Pool low) : ', (_equip2 & 64) >> 6)
console.log('bit8 (Feature 6) : ', (_equip2 & 128) >> 7)
console.log('Equip3')
console.log('bit 1 (Feature 7)', _equip3 & 1)
console.log('bit 2 (Feature 8)', (_equip3 & 2) >> 1)
}
parsedataCopy[packetFields.FROM] = 'Src: ' + parsedataCopy[packetFields.FROM];
parsedataCopy[packetFields.DEST] = 'Dest: ' + parsedataCopy[packetFields.DEST];
parsedataCopy[packetFields.ACTION] = 'Action: ' + parsedataCopy[packetFields.ACTION];
parsedataCopy[packetFields.DATASIZE] = 'Len: ' + parsedataCopy[packetFields.DATASIZE];
parsedataCopy[packetFields.HOUR] = 'Hr: ' + parsedataCopy[packetFields.HOUR];
parsedataCopy[packetFields.MIN] = 'Min: ' + parsedataCopy[packetFields.MIN];
parsedataCopy[packetFields.EQUIP1] = 'MODE1: ' + parsedataCopy[packetFields.EQUIP1];
parsedataCopy[packetFields.EQUIP2] = 'MODE2: ' + parsedataCopy[packetFields.EQUIP2];
parsedataCopy[packetFields.EQUIP3] = 'MODE3: ' + parsedataCopy[packetFields.EQUIP3];
parsedataCopy[packetFields.WATER_TEMP] = 'WtrTemp: ' + parsedataCopy[packetFields.WATER_TEMP];
parsedataCopy[packetFields.AIR_TEMP] = 'AirTemp: ' + parsedataCopy[packetFields.AIR_TEMP];
parsedataCopy[len - 1] = 'ChkH: ' + parsedataCopy[len - 1];
parsedataCopy[len - 2] = 'ChkL: ' + parsedataCopy[len - 2];
return (JSON.stringify(parsedataCopy) + ' & Length: ', len);
} else if (len == 7) //Pump actions?
{
returnStr = 'Unknown chatter of length: %s to Dest: %s from %S ', len, ctrlString[parsedataCopy[packetFields.DEST]], ctrlString[parsedataCopy[packetFields.SRC]];
parsedataCopy[packetFields.DEST] = 'Dest: ' + parsedataCopy[packetFields.DEST];
parsedataCopy[packetFields.FROM] = 'Src: ' + parsedataCopy[packetFields.FROM];
return (returnStr + 'Pump Instructions? \n', JSON.stringify(parsedataCopy) + ' & Length: ', len);
}
}
//Credit to this function belongs somewhere on Stackoverflow. Need to find it to give credit.
Object.prototype.equals = function (object2) {
//For the first loop, we only check for types
for (propName in this) {
//Check for inherited methods and properties - like .equals itself
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
//Return false if the return value is different
if (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
}
//Check instance type
else if (typeof this[propName] != typeof object2[propName]) {
//Different types => not equal
return false;
}
}
//Now a deeper check using other objects property names
for (propName in object2) {
//We must check instances anyway, there may be a property that only exists in object2
//I wonder, if remembering the checked values from the first loop would be faster or not
if (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
return false;
} else if (typeof this[propName] != typeof object2[propName]) {
return false;
}
//If the property is inherited, do not check any more (it must be equa if both objects inherit it)
if (!this.hasOwnProperty(propName))
continue;
//Now the detail check and recursion
//This returns the script back to the array comparing
/**REQUIRES Array.equals**/
if (this[propName] instanceof Array && object2[propName] instanceof Array) {
// recurse into the nested arrays
if (!this[propName].equals(object2[propName]))
return false;
} else if (this[propName] instanceof Object && object2[propName] instanceof Object) {
// recurse into another objects
//console.log("Recursing to compare ", this[propName],"with",object2[propName], " both named \""+propName+"\"");
if (!this[propName].equals(object2[propName]))
return false;
}
//Normal value comparison for strings and numbers
else if (this[propName] != object2[propName]) {
return false;
}
}
//If everything passed, let's say YES
return true;
}
//This function adapted from the prototype.equals method above
Object.prototype.whatsDifferent = function (object2) {
//For the first loop, we only check for types
var diffString = '';
for (propName in this) {
//Check for inherited methods and properties - like .equals itself
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
//Return false if the return value is different
if (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
diffString += ' ' + this.hasOwnProperty(propName);
//return this.hasOwnProperty(propName);
}
//Check instance type
else if (typeof this[propName] != typeof object2[propName]) {
//Different types => not equal
diffString += ' Object type '
//return 'Object type';
}
}
//Now a deeper check using other objects property names
for (propName in object2) {
//We must check instances anyway, there may be a property that only exists in object2
//I wonder, if remembering the checked values from the first loop would be faster or not
if (this.hasOwnProperty(propName) != object2.hasOwnProperty(propName)) {
diffString += ' ' + this.hasOwnProperty(propName);
//return this.hasOwnProperty(propName);
} else if (typeof this[propName] != typeof object2[propName]) {
diffString += ' Object type '
//return 'Object type';
}
//If the property is inherited, do not check any more (it must be equa if both objects inherit it)
if (!this.hasOwnProperty(propName))
continue;
//Now the detail check and recursion
//This returns the script back to the array comparing
/**REQUIRES Array.equals**/
if (this[propName] instanceof Array && object2[propName] instanceof Array) {
// recurse into the nested arrays
if (!this[propName].equals(object2[propName]))
diffString += ' '
propName + ': ' + this[propName] + ' --> ' + object2[propName];
//return (propName + ': ' + this[propName]);
} else if (this[propName] instanceof Object && object2[propName] instanceof Object) {
// recurse into another objects
//console.log("Recursing to compare ", this[propName],"with",object2[propName], " both named \""+propName+"\"");
if (!this[propName].equals(object2[propName]))
diffString += ' ' + propName + ': ' + this[propName] + ' --> ' + object2[propName]
//return (propName + ': ' + this[propName]);
}
//Normal value comparison for strings and numbers
else if (this[propName] != object2[propName]) {
diffString += ' ' + propName + ': ' + this[propName] + ' --> ' + object2[propName]
//return (propName + ': ' + this[propName]);
}
}
if (diffString == '') {
return 'Nothing!';
} else {
return diffString;
}
}
//Credit for this function belongs to: http://stackoverflow.com/questions/728360/most-elegant-way-to-clone-a-javascript-object
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
Object.prototype.isResponse = function (object2) {
//make copies of the object so we assign the variables by value and not change the originals (by reference)
//need to use the clone function because slice doesn't work on objects for by value copy
tempObj = clone(this);
//this doesn't work -- it copies by reference not by value
//tempObj = this.slice();
objSrc = this[packetFields.FROM];
objDest = this[packetFields.DEST];
tempObj[packetFields.DEST] = objSrc;
tempObj[packetFields.FROM] = objDest;
//If the objects are now equal, return true.
return (tempObj.equals(object2));
}