]> git.basschouten.com Git - openhab-addons.git/blob
b266dfac7c293e3801a26b344fde3da5cbd53a96
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.model.impl;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.solax.internal.connectivity.rawdata.LocalConnectRawDataBean;
18 import org.openhab.binding.solax.internal.model.InverterData;
19 import org.openhab.binding.solax.internal.model.InverterType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * The {@link CommonInverterData} is an abstract class that contains the common information, applicable for all
25  * inverters.
26  *
27  * @author Konstantin Polihronov - Initial contribution
28  */
29 @NonNullByDefault
30 public abstract class CommonInverterData implements InverterData {
31
32     private final Logger logger = LoggerFactory.getLogger(CommonInverterData.class);
33
34     private LocalConnectRawDataBean data;
35
36     public CommonInverterData(LocalConnectRawDataBean data) {
37         this.data = data;
38     }
39
40     @Override
41     public @Nullable String getRawData() {
42         return data.getRawData();
43     }
44
45     @Override
46     public @Nullable String getWifiSerial() {
47         return data.getSn();
48     }
49
50     @Override
51     public @Nullable String getWifiVersion() {
52         return data.getVer();
53     }
54
55     @Override
56     public InverterType getInverterType() {
57         return InverterType.fromIndex(data.getType());
58     }
59
60     protected short getData(int index) {
61         try {
62             short[] dataArray = data.getData();
63             if (dataArray != null) {
64                 return dataArray[index];
65             }
66         } catch (IndexOutOfBoundsException e) {
67             logger.debug("Tried to get data out of bounds of the raw data array.", e);
68         }
69         return 0;
70     }
71
72     public long packU16(int indexMajor, int indexMinor) {
73         short major = getData(indexMajor);
74         short minor = getData(indexMinor);
75         if (major == 0) {
76             return minor;
77         }
78
79         return Integer.toUnsignedLong(major << 16 | minor & 0xFFFF);
80     }
81 }