]> git.basschouten.com Git - openhab-addons.git/blob
e0ff59ab4651fe2971963dc88d3010a28db4bcd8
[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.openhab.binding.souliss.internal.SoulissBindingConstants;
19 import org.openhab.binding.souliss.internal.SoulissProtocolConstants;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.PrimitiveType;
26 import org.openhab.core.types.RefreshType;
27
28 /**
29  * The {@link SoulissT12Handler} is responsible for handling commands, which are
30  * sent to one of the channels.
31  *
32  * @author Tonino Fazio - Initial contribution
33  * @author Luca Calcaterra - Refactor for OH3
34  */
35 @NonNullByDefault
36 public class SoulissT12Handler extends SoulissGenericHandler {
37
38     private byte t1nRawState = 0xF;
39     private byte xSleepTime = 0;
40
41     public SoulissT12Handler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public void initialize() {
47         super.initialize();
48
49         updateStatus(ThingStatus.UNKNOWN);
50
51         var configurationMap = getThing().getConfiguration();
52         if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
53             xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
54         }
55         if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
56             bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
57         }
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         if (command instanceof RefreshType) {
63             switch (channelUID.getId()) {
64                 case SoulissBindingConstants.ONOFF_CHANNEL:
65                     switch (t1nRawState) {
66                         case SoulissProtocolConstants.SOULISS_T1N_ON_COIL_AUTO:
67                         case SoulissProtocolConstants.SOULISS_T1N_ON_COIL:
68                             this.setState(OnOffType.ON);
69                             break;
70                         case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL_AUTO:
71                         case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL:
72                             this.setState(OnOffType.OFF);
73                             break;
74                         default:
75                             break;
76                     }
77                     break;
78                 case SoulissBindingConstants.AUTOMODE_CHANNEL:
79                     switch (t1nRawState) {
80                         case SoulissProtocolConstants.SOULISS_T1N_ON_COIL_AUTO:
81                         case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL_AUTO:
82                             this.setStateAutomode(OnOffType.ON);
83                             break;
84                         case SoulissProtocolConstants.SOULISS_T1N_ON_COIL:
85                         case SoulissProtocolConstants.SOULISS_T1N_OFF_COIL:
86                             this.setStateAutomode(OnOffType.OFF);
87                             break;
88                         default:
89                             break;
90                     }
91                     break;
92                 default:
93                     break;
94             }
95         } else
96
97         {
98             switch (channelUID.getId()) {
99                 case SoulissBindingConstants.ONOFF_CHANNEL:
100                     if (command.equals(OnOffType.ON)) {
101                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
102                     } else if (command.equals(OnOffType.OFF)) {
103                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
104                     }
105                     break;
106                 case SoulissBindingConstants.AUTOMODE_CHANNEL:
107                     if (command.equals(OnOffType.ON)) {
108                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_AUTO_CMD);
109                     }
110                     break;
111                 case SoulissBindingConstants.SLEEP_CHANNEL:
112                     if (command.equals(OnOffType.ON)) {
113                         commandSEND((byte) (SoulissProtocolConstants.SOULISS_T1N_TIMED + xSleepTime));
114                         // set Off
115                         updateState(channelUID, OnOffType.OFF);
116                     }
117                     break;
118                 default:
119                     break;
120
121             }
122         }
123     }
124
125     public void setState(PrimitiveType state) {
126         this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
127     }
128
129     public void setStateAutomode(PrimitiveType state) {
130         this.updateState(SoulissBindingConstants.AUTOMODE_CHANNEL, (OnOffType) state);
131     }
132
133     @Override
134     public void setRawState(byte rawState) {
135         // update Last Status stored time
136         super.setLastStatusStored();
137
138         // update item state only if it is different from previous
139         if (t1nRawState != rawState) {
140             if (rawState == SoulissProtocolConstants.SOULISS_T1N_ON_COIL_AUTO) {
141                 this.setState(OnOffType.ON);
142                 this.setStateAutomode(OnOffType.ON);
143             } else if (rawState == SoulissProtocolConstants.SOULISS_T1N_OFF_COIL_AUTO) {
144                 this.setState(OnOffType.OFF);
145                 this.setStateAutomode(OnOffType.ON);
146             } else if (rawState == SoulissProtocolConstants.SOULISS_T1N_ON_COIL) {
147                 this.setState(OnOffType.ON);
148                 this.setStateAutomode(OnOffType.OFF);
149             } else if (rawState == SoulissProtocolConstants.SOULISS_T1N_OFF_COIL) {
150                 this.setState(OnOffType.OFF);
151                 this.setStateAutomode(OnOffType.OFF);
152             }
153         }
154         t1nRawState = rawState;
155     }
156
157     @Override
158     public byte getRawState() {
159         return t1nRawState;
160     }
161
162     @Override
163     public byte getExpectedRawState(byte bCommand) {
164         if (bSecureSend) {
165             if (bCommand == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
166                 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
167             } else if (bCommand == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
168                 return SoulissProtocolConstants.SOULISS_T1N_OFF_COIL;
169             } else if (bCommand >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
170                 // SLEEP
171                 return SoulissProtocolConstants.SOULISS_T1N_ON_COIL;
172             }
173         }
174         return -1;
175     }
176 }