Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[meteostick] Use serial transport #7632

Merged
merged 1 commit into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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