-
Notifications
You must be signed in to change notification settings - Fork 5
/
pelcod_decoder.js
1884 lines (1630 loc) · 86.2 KB
/
pelcod_decoder.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
/*
* Read and decode Pelco D, Pelco P, BBV422, Bosch/Philips, Forward Vision, Vicon, American Dynamics/Sensormatic, VCL, JVC and HikVision CCTV PTZ commands
* This code is not designed to decode every command.
* The purpose is to monitor the output from various CCTV systems to confirm the protocols and camera addresses in use.
* The meanings of certain Aux signals and special Preset values is not shown
*
* (c) Copyright 2016-2020 Roger Hardiman
*
* Processes NodeJS Buffer() data.
* Buffer() objects may have multiple PTZ messages or just part of a message so bytes are cached if needed.
*
*/
var EventEmitter = require('events');
class PelcoD_Decoder extends EventEmitter {
constructor() {
super();
// A Buffer used for American Dynamics/Sensormatic (variable length message)
this.ad_command_buffer = new Buffer(128);
this.ad_command_index = 0;
// A Buffer used for byte Bosch/Philips BiPhase
// Max length is 128 as length is bits 0 to 6 of header
this.bosch_command_buffer = new Buffer(128);
this.bosch_command_index = 0;
// A Buffer used for byte Forward Vision Protocol (FV Protocol)
// Min length is 8. Max length is 255
this.fv_command_buffer = new Buffer(255);
this.fv_command_index = 0;
// A Buffer used to cache partial commands
this.hik_command_buffer = new Buffer(8);
this.hik_command_index = 0;
// A Buffer used for JVC (variable length message)
this.jvc_command_buffer = new Buffer(128);
this.jvc_command_index = 0;
// A Buffer used for Panasonic (variable length message)
this.panasonic_command_buffer = new Buffer(128);
this.panasonic_command_index = 0;
// A Buffer used to cache partial commands
this.pelco_command_buffer = new Buffer(7);
this.pelco_command_index = 0;
// A Buffer used to cache partial commands for Pelco P
this.pelco_p_command_buffer = new Buffer(8);
this.pelco_p_command_index = 0;
// A Buffer used to cache partial commands for Samsung
this.samsung_command_buffer = new Buffer(9);
this.samsung_command_index = 0;
// A Buffer used for VCL (variable length message)
this.vcl_command_buffer = new Buffer(128);
this.vcl_command_index = 0;
// A Buffer used for Vicon
this.vicon_command_buffer = new Buffer(10);
this.vicon_command_index = 0;
// A Buffer used for Visca (variable length message)
this.visca_command_buffer = new Buffer(128);
this.visca_command_index = 0;
}
// Get the ascii value of a character. Even 'A' is a String in javascript so cannot
// simply match 'A' with 65 without a helper function
ascii(str) {
return str.charCodeAt(0);
}
// new_data_buffer can be a NodeJS Buffer or a Javascript array
// as the only methods called are .length and the array index '[]' operator
processBuffer(new_data_buffer) {
// this.emit("log",'received ' + this.bytes_to_string(new_data_buffer,new_data_buffer.length) );
// process each byte from new_data_buffer in turn
for (var i = 0; i < new_data_buffer.length; i++) {
// Get the next new byte
var new_byte = new_data_buffer[i];
this.send_byte_to_ad(new_byte);
this.send_byte_to_samsung(new_byte);
//this.send_byte_to_vcl_decoder(new_byte);
// Add to the end of the Pelco D buffer
// We cannot simply look for 0xFF as this could be
// part of the payload as well as the header
if (this.pelco_command_index < this.pelco_command_buffer.length) {
// Add the new_byte to the end of the pelco_command_buffer
this.pelco_command_buffer[this.pelco_command_index] = new_byte;
this.pelco_command_index++;
} else {
// Shift the bytes to make room for the new_byte at the end
for (var x = 0; x < (this.pelco_command_buffer.length - 1); x++) {
this.pelco_command_buffer[x] = this.pelco_command_buffer[x + 1];
}
// Then add the new_byte to the end
this.pelco_command_buffer[this.pelco_command_buffer.length - 1] = new_byte;
}
// Add to the end of Pelco P buffer
// We cannot simply look for 0xA0 as this could be
// part of the payload as well as the header value
if (this.pelco_p_command_index < this.pelco_p_command_buffer.length) {
// Add the new_byte to the end of the pelco_p_command_buffer
this.pelco_p_command_buffer[this.pelco_p_command_index] = new_byte;
this.pelco_p_command_index++;
} else {
// Shift the bytes to make room for the new_byte at the end
for (var x = 0; x < (this.pelco_p_command_buffer.length - 1); x++) {
this.pelco_p_command_buffer[x] = this.pelco_p_command_buffer[x + 1];
}
// Then add the new_byte to the end
this.pelco_p_command_buffer[this.pelco_p_command_buffer.length - 1] = new_byte;
}
// Add to Bosch byte buffer
if (new_byte & 0x80) {
// MSB is set to 1. This marks the start of a Bosch command so reset buffer counter
this.bosch_command_index = 0;
}
if (this.bosch_command_index < this.bosch_command_buffer.length) {
// Add the new_byte to the end of the bosch_command_buffer
this.bosch_command_buffer[this.bosch_command_index] = new_byte;
this.bosch_command_index++;
}
// Add to Forward Vision (FV) byte buffer
if (new_byte == 0x0A) {
// Always starts with 0x0A (LineFeed). Other bytes are 'ascii range'. Checksum is >= 128 (0x80 to 0xFF) so 0x0A is unique.
this.fv_command_index = 0;
}
if (this.fv_command_index < this.fv_command_buffer.length) {
// Add the new_byte to the end of the fv_command_buffer
this.fv_command_buffer[this.fv_command_index] = new_byte;
this.fv_command_index++;
}
// Add to Vicon byte buffer
if (new_byte & 0x80) {
// MSB is set to 1. This marks the start of a Vicon command so reset buffer counter
this.vicon_command_index = 0;
}
if (this.vicon_command_index < this.vicon_command_buffer.length) {
// Add the new_byte to the end of the vicon_command_buffer
this.vicon_command_buffer[this.vicon_command_index] = new_byte;
this.vicon_command_index++;
}
// Add to Panasonic byte buffer accumulating bytes
// It starts with 0x02 (STX) and ends with 0x03 (ETX)
// The rest of the data bytes are ASCII characters 0..9 and A..Z
// and also : and ; which are used to split parts of the command
// and a length byte that is is 7,F,N,V,^[0x5e],f,n,v,~[0x7e] and '('[0x28]
if (new_byte == 0x02) {
// Always starts with 0x02 (STX)
this.panasonic_command_index = 0;
}
var valid_byte = false;
if (new_byte == 0x02 || new_byte == 0x03) valid_byte = true;
if (new_byte >= this.ascii('0') && new_byte <= this.ascii('9')) valid_byte = true;
if (new_byte >= this.ascii('A') && new_byte <= this.ascii('Z')) valid_byte = true;
if (new_byte == this.ascii(':') || new_byte == this.ascii(';')) valid_byte = true;
if (new_byte == this.ascii('7') || new_byte == this.ascii('F')
|| new_byte == this.ascii('N') || new_byte == this.ascii('V')
|| new_byte == 0x5E || new_byte == this.ascii('f')
|| new_byte == this.ascii('n') || new_byte == this.ascii('v')
|| new_byte == 0x7E || new_byte == 0x28) valid_byte = true;
if (valid_byte == false) {
// not panasonic data. Reset the buffer
this.panasonic_command_index = 0;
}
if (this.panasonic_command_index < this.panasonic_command_buffer.length
&& valid_byte == true) {
// Add the new_byte to the end of the panasonic_command_buffer
this.panasonic_command_buffer[this.panasonic_command_index] = new_byte;
this.panasonic_command_index++;
}
// Pelco D Test. Check if we have 7 bytes with byte 0 = 0xFF and with a valid SUM checksum
if (this.pelco_command_index === 7 && this.pelco_command_buffer[0] === 0xFF
&& this.checksum_valid(this.pelco_command_buffer)) {
// Looks like we have a Pelco command. Try and process it
this.decode_pelco(this.pelco_command_buffer);
this.pelco_command_index = 0; // empty the buffer
}
// Pelco P Test. Check if we have 8 bytes with byte 0 = 0xA0, byte 6 = 0xAF and with a valid XOR checksum
if (this.pelco_p_command_index === 8 && this.pelco_p_command_buffer[0] === 0xA0
&& this.pelco_p_command_buffer[6] === 0xAF
&& this.checksum_p_valid(this.pelco_p_command_buffer)) {
// Looks like we have a Pelco command. Try and process it
this.decode_pelco(this.pelco_p_command_buffer);
this.pelco_p_command_index = 0; // empty the buffer
}
// BBV422 Protocol Test. Check if we have 8 bytes with byte 0 = 0xB0, byte 6 = 0xBF and with a valid XOR checksum
if (this.pelco_p_command_index === 8 && this.pelco_p_command_buffer[0] === 0xB0
&& this.pelco_p_command_buffer[6] === 0xBF
&& this.checksum_p_valid(this.pelco_p_command_buffer)) {
// Looks like we have a Pelco command. Try and process it
this.decode_pelco(this.pelco_p_command_buffer);
this.pelco_p_command_index = 0; // empty the buffer
}
// Bosch Test. First byte has MSB of 1. First byte is the message size (excluding the checksum)
var bosch_len = this.bosch_command_buffer[0] & 0x7F;
if ((bosch_len > 1)
&& (this.bosch_command_buffer[0] & 0x80)
&& this.bosch_command_index == (bosch_len + 1)
&& this.checksum_bosch_valid(this.bosch_command_buffer, this.bosch_command_index)) {
// Looks like we have a Bosch command. Try and process it
this.decode_bosch(this.bosch_command_buffer);
this.bosch_command_index = 0; // empty the buffer
}
// Forward Vision. Byte 0 is 0x0A. Checksum is only byte with MSB set to 1
if ((this.fv_command_buffer[0] == 0x0A)
&& this.fv_command_index >= 8
&& (this.fv_command_buffer[this.fv_command_index - 1] >= 128) // checksum
&& this.checksum_fv_valid(this.fv_command_buffer, this.fv_command_index)) {
// Looks like we have a Forward Vision command. Try and process it
this.decode_forward_vision(this.fv_command_buffer, this.fv_command_index);
this.fv_command_index = 0; // empty the buffer
}
// Vicon. 10 bytes where Byte 1 upper nibble is 1000
// and Byte 2 has bit 6 set (meaning extended command)
if (((this.vicon_command_buffer[0] & 0xF0) === 0x80)
&& (this.vicon_command_buffer[1] & 0x40)
&& (this.vicon_command_index == 10)) {
// Looks like we have a Vicon command. Try and process it
this.decode_vicon(this.vicon_command_buffer, this.vicon_command_index);
this.vicon_command_index = 0; // empty the buffer
}
// Panasonic is STX (0x02) then payload and then ETX (0x03)
// The Payload is at least 11 bytes eg GC7:9034002 so the shortest message is 13 bytes
if ((this.panasonic_command_buffer[0] == 0x02)
&& (this.panasonic_command_buffer[this.panasonic_command_index - 1] == 0x03)
&& (this.panasonic_command_index >= 13)) {
// Looks like we have a Panasonic command. Try and process it
this.decode_panasonic(this.panasonic_command_buffer, this.panasonic_command_index);
this.panasonic_command_index = 0; // empty the buffer
}
// Collect VISCA data
// Starts with MSBit = 1
// Ends with 0xFF
if ((new_byte & 0x80) && (new_byte != 0xFF)) {
// MSB is set to 1. This marks the start of a the command so reset buffer counter
this.visca_command_buffer[0] = new_byte;
this.visca_command_index = 1;
} else if (this.visca_command_index < this.visca_command_buffer.length) {
// Add the new_byte to the end of the command_buffer
this.visca_command_buffer[this.visca_command_index] = new_byte;
this.visca_command_index++;
}
// Check for valid command
if ((this.visca_command_index >= 3) // at least 3 bytes
&& (this.visca_command_buffer[0] & 0x80) // first byte has MSB set to 1
&& (this.visca_command_buffer[this.visca_command_index - 1] == 0xFF) // last byte is 0xFF
) {
// Looks like a VISCA command. Process it
this.decode_visca(this.visca_command_buffer, this.visca_command_index);
this.visca_command_index = 0;
}
// Collect JVC data
// Starts with 0xB1
// Either 6 or 7 bytes. Length is in 4th byte, lower nibble
if (new_byte == 0xB1) {
// This marks the start of a the command so reset buffer counter
this.jvc_command_buffer[0] = new_byte;
this.jvc_command_index = 1;
} else if (this.jvc_command_index < this.jvc_command_buffer.length) {
// Add the new_byte to the end of the command_buffer
this.jvc_command_buffer[this.jvc_command_index] = new_byte;
this.jvc_command_index++;
}
// Check for valid command
if (((this.jvc_command_index == 6) // 6 bytes commands have 0x82 in 4th byte
&& ((this.jvc_command_buffer[3] & 0x0F) == 2)) // 4th byte, 0x82
|| ((this.jvc_command_index == 7) // 7 byte commands have 0x83 in 4th byte
&& ((this.jvc_command_buffer[3] & 0x0F) == 3)) // 4th byte, 0x83
) {
// Looks like a JVC command. Process it
this.decode_jvc(this.jvc_command_buffer, this.jvc_command_index);
this.jvc_command_index = 0;
}
// Collect HikVision data
// Starts with 0xB5
// 0xB5 could be part of the payload (eg a high numbered preset)
// So we keep a cache of the last 8 bytes
if (this.hik_command_index < this.hik_command_buffer.length) {
// Add the new_byte to the end of the buffer
this.hik_command_buffer[this.hik_command_index] = new_byte;
this.hik_command_index++;
} else {
// Shift the bytes to make room for the new_byte at the end
for (var x = 0; x < (this.hik_command_buffer.length - 1); x++) {
this.hik_command_buffer[x] = this.hik_command_buffer[x + 1];
}
// Then add the new_byte to the end
this.hik_command_buffer[this.hik_command_buffer.length - 1] = new_byte;
}
// Check for valid command
// We require 8 bytes with byte 0 = 0xB5 and with a valid checksum
if (this.hik_command_index === 8 && this.hik_command_buffer[0] === 0xB5
&& this.checksum_hik_valid(this.hik_command_buffer, 8)) {
// Looks like we have a HikVision command. Try and process it
this.decode_hik(this.hik_command_buffer, 8);
this.hik_command_index = 0; // empty the buffer
}
}
}
checksum_valid(buffer) {
var total = 0;
// The 0xFF start byte is not included in the checksum
for (var x = 1; x < (buffer.length - 1); x++) {
total += buffer[x];
}
var computed_checksum = total % 256;
// Check if computed_checksum matches the last byte in the buffer
if (computed_checksum === buffer[buffer.length - 1]) {
return true;
} else {
return false;
}
}
checksum_p_valid(buffer) {
var computed_checksum = 0x00;
for (var x = 0; x < (buffer.length - 1); x++) {
computed_checksum = computed_checksum ^ buffer[x]; // xor
}
// Check if computed_checksum matches the last byte in the buffer
if (computed_checksum === buffer[buffer.length - 1]) {
return true;
} else {
return false;
}
}
checksum_bosch_valid(buffer, message_length) {
var total = 0;
for (var x = 0; x < (message_length - 1); x++) {
total += buffer[x];
}
var computed_checksum = total & 0x7F; // Checksum has MSB of zero. MSB of 1 is reserved for first byte of message
// Check if computed_checksum matches the last byte in the buffer
if (computed_checksum === buffer[message_length - 1]) {
return true;
} else {
return false;
}
}
checksum_fv_valid(buffer, message_length) {
var computed_checksum = 0x00;
for (var x = 0; x < (message_length - 1); x++) {
computed_checksum = computed_checksum ^ buffer[x]; // xor
}
computed_checksum = computed_checksum | 0x80; // set MSB to 1
// Check if computed_checksum matches the last byte in the buffer
if (computed_checksum === buffer[message_length - 1]) {
return true;
} else {
return false;
}
}
checksum_hik_valid(buffer, message_length) {
// Sum mod 256 including header byte
var total = 0;
for (var x = 0; x < (message_length - 1); x++) {
total += buffer[x];
}
var computed_checksum = total % 256;
// Check if computed_checksum matches the last byte in the buffer
if (computed_checksum === buffer[message_length - 1]) {
return true;
} else {
return false;
}
}
decode_pelco(pelco_command_buffer) {
var pelco_d = false;
var pelco_p = false;
var msg_string = '';
if (pelco_command_buffer.length == 7) pelco_d = true;
if (pelco_command_buffer.length == 8) pelco_p = true;
if (pelco_d) {
//var sync = pelco_command_buffer[0];
var camera_id = pelco_command_buffer[1];
var command_1 = pelco_command_buffer[2];
var command_2 = pelco_command_buffer[3];
var data_1 = pelco_command_buffer[4];
var data_2 = pelco_command_buffer[5];
//var checksum = pelco_command_buffer[6];
var extended_command = ((command_2 & 0x01) == 1);
msg_string += 'D ';
}
if (pelco_p) {
var stx = pelco_command_buffer[0];
var camera_id = pelco_command_buffer[1] + 1; // Pelco P sends Cam1 as 0x00
var command_1 = pelco_command_buffer[2];
var command_2 = pelco_command_buffer[3];
var data_1 = pelco_command_buffer[4];
var data_2 = pelco_command_buffer[5];
//var etx = pelco_command_buffer[6];
//var checksum = pelco_command_buffer[7];
var extended_command = ((command_2 & 0x01) == 1);
if (stx === 0xA0) msg_string += 'P ';
if (stx === 0xB0) msg_string += 'BBV ';
}
msg_string += 'Camera ' + camera_id + ' ';
if (extended_command) {
// Process extended commands
// Command 1 (byte3) and Command 2 (byte 4) identifies the Extended Command
// byte 5 and 6 contain additional data used by the extended commands
if (command_2 === 0x03 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[SET PRESET ' + data_2 + ']';
} else if (command_2 === 0x05 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[CLEAR PRESET ' + data_2 + ']';
} else if (command_2 === 0x07 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[GOTO PRESET ' + data_2 + ']';
} else if (command_2 === 0x09 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[SET AUX ' + data_2 + ']';
} else if (command_2 === 0x0B && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[CLEAR AUX ' + data_2 + ']';
} else if (command_2 === 0x1F && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[START RECORDING TOUR ' + data_2 + ']';
} else if (command_2 === 0x21 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[STOP RECORDING TOUR]';
} else if (command_2 === 0x23 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[START TOUR ' + data_2 + ']';
} else if (command_2 === 0x25 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[SET ZOOM SPEED ' + data_2 + ']';
} else if (command_2 === 0x27 && command_1 === 0x00 && data_1 === 0x00) {
msg_string += '[SET FOCUS SPEED ' + data_2 + ']';
} else if (command_2 === 0x2B && command_1 === 0x00 && data_1 === 0x00 && data_2 == 0x00) {
msg_string += '[AUTO FOCUS SET TO AUTOMATIC]';
} else if (command_2 === 0x2B && command_1 === 0x00 && data_1 === 0x00 && data_2 != 0x00) {
// 2012 spec says 0 = Automatic Operation. 1 = Auto Focus Off
// 1999 spec says range 0..2 Automatic,On,Off
msg_string += '[AUTO FOCUS SETTING ' + data_2 + ']';
} else if (command_2 === 0x2D && command_1 === 0x00 && data_1 === 0x00 && data_2 == 0x00) {
msg_string += '[AUTO IRIS SET TO AUTOMATIC]';
} else if (command_2 === 0x2D && command_1 === 0x00 && data_1 === 0x00 && data_2 != 0x00) {
// 2012 spec says 0 = Automatic Operation. 1 = Auto Iris Off
// 1999 spec says range 0..2 Automatic,On,Off
msg_string += '[AUTO IRIS SETTING ' + data_2 + ']';
} else if (command_2 === 0x31 && command_1 === 0x00 && data_1 === 0x00 && data_2 == 0x00) {
// 2012 spec says Prior to Spectra IV, data_2 is 1=OFF, 2=ON
// 2012 spec says but Spectra IV the data_2 is 0=OFF, NON-ZERO=ON
msg_string += '[BACKLIGHT COMPENSATION ' + data_2 + '] SPECTRA IV MODE = OFF';
} else if (command_2 === 0x31 && command_1 === 0x00 && data_1 === 0x00 && data_2 == 0x01) {
msg_string += '[BACKLIGHT COMPENSATION ' + data_2 + '] GENERIC MODE = ON / SPECTRA IV MODE = ON';
} else if (command_2 === 0x31 && command_1 === 0x00 && data_1 === 0x00 && data_2 == 0x02) {
msg_string += '[BACKLIGHT COMPENSATION ' + data_2 + '] GENERIC MODE = OFF / SPECTRA IV MODE = ON';
} else if (command_2 === 0x31 && command_1 === 0x00 && data_1 === 0x00 && data_2 >= 0x02) {
msg_string += '[BACKLIGHT COMPENSATION ' + data_2 + '] SPECTRA IV MODE = ON';
} else {
msg_string += 'Unknown extended command 0x' + this.DecToHexPad(command_2, 2);
}
} else {
// Process a normal Pan, Tilt, Zoom, Focus and Iris command
if (pelco_d) {
var iris_close = (command_1 >> 2) & 0x01;
var iris_open = (command_1 >> 1) & 0x01;
var focus_near = (command_1 >> 0) & 0x01;
var focus_far = (command_2 >> 7) & 0x01;
var zoom_out = (command_2 >> 6) & 0x01;
var zoom_in = (command_2 >> 5) & 0x01;
var down = (command_2 >> 4) & 0x01;
var up = (command_2 >> 3) & 0x01;
var left = (command_2 >> 2) & 0x01;
var right = (command_2 >> 1) & 0x01;
}
if (pelco_p) {
var iris_close = (command_1 >> 3) & 0x01;
var iris_open = (command_1 >> 2) & 0x01;
var focus_near = (command_1 >> 1) & 0x01;
var focus_far = (command_1 >> 0) & 0x01;
var zoom_out = (command_2 >> 6) & 0x01;
var zoom_in = (command_2 >> 5) & 0x01;
var down = (command_2 >> 4) & 0x01;
var up = (command_2 >> 3) & 0x01;
var left = (command_2 >> 2) & 0x01;
var right = (command_2 >> 1) & 0x01;
}
if (left === 0 && right === 0) {
msg_string += '[pan stop ]';
} else if (left === 1 && right === 0) {
msg_string += '[PAN LEFT (' + data_1 + ')]';
} else if (left === 0 && right === 1) {
msg_string += '[PAN RIGHT(' + data_1 + ')]';
} else { // left === 1 && right === 1)
msg_string += '[PAN ???? (' + data_1 + ')]';
}
if (up === 0 && down === 0) {
msg_string += '[tilt stop ]';
} else if (up === 1 && down === 0) {
msg_string += '[TILT UP (' + data_2 + ')]';
} else if (up === 0 && down === 1) {
msg_string += '[TILT DOWN(' + data_2 + ')]';
} else { // (up === 1 && down === 1)
msg_string += '[TILT ????(' + data_2 + ')]';
}
if (zoom_in === 0 && zoom_out === 0) {
msg_string += '[zoom stop]';
} else if (zoom_in === 1 && zoom_out === 0) {
msg_string += '[ZOOM IN ]';
} else if (zoom_in === 0 && zoom_out === 1) {
msg_string += '[ZOOM OUT ]';
} else { // (zoom_in === 1 && zoom_out === 1)
msg_string += '[ZOOM ????]';
}
if (iris_open === 0 && iris_close === 0) {
msg_string += '[iris stop ]';
} else if (iris_open === 1 && iris_close === 0) {
msg_string += '[IRIS OPEN ]';
} else if (iris_open === 0 && iris_close === 1) {
msg_string += '[IRIS CLOSE]';
} else { // (iris_open === 1 && iris_close === 1)
msg_string += '[IRIS ???? ]';
}
if (focus_near === 0 && focus_far === 0) {
msg_string += '[focus stop]';
} else if (focus_near === 1 && focus_far === 0) {
msg_string += '[FOCUS NEAR]';
} else if (focus_near === 0 && focus_far === 1) {
msg_string += '[FOCUS FAR ]';
} else { // (focus_near === 1 && focus_far === 1)
msg_string += '[FOCUS ????]';
}
}
this.emit("log", this.bytes_to_string(pelco_command_buffer, pelco_command_buffer.length) + ' ' + msg_string);
}
decode_bosch(bosch_command_buffer) {
// Note Bosch is 9600 8-N-1
var msg_string = '';
msg_string += 'Bosch ';
var length = bosch_command_buffer[0] & 0x7F;
var high_order_address = bosch_command_buffer[1];
var low_order_address = bosch_command_buffer[2];
var op_code = bosch_command_buffer[3];
//var data_byte_1 = bosch_command_buffer[4];
//var data_byte_2 = bosch_command_buffer[5];
//var data_byte_3 = bosch_command_buffer[6];
//var data_byte_X = bosch_command_buffer[xxx];
//var checksum = bosch_command_buffer[the last byte];
var camera_id = (high_order_address << 7) + low_order_address + 1;
msg_string += 'Camera ' + camera_id + ' ';
//
// OSRD Commands
//
if (op_code == 0x02) {
msg_string += 'Start/Stop Fixed Speed PTZ, Focus and Iris';
}
else if (op_code == 0x03) {
msg_string += 'Fixed Speed PTZ for a specified period';
}
else if (op_code == 0x04) {
msg_string += 'Repetitive Fixed Speed PTZ';
}
else if (op_code == 0x05) {
msg_string += 'Start/Stop Variable Speed PTZ = ';
// 3 data bytes used with this Op Code
var data_1 = bosch_command_buffer[4];
var data_2 = bosch_command_buffer[5];
var data_3 = bosch_command_buffer[6];
var zoom_speed = (data_1 >> 4) & 0x07;
var tilt_speed = (data_1 >> 0) & 0x0F;
var pan_speed = (data_2 >> 3) & 0x0F;
var iris_open = (data_2 >> 2) & 0x01;
var iris_close = (data_2 >> 1) & 0x01;
var focus_far = (data_2 >> 0) & 0x01;
var focus_near = (data_3 >> 6) & 0x01;
var zoom_in = (data_3 >> 5) & 0x01;
var zoom_out = (data_3 >> 4) & 0x01;
var up = (data_3 >> 3) & 0x01;
var down = (data_3 >> 2) & 0x01;
var left = (data_3 >> 1) & 0x01;
var right = (data_3 >> 0) & 0x01;
if (left === 0 && right === 0) {
msg_string += '[pan stop (' + pan_speed + ')]';
} else if (left === 1 && right === 0) {
msg_string += '[PAN LEFT (' + pan_speed + ')]';
} else if (left === 0 && right === 1) {
msg_string += '[PAN RIGHT(' + pan_speed + ')]';
} else { // left === 1 && right === 1)
msg_string += '[PAN ???? (' + pan_speed + ')]';
}
if (up === 0 && down === 0) {
msg_string += '[tilt stop(' + tilt_speed + ')]';
} else if (up === 1 && down === 0) {
msg_string += '[TILT UP (' + tilt_speed + ')]';
} else if (up === 0 && down === 1) {
msg_string += '[TILT DOWN(' + tilt_speed + ')]';
} else { // (up === 1 && down === 1)
msg_string += '[TILT ????(' + tilt_speed + ')]';
}
if (zoom_in === 0 && zoom_out === 0) {
msg_string += '[zoom stop(' + zoom_speed + ')]';
} else if (zoom_in === 1 && zoom_out === 0) {
msg_string += '[ZOOM IN(' + zoom_speed + ')]';
} else if (zoom_in === 0 && zoom_out === 1) {
msg_string += '[ZOOM OUT(' + zoom_speed + ')]';
} else { // (zoom_in === 1 && zoom_out === 1)
msg_string += '[ZOOM ????]';
}
if (iris_open === 0 && iris_close === 0) {
msg_string += '[iris stop ]';
} else if (iris_open === 1 && iris_close === 0) {
msg_string += '[IRIS OPEN ]';
} else if (iris_open === 0 && iris_close === 1) {
msg_string += '[IRIS CLOSE]';
} else { // (iris_open === 1 && iris_close === 1)
msg_string += '[IRIS ???? ]';
}
if (focus_near === 0 && focus_far === 0) {
msg_string += '[focus stop]';
} else if (focus_near === 1 && focus_far === 0) {
msg_string += '[FOCUS NEAR]';
} else if (focus_near === 0 && focus_far === 1) {
msg_string += '[FOCUS FAR ]';
} else { // (focus_near === 1 && focus_far === 1)
msg_string += '[FOCUS ????]';
}
}
else if (op_code == 0x06) {
msg_string += 'Repetitive Fixed speed Zoom, Focus and Iris';
}
else if (op_code == 0x07) {
msg_string += 'Auxiliary On/Off and Preposition Set/Shot = ';
// 2 data bytes used with this Op Code
var data_1 = bosch_command_buffer[4];
var data_2 = bosch_command_buffer[5];
var function_code = data_1 & 0x0F;
var data = ((data_1 & 0x70) << 3) + data_2;
if (function_code == 1) msg_string += 'Aux On ' + data;
else if (function_code == 2) msg_string += 'Aux Off ' + data;
else if (function_code == 4) msg_string += 'Pre-position SET ' + data;
else if (function_code == 5) msg_string += 'Pre-position SHOT ' + data;
else if (function_code == 8) msg_string += 'Cancel Latching Aux ' + data;
else if (function_code == 9) msg_string += 'Latching Aux On ' + data;
else if (function_code == 10) msg_string += 'Latching Aux Off ' + data;
else msg_string += 'unknown aux or pre-position command ' + function_code + ' with value ' + data;
}
else if (op_code == 0x08) {
msg_string += 'Repetitive Variable-speed PTZ, Focus and Iris';
}
//
// OSRD Extended Commands
//
else if (op_code == 0x09) {
msg_string += 'Fine Speed PTZ';
}
else if (op_code == 0x0A) {
msg_string += 'Position Report and Replay / Position Commands';
}
else if (op_code == 0x0C) {
msg_string += 'Ping Command';
}
else if (op_code == 0x0F) {
msg_string += 'Information Requested / Reply';
}
else if (op_code == 0x10) {
msg_string += 'Title set';
}
else if (op_code == 0x12) {
msg_string += 'Auxiliary Commands with Data';
}
else if (op_code == 0x13) {
msg_string += 'Set Position / Get Position';
}
else if (op_code == 0x14) {
msg_string += 'BiCom message';
}
//
// BiCom within OSRD
//
else {
msg_string += 'Unknown Op Code ' + op_code;
}
this.emit("log", this.bytes_to_string(bosch_command_buffer, length + 1) + ' ' + msg_string);
}
fv_hex_ascii(byte_1, byte_2) {
// Byte 1 could be 0x31 = ASCII "1"
// Byte 2 could be 0x46 = ASCII "F"
// Convert Byte 1 and Byte 2 from 'bytes' into Chars, eg to "1" and "F"
// Combine the chars to a Hex String eg "0x1F"
// Convert the Hex String into an integer value
var hex_string = '0x' + String.fromCharCode(byte_1, byte_2);
var value = parseInt(hex_string);
return value;
}
decode_forward_vision(fv_command_buffer, fv_command_length) {
// Note Forward Vision is 9600 8-O-1 *** ODD PARITY ***
var msg_string = '';
msg_string += 'FV ';
var camera_id = this.fv_hex_ascii(fv_command_buffer[1], fv_command_buffer[2]);
var length = this.fv_hex_ascii(fv_command_buffer[3], fv_command_buffer[4]);
var control_flag_char = String.fromCharCode(fv_command_buffer[5]);
var control_code_char = String.fromCharCode(fv_command_buffer[6]);
msg_string += 'Camera ' + camera_id + ' ';
if (control_code_char === 'G') {
var data1 = this.fv_hex_ascii(fv_command_buffer[7], fv_command_buffer[8]);
var data2 = this.fv_hex_ascii(fv_command_buffer[9], fv_command_buffer[10]);
var data3 = this.fv_hex_ascii(fv_command_buffer[11], fv_command_buffer[12]);
var pan_speed = this.fv_hex_ascii(fv_command_buffer[13], fv_command_buffer[14]);
var tilt_speed = this.fv_hex_ascii(fv_command_buffer[15], fv_command_buffer[16]);
var focus_off_on = (data1 >> 7) & 0x01;
var focus_near_far = (data1 >> 6) & 0x01;
var zoom_off_on = (data1 >> 5) & 0x01;
var zoom_tele_wide = (data1 >> 4) & 0x01;
var tilt_off_on = (data1 >> 3) & 0x01;
var tilt_up_down = (data1 >> 2) & 0x01;
var pan_off_on = (data1 >> 1) & 0x01;
var pan_left_right = (data1 >> 0) & 0x01;
var iris_sense_peak = (data2 >> 7) & 0x01;
var iris_control = (data2 >> 4) & 0x07;
var iris_slow_fast = (data2 >> 3) & 0x01;
var focus_slow_fast = (data2 >> 2) & 0x01;
var zoom_slow_fast = (data2 >> 1) & 0x01;
var autopan_off_on = (data2 >> 0) & 0x01;
var pan_tilt_scale_off_on = (data3 >> 7) & 0x01;
var wiper_off_on = (data3 >> 6) & 0x01;
var washer_off_on = (data3 >> 5) & 0x01;
var lamp_control = (data3 >> 3) & 0x03; // 2 bit value
var aux_3_off_on = (data3 >> 2) & 0x01;
var aux_2_off_on = (data3 >> 1) & 0x01;
var aux_1_off_on = (data3 >> 0) & 0x01;
if (pan_off_on === 0) {
msg_string += '[pan stop ]';
} else if (pan_off_on === 1 && pan_left_right === 0) {
msg_string += '[PAN LEFT (' + pan_speed + ')]';
} else if (pan_off_on === 1 && pan_left_right === 1) {
msg_string += '[PAN RIGHT(' + pan_speed + ')]';
} else {
msg_string += '[PAN ???? (' + pan_speed + ')]';
}
if (tilt_off_on === 0) {
msg_string += '[tilt stop ]';
} else if (tilt_off_on === 1 && tilt_up_down === 0) {
msg_string += '[TILT UP (' + tilt_speed + ')]';
} else if (tilt_off_on === 1 && tilt_up_down === 1) {
msg_string += '[TILT DOWN(' + tilt_speed + ')]';
} else {
msg_string += '[TILT ????(' + tilt_speed + ')]';
}
if (zoom_off_on === 0) {
msg_string += '[zoom stop]';
} else if (zoom_off_on === 1 && zoom_tele_wide === 0) {
msg_string += '[ZOOM IN (' + zoom_slow_fast + ')]';
} else if (zoom_off_on === 1 && zoom_tele_wide === 1) {
msg_string += '[ZOOM OUT(' + zoom_slow_fast + ')]';
} else {
msg_string += '[ZOOM ???]';
}
if (iris_control === 0) {
msg_string += '[iris stop]';
} else if (iris_control === 1) {
msg_string += '[IRIS CLOSE]';
} else if (iris_control === 2) {
msg_string += '[IRIS OPEN]';
} else if (iris_control === 3) {
msg_string += '[IRIS AUTO]';
} else {
msg_string += '[IRIS ????]';
}
if (focus_off_on === 0) {
msg_string += '[focus stop]';
} else if (focus_off_on === 1 && focus_near_far === 0) {
msg_string += '[FOCUS NEAR]';
} else if (focus_off_on === 1 && focus_near_far === 1) {
msg_string += '[FOCUS FAR ]';
} else {
msg_string += '[FOCUS ????]';
}
msg_string += ' Aux1=' + aux_1_off_on;
msg_string += ' Aux2=' + aux_2_off_on;
msg_string += ' Aux3=' + aux_3_off_on;
msg_string += ' Wipe=' + wiper_off_on;
msg_string += ' Wash=' + washer_off_on;
msg_string += ' Lamp=' + lamp_control;
msg_string += ' PropZoom=' + pan_tilt_scale_off_on;
}
else if (control_code_char === 'L') {
var preset = this.fv_hex_ascii(fv_command_buffer[7], fv_command_buffer[8]);
msg_string += 'Goto Preset ' + preset;
}
else if (control_code_char === 'M') {
var preset = this.fv_hex_ascii(fv_command_buffer[7], fv_command_buffer[8]);
msg_string += 'Store Preset ' + preset;
}
else if (control_code_char === 'O') {
msg_string += 'Get Current Position';
}
else if (control_code_char === 'W') {
var data1 = this.fv_hex_ascii(fv_command_buffer[7], fv_command_buffer[8]);
msg_string += 'Reset Value=' + data1;
}
else if (control_code_char === 'Y') {
msg_string += 'Get SW Version';
}
else {
msg_string += 'Unknown Command Code ' + control_code_char;
}
this.emit("log", this.bytes_to_string(fv_command_buffer, fv_command_length) + ' ' + msg_string);
}
decode_vicon(vicon_command_buffer, vicon_command_length) {
// Does not appear to be any checksum
// Byte 1. MSB set to 1.
var msg_string = '';
msg_string += 'Vicon ';
var camera_id = ((vicon_command_buffer[0] & 0x0F) * 16) + (vicon_command_buffer[1] & 0x0F);
var left = (vicon_command_buffer[2] >> 6) & 0x01; // 0x40
var right = (vicon_command_buffer[2] >> 5) & 0x01; // 0x20
var up = (vicon_command_buffer[2] >> 4) & 0x01; // 0x10
var down = (vicon_command_buffer[2] >> 3) & 0x01; // 0x08
var auto_pan = (vicon_command_buffer[2] >> 2) & 0x01; // 0x04
var zoom_out = (vicon_command_buffer[3] >> 6) & 0x01; // 0x40
var zoom_in = (vicon_command_buffer[3] >> 5) & 0x01; // 0x20
var focus_far = (vicon_command_buffer[3] >> 4) & 0x01; // 0x10
var focus_near = (vicon_command_buffer[3] >> 3) & 0x01; // 0x08
var iris_open = (vicon_command_buffer[3] >> 2) & 0x01; // 0x04
var iris_close = (vicon_command_buffer[3] >> 1) & 0x01; // 0x02
var goto_preset = (vicon_command_buffer[6] === 0x10 ? 1 : 0); // >> 4) & 0x01; // 0x10
var preset_value = (vicon_command_buffer[7]);
var pan_speed = (vicon_command_buffer[6] << 7) | (vicon_command_buffer[7]);
var tilt_speed = (vicon_command_buffer[8] << 7) | (vicon_command_buffer[9]);
msg_string += 'Camera ' + camera_id + ' ';
if (left === 0 && right === 0) {
msg_string += '[pan stop ]';
} else if (left === 1 && right === 0) {
msg_string += '[PAN LEFT (' + pan_speed + ')]';
} else if (left === 0 && right === 1) {
msg_string += '[PAN RIGHT(' + pan_speed + ')]';
} else { // left === 1 && right === 1)
msg_string += '[PAN ???? (' + pan_speed + ')]';
}
if (up === 0 && down === 0) {
msg_string += '[tilt stop ]';
} else if (up === 1 && down === 0) {
msg_string += '[TILT UP (' + tilt_speed + ')]';
} else if (up === 0 && down === 1) {
msg_string += '[TILT DOWN(' + tilt_speed + ')]';
} else { // (up === 1 && down === 1)
msg_string += '[TILT ????(' + tilt_speed + ')]';
}
if (zoom_in === 0 && zoom_out === 0) {
msg_string += '[zoom stop]';
} else if (zoom_in === 1 && zoom_out === 0) {
msg_string += '[ZOOM IN]';
} else if (zoom_in === 0 && zoom_out === 1) {
msg_string += '[ZOOM OUT]';
} else { // (zoom_in === 1 && zoom_out === 1)
msg_string += '[ZOOM ???]';
}
if (iris_open === 0 && iris_close === 0) {
msg_string += '[iris stop ]';
} else if (iris_open === 1 && iris_close === 0) {
msg_string += '[IRIS OPEN ]';
} else if (iris_open === 0 && iris_close === 1) {
msg_string += '[IRIS CLOSE]';
} else { // (iris_open === 1 && iris_close === 1)
msg_string += '[IRIS ???? ]';
}
if (focus_near === 0 && focus_far === 0) {
msg_string += '[focus stop]';
} else if (focus_near === 1 && focus_far === 0) {
msg_string += '[FOCUS NEAR]';
} else if (focus_near === 0 && focus_far === 1) {
msg_string += '[FOCUS FAR ]';
} else { // (focus_near === 1 && focus_far === 1)
msg_string += '[FOCUS ????]';
}
if (auto_pan === 1) {
msg_string += '[AutoPan]';
}
if (goto_preset === 1) {
msg_string += '[Goto Preset ' + preset_value + ']';
}
this.emit("log", this.bytes_to_string(vicon_command_buffer, vicon_command_length) + ' ' + msg_string);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// VLC
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////