]> git.basschouten.com Git - openhab-addons.git/blob
1d1defb978f9ac1a9f022c25a3dfa958c1968323
[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.alarmdecoder.internal.handler;
14
15 import static org.openhab.binding.alarmdecoder.internal.AlarmDecoderBindingConstants.*;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.alarmdecoder.internal.config.RFZoneConfig;
19 import org.openhab.binding.alarmdecoder.internal.protocol.ADMessage;
20 import org.openhab.binding.alarmdecoder.internal.protocol.RFXMessage;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.OpenClosedType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link RFZoneHandler} is responsible for handling wired zones (i.e. RFX messages).
34  *
35  * @author Bob Adair - Initial contribution
36  * @author Bill Forsyth - Initial contribution
37  */
38 @NonNullByDefault
39 public class RFZoneHandler extends ADThingHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(RFZoneHandler.class);
42
43     private RFZoneConfig config = new RFZoneConfig();
44
45     public RFZoneHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void initialize() {
51         config = getConfigAs(RFZoneConfig.class);
52
53         if (config.serial < 0) {
54             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Invalid serial setting");
55             return;
56         }
57         logger.debug("RF Zone handler initializing for serial {}", config.serial);
58
59         initDeviceState();
60
61         logger.trace("RF Zone handler finished initializing");
62     }
63
64     /**
65      * Set contact channel states to "UNDEF" at init time. The real states will be set either when the first message
66      * arrives for the zone, or they will be set to "CLOSED" the first time the panel goes into the "READY" state.
67      */
68     @Override
69     public void initChannelState() {
70         UnDefType state = UnDefType.UNDEF;
71         updateState(CHANNEL_RF_LOWBAT, state);
72         updateState(CHANNEL_RF_SUPERVISION, state);
73         updateState(CHANNEL_RF_LOOP1, state);
74         updateState(CHANNEL_RF_LOOP2, state);
75         updateState(CHANNEL_RF_LOOP3, state);
76         updateState(CHANNEL_RF_LOOP4, state);
77         firstUpdateReceived.set(false);
78     }
79
80     @Override
81     public void notifyPanelReady() {
82         logger.trace("RF Zone handler for {} received panel ready notification.", config.serial);
83         if (firstUpdateReceived.compareAndSet(false, true)) {
84             updateState(CHANNEL_RF_LOOP1, OpenClosedType.CLOSED);
85             updateState(CHANNEL_RF_LOOP2, OpenClosedType.CLOSED);
86             updateState(CHANNEL_RF_LOOP3, OpenClosedType.CLOSED);
87             updateState(CHANNEL_RF_LOOP4, OpenClosedType.CLOSED);
88         }
89     }
90
91     @Override
92     public void handleCommand(ChannelUID channelUID, Command command) {
93         // Does not accept any commands
94     }
95
96     @Override
97     public void handleUpdate(ADMessage msg) {
98         if (!(msg instanceof RFXMessage)) {
99             return;
100         }
101         RFXMessage rfxMsg = (RFXMessage) msg;
102
103         if (config.serial == rfxMsg.serial) {
104             logger.trace("RF Zone handler for serial {} received update: {}", config.serial, rfxMsg.data);
105             firstUpdateReceived.set(true);
106
107             updateState(CHANNEL_RF_LOWBAT, OnOffType.from((rfxMsg.data & RFXMessage.BIT_LOWBAT) != 0));
108             updateState(CHANNEL_RF_SUPERVISION, OnOffType.from((rfxMsg.data & RFXMessage.BIT_SUPER) != 0));
109
110             updateState(CHANNEL_RF_LOOP1,
111                     (rfxMsg.data & RFXMessage.BIT_LOOP1) == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
112             updateState(CHANNEL_RF_LOOP2,
113                     (rfxMsg.data & RFXMessage.BIT_LOOP2) == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
114             updateState(CHANNEL_RF_LOOP3,
115                     (rfxMsg.data & RFXMessage.BIT_LOOP3) == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
116             updateState(CHANNEL_RF_LOOP4,
117                     (rfxMsg.data & RFXMessage.BIT_LOOP4) == 0 ? OpenClosedType.CLOSED : OpenClosedType.OPEN);
118         }
119     }
120 }