]> git.basschouten.com Git - openhab-addons.git/blob
ba69f3076f981a84434f8e79774c8feb6db41452
[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.solarwatt.internal.domain.model;
14
15 import java.math.BigDecimal;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import javax.measure.Unit;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.solarwatt.internal.domain.SolarwattChannel;
24 import org.openhab.binding.solarwatt.internal.domain.SolarwattTag;
25 import org.openhab.binding.solarwatt.internal.domain.dto.DeviceDTO;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.QuantityType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.library.unit.SIUnits;
30 import org.openhab.core.library.unit.Units;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.types.State;
33
34 /**
35  * Base class for all devices which are connected to the energy manager.
36  *
37  * This fields have been identified to exist:
38  * com.kiwigrid.lib.device.Device=[
39  * IdFingerPrint,
40  * IdInterfaceList,
41  * IdName,
42  * IdFirmware,
43  * PasswordLock,
44  * IdLabelSet,
45  * StateDevice,
46  * StateVisibleIsSet,
47  * IdFingerPrintVersion,
48  * IdDriver,
49  * IdModelCode,
50  * StateLockedIsSet,
51  * IdManufacturer,
52  * IdSerialNumber,
53  * StateErrorList
54  * ]
55  *
56  * @author Sven Carstens - Initial contribution
57  */
58 @NonNullByDefault
59 public class Device {
60
61     public static final String SOLAR_WATT_CLASSNAME = "com.kiwigrid.lib.device.Device";
62     public static final String WATT_HOUR_CATEGORY = "energy";
63     public static final String WATT_CATEGORY = "energy";
64     private final String guid;
65     private @Nullable String idName;
66     private @Nullable String idFirmware;
67     private @Nullable String idManufacturer;
68     private ThingStatus stateDevice = ThingStatus.UNINITIALIZED;
69     protected final Map<String, State> stateValues;
70     protected final Map<String, SolarwattChannel> solarwattChannelSet;
71
72     public Device(DeviceDTO deviceDTO) {
73         this.stateValues = new HashMap<>();
74         this.solarwattChannelSet = new HashMap<>();
75         this.guid = deviceDTO.getGuid();
76
77         this.update(deviceDTO);
78     }
79
80     public void update(DeviceDTO deviceDTO) {
81         this.idName = deviceDTO.getStringTag("IdName");
82         this.idFirmware = deviceDTO.getStringTag("IdFirmware");
83         this.idManufacturer = deviceDTO.getStringTag("IdManufacturer");
84         if ("OK".equals(deviceDTO.getStringTag("StateDevice"))) {
85             this.stateDevice = ThingStatus.ONLINE;
86         } else {
87             this.stateDevice = ThingStatus.OFFLINE;
88         }
89     }
90
91     public BigDecimal getBigDecimalFromChannel(String channelName) {
92         State state = this.getStateValues().get(channelName);
93         if (state != null) {
94             @SuppressWarnings("rawtypes")
95             QuantityType quantity = state.as(QuantityType.class);
96             if (quantity != null) {
97                 return quantity.toBigDecimal();
98             }
99         }
100         return BigDecimal.ZERO;
101     }
102
103     /**
104      * Add a channeltype to the known channel types.
105      *
106      * {@link org.openhab.core.thing.type.ChannelType} is only created if it does noct exist.
107      *
108      * @param tagName name for the channel
109      * @param unit unit for channel
110      * @param category text category
111      * @param advanced wether or not to display only in advanced
112      */
113     public void addChannel(String tagName, @Nullable Unit<?> unit, String category, Boolean advanced) {
114         this.solarwattChannelSet.computeIfAbsent(tagName, s -> new SolarwattChannel(tagName, unit, category, advanced));
115     }
116
117     /**
118      * Add a state with unit and BigInteger as value.
119      *
120      * @param solarwattTag combined tag and channel name
121      * @param deviceDTO raw device data
122      * @param unit unit for value
123      */
124     public void addStateBigInteger(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Unit<?> unit) {
125         this.addState(solarwattTag.getChannelName(), deviceDTO.getState(
126                 (jsonElement -> new QuantityType<>(jsonElement.getAsBigInteger(), unit)), solarwattTag.getTagName()));
127     }
128
129     /**
130      * Add a state from a json path with unit and BigInteger as value.
131      *
132      * @param channelName target channe
133      * @param tagName tag for value
134      * @param path to find value
135      * @param deviceDTO raw device data
136      * @param unit unit for value
137      */
138     public void addStateBigInteger(String channelName, String tagName, String path, DeviceDTO deviceDTO, Unit<?> unit) {
139         this.addState(channelName, deviceDTO.getState(
140                 (jsonElement -> new QuantityType<>(jsonElement.getAsBigInteger(), unit)), channelName, tagName, path));
141     }
142
143     /**
144      * Add a state with unit and BigDecimal as value.
145      *
146      * @param solarwattTag combined tag and channel name
147      * @param deviceDTO raw device data
148      * @param unit unit for value
149      */
150     public void addStateBigDecimal(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Unit<?> unit) {
151         this.addState(solarwattTag.getChannelName(), deviceDTO.getState(
152                 (jsonElement -> new QuantityType<>(jsonElement.getAsBigDecimal(), unit)), solarwattTag.getTagName()));
153     }
154
155     /**
156      * Add a state with unit and BigDecimal as value.
157      *
158      * @param solarwattTag combined tag and channel name
159      * @param value BigDecimal value
160      * @param unit unit for value
161      */
162     public void addStateBigDecimal(SolarwattTag solarwattTag, BigDecimal value, Unit<?> unit) {
163         this.addState(solarwattTag.getChannelName(), new QuantityType<>(value, unit));
164     }
165
166     /**
167      * Add a string state.
168      *
169      * @param solarwattTag combined tag and channel name
170      * @param deviceDTO raw device data
171      */
172     public void addStateString(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
173         this.addState(solarwattTag.getChannelName(), deviceDTO
174                 .getState((jsonElement -> new StringType(jsonElement.getAsString())), solarwattTag.getTagName()));
175     }
176
177     public void addStateSwitch(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
178         this.addState(solarwattTag.getChannelName(), deviceDTO
179                 .getState((jsonElement -> OnOffType.from(jsonElement.getAsString())), solarwattTag.getTagName()));
180     }
181
182     public ThingStatus getStateDevice() {
183         return this.stateDevice;
184     }
185
186     public Map<String, State> getStateValues() {
187         return this.stateValues;
188     }
189
190     public Map<String, SolarwattChannel> getSolarwattChannelSet() {
191         return this.solarwattChannelSet;
192     }
193
194     protected void addWattHourQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
195         this.addWattHourQuantity(solarwattTag, deviceDTO, false);
196     }
197
198     protected void addWattHourQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
199         this.addChannel(solarwattTag.getChannelName(), Units.WATT_HOUR, WATT_HOUR_CATEGORY, advanced);
200
201         this.addStateBigDecimal(solarwattTag, deviceDTO, Units.WATT_HOUR);
202     }
203
204     protected void addWattQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
205         this.addWattQuantity(solarwattTag, deviceDTO, false);
206     }
207
208     protected void addWattQuantity(SolarwattTag solarwattTag, BigDecimal value, Boolean advanced) {
209         this.addChannel(solarwattTag.getChannelName(), Units.WATT, WATT_CATEGORY, advanced);
210
211         this.addStateBigDecimal(solarwattTag, value, Units.WATT);
212     }
213
214     protected void addWattQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
215         this.addChannel(solarwattTag.getChannelName(), Units.WATT, WATT_CATEGORY, advanced);
216
217         this.addStateBigDecimal(solarwattTag, deviceDTO, Units.WATT);
218     }
219
220     protected void addSecondsQuantity(String channelName, String tagName, String path, DeviceDTO deviceDTO) {
221         this.addChannel(channelName, Units.SECOND, "time", false);
222
223         this.addStateBigInteger(channelName, tagName, path, deviceDTO, Units.SECOND);
224     }
225
226     protected void addPercentQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
227         this.addChannel(solarwattTag.getChannelName(), Units.PERCENT, "status", false);
228
229         this.addStateBigDecimal(solarwattTag, deviceDTO, Units.PERCENT);
230     }
231
232     protected void addCelsiusQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
233         this.addChannel(solarwattTag.getChannelName(), SIUnits.CELSIUS, "temperature", false);
234
235         this.addStateBigDecimal(solarwattTag, deviceDTO, SIUnits.CELSIUS);
236     }
237
238     protected void addAmpereQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
239         this.addAmpereQuantity(solarwattTag, deviceDTO, false);
240     }
241
242     protected void addAmpereQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
243         this.addChannel(solarwattTag.getChannelName(), Units.AMPERE, "current", advanced);
244
245         this.addStateBigDecimal(solarwattTag, deviceDTO, Units.AMPERE);
246     }
247
248     protected void addVoltageQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
249         this.addVoltageQuantity(solarwattTag, deviceDTO, false);
250     }
251
252     protected void addVoltageQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
253         this.addChannel(solarwattTag.getChannelName(), Units.VOLT, "voltage", advanced);
254
255         this.addStateBigDecimal(solarwattTag, deviceDTO, Units.VOLT);
256     }
257
258     protected void addStringState(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
259         this.addChannel(solarwattTag.getChannelName(), null, "status", false);
260
261         this.addStateString(solarwattTag, deviceDTO);
262     }
263
264     protected void addSwitchState(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
265         this.addChannel(solarwattTag.getChannelName(), null, "switch", false);
266
267         this.addStateSwitch(solarwattTag, deviceDTO);
268     }
269
270     /**
271      * Add state to map and return it for further usage.
272      *
273      * @param channelName where to put the state
274      * @param state to put
275      */
276     public void addState(String channelName, @Nullable State state) {
277         if (state != null) {
278             this.stateValues.put(channelName, state);
279         }
280     }
281
282     /**
283      * Get state from map
284      * 
285      * @param channelName state to return
286      * @return {@link State} found
287      */
288     public @Nullable State getState(String channelName) {
289         return this.stateValues.get(channelName);
290     }
291
292     public String getGuid() {
293         return this.guid;
294     }
295
296     public @Nullable String getIdName() {
297         return this.idName;
298     }
299
300     public @Nullable String getIdFirmware() {
301         return this.idFirmware;
302     }
303
304     public @Nullable String getIdManufacturer() {
305         return this.idManufacturer;
306     }
307
308     protected String getSolarWattLabel() {
309         return "Device";
310     }
311
312     public String getLabel() {
313         return this.getSolarWattLabel() + " " + this.getIdName();
314     }
315 }