]> git.basschouten.com Git - openhab-addons.git/blob
a1b55271e043ef67e91f0838ae2ad3d461457002
[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.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.PrimitiveType;
27 import org.openhab.core.types.RefreshType;
28
29 /**
30  * The {@link SoulissT18Handler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author Tonino Fazio - Initial contribution
34  * @author Luca Calcaterra - Refactor for OH3
35  */
36
37 @NonNullByDefault
38 public class SoulissT18Handler extends SoulissGenericHandler {
39
40     byte t1nRawState = 0xF;
41     byte xSleepTime = 0;
42
43     @Override
44     public void initialize() {
45         super.initialize();
46
47         updateStatus(ThingStatus.UNKNOWN);
48
49         var configurationMap = getThing().getConfiguration();
50
51         if (configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL) != null) {
52             xSleepTime = ((BigDecimal) configurationMap.get(SoulissBindingConstants.SLEEP_CHANNEL)).byteValue();
53         }
54         if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
55             bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
56         }
57     }
58
59     public SoulissT18Handler(Thing thing) {
60         super(thing);
61     }
62
63     @Override
64     public void handleCommand(ChannelUID channelUID, Command command) {
65         if (command instanceof RefreshType) {
66             switch (channelUID.getId()) {
67                 case SoulissBindingConstants.PULSE_CHANNEL:
68                     OnOffType valPulse = getOhStateOnOffFromSoulissVal(t1nRawState);
69                     if (valPulse != null) {
70                         updateState(channelUID, valPulse);
71                     }
72                     break;
73                 default:
74                     break;
75             }
76         } else {
77             switch (channelUID.getId()) {
78                 case SoulissBindingConstants.ONOFF_CHANNEL:
79                     if (command.equals(OnOffType.ON)) {
80                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_ON_CMD);
81                     } else if (command.equals(OnOffType.OFF)) {
82                         commandSEND(SoulissProtocolConstants.SOULISS_T1N_OFF_CMD);
83                     }
84                     break;
85                 default:
86                     break;
87             }
88         }
89     }
90
91     void setState(@Nullable PrimitiveType state) {
92         if (state != null) {
93             updateState(SoulissBindingConstants.SLEEP_CHANNEL, OnOffType.OFF);
94             this.updateState(SoulissBindingConstants.ONOFF_CHANNEL, (OnOffType) state);
95         }
96     }
97
98     @Override
99     public void setRawState(byte rawState) {
100         // update Last Status stored time
101         super.setLastStatusStored();
102         // update item state only if it is different from previous
103         if (t1nRawState != rawState) {
104             this.setState(getOhStateOnOffFromSoulissVal(rawState));
105         }
106         t1nRawState = rawState;
107     }
108
109     @Override
110     public byte getRawState() {
111         return t1nRawState;
112     }
113
114     @Override
115     public byte getExpectedRawState(byte bCmd) {
116         if (bSecureSend) {
117             if (bCmd == SoulissProtocolConstants.SOULISS_T1N_ON_CMD) {
118                 return SoulissProtocolConstants.SOULISS_T1N_ON_FEEDBACK;
119             } else if (bCmd == SoulissProtocolConstants.SOULISS_T1N_OFF_CMD) {
120                 return SoulissProtocolConstants.SOULISS_T1N_OFF_FEEDBACK;
121             } else if (bCmd >= SoulissProtocolConstants.SOULISS_T1N_TIMED) {
122                 // SLEEP
123                 return SoulissProtocolConstants.SOULISS_T1N_ON_FEEDBACK;
124             }
125         }
126         return -1;
127     }
128 }