]> git.basschouten.com Git - openhab-addons.git/blob
c387f07f0381a63b4d793927210046af658dbfea
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.bigassfan.internal.utils;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.core.library.types.PercentType;
17
18 /**
19  * The {@link BigAssFanConverter} is responsible for converting between
20  * Dimmer values and values used for fan speed, light brightness, and
21  * light color temperature.
22  *
23  * @author Mark Hilbush - Initial contribution
24  */
25 @NonNullByDefault
26 public class BigAssFanConverter {
27     /*
28      * Conversion factor for fan range (0-7) to dimmer range (0-100).
29      */
30     private static final double SPEED_CONVERSION_FACTOR = 14.2857;
31
32     /*
33      * Conversion factor for light range (0-16) to dimmer range (0-100).
34      */
35     private static final double BRIGHTNESS_CONVERSION_FACTOR = 6.25;
36
37     /*
38      * Conversion factor for hue range (2200-5000) to dimmer range (0-100).
39      */
40     private static final double HUE_CONVERSION_FACTOR = 28.0;
41
42     /*
43      * Dimmer item will produce PercentType value, which is 0-100
44      * Convert that value to what the fan expects, which is 0-7
45      */
46     public static String percentToSpeed(PercentType command) {
47         return String.valueOf((int) Math.round(command.doubleValue() / SPEED_CONVERSION_FACTOR));
48     }
49
50     /*
51      * Fan will supply fan speed value in range of 0-7
52      * Convert that value to a PercentType in range 0-100, which is what Dimmer item expects
53      */
54     public static PercentType speedToPercent(String speed) {
55         return new PercentType((int) Math.round(Integer.parseInt(speed) * SPEED_CONVERSION_FACTOR));
56     }
57
58     /*
59      * Dimmer item will produce PercentType value, which is 0-100
60      * Convert that value to what the light expects, which is 0-16
61      */
62     public static String percentToLevel(PercentType command) {
63         return String.valueOf((int) Math.round(command.doubleValue() / BRIGHTNESS_CONVERSION_FACTOR));
64     }
65
66     /*
67      * Light will supply brightness value in range of 0-16
68      * Convert that value to a PercentType in range 0-100, which is what Dimmer item expects
69      */
70     public static PercentType levelToPercent(String level) {
71         return new PercentType((int) Math.round(Integer.parseInt(level) * BRIGHTNESS_CONVERSION_FACTOR));
72     }
73
74     /*
75      * Dimmer item will produce PercentType value, which is 0-100
76      * Convert that value to what the light expects, which is 2200-5000
77      */
78     public static String percentToHue(PercentType command) {
79         return String.valueOf(2200 + (int) Math.round(command.doubleValue() * HUE_CONVERSION_FACTOR));
80     }
81
82     /*
83      * Light will supply hue value in range of 2200-5000
84      * Convert that value to a PercentType in range 0-100, which is what Dimmer item expects
85      */
86     public static PercentType hueToPercent(String hue) {
87         return new PercentType((int) Math.round((Integer.parseInt(hue) - 2200) / HUE_CONVERSION_FACTOR));
88     }
89 }