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.util.concurrent.TimeUnit;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonPrimitive;
25 * Conversion for values
27 * @author Marcel Verpaalen - Initial contribution
30 public class Conversions {
31 private static final Logger LOGGER = LoggerFactory.getLogger(Conversions.class);
33 public static JsonElement secondsToHours(JsonElement seconds) {
34 long hours = TimeUnit.SECONDS.toHours(seconds.getAsInt());
35 return new JsonPrimitive(hours);
38 public static JsonElement yeelightSceneConversion(JsonElement intValue) {
39 switch (intValue.getAsInt()) {
41 return new JsonPrimitive("color");
43 return new JsonPrimitive("hsv");
45 return new JsonPrimitive("ct");
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");
53 return new JsonPrimitive("unknown");
57 public static JsonElement divideTen(JsonElement value10) {
58 double value = value10.getAsDouble() / 10.0;
59 return new JsonPrimitive(value);
62 public static JsonElement divideHundred(JsonElement value10) {
63 double value = value10.getAsDouble() / 100.0;
64 return new JsonPrimitive(value);
67 public static JsonElement tankLevel(JsonElement value12) {
68 // 127 without water tank. 120 = 100% water
69 if (value12.getAsInt() == 127) {
70 return new JsonPrimitive(-1);
72 double value = value12.getAsDouble();
73 return new JsonPrimitive(value / 1.2);
77 public static JsonElement execute(String transfortmation, JsonElement value) {
78 switch (transfortmation.toUpperCase()) {
79 case "YEELIGHTSCENEID":
80 return yeelightSceneConversion(value);
81 case "SECONDSTOHOURS":
82 return secondsToHours(value);
84 return divideTen(value);
86 return divideHundred(value);
88 return tankLevel(value);
90 LOGGER.debug("Transformation {} not found. Returning '{}'", transfortmation, value.toString());