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.dto;
15 import java.util.Collection;
17 import java.util.stream.Collectors;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.solarwatt.internal.domain.converter.JsonStateConverter;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.UnDefType;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.JsonArray;
27 import com.google.gson.JsonElement;
28 import com.google.gson.JsonNull;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonPrimitive;
33 * DTO class for the devices returned by the energy manager.
35 * Properties without setters are only filled by gson JSON parsing.
37 * @author Sven Carstens - Initial contribution
39 public class DeviceDTO {
40 private final Logger logger = LoggerFactory.getLogger(DeviceDTO.class);
43 private Collection<DeviceModel> deviceModel;
44 private Map<String, TagValueDTO> tagValues;
46 public String getGuid() {
50 public Collection<String> getDeviceModelStrings() {
51 return this.deviceModel.stream().map(DeviceModel::getDeviceClass).collect(Collectors.toList());
54 public Collection<DeviceModel> getDeviceModel() {
55 return this.deviceModel;
58 public Map<String, TagValueDTO> getTagValues() {
59 return this.tagValues;
63 * Helper to get a string value from the list of tag values.
65 * @param tagName name of tag to read
68 public @Nullable String getStringTag(String tagName) {
69 JsonPrimitive jsonPrimitive = this.getJsonPrimitiveFromTag(tagName);
70 if (jsonPrimitive != null) {
71 return jsonPrimitive.getAsString();
78 * Helper to get a {@link JsonPrimitive} from the list of tag values.
80 * The primitves are later converted to the desired target type.
82 * @param tagName name of tag to read
83 * @return raw json from tag value
85 protected @Nullable JsonPrimitive getJsonPrimitiveFromTag(String tagName) {
86 Map<String, TagValueDTO> localTagValues = this.getTagValues();
87 if (localTagValues != null) {
88 TagValueDTO localTag = localTagValues.get(tagName);
89 if (localTag != null && localTag.getValue().isJsonPrimitive()) {
90 return (JsonPrimitive) localTag.getValue();
97 * Helper to get a {@link JsonObject} from the list of tag values.
99 * The objects are used by the concrete devices to read interesting
100 * but deeply nested values.
102 * @param tagName name of tag to read
103 * @return raw json from tag value
105 public @Nullable JsonObject getJsonObjectFromTag(String tagName) {
106 Map<String, TagValueDTO> localTagValues = this.getTagValues();
107 if (localTagValues != null) {
108 TagValueDTO localTag = localTagValues.get(tagName);
109 if (localTag != null && localTag.getValue().isJsonObject()) {
110 return (JsonObject) localTag.getValue();
117 * Helper to get a {@link JsonObject} from a tag value which contains JSON.
119 * The objects are used by the concrete devices to read interesting
120 * but deeply nested values.
122 * @param tagName name of tag to read
123 * @return raw json from tag value
125 public JsonPrimitive getJsonPrimitiveFromPath(String tagName, String path) {
126 JsonElement jsonElement = this.getJsonFromPath(tagName, path);
128 if (jsonElement != null && jsonElement.isJsonPrimitive()) {
129 return (JsonPrimitive) jsonElement;
135 * Helper to get a {@link JsonObject} from a tag value which contains JSON.
137 * The json path is traversed according to the supplied path. Only simple pathes
140 * @param tagName name of tag to read
141 * @return raw json from tag value
143 public JsonElement getJsonFromPath(String tagName, String path) {
144 JsonObject json = this.getJsonObjectFromTag(tagName);
146 String[] parts = path.split("\\.|\\[|\\]");
147 JsonElement result = json;
149 for (String key : parts) {
156 if (result == null) {
157 result = JsonNull.INSTANCE;
161 if (result.isJsonObject()) {
162 result = ((JsonObject) result).get(key);
163 } else if (result.isJsonArray()) {
164 int ix = Integer.valueOf(key) - 1;
165 result = ((JsonArray) result).get(ix);
178 * Transform a value from a tag to a state.
180 * @param converter applied the the {@link JsonPrimitive}
181 * @param tagName to find value
182 * @return state for channel
184 public State getState(JsonStateConverter converter, String tagName) {
185 return this.getState(converter, tagName, tagName, null);
189 * Transform a value specified via JSON path from a tag to a state.
191 * @param converter applied the the {@link JsonPrimitive}
192 * @param channelName for the state
193 * @param tagName to find value
194 * @param jsonPath to find value
195 * @return state for channel
197 public State getState(JsonStateConverter converter, String channelName, String tagName, String jsonPath) {
198 State state = UnDefType.UNDEF;
200 JsonPrimitive jsonPrimitive = jsonPath == null ? this.getJsonPrimitiveFromTag(tagName)
201 : this.getJsonPrimitiveFromPath(tagName, jsonPath);
202 if (jsonPrimitive != null) {
203 state = converter.convert(jsonPrimitive);
205 state = UnDefType.NULL;
207 } catch (Exception ex) {
208 this.logger.warn("failed getting state for {}", channelName, ex);
214 public static class DeviceModel {
215 private String deviceClass;
217 public String getDeviceClass() {
218 return this.deviceClass;
222 public String toString() {
223 return this.deviceClass;