From 04143c6f0b3d8499eb05a6da59d1567f5c27864b Mon Sep 17 00:00:00 2001 From: renarj Date: Mon, 23 Nov 2015 20:21:42 +0100 Subject: [PATCH] Made param optional and throw a configuration exception if not configured --- .../home/zwave/SerialZWaveConnector.java | 15 +++++++------- .../ZWaveConfigurationException.java | 10 ++++++++++ src/test/resources/logback.xml | 20 +------------------ 3 files changed, 19 insertions(+), 26 deletions(-) create mode 100644 src/main/java/com/oberasoftware/home/zwave/exceptions/ZWaveConfigurationException.java diff --git a/src/main/java/com/oberasoftware/home/zwave/SerialZWaveConnector.java b/src/main/java/com/oberasoftware/home/zwave/SerialZWaveConnector.java index b4f30fa..c3f9496 100644 --- a/src/main/java/com/oberasoftware/home/zwave/SerialZWaveConnector.java +++ b/src/main/java/com/oberasoftware/home/zwave/SerialZWaveConnector.java @@ -4,20 +4,17 @@ import com.oberasoftware.home.zwave.api.messages.ZWaveRawMessage; import com.oberasoftware.home.zwave.core.utils.IOSupplier; import com.oberasoftware.home.zwave.exceptions.RuntimeAutomationException; +import com.oberasoftware.home.zwave.exceptions.ZWaveConfigurationException; import com.oberasoftware.home.zwave.exceptions.ZWaveException; import com.oberasoftware.home.zwave.threading.ReceiverThread; import com.oberasoftware.home.zwave.threading.SenderThread; -import gnu.io.CommPort; -import gnu.io.CommPortIdentifier; -import gnu.io.NoSuchPortException; -import gnu.io.PortInUseException; -import gnu.io.SerialPort; -import gnu.io.UnsupportedCommOperationException; +import gnu.io.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import org.springframework.util.StringUtils; import javax.annotation.PreDestroy; import java.io.IOException; @@ -34,7 +31,7 @@ public class SerialZWaveConnector implements ControllerConnector { private static final Logger LOG = LoggerFactory.getLogger(SerialZWaveConnector.class); - @Value("${zwave.serial.port}") + @Value("${zwave.serial.port:}") private String portName; private SerialPort serialPort; @@ -53,6 +50,10 @@ public class SerialZWaveConnector implements ControllerConnector { * @throws ZWaveException if unable to connect to the serial device */ public synchronized void connect() throws ZWaveException { + if(StringUtils.isEmpty(portName)) { + throw new ZWaveConfigurationException("No port configured for ZWave"); + } + if(!isConnected) { LOG.info("Connecting to ZWave serial port device: {}", portName); try { diff --git a/src/main/java/com/oberasoftware/home/zwave/exceptions/ZWaveConfigurationException.java b/src/main/java/com/oberasoftware/home/zwave/exceptions/ZWaveConfigurationException.java new file mode 100644 index 0000000..3c0be17 --- /dev/null +++ b/src/main/java/com/oberasoftware/home/zwave/exceptions/ZWaveConfigurationException.java @@ -0,0 +1,10 @@ +package com.oberasoftware.home.zwave.exceptions; + +/** + * @author Renze de Vries + */ +public class ZWaveConfigurationException extends ZWaveException { + public ZWaveConfigurationException(String message) { + super(message); + } +} diff --git a/src/test/resources/logback.xml b/src/test/resources/logback.xml index 10786f4..5da2eb3 100755 --- a/src/test/resources/logback.xml +++ b/src/test/resources/logback.xml @@ -9,25 +9,7 @@ - - - - - - - - - - - - - - - - - - - +