mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-10 15:11:59 +01:00
[omnikinverter]fix missing dispose (#9010)
* fix missing dispose Signed-off-by: Jan N. Klug <jan.n.klug@rub.de>
This commit is contained in:
parent
9e7836b4f9
commit
d055c057d7
@ -12,10 +12,8 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.binding.omnikinverter.internal;
|
package org.openhab.binding.omnikinverter.internal;
|
||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
import org.apache.commons.lang.ArrayUtils;
|
import org.apache.commons.lang.ArrayUtils;
|
||||||
@ -29,19 +27,19 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
|||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class OmnikInverter {
|
public class OmnikInverter {
|
||||||
|
|
||||||
private int serialNumber;
|
private final int serialNumber;
|
||||||
private String host;
|
private final String host;
|
||||||
private int port;
|
private final int port;
|
||||||
private byte[] magicPacket;
|
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.host = host;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
this.serialNumber = serialNumber;
|
this.serialNumber = serialNumber;
|
||||||
this.magicPacket = generateMagicPacket();
|
this.magicPacket = generateMagicPacket();
|
||||||
}
|
}
|
||||||
|
|
||||||
public OmnikInverterMessage pullCurrentStats() throws UnknownHostException, IOException {
|
public OmnikInverterMessage pullCurrentStats() throws IOException {
|
||||||
byte[] magicPacket = this.magicPacket;
|
byte[] magicPacket = this.magicPacket;
|
||||||
byte[] returnMessage = new byte[1024];
|
byte[] returnMessage = new byte[1024];
|
||||||
|
|
||||||
@ -54,12 +52,9 @@ public class OmnikInverter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte[] generateMagicPacket() throws IOException {
|
private byte[] generateMagicPacket() {
|
||||||
byte[] magic = { 0x68, 0x02, 0x40, 0x30 };
|
|
||||||
|
|
||||||
ByteBuffer serialByteBuffer = ByteBuffer.allocate(8).putInt(serialNumber).putInt(serialNumber);
|
ByteBuffer serialByteBuffer = ByteBuffer.allocate(8).putInt(serialNumber).putInt(serialNumber);
|
||||||
byte[] serialBytes = serialByteBuffer.array();
|
byte[] serialBytes = serialByteBuffer.array();
|
||||||
// Reverse serialBytes in a very mutable way.
|
|
||||||
ArrayUtils.reverse(serialBytes);
|
ArrayUtils.reverse(serialBytes);
|
||||||
|
|
||||||
byte checksumCount = 115;
|
byte checksumCount = 115;
|
||||||
@ -67,17 +62,11 @@ public class OmnikInverter {
|
|||||||
checksumCount += (char) b;
|
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()) {
|
return result;
|
||||||
outputStream.write(magic);
|
|
||||||
outputStream.write(serialBytes);
|
|
||||||
outputStream.write(0x01);
|
|
||||||
outputStream.write(0x00);
|
|
||||||
outputStream.write(checksum);
|
|
||||||
outputStream.write(0x16);
|
|
||||||
|
|
||||||
return outputStream.toByteArray();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ import java.io.IOException;
|
|||||||
import java.net.ConnectException;
|
import java.net.ConnectException;
|
||||||
import java.net.NoRouteToHostException;
|
import java.net.NoRouteToHostException;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.concurrent.ScheduledFuture;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.measure.quantity.Power;
|
import javax.measure.quantity.Power;
|
||||||
@ -46,10 +47,11 @@ import org.slf4j.LoggerFactory;
|
|||||||
*/
|
*/
|
||||||
@NonNullByDefault
|
@NonNullByDefault
|
||||||
public class OmnikInverterHandler extends BaseThingHandler {
|
public class OmnikInverterHandler extends BaseThingHandler {
|
||||||
private @Nullable OmnikInverter inverter;
|
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
|
private final Logger logger = LoggerFactory.getLogger(OmnikInverterHandler.class);
|
||||||
|
|
||||||
|
private @Nullable OmnikInverter inverter;
|
||||||
|
private @Nullable ScheduledFuture<?> pollJob;
|
||||||
|
|
||||||
public OmnikInverterHandler(Thing thing) {
|
public OmnikInverterHandler(Thing thing) {
|
||||||
super(thing);
|
super(thing);
|
||||||
}
|
}
|
||||||
@ -67,14 +69,19 @@ public class OmnikInverterHandler extends BaseThingHandler {
|
|||||||
public void initialize() {
|
public void initialize() {
|
||||||
OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
|
OmnikInverterConfiguration config = getConfigAs(OmnikInverterConfiguration.class);
|
||||||
|
|
||||||
try {
|
inverter = new OmnikInverter(config.hostname, config.port, config.serial);
|
||||||
inverter = new OmnikInverter(config.hostname, config.port, config.serial);
|
updateStatus(ThingStatus.UNKNOWN);
|
||||||
scheduler.scheduleWithFixedDelay(this::updateData, 0, 10, TimeUnit.SECONDS);
|
pollJob = 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,
|
@Override
|
||||||
"Failed to initialize: " + e.getMessage());
|
public void dispose() {
|
||||||
|
ScheduledFuture<?> pollJob = this.pollJob;
|
||||||
|
if (pollJob != null) {
|
||||||
|
pollJob.cancel(true);
|
||||||
|
this.pollJob = null;
|
||||||
}
|
}
|
||||||
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateData() {
|
private void updateData() {
|
||||||
|
Loading…
Reference in New Issue
Block a user