Skip to content

Commit

Permalink
[jeelink] Use serial transport
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 committed May 12, 2020
1 parent 700c76c commit f61231f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 29 deletions.
4 changes: 0 additions & 4 deletions bundles/org.openhab.binding.jeelink/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@

<name>openHAB Add-ons :: Bundles :: JeeLink 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 @@ -29,6 +29,7 @@
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.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;
Expand All @@ -44,28 +45,30 @@
public class JeeLinkHandler extends BaseBridgeHandler implements BridgeHandler, ConnectionListener {
private final Logger logger = LoggerFactory.getLogger(JeeLinkHandler.class);

private JeeLinkConnection connection;
private List<JeeLinkReadingConverter<?>> converters = new ArrayList<>();
private Map<String, JeeLinkReadingConverter<?>> sensorTypeConvertersMap = new HashMap<>();
private Map<Class<?>, Set<ReadingHandler<? extends Reading>>> readingClassHandlerMap = new HashMap<>();
private final List<JeeLinkReadingConverter<?>> converters = new ArrayList<>();
private final Map<String, JeeLinkReadingConverter<?>> sensorTypeConvertersMap = new HashMap<>();
private final Map<Class<?>, Set<ReadingHandler<? extends Reading>>> readingClassHandlerMap = new HashMap<>();
private final SerialPortManager serialPortManager;

private JeeLinkConnection connection;
private AtomicBoolean connectionInitialized = new AtomicBoolean(false);
private ScheduledFuture<?> connectJob;
private ScheduledFuture<?> initJob;

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);
connection = AbstractJeeLinkConnection.createFor(cfg, scheduler, this, serialPortManager);
connection.openConnection();
} catch (java.net.ConnectException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -41,7 +44,13 @@
public class JeeLinkHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(JeeLinkHandlerFactory.class);

private Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegs = new HashMap<>();
private final SerialPortManager serialPortManager;

@Activate
public JeeLinkHandlerFactory(final @Reference SerialPortManager serialPortManager) {
this.serialPortManager = serialPortManager;
}

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUid) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.smarthome.io.transport.serial.SerialPortManager;
import org.openhab.binding.jeelink.internal.JeeLinkHandler;
import org.openhab.binding.jeelink.internal.config.JeeLinkConfig;
import org.slf4j.Logger;
Expand Down Expand Up @@ -103,11 +104,11 @@ public void sendCommands(String commands) {
}

public static JeeLinkConnection createFor(JeeLinkConfig config, ScheduledExecutorService scheduler,
JeeLinkHandler h) throws ConnectException {
JeeLinkHandler h, SerialPortManager serialPortManager) throws ConnectException {
JeeLinkConnection connection;

if (config.serialPort != null && config.baudRate != null) {
connection = new JeeLinkSerialConnection(config.serialPort, config.baudRate, h);
connection = new JeeLinkSerialConnection(config.serialPort, config.baudRate, h, serialPortManager);
} else if (config.ipAddress != null && config.port != null) {
connection = new JeeLinkTcpConnection(config.ipAddress + ":" + config.port, scheduler, h);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,16 @@
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.SerialPortManager;
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.
*
Expand All @@ -37,16 +36,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 SerialPortManager serialPortManager;
private boolean open;

public JeeLinkSerialConnection(String portName, int baudRate, ConnectionListener l) {
public JeeLinkSerialConnection(String portName, int baudRate, ConnectionListener l,
SerialPortManager serialPortManager) {
super(portName, l);

logger.debug("Creating serial connection for port {} with baud rate {}...", portName, baudRate);
this.baudRate = baudRate;
this.serialPortManager = serialPortManager;
}

@Override
Expand All @@ -69,9 +70,12 @@ public synchronized void openConnection() {
if (!open) {
logger.debug("Opening serial connection to port {} with baud rate {}...", port, baudRate);

CommPortIdentifier portIdentifier;
SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(port);
if (portIdentifier == null) {
notifyAbort("Port not found: " + port);
return;
}

portIdentifier = CommPortIdentifier.getPortIdentifier(port);
serialPort = portIdentifier.open("openhab", 3000);
open = true;

Expand Down Expand Up @@ -101,8 +105,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);
}
Expand Down

0 comments on commit f61231f

Please sign in to comment.