'use strict';
/*
 * EnOcean Serial Node for Packet Type 10: RADIO_ERP2
 * Supported Protocols (Partially):
 * - EnOcean Serial Protocol 3 (ESP3) V1.27 / July 30, 2014
 * - EnOcean Radio Protocol 2 SPECIFICATION V1.0 September 26, 2013
 *
 * This node expects "node-red-node-serialport" to be available
 * on the editor in order to use its `/serialports` endpoint.
 */

import { SerialPool } from './lib/enocean';
import { ERP2_HANDLERS } from './lib/eep_handlers';

export default function(RED) {
  function EnOceanPortNode(n) {
    RED.nodes.createNode(this, n);
    let that = this;
    that.serialPort = n.serialPort;
    EnOceanPortNode.pool.add(that);
  }
  EnOceanPortNode.pool = new SerialPool(RED);
  RED.nodes.registerType('EnOcean Port', EnOceanPortNode);

  function EnOceanInNode(n) {
    RED.nodes.createNode(this, n);
    let that = this;
    that.name = n.name;
    that.originatorId = n.originatorId;
    that.eepType = n.eepType;
    that.addEepType = n.addEepType;
    that.useString = n.useString;
    that.enoceanPortNodeId = n.enoceanPort;
    that.enoceanPortNode = RED.nodes.getNode(that.enoceanPortNodeId);
    that.on('close', done => {
      if (that.enoceanPortNode) {
        EnOceanPortNode.pool.close(that.enoceanPortNode.serialPort).then(() => {
          done();
        });
      } else {
        done();
      }
    });
    let enocean = EnOceanPortNode.pool.get(that.enoceanPortNode.serialPort);
    enocean.port.on(`ctx-${that.originatorId}`, ctx => {
      let handleIt = ERP2_HANDLERS[that.eepType];
      if (!handleIt) {
        RED.log.warn('enocean.warn.noHandler', { eepType: that.eepType });
        return;
      }
      let data = handleIt(ctx);
      let payload = {
        data: data,
        tstamp: Date.now(),
        rssi: ctx.container.dBm,
        id: ctx.originatorId
      };
      if (that.addEepType) {
        payload.eep = that.eepType;
      }
      if (that.useString) {
        payload = JSON.stringify(payload);
      }
      that.send({ payload: payload });
    });
    enocean.port.on('ready', () => {
      that.status({ fill: 'green', shape: 'dot', text: 'node-red:common.status.connected'});
    });
    enocean.port.on('closed', () => {
      that.status({ fill: 'red', shape: 'ring', text: 'node-red:common.status.not-connected'});
    });
  }
  RED.nodes.registerType('EnOcean in', EnOceanInNode);

  RED.httpAdmin.get("/eeps", RED.auth.needsPermission('eep.read'), function(req,res) {
    res.json(Object.keys(ERP2_HANDLERS));
  });

}

@@ -570,32 +570,44 @@
 et that = this;%0A
+    try %7B%0A  
     that.serialP
@@ -626,24 +626,26 @@
 alPort;%0A    
+  
 EnOceanPortN
@@ -664,16 +664,115 @@
 (that);%0A
+    %7D catch (e) %7B%0A      RED.log.warn(RED._('enocean.errors.serialPortError', %7B error: e %7D));%0A    %7D%0A
   %7D%0A  En
@@ -1456,24 +1456,36 @@
   %7D%0A    %7D);%0A
+    try %7B%0A  
     let enoc
@@ -1545,24 +1545,26 @@
 erialPort);%0A
+  
     enocean.
@@ -1610,24 +1610,26 @@
  =%3E %7B%0A      
+  
 let handleIt
@@ -1662,24 +1662,26 @@
 ype%5D;%0A      
+ 
+ 
 if (!handleI
@@ -1689,24 +1689,26 @@
 ) %7B%0A        
+  
 RED.log.warn
@@ -1708,16 +1708,22 @@
 og.warn(
+RED._(
 'enocean
@@ -1766,18 +1766,21 @@
 pType %7D)
+)
 ;%0A
+  
         
@@ -1789,26 +1789,30 @@
 turn;%0A      
-%7D%0A
+  %7D%0A  
       let da
@@ -1833,24 +1833,26 @@
 ctx);%0A      
+  
 let payload 
@@ -1863,16 +1863,18 @@
         
+  
 data: da
@@ -1885,16 +1885,18 @@
         
+ 
+ 
 tstamp: 
@@ -1907,16 +1907,18 @@
 .now(),%0A
+  
         
@@ -1942,16 +1942,18 @@
 er.dBm,%0A
+  
         
@@ -1983,11 +1983,15 @@
     
+  
 %7D;%0A
+  
     
@@ -2011,24 +2011,26 @@
 dEepType) %7B%0A
+  
         payl
@@ -2055,26 +2055,30 @@
 Type;%0A      
-%7D%0A
+  %7D%0A  
       if (th
@@ -2097,24 +2097,26 @@
 ) %7B%0A        
+ 
+ 
 payload = JS
@@ -2134,24 +2134,26 @@
 y(payload);%0A
+  
       %7D%0A    
@@ -2150,24 +2150,26 @@
     %7D%0A      
+  
 that.send(%7B 
@@ -2185,36 +2185,40 @@
 payload %7D);%0A    
+ 
+ 
 %7D);%0A
+  
     enocean.port
@@ -2230,32 +2230,34 @@
 ready', () =%3E %7B%0A
+  
       that.statu
@@ -2333,28 +2333,32 @@
 ted'%7D);%0A    
+  
 %7D);%0A
+  
     enocean.
@@ -2379,24 +2379,26 @@
 d', () =%3E %7B%0A
+  
       that.s
@@ -2485,20 +2485,121 @@
 %7D);%0A    
+  
 %7D);%0A
+    %7D catch (e) %7B%0A      RED.log.warn(RED._('enocean.errors.serialPortError', %7B error: e %7D));%0A    %7D%0A
   %7D%0A  RE

Fix issues where serial port errors weren't handled properly