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