]> git.basschouten.com Git - openhab-addons.git/blob
1eb23548d85fbf9c9dde0ebea9bce60b4ca39d14
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.util.concurrent.TimeUnit;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonPrimitive;
23
24 /**
25  * Conversion for values
26  *
27  * @author Marcel Verpaalen - Initial contribution
28  */
29 @NonNullByDefault
30 public class Conversions {
31     private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
32
33     public static JsonElement secondsToHours(JsonElement seconds) {
34         long hours = TimeUnit.SECONDS.toHours(seconds.getAsInt());
35         return new JsonPrimitive(hours);
36     }
37
38     public static JsonElement yeelightSceneConversion(JsonElement intValue) {
39         switch (intValue.getAsInt()) {
40             case 1:
41                 return new JsonPrimitive("color");
42             case 2:
43                 return new JsonPrimitive("hsv");
44             case 3:
45                 return new JsonPrimitive("ct");
46             case 4:
47                 return new JsonPrimitive("nightlight");
48             case 5: // don't know the number for colorflow...
49                 return new JsonPrimitive("cf");
50             case 6: // don't know the number for auto_delay_off, or if it is even in the properties visible...
51                 return new JsonPrimitive("auto_delay_off");
52             default:
53                 return new JsonPrimitive("unknown");
54         }
55     }
56
57     public static JsonElement divideTen(JsonElement value10) {
58         double value = value10.getAsDouble() / 10;
59         return new JsonPrimitive(value);
60     }
61
62     public static JsonElement tankLevel(JsonElement value12) {
63         // 127 without water tank. 120 = 100% water
64         if (value12.getAsInt() == 127) {
65             return new JsonPrimitive(-1);
66         } else {
67             double value = value12.getAsDouble();
68             return new JsonPrimitive(value / 1.2);
69         }
70     }
71
72     public static JsonElement execute(String transfortmation, JsonElement value) {
73         switch (transfortmation.toUpperCase()) {
74             case "YEELIGHTSCENEID":
75                 return yeelightSceneConversion(value);
76             case "SECONDSTOHOURS":
77                 return secondsToHours(value);
78             case "/10":
79                 return divideTen(value);
80             case "TANKLEVEL":
81                 return tankLevel(value);
82             default:
83                 LOGGER.debug("Transformation {} not found. Returning '{}'", transfortmation, value.toString());
84                 return value;
85         }
86     }
87 }