]> git.basschouten.com Git - openhab-addons.git/blob
d3f4202ea4e3c6ed176131e4d8a66526ec6f02f8
[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.souliss.internal.handler;
14
15 import java.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.souliss.internal.SoulissBindingConstants;
20 import org.openhab.binding.souliss.internal.SoulissProtocolConstants;
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.UpDownType;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.PrimitiveType;
30 import org.openhab.core.types.RefreshType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * The {@link SoulissT16Handler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Tonino Fazio - Initial contribution
39  * @author Luca Calcaterra - Refactor for OH3
40  */
41 @NonNullByDefault
42 public class SoulissT16Handler extends SoulissGenericHandler {
43     private final Logger logger = LoggerFactory.getLogger(SoulissT16Handler.class);
44     private byte t1nRawStateByte0 = 0xF;
45     private byte t1nRawStateRedByte1 = 0x00;
46     private byte t1nRawStateGreenByte2 = 0x00;
47     private byte t1nRawStateBluByte3 = 0x00;
48
49     private HSBType hsbState = HSBType.WHITE;
50
51     byte xSleepTime = 0;
52
53     public SoulissT16Handler(Thing thing) {
54         super(thing);
55     }
56
57     @Override
58     public void handleCommand(ChannelUID channelUID, Command command) {
59         if (command instanceof RefreshType) {
60             switch (channelUID.getId()) {
61                 case SoulissBindingConstants.ONOFF_CHANNEL:
62                     OnOffType valOnOff = getOhStateOnOffFromSoulissVal(t1nRawStateByte0);
63                     if (valOnOff != null) {
64                         updateState(channelUID, valOnOff);
65                     }
66                     break;
67                 case SoulissBindingConstants.LED_COLOR_CHANNEL:
68                     updateState(channelUID, gethsb(t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3));
69                     break;
70                 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
71                     updateState(channelUID,
72                             PercentType.valueOf(gethsb(t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3)
73                                     .getBrightness().toString()));
74                     break;
75                 default:
76                     break;
77             }
78         } else {
79             switch (channelUID.getId()) {
80                 case SoulissBindingConstants.ONOFF_CHANNEL:
81                     if (command.equals(OnOffType.ON)) {
82                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
83
84                     } else if (command.equals(OnOffType.OFF)) {
85                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
86                     }
87                     break;
88                 case SoulissBindingConstants.WHITE_MODE_CHANNEL:
89                     if (command instanceof OnOffType) {
90                         hsbState = HSBType.fromRGB(255, 255, 255);
91                         commandSendRgb(SoulissProtocolConstants.SOULISS_T1N_SET, (byte) 255, (byte) 255, (byte) 255);
92                         updateState(SoulissBindingConstants.LED_COLOR_CHANNEL, hsbState);
93                     }
94                     break;
95                 case SoulissBindingConstants.SLEEP_CHANNEL:
96                     if (command instanceof OnOffType) {
97                         commandSEND((byte) (SoulissProtocolConstants.SOULISS_T1N_TIMED + xSleepTime));
98                         // set Off
99                         updateState(channelUID, OnOffType.OFF);
100                     }
101                     break;
102
103                 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
104                     if (command instanceof PercentType) {
105                         updateState(SoulissBindingConstants.LED_COLOR_CHANNEL,
106                                 gethsb(t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3));
107                         commandSendRgb(SoulissProtocolConstants.SOULISS_T1N_SET,
108                                 (byte) (hsbState.getRed().shortValue() * (255.00 / 100)),
109                                 (byte) (hsbState.getGreen().shortValue() * (255.00 / 100)),
110                                 (byte) (hsbState.getBlue().shortValue() * (255.00 / 100)));
111
112                     } else if (command.equals(OnOffType.ON)) {
113                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
114
115                     } else if (command.equals(OnOffType.OFF)) {
116                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
117                     }
118                     break;
119
120                 case SoulissBindingConstants.ROLLER_BRIGHTNESS_CHANNEL:
121                     if (command.equals(UpDownType.UP)) {
122                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_BRIGHT_UP);
123                     } else if (command.equals(UpDownType.DOWN)) {
124                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_BRIGHT_DOWN);
125                     }
126                     break;
127
128                 case SoulissBindingConstants.LED_COLOR_CHANNEL:
129                     if (command instanceof HSBType localHsbState) {
130                         updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
131                                 PercentType.valueOf(hsbState.getBrightness().toString()));
132                         commandSendRgb(SoulissProtocolConstants.SOULISS_T1N_SET,
133                                 (byte) (localHsbState.getRed().shortValue() * 255.00 / 100),
134                                 (byte) (localHsbState.getGreen().shortValue() * 255.00 / 100),
135                                 (byte) (localHsbState.getBlue().shortValue() * 255.00 / 100));
136                     }
137                     break;
138                 default:
139                     break;
140             }
141         }
142     }
143
144     @Override
145     public void initialize() {
146         super.initialize();
147
148         updateStatus(ThingStatus.UNKNOWN);
149
150         var configurationMap = getThing().getConfiguration();
151
152         if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
153             xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
154         }
155         if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
156             bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
157         }
158     }
159
160     void setState(@Nullable PrimitiveType state) {
161         super.setLastStatusStored();
162         updateState(SoulissBindingConstants.SLEEP_CHANNEL, OnOffType.OFF);
163         if (state != null) {
164             logger.debug("T16, setting state to {}", state.toFullString());
165             this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
166         }
167     }
168
169     @Override
170     public void setRawState(byte rawState) {
171         throw new UnsupportedOperationException("Not Implemented, yet.");
172     }
173
174     public void setRawStateCommand(byte rawStateByte0) {
175         super.setLastStatusStored();
176         if (rawStateByte0 != t1nRawStateByte0) {
177             this.setState(getOhStateOnOffFromSoulissVal(rawStateByte0));
178         }
179     }
180
181     public void setRawStateRgb(byte rawStateRedByte1, byte rawStateGreenByte2, byte rawStateBluByte3) {
182         super.setLastStatusStored();
183
184         if (rawStateRedByte1 != t1nRawStateRedByte1 || rawStateGreenByte2 != t1nRawStateGreenByte2
185                 || rawStateBluByte3 != t1nRawStateBluByte3) {
186             HSBType localHsbState = gethsb(rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);
187             logger.debug("T16, setting color to {},{},{}", rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);
188
189             updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
190                     PercentType.valueOf(localHsbState.getBrightness().toString()));
191
192             updateState(SoulissBindingConstants.LED_COLOR_CHANNEL, localHsbState);
193         }
194
195         t1nRawStateRedByte1 = rawStateRedByte1;
196         t1nRawStateGreenByte2 = rawStateGreenByte2;
197         t1nRawStateBluByte3 = rawStateBluByte3;
198     }
199
200     @Override
201     public byte getRawState() {
202         throw new UnsupportedOperationException("Not Implemented, yet.");
203     }
204
205     public byte getRawStateCommand() {
206         return t1nRawStateByte0;
207     }
208
209     public byte[] getRawStateValues() {
210         return new byte[] { t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3 };
211     }
212
213     @Override
214     public byte getExpectedRawState(byte bCmd) {
215         if (bSecureSend) {
216             if (bCmd == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
217                 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
218             } else if (bCmd == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
219                 return SoulissProtocolConstants.SOULISS_T1N_OFF_COIL;
220             } else if (bCmd >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
221                 // SLEEP
222                 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
223             }
224         }
225         return -1;
226     }
227
228     HSBType gethsb(byte rawStateRedByte1, byte rawStateGreenByte2, byte rawStateBluByte3) {
229         return HSBType.fromRGB(rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);
230     }
231 }