]> git.basschouten.com Git - openhab-addons.git/blob
52feb12959d4e3a95982f16ec19430121c5ad0f7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.core.library.types.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.Thing;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link ParadoxZoneHandler} Handler that updates states of paradox zones from the cache.
31  *
32  * @author Konstantin Polihronov - Initial contribution
33  */
34 public class ParadoxZoneHandler extends EntityBaseHandler {
35
36     private final Logger logger = LoggerFactory.getLogger(ParadoxZoneHandler.class);
37
38     public ParadoxZoneHandler(@NonNull Thing thing) {
39         super(thing);
40     }
41
42     @Override
43     protected void updateEntity() {
44         int index = calculateEntityIndex();
45         List<Zone> zones = ParadoxPanel.getInstance().getZones();
46         if (zones == null) {
47             logger.debug(
48                     "Zones collection of Paradox Panel object is null. Probably not yet initialized. Skipping update.");
49             return;
50         }
51         if (zones.size() <= index) {
52             logger.debug("Attempted to access zone out of bounds of current zone list. Index: {}, List: {}", index,
53                     zones);
54             return;
55         }
56
57         Zone zone = zones.get(index);
58         if (zone != null) {
59             updateState(ZONE_LABEL_CHANNEL_UID, new StringType(zone.getLabel()));
60             updateState(ZONE_OPENED_CHANNEL_UID, booleanToContactState(zone.getZoneState().isOpened()));
61             updateState(ZONE_TAMPERED_CHANNEL_UID, booleanToSwitchState(zone.getZoneState().isTampered()));
62             updateState(ZONE_LOW_BATTERY_CHANNEL_UID, booleanToSwitchState(zone.getZoneState().hasLowBattery()));
63         }
64     }
65
66     private OpenClosedType booleanToContactState(boolean value) {
67         return value ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
68     }
69
70     private OnOffType booleanToSwitchState(boolean value) {
71         return value ? OnOffType.ON : OnOffType.OFF;
72     }
73 }