From e4b432bb6b4f8bce52513cc4cbaf53dd271d9b2b Mon Sep 17 00:00:00 2001 From: clinique Date: Sat, 21 Dec 2024 15:20:10 +0100 Subject: [PATCH] Ensure ressources are freed. A bit of code revamp Signed-off-by: clinique --- .../handler/Ipx800DeviceConnector.java | 7 ++++- .../gce/internal/handler/Ipx800v3Handler.java | 14 +++++---- .../gce/internal/model/M2MMessageParser.java | 10 +++++-- .../binding/gce/internal/model/PortData.java | 8 ++--- .../gce/internal/model/PortDefinition.java | 30 +++++-------------- .../internal/model/StatusFileInterpreter.java | 18 +++++------ 6 files changed, 42 insertions(+), 45 deletions(-) diff --git a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800DeviceConnector.java b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800DeviceConnector.java index 965347d440b..ceb623504e2 100644 --- a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800DeviceConnector.java +++ b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800DeviceConnector.java @@ -120,6 +120,7 @@ public class Ipx800DeviceConnector extends Thread { public void dispose() { interrupt(); disconnect(); + releaseParser(); } /** @@ -185,6 +186,10 @@ public class Ipx800DeviceConnector extends Thread { } public void setParser(M2MMessageParser parser) { - this.messageParser = Optional.of(parser); + messageParser = Optional.of(parser); + } + + public void releaseParser() { + messageParser = Optional.empty(); } } diff --git a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800v3Handler.java b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800v3Handler.java index d69902345f4..783555ca6b2 100644 --- a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800v3Handler.java +++ b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/handler/Ipx800v3Handler.java @@ -15,8 +15,7 @@ package org.openhab.binding.gce.internal.handler; import static org.openhab.binding.gce.internal.GCEBindingConstants.*; import java.time.Duration; -import java.time.ZoneId; -import java.time.ZonedDateTime; +import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.ArrayList; import java.util.Collection; @@ -82,7 +81,7 @@ public class Ipx800v3Handler extends BaseThingHandler implements Ipx800EventList private final Map portDatas = new HashMap<>(); private class LongPressEvaluator implements Runnable { - private final ZonedDateTime referenceTime; + private final Instant referenceTime; private final String port; private final String eventChannelId; @@ -121,7 +120,7 @@ public class Ipx800v3Handler extends BaseThingHandler implements Ipx800EventList } List channels = new ArrayList<>(getThing().getChannels()); - PortDefinition.asStream().forEach(portDefinition -> { + PortDefinition.AS_STREAM.forEach(portDefinition -> { int nbElements = statusFile.getMaxNumberofNodeType(portDefinition); for (int i = 0; i < nbElements; i++) { ChannelUID portChannelUID = createChannels(portDefinition, i, channels); @@ -150,9 +149,12 @@ public class Ipx800v3Handler extends BaseThingHandler implements Ipx800EventList connector.ifPresent(Ipx800DeviceConnector::dispose); connector = Optional.empty(); + parser.ifPresent(M2MMessageParser::dispose); parser = Optional.empty(); portDatas.values().stream().forEach(PortData::dispose); + portDatas.clear(); + super.dispose(); } @@ -209,7 +211,7 @@ public class Ipx800v3Handler extends BaseThingHandler implements Ipx800EventList } private boolean ignoreCondition(double newValue, PortData portData, Configuration configuration, - PortDefinition portDefinition, ZonedDateTime now) { + PortDefinition portDefinition, Instant now) { if (!portData.isInitializing()) { // Always accept if portData is not initialized double prevValue = portData.getValue(); if (newValue == prevValue) { // Always reject if the value did not change @@ -237,7 +239,7 @@ public class Ipx800v3Handler extends BaseThingHandler implements Ipx800EventList String groupId = channel.getUID().getGroupId(); PortData portData = portDatas.get(channelId); if (portData != null && groupId != null) { - ZonedDateTime now = ZonedDateTime.now(ZoneId.systemDefault()); + Instant now = Instant.now(); long sinceLastChange = Duration.between(portData.getTimestamp(), now).toMillis(); Configuration configuration = channel.getConfiguration(); PortDefinition portDefinition = PortDefinition.fromGroupId(groupId); diff --git a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/M2MMessageParser.java b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/M2MMessageParser.java index 1b4d97d8106..b982136724e 100644 --- a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/M2MMessageParser.java +++ b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/M2MMessageParser.java @@ -44,6 +44,10 @@ public class M2MMessageParser { connector.setParser(this); } + public void dispose() { + connector.releaseParser(); + } + /** * * @param data @@ -67,7 +71,7 @@ public class M2MMessageParser { portNumShift = 0; // Align counters on 1 based array case ANALOG: { int portNumber = Integer.parseInt(statusPart[0].substring(1)) + portNumShift; - setStatus(portDefinition.getPortName() + portNumber, Double.parseDouble(statusPart[1])); + setStatus(portDefinition.portName + portNumber, Double.parseDouble(statusPart[1])); } } } @@ -80,7 +84,7 @@ public class M2MMessageParser { private void decodeDataLine(PortDefinition portDefinition, String data) { for (int count = 0; count < data.length(); count++) { - setStatus(portDefinition.getPortName() + (count + 1), (double) data.charAt(count) - '0'); + setStatus(portDefinition.portName + (count + 1), (double) data.charAt(count) - '0'); } } @@ -94,7 +98,7 @@ public class M2MMessageParser { this.expectedResponse = expectedResponse; } else { // GetAnx or GetCountx PortDefinition portType = PortDefinition.fromM2MCommand(expectedResponse); - this.expectedResponse = expectedResponse.replaceAll(portType.getM2mCommand(), portType.getPortName()); + this.expectedResponse = expectedResponse.replaceAll(portType.m2mCommand, portType.portName); } } diff --git a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortData.java b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortData.java index 8e21165bdfe..d155e284c33 100644 --- a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortData.java +++ b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortData.java @@ -12,7 +12,7 @@ */ package org.openhab.binding.gce.internal.model; -import java.time.ZonedDateTime; +import java.time.Instant; import java.util.Optional; import java.util.concurrent.ScheduledFuture; @@ -26,7 +26,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault; @NonNullByDefault public class PortData { private double value = -1; - private ZonedDateTime timestamp = ZonedDateTime.now(); + private Instant timestamp = Instant.now(); private Optional> pulsing = Optional.empty(); public void cancelPulsing() { @@ -38,7 +38,7 @@ public class PortData { cancelPulsing(); } - public void setData(double value, ZonedDateTime timestamp) { + public void setData(double value, Instant timestamp) { this.value = value; this.timestamp = timestamp; } @@ -47,7 +47,7 @@ public class PortData { return value; } - public ZonedDateTime getTimestamp() { + public Instant getTimestamp() { return timestamp; } diff --git a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortDefinition.java b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortDefinition.java index f6a2cba01be..26ea30be311 100644 --- a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortDefinition.java +++ b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/PortDefinition.java @@ -29,9 +29,9 @@ public enum PortDefinition { RELAY("led", "O", "GetOut", 8), CONTACT("btn", "I", "GetIn", 8); - private final String nodeName; // Name used in the status xml file - private final String portName; // Name used by the M2M protocol - private final String m2mCommand; // associated M2M command + public final String nodeName; // Name used in the status xml file + public final String portName; // Name used by the M2M protocol + public final String m2mCommand; // associated M2M command private final int quantity; // base number of ports PortDefinition(String nodeName, String portName, String m2mCommand, int quantity) { @@ -41,13 +41,7 @@ public enum PortDefinition { this.quantity = quantity; } - public String getNodeName() { - return nodeName; - } - - public String getPortName() { - return portName; - } + public static final Stream AS_STREAM = Stream.of(PortDefinition.values()); @Override public String toString() { @@ -58,20 +52,12 @@ public enum PortDefinition { return id >= quantity; } - public String getM2mCommand() { - return m2mCommand; - } - - public static Stream asStream() { - return Stream.of(PortDefinition.values()); - } - public static PortDefinition fromM2MCommand(String m2mCommand) { - return asStream().filter(v -> m2mCommand.startsWith(v.m2mCommand)).findFirst().get(); + return AS_STREAM.filter(v -> m2mCommand.startsWith(v.m2mCommand)).findFirst().get(); } public static PortDefinition fromPortName(String portName) { - return asStream().filter(v -> portName.startsWith(v.portName)).findFirst().get(); + return AS_STREAM.filter(v -> portName.startsWith(v.portName)).findFirst().get(); } public static PortDefinition fromGroupId(String groupId) { @@ -80,7 +66,7 @@ public enum PortDefinition { public static String asChannelId(String portDefinition) { String portKind = portDefinition.substring(0, 1); - PortDefinition result = asStream().filter(v -> v.portName.startsWith(portKind)).findFirst().get(); - return result.toString() + "#" + portDefinition.substring(1); + PortDefinition result = AS_STREAM.filter(v -> v.portName.startsWith(portKind)).findFirst().get(); + return "%s#%s".formatted(result.toString(), portDefinition.substring(1)); } } diff --git a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/StatusFileInterpreter.java b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/StatusFileInterpreter.java index 652dc82bca7..99012270f63 100644 --- a/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/StatusFileInterpreter.java +++ b/bundles/org.openhab.binding.gce/src/main/java/org/openhab/binding/gce/internal/model/StatusFileInterpreter.java @@ -19,9 +19,9 @@ import java.util.Comparator; import java.util.List; import java.util.Objects; import java.util.Optional; -import java.util.stream.Collectors; import java.util.stream.IntStream; +import javax.ws.rs.HttpMethod; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -78,7 +78,7 @@ public class StatusFileInterpreter { public void read() { try { - String statusPage = HttpUtil.executeUrl("GET", url, 5000); + String statusPage = HttpUtil.executeUrl(HttpMethod.GET, url, 5000); InputStream inputStream = new ByteArrayInputStream(statusPage.getBytes()); Document document = builder.parse(inputStream); document.getDocumentElement().normalize(); @@ -92,13 +92,13 @@ public class StatusFileInterpreter { private void pushDatas() { getRoot().ifPresent(root -> { - PortDefinition.asStream().forEach(portDefinition -> { - List xmlNodes = getMatchingNodes(root.getChildNodes(), portDefinition.getNodeName()); + PortDefinition.AS_STREAM.forEach(portDefinition -> { + List xmlNodes = getMatchingNodes(root.getChildNodes(), portDefinition.nodeName); xmlNodes.forEach(xmlNode -> { - String sPortNum = xmlNode.getNodeName().replace(portDefinition.getNodeName(), ""); + String sPortNum = xmlNode.getNodeName().replace(portDefinition.nodeName, ""); int portNum = Integer.parseInt(sPortNum) + 1; double value = Double.parseDouble(xmlNode.getTextContent().replace("dn", "1").replace("up", "0")); - listener.dataReceived(String.format("%s%d", portDefinition.getPortName(), portNum), value); + listener.dataReceived("%s%d".formatted(portDefinition.portName, portNum), value); }); }); }); @@ -113,12 +113,12 @@ public class StatusFileInterpreter { private List getMatchingNodes(NodeList nodeList, String criteria) { return IntStream.range(0, nodeList.getLength()).boxed().map(nodeList::item) .filter(node -> node.getNodeName().startsWith(criteria)).sorted(Comparator.comparing(Node::getNodeName)) - .collect(Collectors.toList()); + .toList(); } public int getMaxNumberofNodeType(PortDefinition portDefinition) { - return getRoot().map(root -> getMatchingNodes(root.getChildNodes(), portDefinition.getNodeName()).size()) - .orElse(0); + return Objects.requireNonNull(getRoot() + .map(root -> getMatchingNodes(root.getChildNodes(), portDefinition.nodeName).size()).orElse(0)); } private Optional getRoot() {