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

Convert ZWave binding to use ESH SerialPort classes #1152

Merged
merged 1 commit into from Apr 3, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions META-INF/MANIFEST.MF
Expand Up @@ -37,6 +37,7 @@ Import-Package: com.google.common.collect,
org.eclipse.smarthome.core.thing.binding.builder,
org.eclipse.smarthome.core.thing.type,
org.eclipse.smarthome.core.types,
org.eclipse.smarthome.io.transport.serial,
org.junit;version="4.0.0";resolution:=optional,
org.mockito;resolution:=optional,
org.mockito.invocation;resolution:=optional,
Expand Down
Expand Up @@ -18,20 +18,18 @@
import org.eclipse.smarthome.core.thing.ThingStatus;
import org.eclipse.smarthome.core.thing.ThingStatusDetail;
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.openhab.binding.zwave.ZWaveBindingConstants;
import org.openhab.binding.zwave.internal.protocol.SerialMessage;
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 {@link ZWaveSerialHandler} is responsible for the serial communications to the ZWave stick.
* <p>
Expand All @@ -44,9 +42,11 @@ public class ZWaveSerialHandler extends ZWaveControllerHandler {

private Logger logger = LoggerFactory.getLogger(ZWaveSerialHandler.class);

private SerialPortManager serialPortManager;

private String portId;

private SerialPort serialPort;
private org.eclipse.smarthome.io.transport.serial.SerialPort serialPort;

private int SOFCount = 0;
private int CANCount = 0;
Expand All @@ -63,6 +63,11 @@ public ZWaveSerialHandler(Bridge bridge) {
super(bridge);
}

public ZWaveSerialHandler(Bridge thing, SerialPortManager serialPortManager) {
super(thing);
this.serialPortManager = serialPortManager;
}

@Override
public void initialize() {
logger.debug("Initializing ZWave serial controller.");
Expand All @@ -77,9 +82,9 @@ public void initialize() {
super.initialize();
logger.info("Connecting to serial port '{}'", portId);
try {
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portId);
CommPort commPort = portIdentifier.open("org.openhab.binding.zwave", 2000);
serialPort = (SerialPort) commPort;
SerialPortIdentifier portIdentifier = serialPortManager.getIdentifier(portId);
SerialPort commPort = portIdentifier.open("org.openhab.binding.zwave", 2000);
serialPort = commPort;
serialPort.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.enableReceiveThreshold(1);
Expand All @@ -96,18 +101,15 @@ public void initialize() {
logger.info("Serial port is initialized");

initializeNetwork();
} catch (NoSuchPortException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
ZWaveBindingConstants.OFFLINE_SERIAL_EXISTS);// , portId));
} catch (PortInUseException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
ZWaveBindingConstants.OFFLINE_SERIAL_INUSE);// , portId));
ZWaveBindingConstants.OFFLINE_SERIAL_INUSE);
} catch (UnsupportedCommOperationException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
ZWaveBindingConstants.OFFLINE_SERIAL_UNSUPPORTED);// , portId));
ZWaveBindingConstants.OFFLINE_SERIAL_UNSUPPORTED);
} catch (TooManyListenersException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR,
ZWaveBindingConstants.OFFLINE_SERIAL_LISTENERS);// , portId));
ZWaveBindingConstants.OFFLINE_SERIAL_LISTENERS);
}
}

Expand Down Expand Up @@ -176,7 +178,7 @@ public void serialEvent(SerialPortEvent arg0) {
* Sends 1 byte frame response.
*
* @param response
* the response code to send.
* the response code to send.
*/
private void sendResponse(int response) {
try {
Expand Down
Expand Up @@ -9,16 +9,19 @@

import static org.openhab.binding.zwave.ZWaveBindingConstants.CONTROLLER_SERIAL;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.thing.Bridge;
import org.eclipse.smarthome.core.thing.Thing;
import org.eclipse.smarthome.core.thing.ThingTypeUID;
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.zwave.ZWaveBindingConstants;
import org.openhab.binding.zwave.handler.ZWaveSerialHandler;
import org.openhab.binding.zwave.handler.ZWaveThingHandler;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -32,6 +35,17 @@
public class ZWaveHandlerFactory extends BaseThingHandlerFactory {
private Logger logger = LoggerFactory.getLogger(BaseThingHandlerFactory.class);

private @NonNullByDefault({}) SerialPortManager serialPortManager;

@Reference
protected void setSerialPortManager(final SerialPortManager serialPortManager) {
this.serialPortManager = serialPortManager;
}

protected void unsetSerialPortManager(final SerialPortManager serialPortManager) {
this.serialPortManager = null;
}

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
if (thingTypeUID.equals(ZWaveBindingConstants.ZWAVE_THING_UID)) {
Expand All @@ -49,7 +63,7 @@ protected ThingHandler createHandler(Thing thing) {

// Handle controllers here
if (thingTypeUID.equals(CONTROLLER_SERIAL)) {
return new ZWaveSerialHandler((Bridge) thing);
return new ZWaveSerialHandler((Bridge) thing, serialPortManager);
}

// Everything else gets handled in a single handler
Expand Down