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 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());
58 * Adds the brightness info (from separate channel) to a HSV value.
62 * @param map with device variables containing the brightness info
65 public 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 public static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
83 double value = seconds.getAsDouble() / 3600;
84 return new JsonPrimitive(value);
87 public 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 public static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
108 double value = value10.getAsDouble() / 10.0;
109 return new JsonPrimitive(value);
112 public static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
113 double value = value10.getAsDouble() / 100.0;
114 return new JsonPrimitive(value);
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);
122 double value = value12.getAsDouble();
123 return new JsonPrimitive(value / 1.2);
127 public static JsonElement getJsonElement(String element, JsonElement responseValue) {
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);
139 } catch (JsonParseException e) {
142 LOGGER.debug("JsonElement '{}' not found in '{}'", element, responseValue);
143 return responseValue;
146 public static JsonElement execute(String transformation, JsonElement value,
147 @Nullable Map<String, Object> deviceVariables) {
149 if (transformation.toUpperCase().startsWith("GETJSONELEMENT")) {
150 if (transformation.length() > 15) {
151 return getJsonElement(transformation.substring(15), value);
153 LOGGER.info("Transformation {} missing element. Returning '{}'", transformation, value.toString());
156 switch (transformation.toUpperCase()) {
157 case "YEELIGHTSCENEID":
158 return yeelightSceneConversion(value);
159 case "SECONDSTOHOURS":
160 return secondsToHours(value);
162 return divideTen(value);
164 return divideHundred(value);
166 return tankLevel(value);
167 case "ADDBRIGHTTOHSV":
168 return addBrightToHSV(value, deviceVariables);
170 return bRGBtoHSV(value);
172 LOGGER.debug("Transformation {} not found. Returning '{}'", transformation, value.toString());
175 } catch (ClassCastException | IllegalStateException e) {
176 LOGGER.debug("Transformation {} failed. Returning '{}'", transformation, value.toString());