2 * Copyright (c) 2010-2021 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.miio.internal.basic;
15 import java.awt.Color;
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;
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;
32 * Conversion for values
34 * @author Marcel Verpaalen - Initial contribution
37 public class Conversions {
38 private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
41 * Converts a RGB+brightness input to a HSV value.
44 * @param RGB + brightness value (note brightness in the first byte)
47 private 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());
58 * Adds the brightness info (from separate channel) to a HSV value.
62 * @param map with device variables containing the brightness info
65 private static JsonElement addBrightToHSV(JsonElement rgbValue, @Nullable Map<String, Object> deviceVariables)
66 throws ClassCastException, IllegalStateException {
68 if (deviceVariables != null) {
69 JsonElement lastBright = (JsonElement) deviceVariables.getOrDefault("bright", new JsonPrimitive(100));
70 bright = lastBright.getAsInt();
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());
82 private static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
83 double value = seconds.getAsDouble() / 3600;
84 return new JsonPrimitive(value);
87 private static JsonElement yeelightSceneConversion(JsonElement intValue)
88 throws ClassCastException, IllegalStateException {
89 switch (intValue.getAsInt()) {
91 return new JsonPrimitive("color");
93 return new JsonPrimitive("hsv");
95 return new JsonPrimitive("ct");
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");
103 return new JsonPrimitive("unknown");
107 private static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
108 double value = value10.getAsDouble() / 10.0;
109 return new JsonPrimitive(value);
112 private static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
113 double value = value10.getAsDouble() / 100.0;
114 return new JsonPrimitive(value);
117 private 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);
122 double value = value12.getAsDouble();
123 return new JsonPrimitive(value / 1.2);
128 * Returns the deviceId element value from the Json response. If not found, returns the input
130 * @param responseValue
131 * @param deviceVariables containing the deviceId
134 private static JsonElement getDidElement(JsonElement responseValue, Map<String, Object> deviceVariables) {
135 String did = (String) deviceVariables.get("deviceId");
137 return getJsonElement(did, responseValue);
139 LOGGER.debug("deviceId not Found, no conversion");
140 return responseValue;
144 * Returns the element from the Json response. If not found, returns the input
146 * @param element to be found
147 * @param responseValue
150 private static JsonElement getJsonElement(String element, JsonElement responseValue) {
152 if (responseValue.isJsonPrimitive() || responseValue.isJsonObject()) {
153 JsonElement jsonElement = responseValue.isJsonObject() ? responseValue
154 : JsonParser.parseString(responseValue.getAsString());
155 if (jsonElement.isJsonObject()) {
156 JsonObject value = jsonElement.getAsJsonObject();
157 if (value.has(element)) {
158 return value.get(element);
162 } catch (JsonParseException e) {
165 LOGGER.debug("JsonElement '{}' not found in '{}'", element, responseValue);
166 return responseValue;
169 public static JsonElement execute(String transformation, JsonElement value, Map<String, Object> deviceVariables) {
171 if (transformation.toUpperCase().startsWith("GETJSONELEMENT")) {
172 if (transformation.length() > 15) {
173 return getJsonElement(transformation.substring(15), value);
175 LOGGER.info("Transformation {} missing element. Returning '{}'", transformation, value.toString());
178 switch (transformation.toUpperCase()) {
179 case "YEELIGHTSCENEID":
180 return yeelightSceneConversion(value);
181 case "SECONDSTOHOURS":
182 return secondsToHours(value);
184 return divideTen(value);
186 return divideHundred(value);
188 return tankLevel(value);
189 case "ADDBRIGHTTOHSV":
190 return addBrightToHSV(value, deviceVariables);
192 return bRGBtoHSV(value);
193 case "GETDIDELEMENT":
194 return getDidElement(value, deviceVariables);
196 LOGGER.debug("Transformation {} not found. Returning '{}'", transformation, value.toString());
199 } catch (ClassCastException | IllegalStateException e) {
200 LOGGER.debug("Transformation {} failed. Returning '{}'", transformation, value.toString());