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