2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.dscalarm.internal.handler;
15 import static org.openhab.binding.dscalarm.internal.DSCAlarmBindingConstants.*;
17 import java.util.EventObject;
19 import org.openhab.binding.dscalarm.internal.DSCAlarmCode;
20 import org.openhab.binding.dscalarm.internal.DSCAlarmEvent;
21 import org.openhab.binding.dscalarm.internal.DSCAlarmMessage;
22 import org.openhab.binding.dscalarm.internal.DSCAlarmMessage.DSCAlarmMessageInfoType;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * This is a class for handling a Partition type Thing.
36 * @author Russell Stephens - Initial Contribution
38 public class PartitionThingHandler extends DSCAlarmBaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(PartitionThingHandler.class);
47 public PartitionThingHandler(Thing thing) {
49 setDSCAlarmThingType(DSCAlarmThingType.PARTITION);
53 public void updateChannel(ChannelUID channelUID, int state, String description) {
54 logger.debug("updateChannel(): Panel Channel UID: {}", channelUID);
59 if (channelUID != null) {
60 switch (channelUID.getId()) {
61 case PARTITION_STATUS:
62 updateState(channelUID, new StringType(description));
64 case PARTITION_ARM_MODE:
65 updateState(channelUID, new DecimalType(state));
69 onOffType = OnOffType.from(trigger);
70 updateState(channelUID, onOffType);
72 case PARTITION_ENTRY_DELAY:
74 onOffType = OnOffType.from(trigger);
75 updateState(channelUID, onOffType);
77 case PARTITION_EXIT_DELAY:
79 onOffType = OnOffType.from(trigger);
80 updateState(channelUID, onOffType);
82 case PARTITION_IN_ALARM:
84 onOffType = OnOffType.from(trigger);
85 updateState(channelUID, onOffType);
87 case PARTITION_OPENING_CLOSING_MODE:
88 updateState(channelUID, new StringType(description));
91 logger.debug("updateChannel(): Partition Channel not updated - {}.", channelUID);
98 public void handleCommand(ChannelUID channelUID, Command command) {
99 logger.debug("handleCommand(): Command Received - {} {}.", channelUID, command);
101 if (command instanceof RefreshType) {
105 if (dscAlarmBridgeHandler != null && dscAlarmBridgeHandler.isConnected()) {
106 switch (channelUID.getId()) {
107 case PARTITION_ARM_MODE:
108 int partitionNumber = getPartitionNumber();
109 if ("0".equals(command.toString())) {
110 dscAlarmBridgeHandler.sendCommand(DSCAlarmCode.PartitionDisarmControl,
111 String.valueOf(partitionNumber));
112 } else if ("1".equals(command.toString())) {
113 dscAlarmBridgeHandler.sendCommand(DSCAlarmCode.PartitionArmControlAway,
114 String.valueOf(partitionNumber));
115 } else if ("2".equals(command.toString())) {
116 dscAlarmBridgeHandler.sendCommand(DSCAlarmCode.PartitionArmControlStay,
117 String.valueOf(partitionNumber));
118 } else if ("3".equals(command.toString())) {
119 dscAlarmBridgeHandler.sendCommand(DSCAlarmCode.PartitionArmControlZeroEntryDelay,
120 String.valueOf(partitionNumber));
121 } else if ("4".equals(command.toString())) {
122 dscAlarmBridgeHandler.sendCommand(DSCAlarmCode.PartitionArmControlZeroEntryDelay,
123 String.valueOf(partitionNumber));
124 } else if ("5".equals(command.toString())) {
125 dscAlarmBridgeHandler.sendCommand(DSCAlarmCode.PartitionArmControlWithUserCode,
126 String.valueOf(partitionNumber));
136 * Method to set Partition Status.
140 private void partitionStatus(String message) {
141 updateState(new ChannelUID(getThing().getUID(), PARTITION_STATUS), new StringType(message));
145 * Method to set Partition Close Open Mode.
149 private void partitionOpenCloseModeEventHandler(EventObject event) {
150 DSCAlarmEvent dscAlarmEvent = (DSCAlarmEvent) event;
151 DSCAlarmMessage dscAlarmMessage = dscAlarmEvent.getDSCAlarmMessage();
152 DSCAlarmCode dscAlarmCode = DSCAlarmCode
153 .getDSCAlarmCodeValue(dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.CODE));
154 ChannelUID channelUID = null;
156 * 0=None, 1=User Closing, 2=Special Closing, 3=Partial Closing, 4=User Opening, 5=Special
160 String strStatus = dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.NAME);
162 switch (dscAlarmCode) {
163 case UserClosing: /* 700 */
166 case SpecialClosing: /* 701 */
169 case PartialClosing: /* 702 */
172 case UserOpening: /* 750 */
175 case SpecialOpening: /* 751 */
182 channelUID = new ChannelUID(getThing().getUID(), PARTITION_OPENING_CLOSING_MODE);
183 updateChannel(channelUID, state, strStatus);
187 @SuppressWarnings("PMD.CompareObjectsWithEquals")
188 public void dscAlarmEventReceived(EventObject event, Thing thing) {
190 if (getThing() == thing) {
191 DSCAlarmEvent dscAlarmEvent = (DSCAlarmEvent) event;
192 DSCAlarmMessage dscAlarmMessage = dscAlarmEvent.getDSCAlarmMessage();
194 ChannelUID channelUID = null;
195 DSCAlarmCode dscAlarmCode = DSCAlarmCode
196 .getDSCAlarmCodeValue(dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.CODE));
197 String dscAlarmMessageName = dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.NAME);
198 String dscAlarmMessageMode = dscAlarmMessage.getMessageInfo(DSCAlarmMessageInfoType.MODE);
200 logger.debug("dscAlarmEventRecieved(): Thing - {} Command - {}", thing.getUID(), dscAlarmCode);
202 switch (dscAlarmCode) {
203 case PartitionReady: /* 650 */
204 case PartitionNotReady: /* 651 */
205 case PartitionReadyForceArming: /* 653 */
206 case SystemArmingInProgress: /* 674 */
207 partitionStatus(dscAlarmMessageName);
209 case PartitionArmed: /* 652 */
210 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ARMED);
211 updateChannel(channelUID, 1, "");
213 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ENTRY_DELAY);
214 updateChannel(channelUID, 0, "");
216 channelUID = new ChannelUID(getThing().getUID(), PARTITION_EXIT_DELAY);
217 updateChannel(channelUID, 0, "");
220 * arm mode:0=disarmed, 1=away armed, 2=stay armed, 3=away no delay, 4=stay no delay, 5=with
223 int armMode = Integer.parseInt(dscAlarmMessageMode) + 1;
224 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ARM_MODE);
225 updateChannel(channelUID, armMode, "");
227 partitionStatus(dscAlarmMessageName);
229 case PartitionDisarmed: /* 655 */
230 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ARMED);
231 updateChannel(channelUID, 0, "");
233 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ENTRY_DELAY);
234 updateChannel(channelUID, 0, "");
236 channelUID = new ChannelUID(getThing().getUID(), PARTITION_EXIT_DELAY);
237 updateChannel(channelUID, 0, "");
239 channelUID = new ChannelUID(getThing().getUID(), PARTITION_IN_ALARM);
240 updateChannel(channelUID, 0, "");
242 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ARM_MODE);
243 updateChannel(channelUID, 0, "");
245 partitionStatus(dscAlarmMessageName);
247 case PartitionInAlarm: /* 654 */
248 channelUID = new ChannelUID(getThing().getUID(), PARTITION_IN_ALARM);
249 updateChannel(channelUID, 1, "");
251 partitionStatus(dscAlarmMessageName);
253 case ExitDelayInProgress: /* 656 */
254 channelUID = new ChannelUID(getThing().getUID(), PARTITION_EXIT_DELAY);
255 updateChannel(channelUID, 1, "");
257 case EntryDelayInProgress: /* 657 */
258 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ENTRY_DELAY);
259 updateChannel(channelUID, 1, "");
261 case FailureToArm: /* 672 */
262 channelUID = new ChannelUID(getThing().getUID(), PARTITION_ARM_MODE);
263 updateChannel(channelUID, 0, "");
264 partitionStatus(dscAlarmMessageName);
266 case UserClosing: /* 700 */
267 case SpecialClosing: /* 701 */
268 case PartialClosing: /* 702 */
269 case UserOpening: /* 750 */
270 case SpecialOpening: /* 751 */
271 partitionOpenCloseModeEventHandler(event);