2 * Copyright (c) 2010-2023 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.eclipse.jdt.annotation.NonNullByDefault;
23 * @author Hans van den Bogert - Initial Contribution
27 public class OmnikInverter {
29 private final int serialNumber;
30 private final String host;
31 private final int port;
32 private final byte[] magicPacket;
34 public OmnikInverter(String host, int port, int serialNumber) {
37 this.serialNumber = serialNumber;
38 this.magicPacket = generateMagicPacket();
41 public OmnikInverterMessage pullCurrentStats() throws IOException {
42 byte[] magicPacket = this.magicPacket;
43 byte[] returnMessage = new byte[1024];
45 try (Socket socket = new Socket(host, port)) {
46 socket.setSoTimeout(5000);
47 socket.getOutputStream().write(magicPacket);
48 socket.getInputStream().read(returnMessage);
50 return new OmnikInverterMessage(returnMessage);
54 private byte[] generateMagicPacket() {
55 ByteBuffer serialByteBuffer = ByteBuffer.allocate(8).putInt(serialNumber).putInt(serialNumber);
56 byte[] serialBytes = serialByteBuffer.array();
59 for (int i = 0; i < serialBytes.length / 2; i++) {
60 byte temp = serialBytes[i];
61 serialBytes[i] = serialBytes[serialBytes.length - i - 1];
62 serialBytes[serialBytes.length - i - 1] = temp;
65 byte checksumCount = 115;
66 for (byte b : serialBytes) {
67 checksumCount += (char) b;
70 byte[] result = new byte[16];
71 System.arraycopy(new byte[] { 0x68, 0x02, 0x40, 0x30 }, 0, result, 0, 4);
72 System.arraycopy(serialBytes, 0, result, 4, 8);
73 System.arraycopy(new byte[] { 0x01, 0x00, checksumCount, 0x16 }, 0, result, 12, 4);