]> git.basschouten.com Git - openhab-addons.git/blob
3741f1ad519812de3ee48ba87edd1194c3ef24db
[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.wifiled.internal.handler;
14
15 import static java.lang.Math.*;
16
17 import java.awt.Color;
18 import java.math.BigDecimal;
19 import java.math.RoundingMode;
20
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.StringType;
26
27 /**
28  * The {@link LEDStateDTO} class holds the data and the settings for a LED device (i.e. the selected colors, the running
29  * program, etc.).
30  *
31  * @author Osman Basha - Initial contribution
32  * @author Stefan Endrullis - Initial contribution
33  * @author Ries van Twisk - Prevent flashes during classic driver color + white updates
34  */
35 public class LEDStateDTO {
36     private HSBType hsbType;
37     private OnOffType power;
38     private PercentType white;
39     private PercentType white2;
40     private StringType program;
41     private PercentType programSpeed;
42
43     public LEDStateDTO(OnOffType power, DecimalType hue, PercentType saturation, PercentType brightness,
44             PercentType white, PercentType white2, StringType program, PercentType programSpeed) {
45         this.hsbType = new HSBType(hue, saturation, brightness);
46         this.power = power;
47         this.white = white;
48         this.white2 = white2;
49         this.program = program;
50         this.programSpeed = programSpeed;
51     }
52
53     public LEDStateDTO(OnOffType power, HSBType hsb, PercentType white, PercentType white2, StringType program,
54             PercentType programSpeed) {
55         this.hsbType = hsb;
56         this.power = power;
57         this.white = white;
58         this.white2 = white2;
59         this.program = program;
60         this.programSpeed = programSpeed;
61     }
62
63     public PercentType getWhite() {
64         return white;
65     }
66
67     public PercentType getWhite2() {
68         return white2;
69     }
70
71     public StringType getProgram() {
72         return program;
73     }
74
75     public PercentType getProgramSpeed() {
76         return programSpeed;
77     }
78
79     @Override
80     public String toString() {
81         return power + "," + hsbType.getHue() + "," + hsbType.getSaturation() + "," + hsbType.getBrightness() + ","
82                 + getWhite() + "," + getWhite2() + " [" + getProgram() + "," + getProgramSpeed() + "]";
83     }
84
85     public static LEDStateDTO valueOf(int state, int program, int programSpeed, int red, int green, int blue, int white,
86             int white2) {
87         OnOffType power = (state & 0x01) != 0 ? OnOffType.ON : OnOffType.OFF;
88
89         float[] hsv = new float[3];
90         Color.RGBtoHSB(red, green, blue, hsv);
91         DecimalType h = new DecimalType(new BigDecimal(hsv[0]).multiply(new BigDecimal(360.0)));
92         PercentType s = new PercentType(new BigDecimal(hsv[1]).multiply(new BigDecimal(100.0)));
93         PercentType b = new PercentType(new BigDecimal(hsv[2]).multiply(new BigDecimal(100.0)));
94         HSBType hsbType = new HSBType(h, s, b);
95
96         PercentType w = new PercentType(new BigDecimal(white).divide(new BigDecimal(255.0), 3, RoundingMode.HALF_UP)
97                 .multiply(new BigDecimal(100.0)));
98         PercentType w2 = new PercentType(new BigDecimal(white2).divide(new BigDecimal(255.0), 3, RoundingMode.HALF_UP)
99                 .multiply(new BigDecimal(100.0)));
100
101         // Range: 0x00 .. 0x1F. Speed is inversed
102         BigDecimal ps = new BigDecimal(programSpeed).divide(new BigDecimal(0x1f), 2, RoundingMode.HALF_UP)
103                 .multiply(new BigDecimal(100.0));
104         PercentType e = new PercentType(new BigDecimal(100.0).subtract(ps));
105
106         StringType p = new StringType(Integer.toString(program));
107
108         return new LEDStateDTO(power, hsbType, w, w2, p, e);
109     }
110
111     public LEDStateDTO withColor(HSBType color) {
112         return new LEDStateDTO(power, color, this.getWhite(), this.getWhite2(), this.getProgram(),
113                 this.getProgramSpeed());
114     }
115
116     public LEDStateDTO withBrightness(PercentType brightness) {
117         return new LEDStateDTO(power, hsbType.getHue(), hsbType.getSaturation(), brightness, this.getWhite(),
118                 this.getWhite2(), this.getProgram(), this.getProgramSpeed());
119     }
120
121     public LEDStateDTO withIncrementedBrightness(int step) {
122         int brightness = hsbType.getBrightness().intValue();
123         brightness = max(min(brightness + step, 0), 100);
124
125         return withBrightness(new PercentType(brightness));
126     }
127
128     public LEDStateDTO withWhite(PercentType white) {
129         return new LEDStateDTO(power, hsbType, white, this.getWhite2(), this.getProgram(), this.getProgramSpeed());
130     }
131
132     public LEDStateDTO withIncrementedWhite(int step) {
133         int white = this.getWhite().intValue();
134         white = max(min(white + step, 0), 100);
135
136         return withWhite(new PercentType(white));
137     }
138
139     public LEDStateDTO withWhite2(PercentType white2) {
140         return new LEDStateDTO(power, hsbType, this.getWhite(), white2, this.getProgram(), this.getProgramSpeed());
141     }
142
143     public LEDStateDTO withIncrementedWhite2(int step) {
144         int white = this.getWhite().intValue();
145         white = max(min(white + step, 0), 100);
146
147         return withWhite(new PercentType(white));
148     }
149
150     public LEDStateDTO withProgram(StringType program) {
151         return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), program, this.getProgramSpeed());
152     }
153
154     public LEDStateDTO withoutProgram() {
155         return withProgram(new StringType(String.valueOf(0x61)));
156     }
157
158     public LEDStateDTO withProgramSpeed(PercentType programSpeed) {
159         return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), this.getProgram(), programSpeed);
160     }
161
162     public LEDStateDTO withIncrementedProgramSpeed(int step) {
163         int programSpeed = this.getProgramSpeed().intValue();
164         programSpeed = max(min(programSpeed + step, 0), 100);
165
166         return withProgramSpeed(new PercentType(programSpeed));
167     }
168
169     public LEDStateDTO withPower(OnOffType power) {
170         return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), this.getProgram(), getProgramSpeed());
171     }
172
173     public int getRGB() {
174         return hsbType.getRGB();
175     }
176
177     public HSBType getHSB() {
178         return hsbType;
179     }
180
181     public OnOffType getPower() {
182         return power;
183     }
184 }