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.solax.internal.connectivity.rawdata;
15 import java.util.Arrays;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.solax.internal.model.InverterData;
20 import org.openhab.binding.solax.internal.model.InverterType;
21 import org.openhab.binding.solax.internal.util.GsonSupplier;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 import com.google.gson.Gson;
26 import com.google.gson.annotations.SerializedName;
29 * The {@link LocalConnectRawDataBean} collects the raw data and the specific implementation to return the parsed data.
30 * If there are differences between the inverters probably would be wise to split the parsing in seprate class(es)
32 * @author Konstantin Polihronov - Initial contribution
35 public class LocalConnectRawDataBean implements RawDataBean, InverterData {
37 private final Logger logger = LoggerFactory.getLogger(LocalConnectRawDataBean.class);
39 private @Nullable String sn;
40 private @Nullable String ver;
42 @SerializedName("Data")
43 private short @Nullable [] data;
44 @SerializedName("Information")
45 private String @Nullable [] information;
46 private @Nullable String rawData;
49 public String toString() {
50 return "LocalConnectRawDataBean [sn=" + sn + ", ver=" + ver + ", type=" + type + ", Information="
51 + Arrays.toString(information) + ", Data=" + Arrays.toString(data) + "]";
54 public @Nullable String getSn() {
58 public void setSn(@Nullable String sn) {
62 public @Nullable String getVer() {
66 public void setVer(@Nullable String ver) {
70 public int getType() {
74 public void setType(int type) {
78 public short @Nullable [] getData() {
82 public void setData(short @Nullable [] data) {
86 public String @Nullable [] getInformation() {
90 public void setInformation(String @Nullable [] information) {
91 this.information = information;
95 public @Nullable String getRawData() {
99 public void setRawData(String rawData) {
100 this.rawData = rawData;
103 public static LocalConnectRawDataBean fromJson(String json) {
104 if (json.isEmpty()) {
105 throw new IllegalArgumentException("JSON payload should not be empty");
108 Gson gson = GsonSupplier.getInstance();
109 LocalConnectRawDataBean deserializedObject = gson.fromJson(json, LocalConnectRawDataBean.class);
110 if (deserializedObject == null) {
111 throw new IllegalStateException("Unexpected null result when deserializing JSON");
113 deserializedObject.setRawData(json);
114 return deserializedObject;
117 // Parsed inverter data interface implementation starts here
120 public @Nullable String getWifiSerial() {
125 public @Nullable String getWifiVersion() {
130 public InverterType getInverterType() {
131 return InverterType.fromIndex(type);
135 public short getInverterVoltage() {
136 return (short) (getData(0) / 10);
140 public short getInverterCurrent() {
141 return (short) (getData(1) / 10);
145 public short getInverterOutputPower() {
150 public short getInverterFrequency() {
151 return (short) (getData(3) / 100);
155 public short getPV1Voltage() {
156 return (short) (getData(4) / 10);
160 public short getPV1Current() {
161 return (short) (getData(6) / 10);
165 public short getPV1Power() {
170 public short getPV2Voltage() {
171 return (short) (getData(5) / 10);
175 public short getPV2Current() {
176 return (short) (getData(7) / 10);
180 public short getPV2Power() {
185 public short getBatteryVoltage() {
186 return (short) (getData(14) / 100);
190 public short getBatteryCurrent() {
191 return (short) (getData(15) / 100);
195 public short getBatteryPower() {
200 public short getBatteryTemperature() {
205 public short getBatterySoC() {
210 public long getOnGridTotalYield() {
211 return packU16(11, 12) / 100;
215 public short getOnGridDailyYield() {
216 return (short) (getData(13) / 10);
220 public short getFeedInPower() {
225 public long getTotalFeedInEnergy() {
226 return packU16(34, 35) / 100;
230 public long getTotalConsumption() {
231 return packU16(36, 37) / 100;
234 private short getData(int index) {
236 short[] dataArray = data;
237 if (dataArray != null) {
238 return dataArray[index];
240 } catch (IndexOutOfBoundsException e) {
241 logger.debug("Tried to get data out of bounds of the raw data array.", e);
246 private long packU16(int indexMajor, int indexMinor) {
247 short major = getData(indexMajor);
248 short minor = getData(indexMinor);
253 return ((major << 16) & 0xFFFF0000) | minor & 0xFFFF;