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 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));
145 public void initialize() {
148 updateStatus(ThingStatus.UNKNOWN);
150 var configurationMap = getThing().getConfiguration();
152 if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
153 xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
155 if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
156 bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
160 void setState(@Nullable PrimitiveType state) {
161 super.setLastStatusStored();
162 updateState(SoulissBindingConstants.SLEEP_CHANNEL, OnOffType.OFF);
164 logger.debug("T16, setting state to {}", state.toFullString());
165 this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
170 public void setRawState(byte rawState) {
171 throw new UnsupportedOperationException("Not Implemented, yet.");
174 public void setRawStateCommand(byte rawStateByte0) {
175 super.setLastStatusStored();
176 if (rawStateByte0 != t1nRawStateByte0) {
177 this.setState(getOhStateOnOffFromSoulissVal(rawStateByte0));
181 public void setRawStateRgb(byte rawStateRedByte1, byte rawStateGreenByte2, byte rawStateBluByte3) {
182 super.setLastStatusStored();
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);
189 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
190 PercentType.valueOf(localHsbState.getBrightness().toString()));
192 updateState(SoulissBindingConstants.LED_COLOR_CHANNEL, localHsbState);
195 t1nRawStateRedByte1 = rawStateRedByte1;
196 t1nRawStateGreenByte2 = rawStateGreenByte2;
197 t1nRawStateBluByte3 = rawStateBluByte3;
201 public byte getRawState() {
202 throw new UnsupportedOperationException("Not Implemented, yet.");
205 public byte getRawStateCommand() {
206 return t1nRawStateByte0;
209 public byte[] getRawStateValues() {
210 return new byte[] { t1nRawStateRedByte1, t1nRawStateGreenByte2, t1nRawStateBluByte3 };
214 public byte getExpectedRawState(byte bCmd) {
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) {
222 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
228 HSBType gethsb(byte rawStateRedByte1, byte rawStateGreenByte2, byte rawStateBluByte3) {
229 return HSBType.fromRGB(rawStateRedByte1, rawStateGreenByte2, rawStateBluByte3);