]> git.basschouten.com Git - openhab-addons.git/blob
60f07bcb7b6a984182a6ddc78c22c373e537819e
[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
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.HSBType;
19 import org.openhab.core.library.types.PercentType;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonPrimitive;
25
26 /**
27  * Conversion for values
28  *
29  * @author Marcel Verpaalen - Initial contribution
30  */
31 @NonNullByDefault
32 public class Conversions {
33     private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
34
35     /**
36      * Converts a RGB+brightness input to a HSV value.
37      * *
38      *
39      * @param RGB + brightness value (note brightness in the first byte)
40      * @return HSV
41      */
42     public static JsonElement bRGBtoHSV(JsonElement bRGB) throws ClassCastException {
43         if (bRGB.isJsonPrimitive() && bRGB.getAsJsonPrimitive().isNumber()) {
44             Color rgb = new Color(bRGB.getAsInt());
45             HSBType hsb = HSBType.fromRGB(rgb.getRed(), rgb.getGreen(), rgb.getBlue());
46             hsb = new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(bRGB.getAsInt() >>> 24));
47             return new JsonPrimitive(hsb.toFullString());
48         }
49         return bRGB;
50     }
51
52     public static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
53         double value = seconds.getAsDouble() / 3600;
54         return new JsonPrimitive(value);
55     }
56
57     public static JsonElement yeelightSceneConversion(JsonElement intValue)
58             throws ClassCastException, IllegalStateException {
59         switch (intValue.getAsInt()) {
60             case 1:
61                 return new JsonPrimitive("color");
62             case 2:
63                 return new JsonPrimitive("hsv");
64             case 3:
65                 return new JsonPrimitive("ct");
66             case 4:
67                 return new JsonPrimitive("nightlight");
68             case 5: // don't know the number for colorflow...
69                 return new JsonPrimitive("cf");
70             case 6: // don't know the number for auto_delay_off, or if it is even in the properties visible...
71                 return new JsonPrimitive("auto_delay_off");
72             default:
73                 return new JsonPrimitive("unknown");
74         }
75     }
76
77     public static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
78         double value = value10.getAsDouble() / 10.0;
79         return new JsonPrimitive(value);
80     }
81
82     public static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
83         double value = value10.getAsDouble() / 100.0;
84         return new JsonPrimitive(value);
85     }
86
87     public static JsonElement tankLevel(JsonElement value12) throws ClassCastException, IllegalStateException {
88         // 127 without water tank. 120 = 100% water
89         if (value12.getAsInt() == 127) {
90             return new JsonPrimitive(-1);
91         } else {
92             double value = value12.getAsDouble();
93             return new JsonPrimitive(value / 1.2);
94         }
95     }
96
97     public static JsonElement execute(String transfortmation, JsonElement value) {
98         try {
99             switch (transfortmation.toUpperCase()) {
100                 case "YEELIGHTSCENEID":
101                     return yeelightSceneConversion(value);
102                 case "SECONDSTOHOURS":
103                     return secondsToHours(value);
104                 case "/10":
105                     return divideTen(value);
106                 case "/100":
107                     return divideHundred(value);
108                 case "TANKLEVEL":
109                     return tankLevel(value);
110                 case "BRGBTOHSV":
111                     return bRGBtoHSV(value);
112                 default:
113                     LOGGER.debug("Transformation {} not found. Returning '{}'", transfortmation, value.toString());
114                     return value;
115             }
116         } catch (ClassCastException | IllegalStateException e) {
117             LOGGER.debug("Transformation {} failed. Returning '{}'", transfortmation, value.toString());
118             return value;
119         }
120     }
121 }