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.bigassfan.internal.utils;
15 import org.openhab.core.library.types.PercentType;
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.
22 * @author Mark Hilbush - Initial contribution
24 public class BigAssFanConverter {
26 * Conversion factor for fan range (0-7) to dimmer range (0-100).
28 private static final double SPEED_CONVERSION_FACTOR = 14.2857;
31 * Conversion factor for light range (0-16) to dimmer range (0-100).
33 private static final double BRIGHTNESS_CONVERSION_FACTOR = 6.25;
36 * Conversion factor for hue range (2200-5000) to dimmer range (0-100).
38 private static final double HUE_CONVERSION_FACTOR = 28.0;
41 * Dimmer item will produce PercentType value, which is 0-100
42 * Convert that value to what the fan expects, which is 0-7
44 public static String percentToSpeed(PercentType command) {
45 return String.valueOf((int) Math.round(command.doubleValue() / SPEED_CONVERSION_FACTOR));
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
52 public static PercentType speedToPercent(String speed) {
53 return new PercentType((int) Math.round(Integer.parseInt(speed) * SPEED_CONVERSION_FACTOR));
57 * Dimmer item will produce PercentType value, which is 0-100
58 * Convert that value to what the light expects, which is 0-16
60 public static String percentToLevel(PercentType command) {
61 return String.valueOf((int) Math.round(command.doubleValue() / BRIGHTNESS_CONVERSION_FACTOR));
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
68 public static PercentType levelToPercent(String level) {
69 return new PercentType((int) Math.round(Integer.parseInt(level) * BRIGHTNESS_CONVERSION_FACTOR));
73 * Dimmer item will produce PercentType value, which is 0-100
74 * Convert that value to what the light expects, which is 2200-5000
76 public static String percentToHue(PercentType command) {
77 return String.valueOf(2200 + (int) Math.round(command.doubleValue() * HUE_CONVERSION_FACTOR));
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
84 public static PercentType hueToPercent(String hue) {
85 return new PercentType((int) Math.round((Integer.parseInt(hue) - 2200) / HUE_CONVERSION_FACTOR));