2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.souliss.internal.handler;
15 import java.math.BigDecimal;
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;
35 * The {@link SoulissT16Handler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Tonino Fazio - Initial contribution
39 * @author Luca Calcaterra - Refactor for OH3
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;
49 private HSBType hsbState = HSBType.WHITE;
53 public SoulissT16Handler(Thing thing) {
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);
67 case SoulissBindingConstants.LED_COLOR_CHANNEL:
68 updateState(channelUID, gethsb(t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3));
70 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
71 updateState(channelUID,
72 PercentType.valueOf(gethsb(t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3)
73 .getBrightness().toString()));
79 switch (channelUID.getId()) {
80 case SoulissBindingConstants.ONOFF_CHANNEL:
81 if (command.equals(OnOffType.ON)) {
82 commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
84 } else if (command.equals(OnOffType.OFF)) {
85 commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
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);
95 case SoulissBindingConstants.SLEEP_CHANNEL:
96 if (command instanceof OnOffType) {
97 commandSEND((byte) (SoulissProtocolConstants.SOULISS_T1N_TIMED + xSleepTime));
99 updateState(channelUID, OnOffType.OFF);
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)));
112 } else if (command.equals(OnOffType.ON)) {
113 commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
115 } else if (command.equals(OnOffType.OFF)) {
116 commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
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);
128 case SoulissBindingConstants.LED_COLOR_CHANNEL:
129 if (command instanceof HSBType) {
130 HSBType localHsbState = (HSBType) command;
132 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
133 PercentType.valueOf(hsbState.getBrightness().toString()));
134 commandSendRgb(SoulissProtocolConstants.SOULISS_T1N_SET,
135 (byte) (localHsbState.getRed().shortValue() * 255.00 / 100),
136 (byte) (localHsbState.getGreen().shortValue() * 255.00 / 100),
137 (byte) (localHsbState.getBlue().shortValue() * 255.00 / 100));
147 public void initialize() {
150 updateStatus(ThingStatus.UNKNOWN);
152 var configurationMap = getThing().getConfiguration();
154 if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
155 xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
157 if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
158 bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
162 void setState(@Nullable PrimitiveType state) {
163 super.setLastStatusStored();
164 updateState(SoulissBindingConstants.SLEEP_CHANNEL, OnOffType.OFF);
166 logger.debug("T16, setting state to {}", state.toFullString());
167 this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
172 public void setRawState(byte rawState) {
173 throw new UnsupportedOperationException("Not Implemented, yet.");
176 public void setRawStateCommand(byte rawStateByte0) {
177 super.setLastStatusStored();
178 if (rawStateByte0 != t1nRawStateByte0) {
179 this.setState(getOhStateOnOffFromSoulissVal(rawStateByte0));
183 public void setRawStateRgb(byte rawStateRedByte1, byte rawStateGreenByte2, byte rawStateBluByte3) {
184 super.setLastStatusStored();
186 if (rawStateRedByte1 != t1nRawStateRedByte1 || rawStateGreenByte2 != t1nRawStateGreenByte2
187 || rawStateBluByte3 != t1nRawStateBluByte3) {
188 HSBType localHsbState = gethsb(rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);
189 logger.debug("T16, setting color to {},{},{}", rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);
191 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
192 PercentType.valueOf(localHsbState.getBrightness().toString()));
194 updateState(SoulissBindingConstants.LED_COLOR_CHANNEL, localHsbState);
197 t1nRawStateRedByte1 = rawStateRedByte1;
198 t1nRawStateGreenByte2 = rawStateGreenByte2;
199 t1nRawStateBluByte3 = rawStateBluByte3;
203 public byte getRawState() {
204 throw new UnsupportedOperationException("Not Implemented, yet.");
207 public byte getRawStateCommand() {
208 return t1nRawStateByte0;
211 public byte[] getRawStateValues() {
212 return new byte[] { t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3 };
216 public byte getExpectedRawState(byte bCmd) {
218 if (bCmd == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
219 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
220 } else if (bCmd == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
221 return SoulissProtocolConstants.SOULISS_T1N_OFF_COIL;
222 } else if (bCmd >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
224 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
230 HSBType gethsb(byte rawStateRedByte1, byte rawStateGreenByte2, byte rawStateBluByte3) {
231 return HSBType.fromRGB(rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);