From 194c471a14ed4d417a9de0d849417ff8dae3d655 Mon Sep 17 00:00:00 2001 From: Wouter Born Date: Wed, 13 May 2020 22:14:03 +0200 Subject: [PATCH] [jeelink] Use serial transport (#7620) * [jeelink] Use serial transport Related to #7573 * Pass around SerialPortIdentifier instead of SerialPortManager Signed-off-by: Wouter Born --- bundles/org.openhab.binding.jeelink/pom.xml | 4 --- .../jeelink/internal/JeeLinkHandler.java | 35 +++++++++++++------ .../internal/JeeLinkHandlerFactory.java | 13 +++++-- .../connection/AbstractJeeLinkConnection.java | 19 ---------- .../connection/JeeLinkSerialConnection.java | 34 ++++++++---------- 5 files changed, 50 insertions(+), 55 deletions(-) diff --git a/bundles/org.openhab.binding.jeelink/pom.xml b/bundles/org.openhab.binding.jeelink/pom.xml index 230e854e24cd2..c23e4bd177136 100644 --- a/bundles/org.openhab.binding.jeelink/pom.xml +++ b/bundles/org.openhab.binding.jeelink/pom.xml @@ -14,8 +14,4 @@ openHAB Add-ons :: Bundles :: JeeLink Binding - - gnu.io;version="[3.12,6)" - - diff --git a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandler.java b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandler.java index d69ec5f510a39..08a7646d14881 100644 --- a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandler.java +++ b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandler.java @@ -29,10 +29,13 @@ import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler; import org.eclipse.smarthome.core.thing.binding.BridgeHandler; import org.eclipse.smarthome.core.types.Command; +import org.eclipse.smarthome.io.transport.serial.SerialPortIdentifier; +import org.eclipse.smarthome.io.transport.serial.SerialPortManager; import org.openhab.binding.jeelink.internal.config.JeeLinkConfig; -import org.openhab.binding.jeelink.internal.connection.AbstractJeeLinkConnection; import org.openhab.binding.jeelink.internal.connection.ConnectionListener; import org.openhab.binding.jeelink.internal.connection.JeeLinkConnection; +import org.openhab.binding.jeelink.internal.connection.JeeLinkSerialConnection; +import org.openhab.binding.jeelink.internal.connection.JeeLinkTcpConnection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -44,11 +47,12 @@ public class JeeLinkHandler extends BaseBridgeHandler implements BridgeHandler, ConnectionListener { private final Logger logger = LoggerFactory.getLogger(JeeLinkHandler.class); - private JeeLinkConnection connection; - private List> converters = new ArrayList<>(); - private Map> sensorTypeConvertersMap = new HashMap<>(); - private Map, Set>> readingClassHandlerMap = new HashMap<>(); + private final List> converters = new ArrayList<>(); + private final Map> sensorTypeConvertersMap = new HashMap<>(); + private final Map, Set>> readingClassHandlerMap = new HashMap<>(); + private final SerialPortManager serialPortManager; + private JeeLinkConnection connection; private AtomicBoolean connectionInitialized = new AtomicBoolean(false); private ScheduledFuture connectJob; private ScheduledFuture initJob; @@ -56,19 +60,30 @@ public class JeeLinkHandler extends BaseBridgeHandler implements BridgeHandler, private long lastReadingTime; private ScheduledFuture monitorJob; - public JeeLinkHandler(Bridge bridge) { + public JeeLinkHandler(Bridge bridge, SerialPortManager serialPortManager) { super(bridge); + this.serialPortManager = serialPortManager; } @Override public void initialize() { JeeLinkConfig cfg = getConfig().as(JeeLinkConfig.class); - try { - connection = AbstractJeeLinkConnection.createFor(cfg, scheduler, this); + if (cfg.serialPort != null && cfg.baudRate != null) { + SerialPortIdentifier serialPortIdentifier = serialPortManager.getIdentifier(cfg.serialPort); + if (serialPortIdentifier == null) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, + "Port not found: " + cfg.serialPort); + return; + } + connection = new JeeLinkSerialConnection(serialPortIdentifier, cfg.baudRate, this); + connection.openConnection(); + } else if (cfg.ipAddress != null && cfg.port != null) { + connection = new JeeLinkTcpConnection(cfg.ipAddress + ":" + cfg.port, scheduler, this); connection.openConnection(); - } catch (java.net.ConnectException e) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage()); + } else { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, + "Connection configuration incomplete"); } } diff --git a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandlerFactory.java b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandlerFactory.java index 93703fca23613..dc9ba3919467f 100644 --- a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandlerFactory.java +++ b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/JeeLinkHandlerFactory.java @@ -26,9 +26,12 @@ import org.eclipse.smarthome.core.thing.binding.BaseThingHandlerFactory; import org.eclipse.smarthome.core.thing.binding.ThingHandler; import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory; +import org.eclipse.smarthome.io.transport.serial.SerialPortManager; import org.openhab.binding.jeelink.internal.discovery.SensorDiscoveryService; import org.osgi.framework.ServiceRegistration; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,7 +44,13 @@ public class JeeLinkHandlerFactory extends BaseThingHandlerFactory { private final Logger logger = LoggerFactory.getLogger(JeeLinkHandlerFactory.class); - private Map> discoveryServiceRegs = new HashMap<>(); + private final Map> discoveryServiceRegs = new HashMap<>(); + private final SerialPortManager serialPortManager; + + @Activate + public JeeLinkHandlerFactory(final @Reference SerialPortManager serialPortManager) { + this.serialPortManager = serialPortManager; + } @Override public boolean supportsThingType(ThingTypeUID thingTypeUid) { @@ -57,7 +66,7 @@ protected ThingHandler createHandler(Thing thing) { || thingTypeUid.equals(LGW_TCP_STICK_THING_TYPE) || thingTypeUid.equals(LGW_USB_STICK_THING_TYPE)) { logger.debug("creating JeeLinkHandler for thing {}...", thing.getUID().getId()); - handler = new JeeLinkHandler((Bridge) thing); + handler = new JeeLinkHandler((Bridge) thing, serialPortManager); registerSensorDiscoveryService((JeeLinkHandler) handler); } else { handler = SensorDefinition.createHandler(thingTypeUid, thing); diff --git a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/AbstractJeeLinkConnection.java b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/AbstractJeeLinkConnection.java index 338506d80bf8a..551e3e8af3a5e 100644 --- a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/AbstractJeeLinkConnection.java +++ b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/AbstractJeeLinkConnection.java @@ -15,12 +15,8 @@ import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; -import java.net.ConnectException; -import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.atomic.AtomicBoolean; -import org.openhab.binding.jeelink.internal.JeeLinkHandler; -import org.openhab.binding.jeelink.internal.config.JeeLinkConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -101,19 +97,4 @@ public void sendCommands(String commands) { notifyAbort("propagate: " + ex.getMessage()); } } - - public static JeeLinkConnection createFor(JeeLinkConfig config, ScheduledExecutorService scheduler, - JeeLinkHandler h) throws ConnectException { - JeeLinkConnection connection; - - if (config.serialPort != null && config.baudRate != null) { - connection = new JeeLinkSerialConnection(config.serialPort, config.baudRate, h); - } else if (config.ipAddress != null && config.port != null) { - connection = new JeeLinkTcpConnection(config.ipAddress + ":" + config.port, scheduler, h); - } else { - throw new ConnectException("Connection configuration incomplete"); - } - - return connection; - } } diff --git a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/JeeLinkSerialConnection.java b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/JeeLinkSerialConnection.java index b889338bece9a..98dd585745f1f 100644 --- a/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/JeeLinkSerialConnection.java +++ b/bundles/org.openhab.binding.jeelink/src/main/java/org/openhab/binding/jeelink/internal/connection/JeeLinkSerialConnection.java @@ -18,17 +18,15 @@ import java.io.OutputStream; import java.util.TooManyListenersException; +import org.eclipse.smarthome.io.transport.serial.PortInUseException; +import org.eclipse.smarthome.io.transport.serial.SerialPort; +import org.eclipse.smarthome.io.transport.serial.SerialPortEvent; +import org.eclipse.smarthome.io.transport.serial.SerialPortEventListener; +import org.eclipse.smarthome.io.transport.serial.SerialPortIdentifier; +import org.eclipse.smarthome.io.transport.serial.UnsupportedCommOperationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import gnu.io.CommPortIdentifier; -import gnu.io.NoSuchPortException; -import gnu.io.PortInUseException; -import gnu.io.SerialPort; -import gnu.io.SerialPortEvent; -import gnu.io.SerialPortEventListener; -import gnu.io.UnsupportedCommOperationException; - /** * Reads lines from serial port and propagates them to registered InputListeners. * @@ -37,16 +35,18 @@ public class JeeLinkSerialConnection extends AbstractJeeLinkConnection { private final Logger logger = LoggerFactory.getLogger(JeeLinkSerialConnection.class); - private int baudRate; - + private final int baudRate; private SerialPort serialPort; + private final SerialPortIdentifier serialPortIdentifier; private boolean open; - public JeeLinkSerialConnection(String portName, int baudRate, ConnectionListener l) { - super(portName, l); + public JeeLinkSerialConnection(SerialPortIdentifier serialPortIdentifier, int baudRate, + ConnectionListener listener) { + super(serialPortIdentifier.getName(), listener); - logger.debug("Creating serial connection for port {} with baud rate {}...", portName, baudRate); + logger.debug("Creating serial connection for port {} with baud rate {}...", port, baudRate); this.baudRate = baudRate; + this.serialPortIdentifier = serialPortIdentifier; } @Override @@ -68,11 +68,7 @@ public synchronized void openConnection() { try { if (!open) { logger.debug("Opening serial connection to port {} with baud rate {}...", port, baudRate); - - CommPortIdentifier portIdentifier; - - portIdentifier = CommPortIdentifier.getPortIdentifier(port); - serialPort = portIdentifier.open("openhab", 3000); + serialPort = serialPortIdentifier.open("openhab", 3000); open = true; serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, @@ -101,8 +97,6 @@ public void serialEvent(SerialPortEvent event) { } catch (UnsupportedCommOperationException | IOException | TooManyListenersException ex) { closeConnection(); notifyAbort(ex.getMessage()); - } catch (NoSuchPortException ex) { - notifyAbort("Port not found: " + port); } catch (PortInUseException ex) { notifyAbort("Port in use: " + port); }