]> git.basschouten.com Git - openhab-addons.git/blob
567232ba3f39de8692a375b21cf8aeec420ac7c2
[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.miio.internal.basic;
14
15 import java.awt.Color;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.library.types.HSBType;
21 import org.openhab.core.library.types.PercentType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonArray;
26 import com.google.gson.JsonElement;
27 import com.google.gson.JsonObject;
28 import com.google.gson.JsonParseException;
29 import com.google.gson.JsonParser;
30 import com.google.gson.JsonPrimitive;
31
32 /**
33  * Conversion for values
34  *
35  * @author Marcel Verpaalen - Initial contribution
36  */
37 @NonNullByDefault
38 public class Conversions {
39     private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
40
41     /**
42      * Converts a RGB+brightness input to a HSV value.
43      * *
44      *
45      * @param RGB + brightness value (note brightness in the first byte)
46      * @return HSV
47      */
48     private static JsonElement bRGBtoHSV(JsonElement bRGB) throws ClassCastException {
49         if (bRGB.isJsonPrimitive() && bRGB.getAsJsonPrimitive().isNumber()) {
50             Color rgb = new Color(bRGB.getAsInt());
51             HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
52             hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(bRGB.getAsInt() >>> 24));
53             return new JsonPrimitive(hsb.toFullString());
54         }
55         return bRGB;
56     }
57
58     /**
59      * Adds the brightness info (from separate channel) to a HSV value.
60      * *
61      *
62      * @param RGB
63      * @param map with device variables containing the brightness info
64      * @return HSV
65      */
66     private static JsonElement addBrightToHSV(JsonElement rgbValue, @Nullable Map<String, Object> deviceVariables)
67             throws ClassCastException, IllegalStateException {
68         int bright = 100;
69         if (deviceVariables != null) {
70             JsonElement lastBright = (JsonElement) deviceVariables.getOrDefault("bright", new JsonPrimitive(100));
71             bright = lastBright.getAsInt();
72         }
73         if (rgbValue.isJsonPrimitive()
74                 && (rgbValue.getAsJsonPrimitive().isNumber() || rgbValue.getAsString().matches("^[0-9]+$"))) {
75             Color rgb = new Color(rgbValue.getAsInt());
76             HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
77             hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(bright));
78             return new JsonPrimitive(hsb.toFullString());
79         }
80         return rgbValue;
81     }
82
83     public static JsonElement deviceDataTab(JsonElement deviceLog, @Nullable Map<String, Object> deviceVariables)
84             throws ClassCastException, IllegalStateException {
85         if (!deviceLog.isJsonObject() && !deviceLog.isJsonPrimitive()) {
86             return deviceLog;
87         }
88         JsonObject deviceLogJsonObj = deviceLog.isJsonObject() ? deviceLog.getAsJsonObject()
89                 : (JsonObject) JsonParser.parseString(deviceLog.getAsString());
90         JsonArray resultLog = new JsonArray();
91         if (deviceLogJsonObj.has("data") && deviceLogJsonObj.get("data").isJsonArray()) {
92             for (JsonElement element : deviceLogJsonObj.get("data").getAsJsonArray()) {
93                 if (element.isJsonObject()) {
94                     JsonObject dataObject = element.getAsJsonObject();
95                     if (dataObject.has("value")) {
96                         String value = dataObject.get("value").getAsString();
97                         JsonElement val = JsonParser.parseString(value);
98                         if (val.isJsonArray()) {
99                             resultLog.add(JsonParser.parseString(val.getAsString()));
100                         } else {
101                             resultLog.add(val);
102                         }
103                     }
104                 }
105             }
106         }
107         return resultLog;
108     }
109
110     private static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
111         double value = seconds.getAsDouble() / 3600;
112         return new JsonPrimitive(value);
113     }
114
115     private static JsonElement yeelightSceneConversion(JsonElement intValue)
116             throws ClassCastException, IllegalStateException {
117         switch (intValue.getAsInt()) {
118             case 1:
119                 return new JsonPrimitive("color");
120             case 2:
121                 return new JsonPrimitive("hsv");
122             case 3:
123                 return new JsonPrimitive("ct");
124             case 4:
125                 return new JsonPrimitive("nightlight");
126             case 5: // don't know the number for colorflow...
127                 return new JsonPrimitive("cf");
128             case 6: // don't know the number for auto_delay_off, or if it is even in the properties visible...
129                 return new JsonPrimitive("auto_delay_off");
130             default:
131                 return new JsonPrimitive("unknown");
132         }
133     }
134
135     private static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
136         double value = value10.getAsDouble() / 10.0;
137         return new JsonPrimitive(value);
138     }
139
140     private static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
141         double value = value10.getAsDouble() / 100.0;
142         return new JsonPrimitive(value);
143     }
144
145     private static JsonElement tankLevel(JsonElement value12) throws ClassCastException, IllegalStateException {
146         // 127 without water tank. 120 = 100% water
147         if (value12.getAsInt() == 127) {
148             return new JsonPrimitive(-1);
149         } else {
150             double value = value12.getAsDouble();
151             return new JsonPrimitive(value / 1.2);
152         }
153     }
154
155     /**
156      * Returns the deviceId element value from the Json response. If not found, returns the input
157      *
158      * @param responseValue
159      * @param deviceVariables containing the deviceId
160      * @return
161      */
162     private static JsonElement getDidElement(JsonElement responseValue, Map<String, Object> deviceVariables) {
163         String did = (String) deviceVariables.get("deviceId");
164         if (did != null) {
165             return getJsonElement(did, responseValue);
166         }
167         LOGGER.debug("deviceId not Found, no conversion");
168         return responseValue;
169     }
170
171     /**
172      * Returns the element from the Json response. If not found, returns the input
173      *
174      * @param element to be found
175      * @param responseValue
176      * @return
177      */
178     private static JsonElement getJsonElement(String element, JsonElement responseValue) {
179         try {
180             if (responseValue.isJsonPrimitive() || responseValue.isJsonObject()) {
181                 JsonElement jsonElement = responseValue.isJsonObject() ? responseValue
182                         : JsonParser.parseString(responseValue.getAsString());
183                 if (jsonElement.isJsonObject()) {
184                     JsonObject value = jsonElement.getAsJsonObject();
185                     if (value.has(element)) {
186                         return value.get(element);
187                     }
188                 }
189             }
190         } catch (JsonParseException e) {
191             // ignore
192         }
193         LOGGER.debug("JsonElement '{}' not found in '{}'", element, responseValue);
194         return responseValue;
195     }
196
197     public static JsonElement execute(String transformation, JsonElement value, Map<String, Object> deviceVariables) {
198         try {
199             if (transformation.toUpperCase().startsWith("GETJSONELEMENT")) {
200                 if (transformation.length() > 15) {
201                     return getJsonElement(transformation.substring(15), value);
202                 } else {
203                     LOGGER.info("Transformation {} missing element. Returning '{}'", transformation, value.toString());
204                 }
205             }
206             switch (transformation.toUpperCase()) {
207                 case "YEELIGHTSCENEID":
208                     return yeelightSceneConversion(value);
209                 case "SECONDSTOHOURS":
210                     return secondsToHours(value);
211                 case "/10":
212                     return divideTen(value);
213                 case "/100":
214                     return divideHundred(value);
215                 case "TANKLEVEL":
216                     return tankLevel(value);
217                 case "ADDBRIGHTTOHSV":
218                     return addBrightToHSV(value, deviceVariables);
219                 case "BRGBTOHSV":
220                     return bRGBtoHSV(value);
221                 case "DEVICEDATATAB":
222                     return deviceDataTab(value, deviceVariables);
223                 case "GETDIDELEMENT":
224                     return getDidElement(value, deviceVariables);
225                 default:
226                     LOGGER.debug("Transformation {} not found. Returning '{}'", transformation, value.toString());
227                     return value;
228             }
229         } catch (ClassCastException | IllegalStateException e) {
230             LOGGER.debug("Transformation {} failed. Returning '{}'", transformation, value.toString());
231             return value;
232         }
233     }
234 }