Skip to content

Commit

Permalink
[jeelink] Use serial transport (openhab#7620)
Browse files Browse the repository at this point in the history
* [jeelink] Use serial transport

Related to openhab#7573
* Pass around SerialPortIdentifier instead of SerialPortManager

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn authored and J-N-K committed Jul 14, 2020
1 parent a9610ce commit 4f0b04d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 55 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,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;

Expand All @@ -44,31 +47,43 @@
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);
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");
}
}

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 @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 4f0b04d

Please sign in to comment.