]> git.basschouten.com Git - openhab-addons.git/blob
3b56b1586025ac3b5945ad4dfba24e95715d3398
[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.OnOffType;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.UpDownType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.PrimitiveType;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link SoulissT19Handler} is responsible for handling commands, which are
35  * sent to one of the channels.
36  *
37  * @author Tonino Fazio - Initial contribution
38  * @author Luca Calcaterra - Refactor for OH3
39  */
40
41 @NonNullByDefault
42 public class SoulissT19Handler extends SoulissGenericHandler {
43     private final Logger logger = LoggerFactory.getLogger(SoulissT19Handler.class);
44     byte t1nRawStateByte0 = 0xF;
45     byte t1nRawStateBrigthnessByte1 = 0x00;
46
47     byte xSleepTime = 0;
48
49     public SoulissT19Handler(Thing thing) {
50         super(thing);
51     }
52
53     @Override
54     public void handleCommand(ChannelUID channelUID, Command command) {
55         if (command instanceof RefreshType) {
56             switch (channelUID.getId()) {
57                 case SoulissBindingConstants.ONOFF_CHANNEL:
58                     OnOffType valOnOff = getOhStateOnOffFromSoulissVal(t1nRawStateByte0);
59                     if (valOnOff != null) {
60                         updateState(channelUID, valOnOff);
61                     }
62                     break;
63                 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
64                     updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
65                             PercentType.valueOf(String.valueOf((t1nRawStateBrigthnessByte1 / 255) * 100)));
66                     break;
67                 default:
68                     break;
69             }
70         } else {
71             switch (channelUID.getId()) {
72                 case SoulissBindingConstants.ONOFF_CHANNEL:
73                     if (command.equals(OnOffType.ON)) {
74                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
75
76                     } else if (command.equals(OnOffType.OFF)) {
77                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
78                     }
79                     break;
80
81                 case SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL:
82                     if (command instanceof PercentType percentCommand) {
83                         updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL, percentCommand);
84                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_SET,
85                                 (byte) (percentCommand.shortValue() * 255.00 / 100.00));
86                     } else if (command.equals(OnOffType.ON)) {
87                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
88
89                     } else if (command.equals(OnOffType.OFF)) {
90                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
91                     }
92                     break;
93
94                 case SoulissBindingConstants.ROLLER_BRIGHTNESS_CHANNEL:
95                     if (command.equals(UpDownType.UP)) {
96                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_BRIGHT_UP);
97                     } else if (command.equals(UpDownType.DOWN)) {
98                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_BRIGHT_DOWN);
99                     }
100                     break;
101                 case SoulissBindingConstants.SLEEP_CHANNEL:
102                     if (command instanceof OnOffType) {
103                         commandSEND((byte) (SoulissProtocolConstants.SOULISS_T1N_TIMED + xSleepTime));
104                     }
105                     break;
106                 default:
107                     break;
108             }
109         }
110     }
111
112     @Override
113     public void initialize() {
114         super.initialize();
115
116         updateStatus(ThingStatus.UNKNOWN);
117
118         var configurationMap = getThing().getConfiguration();
119         if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
120             xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
121         }
122         if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
123             bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
124         }
125     }
126
127     public void setState(@Nullable PrimitiveType state) {
128         super.setLastStatusStored();
129         if (state != null) {
130             updateState(SoulissBindingConstants.SLEEP_CHANNEL, OnOffType.OFF);
131             logger.debug("T19, setting state to {}", state.toFullString());
132             this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
133         }
134     }
135
136     public void setRawStateDimmerValue(byte dimmerValue) {
137         try {
138             if (dimmerValue != t1nRawStateByte0 && dimmerValue >= 0) {
139                 logger.debug("T19, setting dimmer to {}", dimmerValue);
140                 updateState(SoulissBindingConstants.DIMMER_BRIGHTNESS_CHANNEL,
141                         PercentType.valueOf(String.valueOf(Math.round(((double) dimmerValue / 255) * 100))));
142             }
143         } catch (Exception ex) {
144             logger.warn("UUID: {}, had an update dimmer state error:{}", this.getThing().getUID().getAsString(),
145                     ex.getMessage());
146         }
147     }
148
149     @Override
150     public void setRawState(byte rawState) {
151         // update Last Status stored time
152         super.setLastStatusStored();
153         // update item state only if it is different from previous
154         if (t1nRawStateByte0 != rawState) {
155             this.setState(getOhStateOnOffFromSoulissVal(rawState));
156         }
157         t1nRawStateByte0 = rawState;
158     }
159
160     @Override
161     public byte getRawState() {
162         return t1nRawStateByte0;
163     }
164
165     public byte getRawStateDimmerValue() {
166         return t1nRawStateBrigthnessByte1;
167     }
168
169     @Override
170     public byte getExpectedRawState(byte bCmd) {
171         if (bSecureSend) {
172             if (bCmd == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
173                 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
174             } else if (bCmd == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
175                 return SoulissProtocolConstants.SOULISS_T1N_OFF_COIL;
176             } else if (bCmd >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
177                 // SLEEP
178                 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
179             }
180         }
181         return -1;
182     }
183 }