]> git.basschouten.com Git - openhab-addons.git/blob
b9e620216dda1a2de4d544c4f268819fda6e481e
[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.paradoxalarm.internal.handlers;
14
15 import static org.openhab.binding.paradoxalarm.internal.handlers.ParadoxAlarmBindingConstants.*;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.openhab.binding.paradoxalarm.internal.model.ParadoxPanel;
21 import org.openhab.binding.paradoxalarm.internal.model.Zone;
22 import org.openhab.binding.paradoxalarm.internal.model.ZoneState;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.Bridge;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link ParadoxZoneHandler} Handler that updates states of paradox zones from the cache.
35  *
36  * @author Konstantin Polihronov - Initial contribution
37  */
38 public class ParadoxZoneHandler extends EntityBaseHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(ParadoxZoneHandler.class);
41
42     public ParadoxZoneHandler(@NonNull Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     protected void updateEntity() {
48         ParadoxIP150BridgeHandler bridgeHandler = getBridgeHandler();
49         if (bridgeHandler == null) {
50             logger.debug("Paradox bridge handler is null. Skipping update.");
51             return;
52         }
53
54         ParadoxPanel panel = bridgeHandler.getPanel();
55         List<Zone> zones = panel.getZones();
56         if (zones == null) {
57             logger.debug(
58                     "Zones collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
59             return;
60         }
61
62         int index = calculateEntityIndex();
63         if (zones.size() <= index) {
64             logger.debug("Attempted to access zone out of bounds of current zone list. Index: {}, List: {}", index,
65                     zones);
66             return;
67         }
68
69         Zone zone = zones.get(index);
70         if (zone != null) {
71             updateState(ZONE_LABEL_CHANNEL_UID, new StringType(zone.getLabel()));
72             ZoneState zoneState = zone.getZoneState();
73             if (zoneState != null) {
74                 updateState(ZONE_OPENED_CHANNEL_UID, booleanToContactState(zoneState.isOpened()));
75                 updateState(ZONE_TAMPERED_CHANNEL_UID, booleanToSwitchState(zoneState.isTampered()));
76                 updateState(ZONE_LOW_BATTERY_CHANNEL_UID, booleanToSwitchState(zoneState.hasLowBattery()));
77
78                 updateState(ZONE_SUPERVISION_TROUBLE_UID, booleanToSwitchState(zoneState.isSupervisionTrouble()));
79                 updateState(ZONE_IN_TX_DELAY_UID, booleanToSwitchState(zoneState.isInTxDelay()));
80                 updateState(ZONE_SHUTDOWN_UID, booleanToSwitchState(zoneState.isShutdown()));
81                 updateState(ZONE_BYPASSED_UID, booleanToSwitchState(zoneState.isBypassed()));
82                 updateState(ZONE_HAS_ACTIVATED_INTELLIZONE_DELAY_UID,
83                         booleanToSwitchState(zoneState.isHasActivatedIntellizoneDelay()));
84                 updateState(ZONE_HAS_ACTIVATED_ENTRY_DELAY_UID,
85                         booleanToSwitchState(zoneState.isHasActivatedEntryDelay()));
86                 updateState(ZONE_PRESENTLY_IN_ALARM_UID, booleanToSwitchState(zoneState.isPresentlyInAlarm()));
87                 updateState(ZONE_GENERATED_ALARM_UID, booleanToSwitchState(zoneState.isGeneratedAlarm()));
88             }
89         }
90     }
91
92     private OpenClosedType booleanToContactState(boolean value) {
93         return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
94     }
95
96     private OnOffType booleanToSwitchState(boolean value) {
97         return value ? OnOffType.ON : OnOffType.OFF;
98     }
99
100     @Override
101     public void handleCommand(@NonNull ChannelUID channelUID, @NonNull Command command) {
102         if (command instanceof StringType) {
103             Zone zone = getZone();
104             if (zone != null) {
105                 zone.handleCommand(command.toString());
106             }
107         } else {
108             super.handleCommand(channelUID, command);
109         }
110     }
111
112     protected Zone getZone() {
113         ParadoxIP150BridgeHandler bridgeHandler = getBridgeHandler();
114         if (bridgeHandler == null) {
115             logger.debug("Paradox bridge handler is null. Skipping update.");
116             return null;
117         }
118
119         ParadoxPanel panel = bridgeHandler.getPanel();
120         List<Zone> zones = panel.getZones();
121         if (zones == null) {
122             logger.debug(
123                     "Zones collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
124             return null;
125         }
126
127         int index = calculateEntityIndex();
128         if (zones.size() <= index) {
129             logger.debug("Attempted to access a zone out of bounds of current zone list. Index: {}, List: {}", index,
130                     zones);
131             return null;
132         }
133
134         return zones.get(index);
135     }
136
137     private ParadoxIP150BridgeHandler getBridgeHandler() {
138         Bridge bridge = getBridge();
139         if (bridge == null) {
140             logger.debug("Paradox bridge is null. Skipping update.");
141             return null;
142         }
143
144         return (ParadoxIP150BridgeHandler) bridge.getHandler();
145     }
146 }