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

[novafinedust] Nova Fine Dust binding for SDS011 sensors #7528

Merged
merged 13 commits into from
Jun 14, 2020
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.novafinedust/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
t2000 marked this conversation as resolved.
Show resolved Hide resolved
| 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) |
Expand Down
2 changes: 1 addition & 1 deletion bundles/org.openhab.binding.novafinedust/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>2.5.5-SNAPSHOT</version>
<version>2.5.6-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.binding.novafinedust</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -218,18 +202,18 @@ 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
// thing once the cable is plugged in again
dispose();
} else {
logger.trace("Check Alive timer: All OK: lastCommunication={}, interval={}, tollerance={}",
lastCommunication, timeBetweenDataShouldArrive, dataCanBeLateTollerance);
lastCommunication, timeBetweenDataShouldArrive, dataCanBeLateTolerance);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to log the stacktrace here, but you could log the exception message:

Suggested change
logger.warn("Could not read available data from the serial port", e);
logger.warn("Could not read available data from the serial port: {}", e.getMessage());

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this makes sense to only log the message. I have changed it, thanks!

}
if (reply instanceof SensorMeasuredDataReply) {
SensorMeasuredDataReply sensorData = (SensorMeasuredDataReply) reply;
Expand Down Expand Up @@ -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());
}
}
}