<commit_before>'use strict';

const Layer = require('./../Stack/Layers/Layer');
const MessageRouter = require('./Objects/MessageRouter');

class ControlLogix extends Layer {
  constructor(connectionLayer, options) {
    super(connectionLayer);
  }

  ReadTag(address, callback) {
    if (!address || !callback) return;

    let path = MessageRouter.ANSIExtSymbolSegment(address);
    const data = Buffer.from([0x01, 0x00]); // number of elements to read (1)

    let request = MessageRouter.Request(Services.ReadTag, path, data);

    this.send(request, null, false, this.contextCallback(function(message) {
      let reply = MessageRouter.Reply(message);
      let dataType = reply.data.readUInt16LE(0);
      let dataConverter = DataConverters[dataType];
      if (dataConverter) {
        callback(null, dataConverter(reply.data, 2));
      } else {
        callback('ControlLogix Error: No converter for data type: ' + dataType);
      }
    }))
  }

  WriteTag(address, value, callback) {
    if (!address || !callback) return;

    let path = MessageRouter.ANSIExtSymbolSegment(address);
    let data = Buffer.alloc(8);
    data.writeUInt16LE(DataTypes.Float, 0);
    data.writeUInt16LE(1, 2);
    data.writeFloatLE(value, 4);

    let request = MessageRouter.Request(Services.WriteTag, path, data);

    this.send(request, function(message) {
      let reply = MessageRouter.Reply(message);
      callback(null, reply);
    });
  }

  handleData(data, info, context) {
    if (context == null) {
      throw new Error('ControlLogix Error: Unhandled message, context should not be null');<commit_msg>Handle read tag errors<commit_after>'use strict';

const Layer = require('./../Stack/Layers/Layer');
const MessageRouter = require('./Objects/MessageRouter');

class ControlLogix extends Layer {
  constructor(connectionLayer, options) {
    super(connectionLayer);
  }

  ReadTag(address, callback) {
    if (!address || !callback) return;

    let path = MessageRouter.ANSIExtSymbolSegment(address);
    const data = Buffer.from([0x01, 0x00]); // number of elements to read (1)

    let request = MessageRouter.Request(Services.ReadTag, path, data);

    this.send(request, null, false, this.contextCallback(function(message) {
      let reply = MessageRouter.Reply(message);

      if (reply.statusCode !== 0) {
        if (READ_TAG_ERRORS[reply.statusCode] != null) {
          reply.statusDescription = READ_TAG_ERRORS[reply.statusCode];
        }
        callback('ControlLogix Error: Error reading tag: ' + reply.statusDescription);
        return;
      }

      let dataType = reply.data.readUInt16LE(0);
      let dataConverter = DataConverters[dataType];
      if (dataConverter) {
        callback(null, dataConverter(reply.data, 2));
      } else {
        callback('ControlLogix Error: No converter for data type: ' + dataType);
      }
    }));
  }

  WriteTag(address, value, callback) {
    if (!address || !callback) return;

    let path = MessageRouter.ANSIExtSymbolSegment(address);
    let data = Buffer.alloc(8);
    data.writeUInt16LE(DataTypes.Float, 0);
    data.writeUInt16LE(1, 2);
    data.writeFloatLE(value, 4);

    let request = MessageRouter.Request(Services.WriteTag, path, data);

    this.send(request, function(message) {
      let reply = MessageRouter.Reply(message);
      callback(null, reply);
    });
  }

  handleData(data, info, context) {
    if (context == null) {
      throw new Error('ControlLogix Error: Unhandled message, context should not be null');
{'fraction_non_alphanumeric': 0.12710173838700484, 'fraction_numerical': 0.011684240524365916, 'mean_word_length': 3.529032258064516, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '626527', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1011, 'n_words': 245}