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