*/
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;
@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];
}
}
- 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;
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;
}
}
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;
*/
@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);
}
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() {