2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.omnikinverter.internal;
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.nio.ByteBuffer;
19 import org.apache.commons.lang3.ArrayUtils;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
24 * @author Hans van den Bogert - Initial Contribution
28 public class OmnikInverter {
30 private final int serialNumber;
31 private final String host;
32 private final int port;
33 private final byte[] magicPacket;
35 public OmnikInverter(String host, int port, int serialNumber) {
38 this.serialNumber = serialNumber;
39 this.magicPacket = generateMagicPacket();
42 public OmnikInverterMessage pullCurrentStats() throws IOException {
43 byte[] magicPacket = this.magicPacket;
44 byte[] returnMessage = new byte[1024];
46 try (Socket socket = new Socket(host, port)) {
47 socket.setSoTimeout(5000);
48 socket.getOutputStream().write(magicPacket);
49 socket.getInputStream().read(returnMessage);
51 return new OmnikInverterMessage(returnMessage);
55 private byte[] generateMagicPacket() {
56 ByteBuffer serialByteBuffer = ByteBuffer.allocate(8).putInt(serialNumber).putInt(serialNumber);
57 byte[] serialBytes = serialByteBuffer.array();
58 ArrayUtils.reverse(serialBytes);
60 byte checksumCount = 115;
61 for (byte b : serialBytes) {
62 checksumCount += (char) b;
65 byte[] result = new byte[16];
66 System.arraycopy(new byte[] { 0x68, 0x02, 0x40, 0x30 }, 0, result, 0, 4);
67 System.arraycopy(serialBytes, 0, result, 4, 8);
68 System.arraycopy(new byte[] { 0x01, 0x00, checksumCount, 0x16 }, 0, result, 12, 4);