<commit_before>'use strict';

const MBFunctions = {
  ReadCoils: 0x01,
  ReadDiscreteInputs: 0x02,
  ReadHoldingRegisters: 0x03,
  ReadInputRegisters: 0x04,
  WriteSingleCoil: 0x05,
  WriteSingleRegister: 0x06,
  ReadExceptionStatus: 0x07,
  WriteMultipleCoils: 0x0F,
  WriteMultipleRegisters: 0x10,
  ReportSlaveID: 0x11,
  MaskWriteRegister: 0x16,
  WriteAndReadRegisters: 0x17
};

const MBExceptions = {
  IllegalFunction: 0x01,
  IllegalDataAddress: 0x02,
  IllegalDataValue: 0x03,
  SlaveOrServerFailure: 0x04,
  Acknowledge: 0x05,
  SlaveOrServerBusy: 0x06,
  NegativeAcknowledge: 0x07,
  MemoryParity: 0x08,
  NotDefined: 0x09,
  GatewayPath: 0x0A,
  GatewayTarget: 0x0B,
  Max: 0x0C
};

const MBErrorDescriptions = {
  0x01: 'Illegal function',
  0x02: 'Illegal data address',
  0x03: 'Illegal data value',
  0x04: 'Slave or server failure',
  0x05: 'Acknowledge',
  0x06: 'Slave or server busy',
  0x07: 'Negative acknowledge',
  0x08: 'Memory parity',
  0x09: 'Not defined',
  0x0A: 'Gateway path',
  0x0B: 'Gateway target',
  0x0C: 'Max'
};

const NullBuffer = Buffer.alloc(0);


class MBPacket {
  constructor() {
    this.transactionID = 0;
    this.protocolID = 0;
    this.unitID = 0xFF;
    this.data = NullBuffer;
  }

  toBuffer() {
    let buffer = Buffer.alloc(7 + this.data.length);
    buffer.writeUInt16BE(this.transactionID, 0);
    buffer.writeUInt16BE(0, 2);
    buffer.writeUInt16BE(this.data.length + 1, 4);
    buffer.writeUInt8(this.unitID, 6);
    this.data.copy(buffer, 7);
    return buffer;
  }

  static FromBuffer(buffer) {
    let packet = new MBPacket();
    packet.buffer = Buffer.from(buffer);
    packet.transactionID = buffer.readUInt16BE(0);
    packet.protocolID = buffer.readUInt16BE(2);
    let dataLength = MBPacket.DataLength(buffer);
    packet.unitID = buffer.readUInt8(6);
    packet.data = buffer.slice(7, 6 + dataLength);

    packet.reply = {};

    if (packet.data.length > 0) {
      let functionCode = packet.data.readUInt8(0);

      if (functionCode > 0x80) {
        console.log('error');
        packet.reply.error = packet.data.readUInt8(1);
        if (MBErrorDescriptions[packet.reply.error]) {
          packet.reply.errorDescription = MBErrorDescriptions[packet.reply.error];
        }
        console.log(packet);
      } else {
        packet.reply.functionCode = functionCode;
        if (ReplyFunctions[functionCode]) {
          packet.reply.data = ReplyFunctions[functionCode](packet.data, 0, packet.data.length);
        } else {
          packet.reply.data = packet.data.slice(1);
        }
      }
    }<commit_msg>Removed error logging<commit_after>'use strict';

const MBFunctions = {
  ReadCoils: 0x01,
  ReadDiscreteInputs: 0x02,
  ReadHoldingRegisters: 0x03,
  ReadInputRegisters: 0x04,
  WriteSingleCoil: 0x05,
  WriteSingleRegister: 0x06,
  ReadExceptionStatus: 0x07,
  WriteMultipleCoils: 0x0F,
  WriteMultipleRegisters: 0x10,
  ReportSlaveID: 0x11,
  MaskWriteRegister: 0x16,
  WriteAndReadRegisters: 0x17
};

const MBExceptions = {
  IllegalFunction: 0x01,
  IllegalDataAddress: 0x02,
  IllegalDataValue: 0x03,
  SlaveOrServerFailure: 0x04,
  Acknowledge: 0x05,
  SlaveOrServerBusy: 0x06,
  NegativeAcknowledge: 0x07,
  MemoryParity: 0x08,
  NotDefined: 0x09,
  GatewayPath: 0x0A,
  GatewayTarget: 0x0B,
  Max: 0x0C
};

const MBErrorDescriptions = {
  0x01: 'Illegal function',
  0x02: 'Illegal data address',
  0x03: 'Illegal data value',
  0x04: 'Slave or server failure',
  0x05: 'Acknowledge',
  0x06: 'Slave or server busy',
  0x07: 'Negative acknowledge',
  0x08: 'Memory parity',
  0x09: 'Not defined',
  0x0A: 'Gateway path',
  0x0B: 'Gateway target',
  0x0C: 'Max'
};

const NullBuffer = Buffer.alloc(0);


class MBPacket {
  constructor() {
    this.transactionID = 0;
    this.protocolID = 0;
    this.unitID = 0xFF;
    this.data = NullBuffer;
  }

  toBuffer() {
    let buffer = Buffer.alloc(7 + this.data.length);
    buffer.writeUInt16BE(this.transactionID, 0);
    buffer.writeUInt16BE(0, 2);
    buffer.writeUInt16BE(this.data.length + 1, 4);
    buffer.writeUInt8(this.unitID, 6);
    this.data.copy(buffer, 7);
    return buffer;
  }

  static FromBuffer(buffer) {
    let packet = new MBPacket();
    packet.buffer = Buffer.from(buffer);
    packet.transactionID = buffer.readUInt16BE(0);
    packet.protocolID = buffer.readUInt16BE(2);
    let dataLength = MBPacket.DataLength(buffer);
    packet.unitID = buffer.readUInt8(6);
    packet.data = buffer.slice(7, 6 + dataLength);

    packet.reply = {};

    if (packet.data.length > 0) {
      let functionCode = packet.data.readUInt8(0);

      if (functionCode > 0x80) {
        packet.reply.error = packet.data.readUInt8(1);
        if (MBErrorDescriptions[packet.reply.error]) {
          packet.reply.errorDescription = MBErrorDescriptions[packet.reply.error];
        }
      } else {
        packet.reply.functionCode = functionCode;
        if (ReplyFunctions[functionCode]) {
          packet.reply.data = ReplyFunctions[functionCode](packet.data, 0, packet.data.length);
        } else {
          packet.reply.data = packet.data.slice(1);
        }
      }
    }
{'fraction_non_alphanumeric': 0.12010138428543576, 'fraction_numerical': 0.05459153831156171, 'mean_word_length': 3.40343347639485, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '791456', 'n_tokens_mistral': 1976, 'n_tokens_neox': 1828, 'n_words': 358}