]> git.basschouten.com Git - openhab-addons.git/blob
5bd799c9ecce0ea860dd3aba642524ac28262513
[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.milight.internal.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.milight.internal.MilightThingState;
17 import org.openhab.binding.milight.internal.protocol.ProtocolConstants;
18 import org.openhab.binding.milight.internal.protocol.QueueItem;
19 import org.openhab.binding.milight.internal.protocol.QueuedSend;
20 import org.openhab.core.thing.Thing;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Implements functionality for the very first RGB bulbs for protocol version 2.
26  * A lot of stuff is not supported by this type of bulbs and they are not auto discovered
27  * and can only be add manually in the things file.
28  *
29  * @author David Graeff - Initial contribution
30  */
31 @NonNullByDefault
32 public class MilightV2RGBHandler extends AbstractLedHandler {
33     protected final Logger logger = LoggerFactory.getLogger(MilightV2RGBHandler.class);
34     protected static final int BR_LEVELS = 9;
35
36     public MilightV2RGBHandler(Thing thing, QueuedSend sendQueue) {
37         super(thing, sendQueue, 5);
38     }
39
40     protected QueueItem createRepeatable(byte[] data) {
41         return QueueItem.createRepeatable(socket, delayTimeMS, repeatTimes, address, port, data);
42     }
43
44     protected QueueItem createRepeatable(int uidc, byte[] data) {
45         return new QueueItem(socket, uidc, data, true, delayTimeMS, repeatTimes, address, port);
46     }
47
48     protected QueueItem createNonRepeatable(byte[] data) {
49         return QueueItem.createNonRepeatable(socket, delayTimeMS, address, port, data);
50     }
51
52     // We have no real saturation control for RGB bulbs. If the saturation is under a 50% threshold
53     // we just change to white mode instead.
54     @Override
55     public void setHSB(int hue, int saturation, int brightness, MilightThingState state) {
56         if (saturation < 50) {
57             whiteMode(state);
58         } else {
59             setPower(true, state);
60             state.hue360 = hue;
61             sendQueue.queue(createRepeatable(uidc(ProtocolConstants.CAT_COLOR_SET),
62                     new byte[] { 0x20, AbstractLedV3Handler.makeColor(hue), 0x55 }));
63
64             if (brightness != -1) {
65                 setBrightness(brightness, state);
66             }
67         }
68     }
69
70     @Override
71     public void setPower(boolean on, MilightThingState state) {
72         if (on) {
73             logger.debug("milight: sendOn");
74             byte messageBytes[] = null;
75             // message rgb bulbs ON
76             messageBytes = new byte[] { 0x22, 0x00, 0x55 };
77             sendQueue.queue(createRepeatable(uidc(ProtocolConstants.CAT_POWER_MODE), messageBytes));
78         } else {
79             logger.debug("milight: sendOff");
80             byte messageBytes[] = null;
81
82             // message rgb bulbs OFF
83             messageBytes = new byte[] { 0x21, 0x00, 0x55 };
84
85             sendQueue.queue(createRepeatable(uidc(ProtocolConstants.CAT_POWER_MODE), messageBytes));
86         }
87     }
88
89     @Override
90     public void whiteMode(MilightThingState state) {
91     }
92
93     @Override
94     public void nightMode(MilightThingState state) {
95     }
96
97     @Override
98     public void changeColorTemperature(int colorTempRelative, MilightThingState state) {
99     }
100
101     @Override
102     public void setLedMode(int mode, MilightThingState state) {
103     }
104
105     @Override
106     public void setSaturation(int value, MilightThingState state) {
107     }
108
109     @Override
110     public void changeSaturation(int relativeSaturation, MilightThingState state) {
111     }
112
113     @Override
114     public void setColorTemperature(int colorTemp, MilightThingState state) {
115     }
116
117     @Override
118     public void setBrightness(int value, MilightThingState state) {
119         if (value <= 0) {
120             setPower(false, state);
121             return;
122         }
123
124         if (value > state.brightness) {
125             int repeatCount = (value - state.brightness) / 10;
126             for (int i = 0; i < repeatCount; i++) {
127
128                 changeBrightness(+1, state);
129
130             }
131         } else if (value < state.brightness) {
132             int repeatCount = (state.brightness - value) / 10;
133             for (int i = 0; i < repeatCount; i++) {
134
135                 changeBrightness(-1, state);
136             }
137         }
138
139         state.brightness = value;
140     }
141
142     @Override
143     public void changeBrightness(int relativeBrightness, MilightThingState state) {
144         int newPercent = state.brightness + relativeBrightness;
145         if (newPercent < 0) {
146             newPercent = 0;
147         }
148         if (newPercent > 100) {
149             newPercent = 100;
150         }
151         if (state.brightness != -1 && newPercent == 0) {
152             setPower(false, state);
153         } else {
154             setPower(true, state);
155             int steps = (int) Math.abs(Math.floor(relativeBrightness * BR_LEVELS / 100.0));
156             for (int s = 0; s < steps; ++s) {
157                 byte[] t = { (byte) (relativeBrightness < 0 ? 0x24 : 0x23), 0x00, 0x55 };
158                 sendQueue.queue(createNonRepeatable(t));
159             }
160         }
161         state.brightness = newPercent;
162     }
163
164     @Override
165     public void changeSpeed(int relativeSpeed, MilightThingState state) {
166     }
167
168     @Override
169     public void previousAnimationMode(MilightThingState state) {
170         setPower(true, state);
171         sendQueue.queue(createNonRepeatable(new byte[] { 0x28, 0x00, 0x55 }));
172         state.animationMode = Math.min(state.animationMode - 1, 0);
173     }
174
175     @Override
176     public void nextAnimationMode(MilightThingState state) {
177         setPower(true, state);
178         sendQueue.queue(createNonRepeatable(new byte[] { 0x27, 0x00, 0x55 }));
179         state.animationMode = Math.max(state.animationMode + 1, 100);
180     }
181 }