Skip to content

Commit

Permalink
[meteostick] Use serial transport (openhab#7632)
Browse files Browse the repository at this point in the history
Related to openhab#7573

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored and LoungeFlyZ committed Jun 8, 2020
1 parent bf724ca commit 661d9e4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
4 changes: 0 additions & 4 deletions bundles/org.openhab.binding.meteostick/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@

<name>openHAB Add-ons :: Bundles :: meteostick Binding</name>

<properties>
<bnd.importpackage>gnu.io;version="[3.12,6)"</bnd.importpackage>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,24 +35,32 @@
*
* @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)
| MeteostickSensorHandler.SUPPORTED_THING_TYPES.contains(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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -74,8 +73,9 @@ public class MeteostickBridgeHandler extends BaseBridgeHandler {

private ConcurrentMap<Integer, MeteostickEventListener> eventListeners = new ConcurrentHashMap<>();

public MeteostickBridgeHandler(Bridge thing) {
public MeteostickBridgeHandler(Bridge thing, SerialPortManager serialPortManager) {
super(thing);
this.serialPortManager = serialPortManager;
}

@Override
Expand Down Expand Up @@ -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);
Expand All @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 661d9e4

Please sign in to comment.