]> git.basschouten.com Git - openhab-addons.git/blob
b6827d0b352aa2aac75682ffb9ded9e08dea201b
[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.VZoneConfig;
19 import org.openhab.binding.alarmdecoder.internal.protocol.ADCommand;
20 import org.openhab.binding.alarmdecoder.internal.protocol.ADMessage;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.StringType;
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 VZoneHandler} is responsible for sending state commands to virtual zones.
34  *
35  * @author Bob Adair - Initial contribution
36  */
37 @NonNullByDefault
38 public class VZoneHandler extends ADThingHandler {
39
40     public static final String CMD_OPEN = "OPEN";
41     public static final String CMD_CLOSED = "CLOSED";
42
43     private final Logger logger = LoggerFactory.getLogger(VZoneHandler.class);
44
45     private VZoneConfig config = new VZoneConfig();
46
47     public VZoneHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     public void initialize() {
53         config = getConfigAs(VZoneConfig.class);
54
55         if (config.address < 0 || config.address > 99) {
56             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "Invalid address setting");
57             return;
58         }
59         logger.debug("Virtual zone handler initializing for address {}", config.address);
60         initDeviceState();
61     }
62
63     @Override
64     public void initChannelState() {
65         UnDefType state = UnDefType.UNDEF;
66         updateState(CHANNEL_STATE, state);
67         firstUpdateReceived.set(false);
68     }
69
70     @Override
71     public void notifyPanelReady() {
72         logger.trace("Virtual zone handler for {} received panel ready notification.", config.address);
73         if (firstUpdateReceived.compareAndSet(false, true)) {
74             updateState(CHANNEL_STATE, OnOffType.ON);
75         }
76     }
77
78     @Override
79     public void handleCommand(ChannelUID channelUID, Command command) {
80         if (channelUID.getId().equals(CHANNEL_COMMAND)) {
81             if (command instanceof StringType stringCommand) {
82                 String cmd = stringCommand.toString();
83                 if (CMD_OPEN.equalsIgnoreCase(cmd)) {
84                     sendCommand(ADCommand.setZone(config.address, ADCommand.ZONE_OPEN));
85                     setChannelState(OnOffType.OFF);
86                 } else if (CMD_CLOSED.equalsIgnoreCase(cmd)) {
87                     sendCommand(ADCommand.setZone(config.address, ADCommand.ZONE_CLOSED));
88                     setChannelState(OnOffType.ON);
89                 } else {
90                     logger.debug("Virtual zone handler {} received invalid command: {}", config.address, cmd);
91                 }
92             }
93         } else if (channelUID.getId().equals(CHANNEL_STATE)) {
94             if (command instanceof OnOffType) {
95                 if (command == OnOffType.OFF) {
96                     sendCommand(ADCommand.setZone(config.address, ADCommand.ZONE_OPEN));
97                     setChannelState(OnOffType.OFF);
98                 } else if (command == OnOffType.ON) {
99                     sendCommand(ADCommand.setZone(config.address, ADCommand.ZONE_CLOSED));
100                     setChannelState(OnOffType.ON);
101                 }
102             }
103         }
104     }
105
106     private void setChannelState(OnOffType state) {
107         updateState(CHANNEL_STATE, state);
108         firstUpdateReceived.set(true);
109     }
110
111     @Override
112     public void handleUpdate(ADMessage msg) {
113         // There can be no update requests
114     }
115 }