]> git.basschouten.com Git - openhab-addons.git/blob
c0d644a1fe22e683752890be3776acf6687cbc7d
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.avmfritz.internal.dto;
14
15 import javax.xml.bind.annotation.XmlAccessType;
16 import javax.xml.bind.annotation.XmlAccessorType;
17 import javax.xml.bind.annotation.XmlAttribute;
18 import javax.xml.bind.annotation.XmlElement;
19 import javax.xml.bind.annotation.XmlRootElement;
20
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.PercentType;
23
24 /**
25  * See {@link DeviceListModel}.
26  *
27  * @author Joshua Bacher - Initial contribution
28  */
29 @XmlAccessorType(XmlAccessType.FIELD)
30 @XmlRootElement(name = "colorcontrol")
31 public class ColorControlModel {
32
33     private static final double SATURATION_FACTOR = 2.54;
34
35     @XmlAttribute(name = "supported_modes")
36     public int supportedModes;
37     @XmlAttribute(name = "current_mode")
38     public int currentMode;
39     public int hue;
40     public int saturation;
41     @XmlElement(name = "unmapped_hue")
42     public @Nullable Integer unmappedHue;
43     @XmlElement(name = "unmapped_saturation")
44     public @Nullable Integer unmappedSaturation;
45     public int temperature;
46
47     /**
48      * Converts a FRITZ!Box value to a percent value.
49      *
50      * @param fritzValue The FRITZ!Box value to be converted
51      * @return The percent value
52      */
53     public static PercentType toPercent(int saturation) {
54         int saturationInPercent = (int) Math.ceil(saturation / SATURATION_FACTOR);
55         return restrictToBounds(saturationInPercent);
56     }
57
58     /**
59      * Converts a percent value to a FRITZ!Box value.
60      *
61      * @param saturationInPercent The percent value to be converted
62      * @return The FRITZ!Box value
63      */
64     public static int fromPercent(PercentType saturationInPercent) {
65         return (int) Math.floor(saturationInPercent.intValue() * SATURATION_FACTOR);
66     }
67
68     @Override
69     public String toString() {
70         return new StringBuilder("[supportedModes=").append(supportedModes).append(",currentMode=").append(currentMode)
71                 .append(",hue=").append(hue).append(",saturation=").append(saturation).append(",unmapped_hue=")
72                 .append(unmappedHue).append(",unmapped_saturation=").append(unmappedSaturation).append(",temperature=")
73                 .append(temperature).append("]").toString();
74     }
75
76     private static PercentType restrictToBounds(int percentValue) {
77         if (percentValue < 0) {
78             return PercentType.ZERO;
79         } else if (percentValue > 100) {
80             return PercentType.HUNDRED;
81         }
82         return new PercentType(percentValue);
83     }
84 }