]> git.basschouten.com Git - openhab-addons.git/blob
b250e47e008e9447e642347cb27e904b9a405357
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.lifx.internal.util;
14
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.util.UUID;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lifx.internal.LifxProduct.TemperatureRange;
21 import org.openhab.binding.lifx.internal.fields.HSBK;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.IncreaseDecreaseType;
24 import org.openhab.core.library.types.PercentType;
25
26 /**
27  * Utility class for sharing message utility methods between objects.
28  *
29  * @author Wouter Born - Initial contribution
30  */
31 @NonNullByDefault
32 public final class LifxMessageUtil {
33
34     private static final BigDecimal INCREASE_DECREASE_STEP = new BigDecimal(10);
35
36     private static final BigDecimal ZERO = PercentType.ZERO.toBigDecimal();
37     private static final BigDecimal HUNDRED = PercentType.HUNDRED.toBigDecimal();
38
39     private LifxMessageUtil() {
40         // hidden utility class constructor
41     }
42
43     public static PercentType increaseDecreasePercentType(IncreaseDecreaseType increaseDecreaseType, PercentType old) {
44         BigDecimal delta = ZERO;
45         if (increaseDecreaseType == IncreaseDecreaseType.INCREASE) {
46             delta = INCREASE_DECREASE_STEP;
47         } else if (increaseDecreaseType == IncreaseDecreaseType.DECREASE) {
48             delta = INCREASE_DECREASE_STEP.negate();
49         }
50
51         if (!ZERO.equals(delta)) {
52             BigDecimal newValue = old.toBigDecimal().add(delta);
53             newValue = newValue.setScale(0, RoundingMode.HALF_UP);
54             newValue = newValue.min(HUNDRED);
55             newValue = newValue.max(ZERO);
56             return new PercentType(newValue);
57         } else {
58             return old;
59         }
60     }
61
62     private static PercentType intToPercentType(int i) {
63         return new PercentType(Math.round((i / 65535.0f) * 100));
64     }
65
66     private static int percentTypeToInt(PercentType percentType) {
67         return (int) (percentType.floatValue() / 100 * 65535.0f);
68     }
69
70     public static DecimalType hueToDecimalType(int hue) {
71         return new DecimalType(hue * 360 / 65535.0f);
72     }
73
74     public static int decimalTypeToHue(DecimalType hue) {
75         return (int) (hue.floatValue() / 360 * 65535.0f);
76     }
77
78     public static PercentType saturationToPercentType(int saturation) {
79         return intToPercentType(saturation);
80     }
81
82     public static int percentTypeToSaturation(PercentType saturation) {
83         return percentTypeToInt(saturation);
84     }
85
86     public static PercentType brightnessToPercentType(int brightness) {
87         return intToPercentType(brightness);
88     }
89
90     public static int percentTypeToBrightness(PercentType brightness) {
91         return percentTypeToInt(brightness);
92     }
93
94     public static PercentType kelvinToPercentType(int kelvin, TemperatureRange temperatureRange) {
95         if (temperatureRange.getRange() == 0) {
96             return PercentType.HUNDRED;
97         }
98         BigDecimal value = BigDecimal
99                 .valueOf((kelvin - temperatureRange.getMaximum()) / (temperatureRange.getRange() / -100));
100         value = value.min(HUNDRED);
101         value = value.max(ZERO);
102         return new PercentType(value);
103     }
104
105     public static int commandToKelvin(DecimalType temperature, TemperatureRange temperatureRange) {
106         return temperature instanceof PercentType ? percentTypeToKelvin((PercentType) temperature, temperatureRange)
107                 : decimalTypeToKelvin(temperature, temperatureRange);
108     }
109
110     public static int decimalTypeToKelvin(DecimalType temperature, TemperatureRange temperatureRange) {
111         return Math.round(Math.min(Math.max(temperature.intValue(), temperatureRange.getMinimum()),
112                 temperatureRange.getMaximum()));
113     }
114
115     public static int percentTypeToKelvin(PercentType temperature, TemperatureRange temperatureRange) {
116         return Math.round(
117                 temperatureRange.getMaximum() - (temperature.floatValue() * (temperatureRange.getRange() / 100)));
118     }
119
120     public static PercentType infraredToPercentType(int infrared) {
121         return intToPercentType(infrared);
122     }
123
124     public static int percentTypeToInfrared(PercentType infrared) {
125         return percentTypeToInt(infrared);
126     }
127
128     public static boolean sameColors(HSBK... colors) {
129         if (colors.length <= 1) {
130             return true;
131         }
132
133         for (int i = 1; i < colors.length; i++) {
134             if (!colors[0].equals(colors[i])) {
135                 return false;
136             }
137         }
138         return true;
139     }
140
141     public static long randomSourceId() {
142         return UUID.randomUUID().getLeastSignificantBits() & (-1L >>> 32);
143     }
144 }