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.solarwatt.internal.domain.model;
15 import java.math.BigDecimal;
16 import java.util.HashMap;
19 import javax.measure.Unit;
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;
35 * Base class for all devices which are connected to the energy manager.
37 * This fields have been identified to exist:
38 * com.kiwigrid.lib.device.Device=[
47 * IdFingerPrintVersion,
56 * @author Sven Carstens - Initial contribution
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;
72 public Device(DeviceDTO deviceDTO) {
73 this.stateValues = new HashMap<>();
74 this.solarwattChannelSet = new HashMap<>();
75 this.guid = deviceDTO.getGuid();
77 this.update(deviceDTO);
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;
87 this.stateDevice = ThingStatus.OFFLINE;
91 public BigDecimal getBigDecimalFromChannel(String channelName) {
92 State state = this.getStateValues().get(channelName);
94 @SuppressWarnings("rawtypes")
95 QuantityType quantity = state.as(QuantityType.class);
96 if (quantity != null) {
97 return quantity.toBigDecimal();
100 return BigDecimal.ZERO;
104 * Add a channeltype to the known channel types.
106 * {@link org.openhab.core.thing.type.ChannelType} is only created if it does noct exist.
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
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));
118 * Add a state with unit and BigInteger as value.
120 * @param solarwattTag combined tag and channel name
121 * @param deviceDTO raw device data
122 * @param unit unit for value
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()));
130 * Add a state from a json path with unit and BigInteger as value.
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
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));
144 * Add a state with unit and BigDecimal as value.
146 * @param solarwattTag combined tag and channel name
147 * @param deviceDTO raw device data
148 * @param unit unit for value
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()));
156 * Add a state with unit and BigDecimal as value.
158 * @param solarwattTag combined tag and channel name
159 * @param value BigDecimal value
160 * @param unit unit for value
162 public void addStateBigDecimal(SolarwattTag solarwattTag, BigDecimal value, Unit<?> unit) {
163 this.addState(solarwattTag.getChannelName(), new QuantityType<>(value, unit));
167 * Add a string state.
169 * @param solarwattTag combined tag and channel name
170 * @param deviceDTO raw device data
172 public void addStateString(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
173 this.addState(solarwattTag.getChannelName(), deviceDTO
174 .getState((jsonElement -> new StringType(jsonElement.getAsString())), solarwattTag.getTagName()));
177 public void addStateSwitch(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
178 this.addState(solarwattTag.getChannelName(), deviceDTO
179 .getState((jsonElement -> OnOffType.from(jsonElement.getAsString())), solarwattTag.getTagName()));
182 public ThingStatus getStateDevice() {
183 return this.stateDevice;
186 public Map<String, State> getStateValues() {
187 return this.stateValues;
190 public Map<String, SolarwattChannel> getSolarwattChannelSet() {
191 return this.solarwattChannelSet;
194 protected void addWattHourQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
195 this.addWattHourQuantity(solarwattTag, deviceDTO, false);
198 protected void addWattHourQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
199 this.addChannel(solarwattTag.getChannelName(), Units.WATT_HOUR, WATT_HOUR_CATEGORY, advanced);
201 this.addStateBigDecimal(solarwattTag, deviceDTO, Units.WATT_HOUR);
204 protected void addWattQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
205 this.addWattQuantity(solarwattTag, deviceDTO, false);
208 protected void addWattQuantity(SolarwattTag solarwattTag, BigDecimal value, Boolean advanced) {
209 this.addChannel(solarwattTag.getChannelName(), Units.WATT, WATT_CATEGORY, advanced);
211 this.addStateBigDecimal(solarwattTag, value, Units.WATT);
214 protected void addWattQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
215 this.addChannel(solarwattTag.getChannelName(), Units.WATT, WATT_CATEGORY, advanced);
217 this.addStateBigDecimal(solarwattTag, deviceDTO, Units.WATT);
220 protected void addSecondsQuantity(String channelName, String tagName, String path, DeviceDTO deviceDTO) {
221 this.addChannel(channelName, Units.SECOND, "time", false);
223 this.addStateBigInteger(channelName, tagName, path, deviceDTO, Units.SECOND);
226 protected void addPercentQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
227 this.addChannel(solarwattTag.getChannelName(), Units.PERCENT, "status", false);
229 this.addStateBigDecimal(solarwattTag, deviceDTO, Units.PERCENT);
232 protected void addCelsiusQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
233 this.addChannel(solarwattTag.getChannelName(), SIUnits.CELSIUS, "temperature", false);
235 this.addStateBigDecimal(solarwattTag, deviceDTO, SIUnits.CELSIUS);
238 protected void addAmpereQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
239 this.addAmpereQuantity(solarwattTag, deviceDTO, false);
242 protected void addAmpereQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
243 this.addChannel(solarwattTag.getChannelName(), Units.AMPERE, "current", advanced);
245 this.addStateBigDecimal(solarwattTag, deviceDTO, Units.AMPERE);
248 protected void addVoltageQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
249 this.addVoltageQuantity(solarwattTag, deviceDTO, false);
252 protected void addVoltageQuantity(SolarwattTag solarwattTag, DeviceDTO deviceDTO, Boolean advanced) {
253 this.addChannel(solarwattTag.getChannelName(), Units.VOLT, "voltage", advanced);
255 this.addStateBigDecimal(solarwattTag, deviceDTO, Units.VOLT);
258 protected void addStringState(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
259 this.addChannel(solarwattTag.getChannelName(), null, "status", false);
261 this.addStateString(solarwattTag, deviceDTO);
264 protected void addSwitchState(SolarwattTag solarwattTag, DeviceDTO deviceDTO) {
265 this.addChannel(solarwattTag.getChannelName(), null, "switch", false);
267 this.addStateSwitch(solarwattTag, deviceDTO);
271 * Add state to map and return it for further usage.
273 * @param channelName where to put the state
274 * @param state to put
276 public void addState(String channelName, @Nullable State state) {
278 this.stateValues.put(channelName, state);
285 * @param channelName state to return
286 * @return {@link State} found
288 public @Nullable State getState(String channelName) {
289 return this.stateValues.get(channelName);
292 public String getGuid() {
296 public @Nullable String getIdName() {
300 public @Nullable String getIdFirmware() {
301 return this.idFirmware;
304 public @Nullable String getIdManufacturer() {
305 return this.idManufacturer;
308 protected String getSolarWattLabel() {
312 public String getLabel() {
313 return this.getSolarWattLabel() + " " + this.getIdName();