]> git.basschouten.com Git - openhab-addons.git/blob
27bf06454bb0ebd4aecad91dda6cf0ea15e22b77
[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.tradfri.internal.model;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.tradfri.internal.TradfriColor;
20 import org.openhab.core.library.types.HSBType;
21 import org.openhab.core.library.types.PercentType;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import com.google.gson.JsonElement;
26 import com.google.gson.JsonPrimitive;
27
28 /**
29  * The {@link TradfriLightData} class is a Java wrapper for the raw JSON data about the light state.
30  *
31  * @author Kai Kreuzer - Initial contribution
32  * @author Holger Reichert - Support for color bulbs
33  * @author Christoph Weitkamp - Restructuring and refactoring of the binding
34  */
35 @NonNullByDefault
36 public class TradfriLightData extends TradfriDeviceData {
37
38     private final Logger logger = LoggerFactory.getLogger(TradfriLightData.class);
39
40     public TradfriLightData() {
41         super(LIGHT);
42     }
43
44     public TradfriLightData(JsonElement json) {
45         super(LIGHT, json);
46     }
47
48     public TradfriLightData setBrightness(PercentType brightness) {
49         attributes.add(DIMMER, new JsonPrimitive((int) Math.floor(brightness.doubleValue() * 2.54)));
50         return this;
51     }
52
53     public @Nullable PercentType getBrightness() {
54         PercentType result = null;
55
56         JsonElement dimmer = attributes.get(DIMMER);
57         if (dimmer != null) {
58             result = TradfriColor.xyBrightnessToPercentType(dimmer.getAsInt());
59         }
60
61         return result;
62     }
63
64     public TradfriLightData setTransitionTime(int seconds) {
65         attributes.add(TRANSITION_TIME, new JsonPrimitive(seconds));
66         return this;
67     }
68
69     public int getTransitionTime() {
70         JsonElement transitionTime = attributes.get(TRANSITION_TIME);
71         if (transitionTime != null) {
72             return transitionTime.getAsInt();
73         } else {
74             return 0;
75         }
76     }
77
78     public TradfriLightData setColorTemperature(PercentType c) {
79         TradfriColor color = new TradfriColor(c);
80         int x = color.xyX;
81         int y = color.xyY;
82         logger.debug("New color temperature: {},{} ({} %)", x, y, c.intValue());
83         attributes.add(COLOR_X, new JsonPrimitive(x));
84         attributes.add(COLOR_Y, new JsonPrimitive(y));
85         return this;
86     }
87
88     public @Nullable PercentType getColorTemperature() {
89         JsonElement colorX = attributes.get(COLOR_X);
90         JsonElement colorY = attributes.get(COLOR_Y);
91         if (colorX != null && colorY != null) {
92             TradfriColor color = new TradfriColor(colorX.getAsInt(), colorY.getAsInt(), null);
93             return color.getColorTemperature();
94         } else {
95             return null;
96         }
97     }
98
99     public TradfriLightData setColor(HSBType hsb) {
100         TradfriColor color = new TradfriColor(hsb);
101         attributes.add(COLOR_X, new JsonPrimitive(color.xyX));
102         attributes.add(COLOR_Y, new JsonPrimitive(color.xyY));
103         return this;
104     }
105
106     public @Nullable HSBType getColor() {
107         // XY color coordinates plus brightness is needed for color calculation
108         JsonElement colorX = attributes.get(COLOR_X);
109         JsonElement colorY = attributes.get(COLOR_Y);
110         JsonElement dimmer = attributes.get(DIMMER);
111         if (colorX != null && colorY != null && dimmer != null) {
112             int x = colorX.getAsInt();
113             int y = colorY.getAsInt();
114             int brightness = dimmer.getAsInt();
115             TradfriColor color = new TradfriColor(x, y, brightness);
116             return color.getHSB();
117         }
118         return null;
119     }
120
121     public TradfriLightData setOnOffState(boolean on) {
122         attributes.add(ONOFF, new JsonPrimitive(on ? 1 : 0));
123         return this;
124     }
125
126     public boolean getOnOffState() {
127         JsonElement onOff = attributes.get(ONOFF);
128         if (onOff != null) {
129             return onOff.getAsInt() == 1;
130         } else {
131             return false;
132         }
133     }
134
135     public String getJsonString() {
136         return root.toString();
137     }
138 }