]> git.basschouten.com Git - openhab-addons.git/blob
e20487d09a0b80e8cd5bbae8e25db29d579017c6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.JsonElement;
26 import com.google.gson.JsonObject;
27 import com.google.gson.JsonParseException;
28 import com.google.gson.JsonParser;
29 import com.google.gson.JsonPrimitive;
30
31 /**
32  * Conversion for values
33  *
34  * @author Marcel Verpaalen - Initial contribution
35  */
36 @NonNullByDefault
37 public class Conversions {
38     private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
39
40     /**
41      * Converts a RGB+brightness input to a HSV value.
42      * *
43      *
44      * @param RGB + brightness value (note brightness in the first byte)
45      * @return HSV
46      */
47     public static JsonElement bRGBtoHSV(JsonElement bRGB) throws ClassCastException {
48         if (bRGB.isJsonPrimitive() && bRGB.getAsJsonPrimitive().isNumber()) {
49             Color rgb = new Color(bRGB.getAsInt());
50             HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
51             hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(bRGB.getAsInt() >>> 24));
52             return new JsonPrimitive(hsb.toFullString());
53         }
54         return bRGB;
55     }
56
57     /**
58      * Adds the brightness info (from separate channel) to a HSV value.
59      * *
60      *
61      * @param RGB
62      * @param map with device variables containing the brightness info
63      * @return HSV
64      */
65     public static JsonElement addBrightToHSV(JsonElement rgbValue, @Nullable Map<String, Object> deviceVariables)
66             throws ClassCastException, IllegalStateException {
67         int bright = 100;
68         if (deviceVariables != null) {
69             JsonElement lastBright = (JsonElement) deviceVariables.getOrDefault("bright", new JsonPrimitive(100));
70             bright = lastBright.getAsInt();
71         }
72         if (rgbValue.isJsonPrimitive()
73                 && (rgbValue.getAsJsonPrimitive().isNumber() || rgbValue.getAsString().matches("^[0-9]+$"))) {
74             Color rgb = new Color(rgbValue.getAsInt());
75             HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
76             hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(bright));
77             return new JsonPrimitive(hsb.toFullString());
78         }
79         return rgbValue;
80     }
81
82     public static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
83         double value = seconds.getAsDouble() / 3600;
84         return new JsonPrimitive(value);
85     }
86
87     public static JsonElement yeelightSceneConversion(JsonElement intValue)
88             throws ClassCastException, IllegalStateException {
89         switch (intValue.getAsInt()) {
90             case 1:
91                 return new JsonPrimitive("color");
92             case 2:
93                 return new JsonPrimitive("hsv");
94             case 3:
95                 return new JsonPrimitive("ct");
96             case 4:
97                 return new JsonPrimitive("nightlight");
98             case 5: // don't know the number for colorflow...
99                 return new JsonPrimitive("cf");
100             case 6: // don't know the number for auto_delay_off, or if it is even in the properties visible...
101                 return new JsonPrimitive("auto_delay_off");
102             default:
103                 return new JsonPrimitive("unknown");
104         }
105     }
106
107     public static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
108         double value = value10.getAsDouble() / 10.0;
109         return new JsonPrimitive(value);
110     }
111
112     public static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
113         double value = value10.getAsDouble() / 100.0;
114         return new JsonPrimitive(value);
115     }
116
117     public static JsonElement tankLevel(JsonElement value12) throws ClassCastException, IllegalStateException {
118         // 127 without water tank. 120 = 100% water
119         if (value12.getAsInt() == 127) {
120             return new JsonPrimitive(-1);
121         } else {
122             double value = value12.getAsDouble();
123             return new JsonPrimitive(value / 1.2);
124         }
125     }
126
127     public static JsonElement getJsonElement(String element, JsonElement responseValue) {
128         try {
129             if (responseValue.isJsonPrimitive() || responseValue.isJsonObject()) {
130                 JsonElement jsonElement = responseValue.isJsonObject() ? responseValue
131                         : JsonParser.parseString(responseValue.getAsString());
132                 if (jsonElement.isJsonObject()) {
133                     JsonObject value = jsonElement.getAsJsonObject();
134                     if (value.has(element)) {
135                         return value.get(element);
136                     }
137                 }
138             }
139         } catch (JsonParseException e) {
140             // ignore
141         }
142         LOGGER.debug("JsonElement '{}' not found in '{}'", element, responseValue);
143         return responseValue;
144     }
145
146     public static JsonElement execute(String transformation, JsonElement value,
147             @Nullable Map<String, Object> deviceVariables) {
148         try {
149             if (transformation.toUpperCase().startsWith("GETJSONELEMENT")) {
150                 if (transformation.length() > 15) {
151                     return getJsonElement(transformation.substring(15), value);
152                 } else {
153                     LOGGER.info("Transformation {} missing element. Returning '{}'", transformation, value.toString());
154                 }
155             }
156             switch (transformation.toUpperCase()) {
157                 case "YEELIGHTSCENEID":
158                     return yeelightSceneConversion(value);
159                 case "SECONDSTOHOURS":
160                     return secondsToHours(value);
161                 case "/10":
162                     return divideTen(value);
163                 case "/100":
164                     return divideHundred(value);
165                 case "TANKLEVEL":
166                     return tankLevel(value);
167                 case "ADDBRIGHTTOHSV":
168                     return addBrightToHSV(value, deviceVariables);
169                 case "BRGBTOHSV":
170                     return bRGBtoHSV(value);
171                 default:
172                     LOGGER.debug("Transformation {} not found. Returning '{}'", transformation, value.toString());
173                     return value;
174             }
175         } catch (ClassCastException | IllegalStateException e) {
176             LOGGER.debug("Transformation {} failed. Returning '{}'", transformation, value.toString());
177             return value;
178         }
179     }
180 }