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

[jeelink] Use serial transport #7620

Merged
merged 2 commits into from
May 13, 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.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