[omnikinverter]fix missing dispose (#9010)

* fix missing dispose

Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
J-N-K 2020-11-15 00:27:21 +01:00 committed by GitHub
parent 9e7836b4f9
commit d055c057d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 32 deletions

View File

@ -12,10 +12,8 @@
*/
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();
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);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
outputStream.write(magic);
outputStream.write(serialBytes);
outputStream.write(0x01);
outputStream.write(0x00);
outputStream.write(checksum);
outputStream.write(0x16);
return outputStream.toByteArray();
}
return result;
}
}

View 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() {