2 * Copyright (c) 2010-2022 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.lifx.internal.util;
15 import java.math.BigDecimal;
16 import java.math.RoundingMode;
17 import java.util.UUID;
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;
27 * Utility class for sharing message utility methods between objects.
29 * @author Wouter Born - Initial contribution
32 public final class LifxMessageUtil {
34 private static final BigDecimal INCREASE_DECREASE_STEP = new BigDecimal(10);
36 private static final BigDecimal ZERO = PercentType.ZERO.toBigDecimal();
37 private static final BigDecimal HUNDRED = PercentType.HUNDRED.toBigDecimal();
39 private LifxMessageUtil() {
40 // hidden utility class constructor
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();
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);
62 private static PercentType intToPercentType(int i) {
63 return new PercentType(Math.round((i / 65535.0f) * 100));
66 private static int percentTypeToInt(PercentType percentType) {
67 return (int) (percentType.floatValue() / 100 * 65535.0f);
70 public static DecimalType hueToDecimalType(int hue) {
71 return new DecimalType(hue * 360 / 65535.0f);
74 public static int decimalTypeToHue(DecimalType hue) {
75 return (int) (hue.floatValue() / 360 * 65535.0f);
78 public static PercentType saturationToPercentType(int saturation) {
79 return intToPercentType(saturation);
82 public static int percentTypeToSaturation(PercentType saturation) {
83 return percentTypeToInt(saturation);
86 public static PercentType brightnessToPercentType(int brightness) {
87 return intToPercentType(brightness);
90 public static int percentTypeToBrightness(PercentType brightness) {
91 return percentTypeToInt(brightness);
94 public static PercentType kelvinToPercentType(int kelvin, TemperatureRange temperatureRange) {
95 if (temperatureRange.getRange() == 0) {
96 return PercentType.HUNDRED;
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);
105 public static int commandToKelvin(DecimalType temperature, TemperatureRange temperatureRange) {
106 return temperature instanceof PercentType ? percentTypeToKelvin((PercentType) temperature, temperatureRange)
107 : decimalTypeToKelvin(temperature, temperatureRange);
110 public static int decimalTypeToKelvin(DecimalType temperature, TemperatureRange temperatureRange) {
111 return Math.round(Math.min(Math.max(temperature.intValue(), temperatureRange.getMinimum()),
112 temperatureRange.getMaximum()));
115 public static int percentTypeToKelvin(PercentType temperature, TemperatureRange temperatureRange) {
117 temperatureRange.getMaximum() - (temperature.floatValue() * (temperatureRange.getRange() / 100)));
120 public static PercentType infraredToPercentType(int infrared) {
121 return intToPercentType(infrared);
124 public static int percentTypeToInfrared(PercentType infrared) {
125 return percentTypeToInt(infrared);
128 public static boolean sameColors(HSBK... colors) {
129 if (colors.length <= 1) {
133 for (int i = 1; i < colors.length; i++) {
134 if (!colors[0].equals(colors[i])) {
141 public static long randomSourceId() {
142 return UUID.randomUUID().getLeastSignificantBits() & (-1L >>> 32);