]> git.basschouten.com Git - openhab-addons.git/blob
2250dc720d857568a21a77f9e7b9ef4ea2b00a09
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.souliss.internal.SoulissBindingConstants;
17 import org.openhab.binding.souliss.internal.SoulissProtocolConstants;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.library.types.StringType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.types.Command;
24 import org.openhab.core.types.PrimitiveType;
25
26 /**
27  * The {@link SoulissT42Handler} is responsible for handling commands, which are
28  * sent to one of the channels.
29  *
30  * @author Tonino Fazio - Initial contribution
31  * @author Luca Calcaterra - Refactor for OH3
32  */
33
34 @NonNullByDefault
35 public class SoulissT42Handler extends SoulissGenericHandler {
36     byte t4nRawState = 0xF;
37
38     public SoulissT42Handler(Thing thing) {
39         super(thing);
40     }
41
42     // called on every status change or change request
43     @Override
44     public void handleCommand(ChannelUID channelUID, Command command) {
45         if ((channelUID.getAsString().split(":")[3].equals(SoulissBindingConstants.T4N_REARMALARM_CHANNEL))
46                 && (command instanceof OnOffType) && (command.equals(OnOffType.ON))) {
47             commandSEND(SoulissProtocolConstants.SOULISS_T4N_REARM);
48             this.setState(StringType.valueOf(SoulissBindingConstants.T4N_REARMOFF_MESSAGE_CHANNEL));
49         }
50     }
51
52     @Override
53     public void initialize() {
54         super.initialize();
55
56         updateStatus(ThingStatus.UNKNOWN);
57
58         var configurationMap = getThing().getConfiguration();
59         if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
60             bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
61         }
62     }
63
64     public void setState(PrimitiveType state) {
65         if (state instanceof StringType) {
66             switch (String.valueOf(state)) {
67                 case SoulissBindingConstants.T4N_ALARMON_MESSAGE_CHANNEL:
68                     this.updateState(SoulissBindingConstants.T4N_STATUSALARM_CHANNEL, OnOffType.ON);
69                     break;
70                 case SoulissBindingConstants.T4N_ALARMOFF_MESSAGE_CHANNEL:
71                     this.updateState(SoulissBindingConstants.T4N_STATUSALARM_CHANNEL, OnOffType.OFF);
72                     break;
73                 default:
74                     break;
75             }
76         }
77         // Reset the rearm button. This is because if pressed, it does not turn off by itself
78         updateState(SoulissBindingConstants.T4N_REARMALARM_CHANNEL, OnOffType.OFF);
79
80         super.setLastStatusStored();
81     }
82
83     @Override
84     public void setRawState(byte rawState) {
85         // update Last Status stored time
86         super.setLastStatusStored();
87         // update item state only if it is different from previous
88         if (t4nRawState != rawState) {
89             OnOffType onOffVal = getOhStateOnOffFromSoulissVal(rawState);
90             if (onOffVal != null) {
91                 this.setState(onOffVal);
92             }
93         }
94         t4nRawState = rawState;
95     }
96
97     @Override
98     public byte getRawState() {
99         return t4nRawState;
100     }
101
102     @Override
103     public byte getExpectedRawState(byte bCmd) {
104         if ((bSecureSend) && (bCmd == SoulissProtocolConstants.SOULISS_T4N_REARM)) {
105             return SoulissProtocolConstants.SOULISS_T4N_ANTITHEFT;
106         }
107         return -1;
108     }
109 }