<commit_before>/*
 *
 * Read and decode Pelco D, Pelco P, BBV422 and Bosch/Philips CCTV commands
 * Used to monitor the output from Pelco systems or the inputs into Pelco cameras
 * (c) Copyright 2016 <NAME>
 *
 *
 * Read the Buffer() objects from the a stream and process Pelco D, Pelco P, BBV422 and Bosch/Philips messages
 * Buffer() objects may have multiple PTZ messages or just part of a message
 * so bytes are cached if needed
 *
 */
/*
 *  SOURCE MATERIAL
 *  Pelco D data sheet (official Pelco document - 1999 Edition)
 *  Official Pelco KBD300A in Direct D and Direct P modes
 *
 *  NuOptic D Protocol http://www.nuoptic.com/wp-content/uploads/files/NuOptic_VIS-1000_D-Protocol_Reference.pdf
 *  CodeProject Pelco D and Pelco P pages http://www.codeproject.com/Articles/8034/Pelco-P-and-D-protocol-implementation-in-C
 *  ZoneMinder Source Code https://github.com/ZoneMinder/ZoneMinder/tree/master/scripts/ZoneMinder/lib/ZoneMinder/Control 
 *  iSpyConnect Source Code https://github.com/ispysoftware/iSpy/tree/master/Pelco
 *  Node-Pelco Source Code https://github.com/Scoup/node-pelcod
 *  CommFront 232 Analizer https://www.commfront.com/pages/pelco-d-protocol-tutorial
 *  CommFront 232 Analizer https://www.commfront.com/pages/pelco-p-protocol-tutorial
 *
 *  BBV Web Site for BBV422 Telemetry Format
 *
 *  Bosch Web Site for Bosch OSRD protocol format
 * 
 *  Pelco D Commands are 7 bytes long, start with 0xFF and have a 'sum' checksum
 *  Checksum is Sum of bytes 2 to 6 Modulo 256
 *  Camera 1 has Address 01
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+
 *  |   BYTE 1  | BYTE 2  |  BYTE 3   |  BYTE 4   | BYTE 5 | BYTE 6 |  BYTE 7   |
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+
 *  |           |         |           |           |        |        |           |
 *  | Sync(0xFF)| Address | Command 1 | Command 2 | Data 1 | Data 2 | Check Sum |
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+
 *
 *  Pelco P Commands (and BBV422) are 8 bytes long, include STX and ETX and have a 'XOR' checksum
 *  Checksum is XOR of bytes 1 to 7
 *  Camera 1 has Address 00
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+-----------+
 *  |   BYTE 1  | BYTE 2  |  BYTE 3   |  BYTE 4   | BYTE 5 | BYTE 6 |   BYTE 7  |  BYTE 8   |
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+-----------+
 *  |           |         |           |           |        |        |           |           |
 *  | STX(0xA0) | Address | Command 1 | Command 2 | Data 1 | Data 2 | ETX(0xAF) | Check Sum | Pelco P
 *  | STX(0xB0) | Address | Command 1 | Command 2 | Data 1 | Data 2 | ETX(0xBF) | Check Sum | BBV422
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+-----------+
 *
 *  There are two types of command - Standard and Extended
 *
 *
 * STANDARD COMMANDS
 * Pelco D Format
 * Used to control Pan,Tilt,Zoom,Focus and Iris. Bytes 5 and 6 contain Pan and Tilt speeds
 *  +---------+---------+--------+---------+------------------+---------------+----------+---------+----------+
 *  |         |  BIT 7  | BIT 6  |  BIT 5  |      BIT 4       |     BIT 3     |  BIT 2   |  BIT 1  |  BIT 0   |
 *  +---------+---------+--------+---------+------------------+---------------+----------+---------+----------+
 *  |         |         |        |         |                  |               |          |         |          |
 *  |Command 1|Sense    |Reserved|Reserved |Auto / Manual Scan|Camera On / Off|Iris Close|Iris Open|Focus Near|
 *  |         |         |        |         |                  |               |          |         |          |
 *  |Command 2|Focus Far|Zoom    |Zoom Tele|Down              |Up             |Left      |Right    |Always 0  |
 *  +---------+---------+--------+---------+------------------+---------------+----------+---------+----------+
 *
 * Pelco P and BBV422 Format
 * Used to control Pan,Tilt,Zoom,Focus and Iris. Bytes 5 and 6 contain Pan and Tilt speeds
 *  +---------+--------+-------------+---------------+-------------+----------+---------+---------+----------+
 *  |         |  BIT 7 |    BIT 6    |     BIT 5     |   BIT 4     |  BIT 3   |  BIT 2  |  BIT 1  |  BIT 0   |
 *  +---------+--------+-------------+---------------+-------------+----------+---------+---------+----------+
 *  |         |        |             |               |             |          |         |         |          |
 *  |Command 1|Unknown |Unknown      |Unknown        |Unknown      |Iris Close|Iris Open|Focus Near|Focus Far|
 *  |         |        |             |               |             |          |         |         |          |
 *  |Command 2|Unknown |Zoom Wide    |Zoom Tele      |Tilt Down    |Tilt Up   |Pan Left |Pan Right|Always 0  |
 *  +---------+--------+-------------+---------------+-------------+----------+---------+---------+----------+
 * Some web sites state that for Pelco P, Command 1 bits 7,6,5 and 4 and Command 2 bit 7 have Camera On/Off and
 * Auto Scan functions but this has not been verified.
 *
 *
 * EXTENDED COMMANDS
 * Bit 0 of Command 2 is set to '1' for extended commands.
 * Bytes 3,4,5 and 6 contain the extended command and any values
 * There are a large number of extended commands. This code processes the common commands.
 *  +--------------------------------+--------+--------+--------+-------------+----+
 *  |                                | BYTE 3 | BYTE 4 | BYTE 5 | BYTE 6      |D/P |
 *  |                                | Cmd 1  | Cmd 2  | Data 1 | Data 2      |    |
 *  +--------------------------------+--------+--------+--------+-------------+----+
 *  |                                |        |        |        |             |    |
 *  | Set Preset                     | 00     | 03     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Clear Preset                   | 00     | 05     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Go To Preset                   | 00     | 07     | 00     | value       |Both|
 *  |   Flip (180deg about)          | 00     | 07     | 00     | 21          |    |
 *  |   Go To Zero Pan               | 00     | 07     | 00     | 22          |    |
 *  |                                |        |        |        |             |    |
 *  | Set Auxiliary                  | 00     | 09     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Clear Auxiliary                | 00     | 0B     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Set Pattern Start              | 00     | 1F     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Set Pattern Stop               | 00     | 21     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Run Pattern                    | 00     | 23     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Set Zoom Speed                 | 00     | 25     | 00     | value (0-3) |Both|
 *  |                                |        |        |        |             |    |
 *  +--------------------------------+--------+--------+--------+-------------+----+
 *
 */

function PelcoD_Decoder() {

    // A Buffer used to cache partial commands
    this.pelco_command_buffer = new Buffer(7);

    // Number of bytes in the current Buffer
    this.pelco_command_index = 0;

    // A Buffer used to cache partial commands for Pelco P
    this.pelco_p_command_buffer = new Buffer(8);

    // Number of bytes in the current Buffer
    this.pelco_p_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);

    // Number of bytes in the current Buffer
    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);

    // Number of bytes in the current Buffer
    this.fv_command_index = 0;

}


PelcoD_Decoder.prototype.processBuffer = function(new_data_buffer) {

    console.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];

        // Add to Pelco D buffer
        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 Pelco P buffer
        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<commit_msg>Remove console log message<commit_after>/*
 *
 * Read and decode Pelco D, Pelco P, BBV422 and Bosch/Philips CCTV commands
 * Used to monitor the output from Pelco systems or the inputs into Pelco cameras
 * (c) Copyright 2016 <NAME>
 *
 *
 * Read the Buffer() objects from the a stream and process Pelco D, Pelco P, BBV422 and Bosch/Philips messages
 * Buffer() objects may have multiple PTZ messages or just part of a message
 * so bytes are cached if needed
 *
 */
/*
 *  SOURCE MATERIAL
 *  Pelco D data sheet (official Pelco document - 1999 Edition)
 *  Official Pelco KBD300A in Direct D and Direct P modes
 *
 *  NuOptic D Protocol http://www.nuoptic.com/wp-content/uploads/files/NuOptic_VIS-1000_D-Protocol_Reference.pdf
 *  CodeProject Pelco D and Pelco P pages http://www.codeproject.com/Articles/8034/Pelco-P-and-D-protocol-implementation-in-C
 *  ZoneMinder Source Code https://github.com/ZoneMinder/ZoneMinder/tree/master/scripts/ZoneMinder/lib/ZoneMinder/Control 
 *  iSpyConnect Source Code https://github.com/ispysoftware/iSpy/tree/master/Pelco
 *  Node-Pelco Source Code https://github.com/Scoup/node-pelcod
 *  CommFront 232 Analizer https://www.commfront.com/pages/pelco-d-protocol-tutorial
 *  CommFront 232 Analizer https://www.commfront.com/pages/pelco-p-protocol-tutorial
 *
 *  BBV Web Site for BBV422 Telemetry Format
 *
 *  Bosch Web Site for Bosch OSRD protocol format
 * 
 *  Pelco D Commands are 7 bytes long, start with 0xFF and have a 'sum' checksum
 *  Checksum is Sum of bytes 2 to 6 Modulo 256
 *  Camera 1 has Address 01
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+
 *  |   BYTE 1  | BYTE 2  |  BYTE 3   |  BYTE 4   | BYTE 5 | BYTE 6 |  BYTE 7   |
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+
 *  |           |         |           |           |        |        |           |
 *  | Sync(0xFF)| Address | Command 1 | Command 2 | Data 1 | Data 2 | Check Sum |
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+
 *
 *  Pelco P Commands (and BBV422) are 8 bytes long, include STX and ETX and have a 'XOR' checksum
 *  Checksum is XOR of bytes 1 to 7
 *  Camera 1 has Address 00
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+-----------+
 *  |   BYTE 1  | BYTE 2  |  BYTE 3   |  BYTE 4   | BYTE 5 | BYTE 6 |   BYTE 7  |  BYTE 8   |
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+-----------+
 *  |           |         |           |           |        |        |           |           |
 *  | STX(0xA0) | Address | Command 1 | Command 2 | Data 1 | Data 2 | ETX(0xAF) | Check Sum | Pelco P
 *  | STX(0xB0) | Address | Command 1 | Command 2 | Data 1 | Data 2 | ETX(0xBF) | Check Sum | BBV422
 *  +-----------+---------+-----------+-----------+--------+--------+-----------+-----------+
 *
 *  There are two types of command - Standard and Extended
 *
 *
 * STANDARD COMMANDS
 * Pelco D Format
 * Used to control Pan,Tilt,Zoom,Focus and Iris. Bytes 5 and 6 contain Pan and Tilt speeds
 *  +---------+---------+--------+---------+------------------+---------------+----------+---------+----------+
 *  |         |  BIT 7  | BIT 6  |  BIT 5  |      BIT 4       |     BIT 3     |  BIT 2   |  BIT 1  |  BIT 0   |
 *  +---------+---------+--------+---------+------------------+---------------+----------+---------+----------+
 *  |         |         |        |         |                  |               |          |         |          |
 *  |Command 1|Sense    |Reserved|Reserved |Auto / Manual Scan|Camera On / Off|Iris Close|Iris Open|Focus Near|
 *  |         |         |        |         |                  |               |          |         |          |
 *  |Command 2|Focus Far|Zoom    |Zoom Tele|Down              |Up             |Left      |Right    |Always 0  |
 *  +---------+---------+--------+---------+------------------+---------------+----------+---------+----------+
 *
 * Pelco P and BBV422 Format
 * Used to control Pan,Tilt,Zoom,Focus and Iris. Bytes 5 and 6 contain Pan and Tilt speeds
 *  +---------+--------+-------------+---------------+-------------+----------+---------+---------+----------+
 *  |         |  BIT 7 |    BIT 6    |     BIT 5     |   BIT 4     |  BIT 3   |  BIT 2  |  BIT 1  |  BIT 0   |
 *  +---------+--------+-------------+---------------+-------------+----------+---------+---------+----------+
 *  |         |        |             |               |             |          |         |         |          |
 *  |Command 1|Unknown |Unknown      |Unknown        |Unknown      |Iris Close|Iris Open|Focus Near|Focus Far|
 *  |         |        |             |               |             |          |         |         |          |
 *  |Command 2|Unknown |Zoom Wide    |Zoom Tele      |Tilt Down    |Tilt Up   |Pan Left |Pan Right|Always 0  |
 *  +---------+--------+-------------+---------------+-------------+----------+---------+---------+----------+
 * Some web sites state that for Pelco P, Command 1 bits 7,6,5 and 4 and Command 2 bit 7 have Camera On/Off and
 * Auto Scan functions but this has not been verified.
 *
 *
 * EXTENDED COMMANDS
 * Bit 0 of Command 2 is set to '1' for extended commands.
 * Bytes 3,4,5 and 6 contain the extended command and any values
 * There are a large number of extended commands. This code processes the common commands.
 *  +--------------------------------+--------+--------+--------+-------------+----+
 *  |                                | BYTE 3 | BYTE 4 | BYTE 5 | BYTE 6      |D/P |
 *  |                                | Cmd 1  | Cmd 2  | Data 1 | Data 2      |    |
 *  +--------------------------------+--------+--------+--------+-------------+----+
 *  |                                |        |        |        |             |    |
 *  | Set Preset                     | 00     | 03     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Clear Preset                   | 00     | 05     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Go To Preset                   | 00     | 07     | 00     | value       |Both|
 *  |   Flip (180deg about)          | 00     | 07     | 00     | 21          |    |
 *  |   Go To Zero Pan               | 00     | 07     | 00     | 22          |    |
 *  |                                |        |        |        |             |    |
 *  | Set Auxiliary                  | 00     | 09     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Clear Auxiliary                | 00     | 0B     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Set Pattern Start              | 00     | 1F     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Set Pattern Stop               | 00     | 21     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Run Pattern                    | 00     | 23     | 00     | value       |Both|
 *  |                                |        |        |        |             |    |
 *  | Set Zoom Speed                 | 00     | 25     | 00     | value (0-3) |Both|
 *  |                                |        |        |        |             |    |
 *  +--------------------------------+--------+--------+--------+-------------+----+
 *
 */

function PelcoD_Decoder() {

    // A Buffer used to cache partial commands
    this.pelco_command_buffer = new Buffer(7);

    // Number of bytes in the current Buffer
    this.pelco_command_index = 0;

    // A Buffer used to cache partial commands for Pelco P
    this.pelco_p_command_buffer = new Buffer(8);

    // Number of bytes in the current Buffer
    this.pelco_p_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);

    // Number of bytes in the current Buffer
    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);

    // Number of bytes in the current Buffer
    this.fv_command_index = 0;

}


PelcoD_Decoder.prototype.processBuffer = function(new_data_buffer) {

    // console.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];

        // Add to Pelco D buffer
        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 Pelco P buffer
        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
{'fraction_non_alphanumeric': 0.2185668757067955, 'fraction_numerical': 0.024673588979130256, 'mean_word_length': 0.9910961007061713, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 8, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 112, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '719436', 'n_tokens_mistral': 5949, 'n_tokens_neox': 5316, 'n_words': 1536}