]> git.basschouten.com Git - openhab-addons.git/blob
7218a29f3dcc57875f1952d52850229913ac2dfe
[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.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.fields.HSBK;
21 import org.openhab.binding.lifx.internal.protocol.Product.TemperatureRange;
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 - Extracted methods from LifxLightHandler
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 percentTypeToKelvin(PercentType temperature, TemperatureRange temperatureRange) {
106         return Math.round(
107                 temperatureRange.getMaximum() - (temperature.floatValue() * (temperatureRange.getRange() / 100)));
108     }
109
110     public static PercentType infraredToPercentType(int infrared) {
111         return intToPercentType(infrared);
112     }
113
114     public static int percentTypeToInfrared(PercentType infrared) {
115         return percentTypeToInt(infrared);
116     }
117
118     public static boolean sameColors(HSBK... colors) {
119         if (colors.length <= 1) {
120             return true;
121         }
122
123         for (int i = 1; i < colors.length; i++) {
124             if (!colors[0].equals(colors[i])) {
125                 return false;
126             }
127         }
128         return true;
129     }
130
131     public static long randomSourceId() {
132         return UUID.randomUUID().getLeastSignificantBits() & (-1L >>> 32);
133     }
134 }