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.JsonPrimitive;
29 * Conversion for values
31 * @author Marcel Verpaalen - Initial contribution
34 public class Conversions {
35 private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
38 * Converts a RGB+brightness input to a HSV value.
41 * @param RGB + brightness value (note brightness in the first byte)
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());
55 * Adds the brightness info (from separate channel) to a HSV value.
59 * @param map with device variables containing the brightness info
62 public static JsonElement addBrightToHSV(JsonElement rgbValue, @Nullable Map<String, Object> deviceVariables)
63 throws ClassCastException, IllegalStateException {
65 if (deviceVariables != null) {
66 JsonElement lastBright = (JsonElement) deviceVariables.getOrDefault("bright", new JsonPrimitive(100));
67 bright = lastBright.getAsInt();
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());
79 public static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
80 double value = seconds.getAsDouble() / 3600;
81 return new JsonPrimitive(value);
84 public static JsonElement yeelightSceneConversion(JsonElement intValue)
85 throws ClassCastException, IllegalStateException {
86 switch (intValue.getAsInt()) {
88 return new JsonPrimitive("color");
90 return new JsonPrimitive("hsv");
92 return new JsonPrimitive("ct");
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");
100 return new JsonPrimitive("unknown");
104 public static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
105 double value = value10.getAsDouble() / 10.0;
106 return new JsonPrimitive(value);
109 public static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
110 double value = value10.getAsDouble() / 100.0;
111 return new JsonPrimitive(value);
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);
119 double value = value12.getAsDouble();
120 return new JsonPrimitive(value / 1.2);
124 public static JsonElement execute(String transformation, JsonElement value,
125 @Nullable Map<String, Object> deviceVariables) {
127 switch (transformation.toUpperCase()) {
128 case "YEELIGHTSCENEID":
129 return yeelightSceneConversion(value);
130 case "SECONDSTOHOURS":
131 return secondsToHours(value);
133 return divideTen(value);
135 return divideHundred(value);
137 return tankLevel(value);
138 case "ADDBRIGHTTOHSV":
139 return addBrightToHSV(value, deviceVariables);
141 return bRGBtoHSV(value);
143 LOGGER.debug("Transformation {} not found. Returning '{}'", transformation, value.toString());
146 } catch (ClassCastException | IllegalStateException e) {
147 LOGGER.debug("Transformation {} failed. Returning '{}'", transformation, value.toString());