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