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