]> git.basschouten.com Git - openhab-addons.git/commitdiff
[omnikinverter]fix missing dispose (#9010)
authorJ-N-K <J-N-K@users.noreply.github.com>
Sat, 14 Nov 2020 23:27:21 +0000 (00:27 +0100)
committerGitHub <noreply@github.com>
Sat, 14 Nov 2020 23:27:21 +0000 (15:27 -0800)
* fix missing dispose

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/OmnikInverter.java
bundles/org.openhab.binding.omnikinverter/src/main/java/org/openhab/binding/omnikinverter/internal/handler/OmnikInverterHandler.java

index 7281c69ebea72ce41050ddc1a672e9b56270e1bc..8a7894d692b94be746e0403bf98f2bab07213202 100644 (file)
  */
 package org.openhab.binding.omnikinverter.internal;
 
-import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.net.Socket;
-import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
 
 import org.apache.commons.lang.ArrayUtils;
@@ -29,19 +27,19 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
 @NonNullByDefault
 public class OmnikInverter {
 
-    private int serialNumber;
-    private String host;
-    private int port;
-    private byte[] magicPacket;
+    private final int serialNumber;
+    private final String host;
+    private final int port;
+    private final byte[] magicPacket;
 
-    public OmnikInverter(String host, int port, int serialNumber) throws IOException {
+    public OmnikInverter(String host, int port, int serialNumber) {
         this.host = host;
         this.port = port;
         this.serialNumber = serialNumber;
         this.magicPacket = generateMagicPacket();
     }
 
-    public OmnikInverterMessage pullCurrentStats() throws UnknownHostException, IOException {
+    public OmnikInverterMessage pullCurrentStats() throws IOException {
         byte[] magicPacket = this.magicPacket;
         byte[] returnMessage = new byte[1024];
 
@@ -54,12 +52,9 @@ public class OmnikInverter {
         }
     }
 
-    private byte[] generateMagicPacket() throws IOException {
-        byte[] magic = { 0x68, 0x02, 0x40, 0x30 };
-
+    private byte[] generateMagicPacket() {
         ByteBuffer serialByteBuffer = ByteBuffer.allocate(8).putInt(serialNumber).putInt(serialNumber);
         byte[] serialBytes = serialByteBuffer.array();
-        // Reverse serialBytes in a very mutable way.
         ArrayUtils.reverse(serialBytes);
 
         byte checksumCount = 115;
@@ -67,17 +62,11 @@ public class OmnikInverter {
             checksumCount += (char) b;
         }
 
-        byte[] checksum = ByteBuffer.allocate(1).put(checksumCount).array();
-
-        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
-            outputStream.write(magic);
-            outputStream.write(serialBytes);
-            outputStream.write(0x01);
-            outputStream.write(0x00);
-            outputStream.write(checksum);
-            outputStream.write(0x16);
+        byte[] result = new byte[16];
+        System.arraycopy(new byte[] { 0x68, 0x02, 0x40, 0x30 }, 0, result, 0, 4);
+        System.arraycopy(serialBytes, 0, result, 4, 8);
+        System.arraycopy(new byte[] { 0x01, 0x00, checksumCount, 0x16 }, 0, result, 12, 4);
 
-            return outputStream.toByteArray();
-        }
+        return result;
     }
 }
index 051b07775a1f382ced56c279a0aa86ba9f65c25e..e0bf649033346891997194ed726b82114ec5fcaf 100644 (file)
@@ -16,6 +16,7 @@ import java.io.IOException;
 import java.net.ConnectException;
 import java.net.NoRouteToHostException;
 import java.net.UnknownHostException;
+import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.TimeUnit;
 
 import javax.measure.quantity.Power;
@@ -46,10 +47,11 @@ import org.slf4j.LoggerFactory;
  */
 @NonNullByDefault
 public class OmnikInverterHandler extends BaseThingHandler {
-    private @Nullable OmnikInverter inverter;
-
     private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
 
+    private @Nullable OmnikInverter inverter;
+    private @Nullable ScheduledFuture<?> pollJob;
+
     public OmnikInverterHandler(Thing thing) {
         super(thing);
     }
@@ -67,14 +69,19 @@ public class OmnikInverterHandler extends BaseThingHandler {
     public void initialize() {
         OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
 
-        try {
-            inverter = new OmnikInverter(config.hostname, config.port, config.serial);
-            scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
-        } catch (IOException e) {
-            logger.debug("Could not instantiate OmnikInverter object: {}", e.getMessage());
-            updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.HANDLER_INITIALIZING_ERROR,
-                    "Failed to initialize: " + e.getMessage());
+        inverter = new OmnikInverter(config.hostname, config.port, config.serial);
+        updateStatus(ThingStatus.UNKNOWN);
+        pollJob = scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
+    }
+
+    @Override
+    public void dispose() {
+        ScheduledFuture<?> pollJob = this.pollJob;
+        if (pollJob != null) {
+            pollJob.cancel(true);
+            this.pollJob = null;
         }
+        super.dispose();
     }
 
     private void updateData() {