]> git.basschouten.com Git - openhab-addons.git/blob
b6c978418ef35a803dd7bf677785d1bd79aee368
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.solax.internal.connectivity.rawdata;
14
15 import java.util.Arrays;
16
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;
24
25 import com.google.gson.Gson;
26 import com.google.gson.annotations.SerializedName;
27
28 /**
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)
31  *
32  * @author Konstantin Polihronov - Initial contribution
33  */
34 @NonNullByDefault
35 public class LocalConnectRawDataBean implements RawDataBean, InverterData {
36
37     private final Logger logger = LoggerFactory.getLogger(LocalConnectRawDataBean.class);
38
39     private @Nullable String sn;
40     private @Nullable String ver;
41     private int type;
42     @SerializedName("Data")
43     private short @Nullable [] data;
44     @SerializedName("Information")
45     private String @Nullable [] information;
46     private @Nullable String rawData;
47
48     @Override
49     public String toString() {
50         return "LocalConnectRawDataBean [sn=" + sn + ", ver=" + ver + ", type=" + type + ", Information="
51                 + Arrays.toString(information) + ", Data=" + Arrays.toString(data) + "]";
52     }
53
54     public @Nullable String getSn() {
55         return sn;
56     }
57
58     public void setSn(@Nullable String sn) {
59         this.sn = sn;
60     }
61
62     public @Nullable String getVer() {
63         return ver;
64     }
65
66     public void setVer(@Nullable String ver) {
67         this.ver = ver;
68     }
69
70     public int getType() {
71         return type;
72     }
73
74     public void setType(int type) {
75         this.type = type;
76     }
77
78     public short @Nullable [] getData() {
79         return data;
80     }
81
82     public void setData(short @Nullable [] data) {
83         this.data = data;
84     }
85
86     public String @Nullable [] getInformation() {
87         return information;
88     }
89
90     public void setInformation(String @Nullable [] information) {
91         this.information = information;
92     }
93
94     @Override
95     public @Nullable String getRawData() {
96         return rawData;
97     }
98
99     public void setRawData(String rawData) {
100         this.rawData = rawData;
101     }
102
103     public static LocalConnectRawDataBean fromJson(String json) {
104         if (json.isEmpty()) {
105             throw new IllegalArgumentException("JSON payload should not be empty");
106         }
107
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");
112         }
113         deserializedObject.setRawData(json);
114         return deserializedObject;
115     }
116
117     // Parsed inverter data interface implementation starts here
118
119     @Override
120     public @Nullable String getWifiSerial() {
121         return getSn();
122     }
123
124     @Override
125     public @Nullable String getWifiVersion() {
126         return getVer();
127     }
128
129     @Override
130     public InverterType getInverterType() {
131         return InverterType.fromIndex(type);
132     }
133
134     @Override
135     public short getInverterVoltage() {
136         return (short) (getData(0) / 10);
137     }
138
139     @Override
140     public short getInverterCurrent() {
141         return (short) (getData(1) / 10);
142     }
143
144     @Override
145     public short getInverterOutputPower() {
146         return getData(2);
147     }
148
149     @Override
150     public short getInverterFrequency() {
151         return (short) (getData(3) / 100);
152     }
153
154     @Override
155     public short getPV1Voltage() {
156         return (short) (getData(4) / 10);
157     }
158
159     @Override
160     public short getPV1Current() {
161         return (short) (getData(6) / 10);
162     }
163
164     @Override
165     public short getPV1Power() {
166         return getData(8);
167     }
168
169     @Override
170     public short getPV2Voltage() {
171         return (short) (getData(5) / 10);
172     }
173
174     @Override
175     public short getPV2Current() {
176         return (short) (getData(7) / 10);
177     }
178
179     @Override
180     public short getPV2Power() {
181         return getData(9);
182     }
183
184     @Override
185     public short getBatteryVoltage() {
186         return (short) (getData(14) / 100);
187     }
188
189     @Override
190     public short getBatteryCurrent() {
191         return (short) (getData(15) / 100);
192     }
193
194     @Override
195     public short getBatteryPower() {
196         return getData(16);
197     }
198
199     @Override
200     public short getBatteryTemperature() {
201         return getData(17);
202     }
203
204     @Override
205     public short getBatterySoC() {
206         return getData(18);
207     }
208
209     @Override
210     public long getOnGridTotalYield() {
211         return packU16(11, 12) / 100;
212     }
213
214     @Override
215     public short getOnGridDailyYield() {
216         return (short) (getData(13) / 10);
217     }
218
219     @Override
220     public short getFeedInPower() {
221         return getData(32);
222     }
223
224     @Override
225     public long getTotalFeedInEnergy() {
226         return packU16(34, 35) / 100;
227     }
228
229     @Override
230     public long getTotalConsumption() {
231         return packU16(36, 37) / 100;
232     }
233
234     private short getData(int index) {
235         try {
236             short[] dataArray = data;
237             if (dataArray != null) {
238                 return dataArray[index];
239             }
240         } catch (IndexOutOfBoundsException e) {
241             logger.debug("Tried to get data out of bounds of the raw data array.", e);
242         }
243         return 0;
244     }
245
246     private long packU16(int indexMajor, int indexMinor) {
247         short major = getData(indexMajor);
248         short minor = getData(indexMinor);
249         if (major == 0) {
250             return minor;
251         }
252
253         return ((major << 16) & 0xFFFF0000) | minor & 0xFFFF;
254     }
255 }