Skip to content

Commit

Permalink
[lutron] Replace gnu.io with OH serial transport (openhab#7609)
Browse files Browse the repository at this point in the history
Signed-off-by: Bob Adair <bob.github@att.net>
  • Loading branch information
bobadair authored May 12, 2020
1 parent 526ac43 commit 700c76c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerFactory;
import org.eclipse.smarthome.io.net.http.HttpClientFactory;
import org.eclipse.smarthome.io.transport.serial.SerialPortManager;
import org.openhab.binding.lutron.internal.discovery.LutronDeviceDiscoveryService;
import org.openhab.binding.lutron.internal.grxprg.GrafikEyeHandler;
import org.openhab.binding.lutron.internal.grxprg.PrgBridgeHandler;
Expand Down Expand Up @@ -65,6 +66,7 @@
import org.openhab.binding.lutron.internal.radiora.handler.PhantomButtonHandler;
import org.openhab.binding.lutron.internal.radiora.handler.RS232Handler;
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;
Expand Down Expand Up @@ -103,9 +105,17 @@ public class LutronHandlerFactory extends BaseThingHandlerFactory {

private final Logger logger = LoggerFactory.getLogger(LutronHandlerFactory.class);

private final SerialPortManager serialPortManager;

private @NonNullByDefault({}) HttpClient httpClient;
// shared instance obtained from HttpClientFactory service and passed to device discovery service

@Activate
public LutronHandlerFactory(final @Reference SerialPortManager serialPortManager) {
// Obtain the serial port manager service using an OSGi reference
this.serialPortManager = serialPortManager;
}

@Reference
protected void setHttpClientFactory(HttpClientFactory httpClientFactory) {
this.httpClient = httpClientFactory.getCommonHttpClient();
Expand Down Expand Up @@ -180,15 +190,15 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) {
} else if (thingTypeUID.equals(PrgConstants.THING_TYPE_GRAFIKEYE)) {
return new GrafikEyeHandler(thing);
} else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_RS232)) {
return new RS232Handler((Bridge) thing);
return new RS232Handler((Bridge) thing, serialPortManager);
} else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_DIMMER)) {
return new org.openhab.binding.lutron.internal.radiora.handler.DimmerHandler(thing);
} else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_SWITCH)) {
return new org.openhab.binding.lutron.internal.radiora.handler.SwitchHandler(thing);
} else if (thingTypeUID.equals(RadioRAConstants.THING_TYPE_PHANTOM)) {
return new PhantomButtonHandler(thing);
} else if (thingTypeUID.equals(HwConstants.THING_TYPE_HWSERIALBRIDGE)) {
return new HwSerialBridgeHandler((Bridge) thing);
return new HwSerialBridgeHandler((Bridge) thing, serialPortManager);
} else if (thingTypeUID.equals(HwConstants.THING_TYPE_HWDIMMER)) {
return new HwDimmerHandler(thing);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@
import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandlerService;
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.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;

/**
*
* This is the main handler for HomeWorks RS232 Processors.
Expand All @@ -62,12 +61,14 @@ public class HwSerialBridgeHandler extends BaseBridgeHandler implements SerialPo

private HwDiscoveryService discoveryService;

private final SerialPortManager serialPortManager;
private SerialPort serialPort;
private OutputStreamWriter serialOutput;
private BufferedReader serialInput;

public HwSerialBridgeHandler(Bridge bridge) {
public HwSerialBridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
super(bridge);
this.serialPortManager = serialPortManager;
}

@Override
Expand Down Expand Up @@ -99,9 +100,14 @@ public void setDiscoveryService(HwDiscoveryService discoveryService) {
}

private void openConnection() {
SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
if (portIdentifier == null) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Invalid port: " + serialPortName);
return;
}

try {
logger.info("Connecting to Lutron HomeWorks Processor using {}.", serialPortName);
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
serialPort = portIdentifier.open(this.getClass().getName(), 2000);

logger.debug("Connection established using {}. Configuring IO parameters. ", serialPortName);
Expand All @@ -128,9 +134,6 @@ private void openConnection() {
if (updateTime) {
startUpdateProcessorTimeJob();
}

} catch (NoSuchPortException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Invalid port: " + serialPortName);
} catch (PortInUseException portInUseException) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, "Port in use: " + serialPortName);
} catch (UnsupportedCommOperationException | IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,19 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
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.openhab.binding.lutron.internal.radiora.protocol.RadioRAFeedback;
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;

/**
* RS232 connection to the RadioRA Classic system.
*
Expand All @@ -42,32 +38,34 @@ public class RS232Connection implements RadioRAConnection, SerialPortEventListen

private final Logger logger = LoggerFactory.getLogger(RS232Connection.class);

protected SerialPortManager serialPortManager;
protected SerialPort serialPort;

protected BufferedReader inputReader;

protected RadioRAFeedbackListener listener;
protected RS232MessageParser parser = new RS232MessageParser();

public RS232Connection(SerialPortManager serialPortManager) {
super();
this.serialPortManager = serialPortManager;
}

@Override
public void open(String portName, int baud) throws RadioRAConnectionException {
CommPortIdentifier commPort = null;

try {
commPort = CommPortIdentifier.getPortIdentifier(portName);
} catch (NoSuchPortException e) {
logAvailablePorts();
SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(portName);
if (portIdentifier == null) {
throw new RadioRAConnectionException(String.format("Port not found", portName));
}

try {
serialPort = commPort.open("openhab", 5000);
serialPort = portIdentifier.open("openhab", 5000);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(baud, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
serialPort.addEventListener(this);
inputReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
} catch (PortInUseException e) {
throw new RadioRAConnectionException(String.format("Port %s already in use", commPort.getName()));
throw new RadioRAConnectionException(String.format("Port %s already in use", portIdentifier.getName()));
} catch (UnsupportedCommOperationException e) {
throw new RadioRAConnectionException("Error initializing - Failed to set serial port params");
} catch (TooManyListenersException e) {
Expand All @@ -92,28 +90,6 @@ public void disconnect() {
serialPort.close();
}

private void logAvailablePorts() {
if (logger.isDebugEnabled()) {
logger.debug("Available ports:");
for (CommPortIdentifier port : getAvailableSerialPorts()) {
logger.debug("{}", port.getName());
}
}
}

protected List<CommPortIdentifier> getAvailableSerialPorts() {
List<CommPortIdentifier> ports = new ArrayList<>();
Enumeration<?> portIds = CommPortIdentifier.getPortIdentifiers();
while (portIds.hasMoreElements()) {
CommPortIdentifier id = (CommPortIdentifier) portIds.nextElement();
if (CommPortIdentifier.PORT_SERIAL == id.getPortType()) {
ports.add(id);
}
}

return ports;
}

@Override
public void serialEvent(SerialPortEvent ev) {
switch (ev.getEventType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.eclipse.smarthome.core.thing.binding.BaseBridgeHandler;
import org.eclipse.smarthome.core.thing.binding.ThingHandler;
import org.eclipse.smarthome.core.types.Command;
import org.eclipse.smarthome.io.transport.serial.SerialPortManager;
import org.openhab.binding.lutron.internal.radiora.RS232Connection;
import org.openhab.binding.lutron.internal.radiora.RadioRAConnection;
import org.openhab.binding.lutron.internal.radiora.RadioRAConnectionException;
Expand All @@ -48,10 +49,10 @@ public class RS232Handler extends BaseBridgeHandler implements RadioRAFeedbackLi

private ScheduledFuture<?> zoneMapScheduledTask;

public RS232Handler(Bridge bridge) {
public RS232Handler(Bridge bridge, SerialPortManager serialPortManager) {
super(bridge);

this.connection = new RS232Connection();
this.connection = new RS232Connection(serialPortManager);
this.connection.setListener(this);
}

Expand Down

0 comments on commit 700c76c

Please sign in to comment.