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.caddx.internal.handler;
15 import java.util.Collection;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.caddx.internal.CaddxBindingConstants;
20 import org.openhab.binding.caddx.internal.CaddxEvent;
21 import org.openhab.binding.caddx.internal.CaddxMessage;
22 import org.openhab.binding.caddx.internal.CaddxMessageContext;
23 import org.openhab.binding.caddx.internal.CaddxMessageType;
24 import org.openhab.binding.caddx.internal.CaddxProperty;
25 import org.openhab.binding.caddx.internal.action.CaddxZoneActions;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.OpenClosedType;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.binding.ThingHandlerService;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.RefreshType;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * This is a class for handling a Zone type Thing.
41 * @author Georgios Moutsos - Initial contribution
44 public class ThingHandlerZone extends CaddxBaseThingHandler {
46 private final Logger logger = LoggerFactory.getLogger(ThingHandlerZone.class);
47 private long lastRefreshTime = 0;
49 public ThingHandlerZone(Thing thing) {
50 super(thing, CaddxThingType.ZONE);
54 public void initialize() {
57 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
58 if (bridgeHandler == null) {
62 String cmd1 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
63 String cmd2 = CaddxBindingConstants.ZONE_NAME_REQUEST;
64 String data = String.format("%d", getZoneNumber() - 1);
66 bridgeHandler.sendCommand(CaddxMessageContext.COMMAND, cmd1, data);
67 bridgeHandler.sendCommand(CaddxMessageContext.COMMAND, cmd2, data);
71 public void updateChannel(ChannelUID channelUID, String data) {
72 if (channelUID.getId().equals(CaddxBindingConstants.ZONE_NAME)) {
73 updateState(channelUID, new StringType(data));
75 logger.trace(" updateChannel: {} = {}", channelUID, data);
76 } else if (channelUID.getId().equals(CaddxBindingConstants.ZONE_FAULTED)) {
77 OpenClosedType openClosedType = ("true".equals(data)) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
78 updateState(channelUID, openClosedType);
80 logger.trace(" updateChannel: {} = {}", channelUID, data);
82 OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
83 updateState(channelUID, onOffType);
85 logger.trace(" updateChannel: {} = {}", channelUID, onOffType);
90 public void handleCommand(ChannelUID channelUID, Command command) {
91 logger.debug("handleCommand(): Command Received - {} {}.", channelUID, command);
97 if (command instanceof RefreshType) {
98 // Refresh only if 2 seconds have passed from the last refresh
99 if (System.currentTimeMillis() - lastRefreshTime > 2000) {
100 cmd1 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
101 cmd2 = CaddxBindingConstants.ZONE_NAME_REQUEST;
102 data = String.format("%d", getZoneNumber() - 1);
106 lastRefreshTime = System.currentTimeMillis();
107 } else if (channelUID.getId().equals(CaddxBindingConstants.ZONE_BYPASSED)) {
108 cmd1 = CaddxBindingConstants.ZONE_BYPASSED;
109 cmd2 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
110 data = String.format("%d", getZoneNumber() - 1);
112 logger.debug("Unknown command {}", command);
116 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
117 if (bridgeHandler == null) {
120 bridgeHandler.sendCommand(CaddxMessageContext.COMMAND, cmd1, data);
121 bridgeHandler.sendCommand(CaddxMessageContext.COMMAND, cmd2, data);
125 public void caddxEventReceived(CaddxEvent event, Thing thing) {
126 logger.trace("caddxEventReceived(): Event Received - {}", event);
128 if (getThing().equals(thing)) {
129 CaddxMessage message = event.getCaddxMessage();
130 CaddxMessageType mt = message.getCaddxMessageType();
131 ChannelUID channelUID = null;
133 for (CaddxProperty p : mt.properties) {
134 logger.trace(" Checking property: {}", p.getName());
136 if (!p.getId().isEmpty()) {
137 String value = message.getPropertyById(p.getId());
138 channelUID = new ChannelUID(getThing().getUID(), p.getId());
139 updateChannel(channelUID, value);
140 logger.trace("Updating zone channel: {}", channelUID.getAsString());
145 updateStatus(ThingStatus.ONLINE);
149 public Collection<Class<? extends ThingHandlerService>> getServices() {
150 return Set.of(CaddxZoneActions.class);
153 public void bypass() {
154 String cmd = CaddxBindingConstants.ZONE_BYPASSED;
155 String data = String.format("%d", getZoneNumber() - 1);
157 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
158 if (bridgeHandler == null) {
161 bridgeHandler.sendCommand(CaddxMessageContext.COMMAND, cmd, data);