]> git.basschouten.com Git - openhab-addons.git/blob
576d414870b7ddb4920ed43a3e435ec76f4d2432
[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.dto;
14
15 import java.util.Collection;
16 import java.util.Map;
17 import java.util.stream.Collectors;
18
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;
25
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;
31
32 /**
33  * DTO class for the devices returned by the energy manager.
34  *
35  * Properties without setters are only filled by gson JSON parsing.
36  *
37  * @author Sven Carstens - Initial contribution
38  */
39 public class DeviceDTO {
40     private final Logger logger = LoggerFactory.getLogger(DeviceDTO.class);
41
42     private String guid;
43     private Collection<DeviceModel> deviceModel;
44     private Map<String, TagValueDTO> tagValues;
45
46     public String getGuid() {
47         return this.guid;
48     }
49
50     public Collection<String> getDeviceModelStrings() {
51         return this.deviceModel.stream().map(DeviceModel::getDeviceClass).collect(Collectors.toList());
52     }
53
54     public Collection<DeviceModel> getDeviceModel() {
55         return this.deviceModel;
56     }
57
58     public Map<String, TagValueDTO> getTagValues() {
59         return this.tagValues;
60     }
61
62     /**
63      * Helper to get a string value from the list of tag values.
64      *
65      * @param tagName name of tag to read
66      * @return tag value
67      */
68     public @Nullable String getStringTag(String tagName) {
69         JsonPrimitive jsonPrimitive = this.getJsonPrimitiveFromTag(tagName);
70         if (jsonPrimitive != null) {
71             return jsonPrimitive.getAsString();
72         }
73
74         return null;
75     }
76
77     /**
78      * Helper to get a {@link JsonPrimitive} from the list of tag values.
79      *
80      * The primitves are later converted to the desired target type.
81      * 
82      * @param tagName name of tag to read
83      * @return raw json from tag value
84      */
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();
91             }
92         }
93         return null;
94     }
95
96     /**
97      * Helper to get a {@link JsonObject} from the list of tag values.
98      *
99      * The objects are used by the concrete devices to read interesting
100      * but deeply nested values.
101      *
102      * @param tagName name of tag to read
103      * @return raw json from tag value
104      */
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();
111             }
112         }
113         return null;
114     }
115
116     /**
117      * Helper to get a {@link JsonObject} from a tag value which contains JSON.
118      *
119      * The objects are used by the concrete devices to read interesting
120      * but deeply nested values.
121      *
122      * @param tagName name of tag to read
123      * @return raw json from tag value
124      */
125     public JsonPrimitive getJsonPrimitiveFromPath(String tagName, String path) {
126         JsonElement jsonElement = this.getJsonFromPath(tagName, path);
127
128         if (jsonElement != null && jsonElement.isJsonPrimitive()) {
129             return (JsonPrimitive) jsonElement;
130         }
131         return null;
132     }
133
134     /**
135      * Helper to get a {@link JsonObject} from a tag value which contains JSON.
136      *
137      * The json path is traversed according to the supplied path. Only simple pathes
138      * are supported.
139      *
140      * @param tagName name of tag to read
141      * @return raw json from tag value
142      */
143     public JsonElement getJsonFromPath(String tagName, String path) {
144         JsonObject json = this.getJsonObjectFromTag(tagName);
145         if (json != null) {
146             String[] parts = path.split("\\.|\\[|\\]");
147             JsonElement result = json;
148
149             for (String key : parts) {
150
151                 key = key.trim();
152                 if (key.isEmpty()) {
153                     continue;
154                 }
155
156                 if (result == null) {
157                     result = JsonNull.INSTANCE;
158                     break;
159                 }
160
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);
166                 } else {
167                     break;
168                 }
169             }
170
171             return result;
172         }
173
174         return null;
175     }
176
177     /**
178      * Transform a value from a tag to a state.
179      *
180      * @param converter applied the the {@link JsonPrimitive}
181      * @param tagName to find value
182      * @return state for channel
183      */
184     public State getState(JsonStateConverter converter, String tagName) {
185         return this.getState(converter, tagName, tagName, null);
186     }
187
188     /**
189      * Transform a value specified via JSON path from a tag to a state.
190      *
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
196      */
197     public State getState(JsonStateConverter converter, String channelName, String tagName, String jsonPath) {
198         State state = UnDefType.UNDEF;
199         try {
200             JsonPrimitive jsonPrimitive = jsonPath == null ? this.getJsonPrimitiveFromTag(tagName)
201                     : this.getJsonPrimitiveFromPath(tagName, jsonPath);
202             if (jsonPrimitive != null) {
203                 state = converter.convert(jsonPrimitive);
204             } else {
205                 state = UnDefType.NULL;
206             }
207         } catch (Exception ex) {
208             this.logger.warn("failed getting state for {}", channelName, ex);
209         }
210
211         return state;
212     }
213
214     public static class DeviceModel {
215         private String deviceClass;
216
217         public String getDeviceClass() {
218             return this.deviceClass;
219         }
220
221         @Override
222         public String toString() {
223             return this.deviceClass;
224         }
225     }
226 }