]> git.basschouten.com Git - openhab-addons.git/blob
6b30284d608b7036bf95f94dc8b86fc97883ed41
[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
14 package org.openhab.binding.souliss.internal.handler;
15
16 import org.eclipse.jdt.annotation.NonNullByDefault;
17 import org.openhab.binding.souliss.internal.SoulissBindingConstants;
18 import org.openhab.binding.souliss.internal.SoulissProtocolConstants;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.library.types.StringType;
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 SoulissT41Handler} is responsible for handling commands, which are
30  * sent to one of the channels.
31  *
32  * @author Luca Remigio - Initial contribution
33  * @author Luca Calcaterra - Refactor for OH3
34  */
35
36 @NonNullByDefault
37 public class SoulissT41Handler extends SoulissGenericHandler {
38
39     byte t4nRawState = 0xF;
40
41     public SoulissT41Handler(Thing thing) {
42         super(thing);
43     }
44
45     // called on every status change or change request
46     @Override
47     public void handleCommand(ChannelUID channelUID, Command command) {
48         if (!(command instanceof RefreshType)) {
49             if (channelUID.getId().equals(SoulissBindingConstants.T4N_ONOFFALARM_CHANNEL)) {
50                 if (command instanceof OnOffType) {
51                     if (command.equals(OnOffType.OFF)) {
52                         commandSEND(SoulissProtocolConstants.SOULISS_T4N_NOT_ARMED);
53                     } else if (command.equals(OnOffType.ON)) {
54                         commandSEND(SoulissProtocolConstants.SOULISS_T4N_ARMED);
55                     }
56                 }
57             } else if ((channelUID.getAsString().split(":")[3].equals(SoulissBindingConstants.T4N_REARMALARM_CHANNEL))
58                     && (command instanceof OnOffType) && (command.equals(OnOffType.OFF))) {
59                 commandSEND(SoulissProtocolConstants.SOULISS_T4N_REARM);
60                 this.setState(StringType.valueOf(SoulissBindingConstants.T4N_REARMOFF_MESSAGE_CHANNEL));
61             }
62         }
63     }
64
65     @Override
66     public void initialize() {
67         super.initialize();
68
69         updateStatus(ThingStatus.UNKNOWN);
70
71         var configurationMap = getThing().getConfiguration();
72         if (configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND) != null) {
73             bSecureSend = ((Boolean) configurationMap.get(SoulissBindingConstants.CONFIG_SECURE_SEND)).booleanValue();
74         }
75     }
76
77     public void setState(PrimitiveType state) {
78         if (state instanceof OnOffType) {
79             this.updateState(SoulissBindingConstants.T4N_ONOFFALARM_CHANNEL, (OnOffType) state);
80         } else if (state instanceof StringType) {
81             switch (String.valueOf(state)) {
82                 case SoulissBindingConstants.T4N_ALARMON_MESSAGE_CHANNEL:
83                     this.updateState(SoulissBindingConstants.T4N_STATUSALARM_CHANNEL, OnOffType.ON);
84                     break;
85                 case SoulissBindingConstants.T4N_ALARMOFF_MESSAGE_CHANNEL:
86                     this.updateState(SoulissBindingConstants.T4N_STATUSALARM_CHANNEL, OnOffType.OFF);
87                     break;
88                 default:
89                     break;
90             }
91         }
92         // // Reset the rearm button. This is because if pressed, it does not turn off by itself
93         updateState(SoulissBindingConstants.T4N_REARMALARM_CHANNEL, OnOffType.OFF);
94     }
95
96     @Override
97     public void setRawState(byte rawState) {
98         // update Last Status stored time
99         super.setLastStatusStored();
100         // update item state only if it is different from previous
101         if (t4nRawState != rawState) {
102             switch (rawState) {
103                 case SoulissProtocolConstants.SOULISS_T4N_NO_ANTITHEFT:
104                     this.setState(OnOffType.OFF);
105                     this.setState(StringType.valueOf(SoulissBindingConstants.T4N_ALARMOFF_MESSAGE_CHANNEL));
106                     break;
107                 case SoulissProtocolConstants.SOULISS_T4N_ANTITHEFT:
108                     this.setState(OnOffType.ON);
109                     this.setState(StringType.valueOf(SoulissBindingConstants.T4N_ALARMOFF_MESSAGE_CHANNEL));
110                     break;
111                 case SoulissProtocolConstants.SOULISS_T4N_IN_ALARM:
112                     this.setState(StringType.valueOf(SoulissBindingConstants.T4N_ALARMON_MESSAGE_CHANNEL));
113                     break;
114                 case SoulissProtocolConstants.SOULISS_T4N_ARMED:
115                     this.setState(StringType.valueOf(SoulissBindingConstants.T4N_ARMED_MESSAGE_CHANNEL));
116                     break;
117                 default:
118                     break;
119             }
120         }
121         t4nRawState = rawState;
122     }
123
124     @Override
125     public byte getRawState() {
126         return t4nRawState;
127     }
128
129     @Override
130     public byte getExpectedRawState(byte bCmd) {
131         if (bSecureSend) {
132             if (bCmd == SoulissProtocolConstants.SOULISS_T4N_ARMED) {
133                 return SoulissProtocolConstants.SOULISS_T4N_ANTITHEFT;
134             } else if (bCmd == SoulissProtocolConstants.SOULISS_T4N_NOT_ARMED) {
135                 return SoulissProtocolConstants.SOULISS_T4N_NO_ANTITHEFT;
136             } else if (bCmd >= SoulissProtocolConstants.SOULISS_T4N_REARM) {
137                 return SoulissProtocolConstants.SOULISS_T4N_ANTITHEFT;
138             }
139         }
140         return -1;
141     }
142 }