Skip to content

Commit

Permalink
[digiplex] Use serial transport
Browse files Browse the repository at this point in the history
Related to openhab#7573

Also removes the IT100BridgeDiscovery which sends messages to all available serial ports.
Sending messages to other serial devices may cause issues such as message corruption, locking issues and unwanted operations.
There's also a SerialConfigOptionProvider nowadays which simplifies selecting the serial ports in UIs from all available serial devices.

Signed-off-by: Wouter Born <github@maindrain.net>
  • Loading branch information
wborn committed May 12, 2020
1 parent 700c76c commit affe530
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 213 deletions.
3 changes: 1 addition & 2 deletions bundles/org.openhab.binding.dscalarm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ Most settings are through thing configuration parameters.

The DSC Alarm binding incorporates several discovery modes in order to find DSC Alarm systems.
First, there is the Envisalink bridge discovery mode which performs a network query for any Envisalink adapters and adds them to the discovery inbox.
Second, there is The IT-100 bridge discovery mode which will search serial ports for any IT-100 adapters and add them to the discovery inbox.
The bridge discovery modes are started manually through Paper UI.
Third, after a bridge is discovered and available to openHAB, the binding will attempt to discover DSC Alarm things and add them to the discovery inbox.
After a bridge is discovered and available to openHAB, the binding will attempt to discover DSC Alarm things and add them to the discovery inbox.
The TCP Server bridge does not implement bridge discovery but will utilize thing discovery once it is online.

Note:
Expand Down
4 changes: 0 additions & 4 deletions bundles/org.openhab.binding.dscalarm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,4 @@

<name>openHAB Add-ons :: Bundles :: DSCAlarm 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 @@ -21,7 +21,6 @@
import org.eclipse.smarthome.core.thing.ThingUID;
import org.openhab.binding.dscalarm.internal.DSCAlarmBindingConstants;
import org.openhab.binding.dscalarm.internal.config.EnvisalinkBridgeConfiguration;
import org.openhab.binding.dscalarm.internal.config.IT100BridgeConfiguration;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -37,11 +36,7 @@ public class DSCAlarmBridgeDiscovery extends AbstractDiscoveryService {
private final Logger logger = LoggerFactory.getLogger(DSCAlarmBridgeDiscovery.class);

private EnvisalinkBridgeDiscovery envisalinkBridgeDiscovery = new EnvisalinkBridgeDiscovery(this);
private IT100BridgeDiscovery it100BridgeDiscovery = new IT100BridgeDiscovery(this);

/**
* Constructor.
*/
public DSCAlarmBridgeDiscovery() {
super(DSCAlarmBindingConstants.SUPPORTED_BRIDGE_THING_TYPES_UIDS, 15, true);
}
Expand All @@ -50,17 +45,12 @@ public DSCAlarmBridgeDiscovery() {
protected void startScan() {
logger.trace("Start DSC Alarm Bridge discovery.");
scheduler.execute(envisalinkBridgeDiscoveryRunnable);
scheduler.execute(it100BridgeDiscoveryRunnable);
}

private Runnable envisalinkBridgeDiscoveryRunnable = () -> {
envisalinkBridgeDiscovery.discoverBridge();
};

private Runnable it100BridgeDiscoveryRunnable = () -> {
it100BridgeDiscovery.discoverBridge();
};

/**
* Method to add an Envisalink Bridge to the Smarthome Inbox.
*
Expand All @@ -84,40 +74,4 @@ public void addEnvisalinkBridge(String ipAddress) {
logger.error("addBridge(): Error", e);
}
}

/**
* Method to add an IT-100 Bridge to the Smarthome Inbox.
*
* @param port
*/
public void addIT100Bridge(String port) {
logger.trace("addBridge(): Adding new IT-100 Bridge on {} to Smarthome inbox", port);

String bridgeID = "";
boolean containsChar = port.contains("/");

if (containsChar) {
String[] parts = port.split("/");
String id = parts[parts.length - 1].toUpperCase();
bridgeID = id.replaceAll("\\W", "_");

} else {
String id = port.toUpperCase();
bridgeID = id.replaceAll("\\W", "_");
}

Map<String, Object> properties = new HashMap<>(0);
properties.put(IT100BridgeConfiguration.SERIAL_PORT, port);

try {
ThingUID thingUID = new ThingUID(DSCAlarmBindingConstants.IT100BRIDGE_THING_TYPE, bridgeID);

thingDiscovered(DiscoveryResultBuilder.create(thingUID).withProperties(properties)
.withLabel("DSC IT-100 Bridge - " + port).build());

logger.trace("addBridge(): '{}' was added to Smarthome inbox.", thingUID);
} catch (Exception e) {
logger.error("addBridge(): Error", e);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
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.dscalarm.internal.DSCAlarmBindingConstants;
import org.openhab.binding.dscalarm.internal.config.DSCAlarmPartitionConfiguration;
import org.openhab.binding.dscalarm.internal.config.DSCAlarmZoneConfiguration;
Expand All @@ -43,7 +44,9 @@
import org.openhab.binding.dscalarm.internal.handler.TCPServerBridgeHandler;
import org.openhab.binding.dscalarm.internal.handler.ZoneThingHandler;
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 @@ -58,6 +61,13 @@ public class DSCAlarmHandlerFactory extends BaseThingHandlerFactory {
private final Logger logger = LoggerFactory.getLogger(DSCAlarmHandlerFactory.class);
private final Map<ThingUID, ServiceRegistration<?>> discoveryServiceRegistrations = new HashMap<>();

private final SerialPortManager serialPortManager;

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

@Override
public Thing createThing(ThingTypeUID thingTypeUID, Configuration configuration, ThingUID thingUID,
ThingUID bridgeUID) {
Expand Down Expand Up @@ -261,7 +271,7 @@ protected ThingHandler createHandler(Thing thing) {
logger.debug("createHandler(): ENVISALINKBRIDGE_THING: ThingHandler created for {}", thingTypeUID);
return handler;
} else if (thingTypeUID.equals(DSCAlarmBindingConstants.IT100BRIDGE_THING_TYPE)) {
IT100BridgeHandler handler = new IT100BridgeHandler((Bridge) thing);
IT100BridgeHandler handler = new IT100BridgeHandler((Bridge) thing, serialPortManager);
registerDSCAlarmDiscoveryService(handler);
logger.debug("createHandler(): IT100BRIDGE_THING: ThingHandler created for {}", thingTypeUID);
return handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@
import org.apache.commons.io.IOUtils;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.ThingStatus;
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.dscalarm.internal.config.IT100BridgeConfiguration;
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 bridge handler for the DSC IT100 RS232 Serial interface.
*
Expand All @@ -44,22 +42,19 @@
public class IT100BridgeHandler extends DSCAlarmBaseBridgeHandler implements SerialPortEventListener {

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

/**
* Constructor.
*
* @param bridge
*/
public IT100BridgeHandler(Bridge bridge) {
super(bridge, DSCAlarmBridgeType.IT100, DSCAlarmProtocol.IT100_API);
}
private final SerialPortManager serialPortManager;

private String serialPortName = "";
private int baudRate;
private SerialPort serialPort = null;
private OutputStreamWriter serialOutput = null;
private BufferedReader serialInput = null;

public IT100BridgeHandler(Bridge bridge, SerialPortManager serialPortManager) {
super(bridge, DSCAlarmBridgeType.IT100, DSCAlarmProtocol.IT100_API);
this.serialPortManager = serialPortManager;
}

@Override
public void initialize() {
logger.debug("Initializing the DSC IT100 Bridge handler.");
Expand Down Expand Up @@ -98,13 +93,19 @@ public void dispose() {

@Override
public void openConnection() {
try {
logger.debug("openConnection(): Connecting to IT-100 ");
logger.debug("openConnection(): Connecting to IT-100");

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(serialPortName);
CommPort commPort = portIdentifier.open(this.getClass().getName(), 2000);
SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(serialPortName);
if (portIdentifier == null) {
logger.error("openConnection(): No Such Port: {}", serialPort);
setConnected(false);
return;
}

serialPort = (SerialPort) commPort;
try {
SerialPort commPort = portIdentifier.open(this.getClass().getName(), 2000);

serialPort = commPort;
serialPort.setSerialPortParams(baudRate, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
Expand All @@ -116,10 +117,6 @@ public void openConnection() {
setSerialEventHandler(this);

setConnected(true);

} catch (NoSuchPortException noSuchPortException) {
logger.error("openConnection(): No Such Port Exception: {}", noSuchPortException.getMessage());
setConnected(false);
} catch (PortInUseException portInUseException) {
logger.error("openConnection(): Port in Use Exception: {}", portInUseException.getMessage());
setConnected(false);
Expand Down

0 comments on commit affe530

Please sign in to comment.