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;
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;
23 import com.google.gson.JsonElement;
24 import com.google.gson.JsonPrimitive;
27 * Conversion for values
29 * @author Marcel Verpaalen - Initial contribution
32 public class Conversions {
33 private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
36 * Converts a RGB+brightness input to a HSV value.
39 * @param RGB + brightness value (note brightness in the first byte)
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());
52 public static JsonElement secondsToHours(JsonElement seconds) throws ClassCastException {
53 double value = seconds.getAsDouble() / 3600;
54 return new JsonPrimitive(value);
57 public static JsonElement yeelightSceneConversion(JsonElement intValue)
58 throws ClassCastException, IllegalStateException {
59 switch (intValue.getAsInt()) {
61 return new JsonPrimitive("color");
63 return new JsonPrimitive("hsv");
65 return new JsonPrimitive("ct");
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");
73 return new JsonPrimitive("unknown");
77 public static JsonElement divideTen(JsonElement value10) throws ClassCastException, IllegalStateException {
78 double value = value10.getAsDouble() / 10.0;
79 return new JsonPrimitive(value);
82 public static JsonElement divideHundred(JsonElement value10) throws ClassCastException, IllegalStateException {
83 double value = value10.getAsDouble() / 100.0;
84 return new JsonPrimitive(value);
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);
92 double value = value12.getAsDouble();
93 return new JsonPrimitive(value / 1.2);
97 public static JsonElement execute(String transfortmation, JsonElement value) {
99 switch (transfortmation.toUpperCase()) {
100 case "YEELIGHTSCENEID":
101 return yeelightSceneConversion(value);
102 case "SECONDSTOHOURS":
103 return secondsToHours(value);
105 return divideTen(value);
107 return divideHundred(value);
109 return tankLevel(value);
111 return bRGBtoHSV(value);
113 LOGGER.debug("Transformation {} not found. Returning '{}'", transfortmation, value.toString());
116 } catch (ClassCastException | IllegalStateException e) {
117 LOGGER.debug("Transformation {} failed. Returning '{}'", transfortmation, value.toString());