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