From bde3d2b7dce707a863eafa496bd10e55b6dc7cd2 Mon Sep 17 00:00:00 2001 From: Stefan Triller Date: Sat, 23 May 2020 15:01:50 +0200 Subject: [PATCH] Address review comments Signed-off-by: Stefan Triller --- .../README.md | 2 +- .../org.openhab.binding.novafinedust/pom.xml | 2 +- .../novafinedust/internal/SDS011Handler.java | 28 ++++--------------- .../sds011protocol/SDS011Communicator.java | 17 ++++++++--- 4 files changed, 21 insertions(+), 28 deletions(-) diff --git a/bundles/org.openhab.binding.novafinedust/README.md b/bundles/org.openhab.binding.novafinedust/README.md index 8bdcb0be539b4..51426f2b807d2 100644 --- a/bundles/org.openhab.binding.novafinedust/README.md +++ b/bundles/org.openhab.binding.novafinedust/README.md @@ -31,7 +31,7 @@ A full overview about the parameters of the `SDS011` thing is given in the follo | parameter name | mandatory | description | |-------------------|-----------|---------------------------------------------------------------------------------------| -| port | yes | the port the sensor is connected to, i.e. /detv/ttyUSB0. | +| port | yes | the port the sensor is connected to, i.e. /dev/ttyUSB0. | | reporting | yes | whether the reporting mode (value=true) or polling mode should be used. | | reportingInterval | no | the time in minutes between reportings from the sensor (default=1, min=0, max=30). | | pollingInterval | no | the time in seconds between data polls from the device. (default=10, min=3, max=3600) | diff --git a/bundles/org.openhab.binding.novafinedust/pom.xml b/bundles/org.openhab.binding.novafinedust/pom.xml index 66de5f6112bca..06f7b28af079a 100644 --- a/bundles/org.openhab.binding.novafinedust/pom.xml +++ b/bundles/org.openhab.binding.novafinedust/pom.xml @@ -6,7 +6,7 @@ org.openhab.addons.bundles org.openhab.addons.reactor.bundles - 2.5.5-SNAPSHOT + 2.5.6-SNAPSHOT org.openhab.binding.novafinedust diff --git a/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/SDS011Handler.java b/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/SDS011Handler.java index f5ae09596f21c..8be18dc43bfd6 100644 --- a/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/SDS011Handler.java +++ b/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/SDS011Handler.java @@ -15,8 +15,6 @@ import java.io.IOException; import java.time.Duration; import java.time.ZonedDateTime; -import java.time.chrono.ChronoZonedDateTime; -import java.time.temporal.Temporal; import java.util.TooManyListenersException; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -61,11 +59,11 @@ public class SDS011Handler extends BaseThingHandler { private @Nullable ScheduledFuture pollingJob; private @Nullable ScheduledFuture connectionMonitor; - private Temporal lastCommunication = ZonedDateTime.now(); + private ZonedDateTime lastCommunication = ZonedDateTime.now(); // initialize timeBetweenDataShouldArrive with a large number private Duration timeBetweenDataShouldArrive = Duration.ofDays(1); - private final Duration dataCanBeLateTollerance = Duration.ofSeconds(5); + private final Duration dataCanBeLateTolerance = Duration.ofSeconds(5); public SDS011Handler(Thing thing, SerialPortManager serialPortManager) { super(thing); @@ -159,20 +157,6 @@ private boolean validateConfiguration() { updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, "Port must be set!"); return false; } - - if (config.reporting) { - if (config.reportingInterval < 0 || config.reportingInterval > 30) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, - "Reporting interval has to be between 0 and 30 minutes"); - return false; - } - } else { - if (config.pollingInterval < 3 || config.pollingInterval > 3600) { - updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.CONFIGURATION_ERROR, - "Polling interval has to be between 3 and 3600 seconds"); - return false; - } - } return true; } @@ -218,10 +202,10 @@ public void updateChannels(SensorMeasuredDataReply sensorData) { private void verifyIfStillConnected() { ZonedDateTime now = ZonedDateTime.now(); - Temporal lastData = lastCommunication.plus(timeBetweenDataShouldArrive).plus(dataCanBeLateTollerance); - if (now.isAfter((ChronoZonedDateTime) lastData)) { + ZonedDateTime lastData = lastCommunication.plus(timeBetweenDataShouldArrive).plus(dataCanBeLateTolerance); + if (now.isAfter(lastData)) { logger.debug("Check Alive timer: Timeout: lastCommunication={}, interval={}, tollerance={}", - lastCommunication, timeBetweenDataShouldArrive, dataCanBeLateTollerance); + lastCommunication, timeBetweenDataShouldArrive, dataCanBeLateTolerance); updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.OFFLINE.COMMUNICATION_ERROR, "Check connection cable and afterwards disable and enable this thing to make it work again"); // in case someone has pulled the plug, we dispose ourselves and the user has to deactivate/activate the @@ -229,7 +213,7 @@ private void verifyIfStillConnected() { dispose(); } else { logger.trace("Check Alive timer: All OK: lastCommunication={}, interval={}, tollerance={}", - lastCommunication, timeBetweenDataShouldArrive, dataCanBeLateTollerance); + lastCommunication, timeBetweenDataShouldArrive, dataCanBeLateTolerance); } } diff --git a/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/sds011protocol/SDS011Communicator.java b/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/sds011protocol/SDS011Communicator.java index 8a1dc4f8063e2..6463a69890db7 100644 --- a/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/sds011protocol/SDS011Communicator.java +++ b/bundles/org.openhab.binding.novafinedust/src/main/java/org/openhab/binding/novafinedust/internal/sds011protocol/SDS011Communicator.java @@ -19,7 +19,6 @@ import java.util.Arrays; import java.util.TooManyListenersException; -import org.apache.commons.io.IOUtils; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.io.transport.serial.PortInUseException; @@ -253,7 +252,7 @@ public void serialEvent(SerialPortEvent event) { reply = readReply(); logger.debug("Got data from sensor: {}", reply); } catch (IOException e) { - logger.error("Could not read available data from the serial port", e); + logger.warn("Could not read available data from the serial port", e); } if (reply instanceof SensorMeasuredDataReply) { SensorMeasuredDataReply sensorData = (SensorMeasuredDataReply) reply; @@ -281,7 +280,17 @@ public void dispose() { serialPort = null; } } - IOUtils.closeQuietly(inputStream); - IOUtils.closeQuietly(outputStream); + + try { + inputStream.close(); + } catch (IOException e) { + logger.debug("Error while closing the input stream: {}", e.getMessage()); + } + + try { + outputStream.close(); + } catch (IOException e) { + logger.debug("Error while closing the output stream: {}", e.getMessage()); + } } }