<commit_before>'use strict';

/*
 * EnOcean Module
 */

import setUpEnocean from 'node-enocean';
import { ESP3RadioERP2Parser, ERP2Parser } from './esp3_erp2_parser';
import Promise from 'es6-promises';
import fs from 'fs';
import LRU from 'lru-cache';

let unknown = LRU({
  max: 100,
  maxAge: 1000 * 60 * 60
});

const ESP3_PACKET_PARSERS = {
  10: new ESP3RadioERP2Parser() // Packet Type 10: RADIO_ERP2
};

class ESP3Parser {
  constructor(RED) {
    this.RED = RED;
  }

  parse(data) {
    return new Promise((resolve, reject) => {
      let esp3PacketParser = ESP3_PACKET_PARSERS[data.packetType];
      if (esp3PacketParser) {
        resolve({
          parser: esp3PacketParser,
          payload: data.rawByte
        });
      } else {
        let e = new Error('enocean.errors.unsupportedPacketType');
        e.packetType = data.packetType;
        reject(e);
      }
    });
  }
}

export class SerialPool {
  constructor(RED) {
    this.pool = {};
    this.esp3Parser = new ESP3Parser(RED);
    this.erp2Parser = new ERP2Parser();
    this.RED = RED;
  }

  add(enOceanPortNode) {
    let that = this;
    let portName = enOceanPortNode.serialPort;
    if (!portName) {
      throw new Error('serialPort property is missing!');
    }
    if (!fs.existsSync(portName)) {
      throw new Error(`The port [${portName}] is NOT ready!`);
    }
    if (that.pool[portName]) {
      throw new Error(`The serial port [${portName}] is duplicate!`);
    }
    let port = setUpEnocean();
    port.listen(portName);
    port.on('data', data => {
      that.esp3Parser.parse(data).then(result => {
        result.parser.parse(result.payload).then(ctx => {
          that.erp2Parser.parse(ctx).then(ctx => {
            let originatorIdInt = ctx.originatorIdInt;
            if (!port.emit(`ctx-${originatorIdInt}`, ctx)) {
              that.RED.log.warn(that.RED._('enocean.warn.noNode', { originatorId: ctx.originatorId }));
            }
          }).catch(e => {
            that.RED.log.error(that.RED._('enocean.errors.parseError', { error: e, data: JSON.stringify(ctx) }));
          });
        }).catch(e => {
          that.RED.log.error(that.RED._('enocean.errors.parseError', { error: e, data: result.payload }));
        });
      }).catch(e => {
        if (e instanceof Error && e.message === 'enocean.info.unsupportedPacketType') {
          that.RED.log.info(that.RED._('enocean.info.unsupportedPacketType', { packetType: e.packetType }));
        } else {
          that.RED.log.error(that.RED._('enocean.errors.parseError', { error: e, data: JSON.stringify(data) }));
        }
      });
    });
    port.on('error', e => {
      that.RED.log.error(that.RED._('enocean.errors.serialPortError',{ error: e }));
      delete that.pool[portName];
    });
    port.on('close', () => {<commit_msg>Modify log level to warn<commit_after>'use strict';

/*
 * EnOcean Module
 */

import setUpEnocean from 'node-enocean';
import { ESP3RadioERP2Parser, ERP2Parser } from './esp3_erp2_parser';
import Promise from 'es6-promises';
import fs from 'fs';
import LRU from 'lru-cache';

let unknown = LRU({
  max: 100,
  maxAge: 1000 * 60 * 60
});

const ESP3_PACKET_PARSERS = {
  10: new ESP3RadioERP2Parser() // Packet Type 10: RADIO_ERP2
};

class ESP3Parser {
  constructor(RED) {
    this.RED = RED;
  }

  parse(data) {
    return new Promise((resolve, reject) => {
      let esp3PacketParser = ESP3_PACKET_PARSERS[data.packetType];
      if (esp3PacketParser) {
        resolve({
          parser: esp3PacketParser,
          payload: data.rawByte
        });
      } else {
        let e = new Error('enocean.errors.unsupportedPacketType');
        e.packetType = data.packetType;
        reject(e);
      }
    });
  }
}

export class SerialPool {
  constructor(RED) {
    this.pool = {};
    this.esp3Parser = new ESP3Parser(RED);
    this.erp2Parser = new ERP2Parser();
    this.RED = RED;
  }

  add(enOceanPortNode) {
    let that = this;
    let portName = enOceanPortNode.serialPort;
    if (!portName) {
      throw new Error('serialPort property is missing!');
    }
    if (!fs.existsSync(portName)) {
      throw new Error(`The port [${portName}] is NOT ready!`);
    }
    if (that.pool[portName]) {
      throw new Error(`The serial port [${portName}] is duplicate!`);
    }
    let port = setUpEnocean();
    port.listen(portName);
    port.on('data', data => {
      that.esp3Parser.parse(data).then(result => {
        result.parser.parse(result.payload).then(ctx => {
          that.erp2Parser.parse(ctx).then(ctx => {
            let originatorIdInt = ctx.originatorIdInt;
            if (!port.emit(`ctx-${originatorIdInt}`, ctx)) {
              that.RED.log.warn(that.RED._('enocean.warn.noNode', { originatorId: ctx.originatorId }));
            }
          }).catch(e => {
            that.RED.log.error(that.RED._('enocean.errors.parseError', { error: e, data: JSON.stringify(ctx) }));
          });
        }).catch(e => {
          that.RED.log.error(that.RED._('enocean.errors.parseError', { error: e, data: result.payload }));
        });
      }).catch(e => {
        if (e instanceof Error && e.message === 'enocean.info.unsupportedPacketType') {
          that.RED.log.info(that.RED._('enocean.info.unsupportedPacketType', { packetType: e.packetType }));
        } else {
          that.RED.log.error(that.RED._('enocean.errors.parseError', { error: e, data: JSON.stringify(data) }));
        }
      });
    });
    port.on('error', e => {
      that.RED.log.warn(that.RED._('enocean.errors.serialPortError',{ error: e }));
      delete that.pool[portName];
    });
    port.on('close', () => {
{'fraction_non_alphanumeric': 0.15998581057112451, 'fraction_numerical': 0.012770485987938986, 'mean_word_length': 2.6078055022392834, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '7241810', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1869, 'n_words': 350}