diff --git a/bundles/org.openhab.binding.meteostick/pom.xml b/bundles/org.openhab.binding.meteostick/pom.xml index 181f642ac78a9..7df8f21c7537e 100644 --- a/bundles/org.openhab.binding.meteostick/pom.xml +++ b/bundles/org.openhab.binding.meteostick/pom.xml @@ -14,8 +14,4 @@ openHAB Add-ons :: Bundles :: meteostick Binding - - gnu.io;version="[3.12,6)" - - diff --git a/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/MeteostickHandlerFactory.java b/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/MeteostickHandlerFactory.java index bcc3ae6a809e8..329f0b0860343 100644 --- a/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/MeteostickHandlerFactory.java +++ b/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/MeteostickHandlerFactory.java @@ -12,15 +12,20 @@ */ package org.openhab.binding.meteostick.internal; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.thing.Bridge; import org.eclipse.smarthome.core.thing.Thing; import org.eclipse.smarthome.core.thing.ThingTypeUID; 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.meteostick.internal.handler.MeteostickBridgeHandler; import org.openhab.binding.meteostick.internal.handler.MeteostickSensorHandler; +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; @@ -30,10 +35,18 @@ * * @author Chris Jackson - Initial contribution */ +@NonNullByDefault @Component(service = ThingHandlerFactory.class, configurationPid = "binding.meteostick") public class MeteostickHandlerFactory extends BaseThingHandlerFactory { private Logger logger = LoggerFactory.getLogger(MeteostickHandlerFactory.class); + private final SerialPortManager serialPortManager; + + @Activate + public MeteostickHandlerFactory(final @Reference SerialPortManager serialPortManager) { + this.serialPortManager = serialPortManager; + } + @Override public boolean supportsThingType(ThingTypeUID thingTypeUID) { return MeteostickBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID) @@ -41,13 +54,13 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) { } @Override - protected ThingHandler createHandler(Thing thing) { + protected @Nullable ThingHandler createHandler(Thing thing) { logger.debug("MeteoStick thing factory: createHandler {} of type {}", thing.getThingTypeUID(), thing.getUID()); ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (MeteostickBridgeHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) { - return new MeteostickBridgeHandler((Bridge) thing); + return new MeteostickBridgeHandler((Bridge) thing, serialPortManager); } if (MeteostickSensorHandler.SUPPORTED_THING_TYPES.contains(thingTypeUID)) { diff --git a/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/handler/MeteostickBridgeHandler.java b/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/handler/MeteostickBridgeHandler.java index 81f086bd64b8b..883a0597f2334 100644 --- a/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/handler/MeteostickBridgeHandler.java +++ b/bundles/org.openhab.binding.meteostick/src/main/java/org/openhab/binding/meteostick/internal/handler/MeteostickBridgeHandler.java @@ -37,18 +37,16 @@ import org.eclipse.smarthome.core.thing.ThingTypeUID; import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler; import org.eclipse.smarthome.core.types.Command; +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.SerialPortManager; +import org.eclipse.smarthome.io.transport.serial.UnsupportedCommOperationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import gnu.io.CommPort; -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; - /** * The {@link MeteostickBridgeHandler} is responsible for handling commands, which are * sent to one of the channels. @@ -63,6 +61,7 @@ public class MeteostickBridgeHandler extends BaseBridgeHandler { private static final int RECEIVE_TIMEOUT = 3000; private SerialPort serialPort; + private final SerialPortManager serialPortManager; private ReceiveThread receiveThread; private ScheduledFuture offlineTimerJob; @@ -74,8 +73,9 @@ public class MeteostickBridgeHandler extends BaseBridgeHandler { private ConcurrentMap eventListeners = new ConcurrentHashMap<>(); - public MeteostickBridgeHandler(Bridge thing) { + public MeteostickBridgeHandler(Bridge thing, SerialPortManager serialPortManager) { super(thing); + this.serialPortManager = serialPortManager; } @Override @@ -146,13 +146,18 @@ protected void unsubscribeEvents(int channel, MeteostickEventListener handler) { * @throws SerialInterfaceException when a connection error occurs. */ private boolean connectPort(final String serialPortName) { - logger.info("MeteoStick Connecting to serial port {}", serialPortName); + logger.debug("MeteoStick Connecting to serial port {}", serialPortName); + + SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName); + if (portIdentifier == null) { + updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, + "Serial Error: Port " + serialPortName + " does not exist"); + return false; + } boolean success = false; try { - CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName); - CommPort commPort = portIdentifier.open("org.openhab.binding.meteostick", 2000); - serialPort = (SerialPort) commPort; + serialPort = portIdentifier.open("org.openhab.binding.meteostick", 2000); serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); serialPort.enableReceiveThreshold(1); @@ -166,12 +171,9 @@ private boolean connectPort(final String serialPortName) { serialPort.addEventListener(this.receiveThread); serialPort.notifyOnDataAvailable(true); - logger.info("Serial port is initialized"); + logger.debug("Serial port is initialized"); success = true; - } catch (NoSuchPortException e) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, - "Serial Error: Port " + serialPortName + " does not exist"); } catch (PortInUseException e) { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Serial Error: Port " + serialPortName + " in use"); @@ -203,7 +205,7 @@ private void disconnect() { this.serialPort.close(); this.serialPort = null; } - logger.info("Disconnected from serial port"); + logger.debug("Disconnected from serial port"); } private void sendToMeteostick(String string) {