2 * Copyright (c) 2010-2021 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;
16 import java.util.Collections;
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.CaddxMessageType;
23 import org.openhab.binding.caddx.internal.CaddxProperty;
24 import org.openhab.binding.caddx.internal.action.CaddxZoneActions;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.binding.ThingHandlerService;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This is a class for handling a Zone type Thing.
40 * @author Georgios Moutsos - Initial contribution
43 public class ThingHandlerZone extends CaddxBaseThingHandler {
45 private final Logger logger = LoggerFactory.getLogger(ThingHandlerZone.class);
46 private long lastRefreshTime = 0;
48 public ThingHandlerZone(Thing thing) {
49 super(thing, CaddxThingType.ZONE);
53 public void updateChannel(ChannelUID channelUID, String data) {
54 if (channelUID.getId().equals(CaddxBindingConstants.ZONE_NAME)) {
55 getThing().setLabel(data);
56 updateState(channelUID, new StringType(data));
58 logger.trace(" updateChannel: {} = {}", channelUID, data);
59 } else if (channelUID.getId().equals(CaddxBindingConstants.ZONE_FAULTED)) {
60 OpenClosedType openClosedType = ("true".equals(data)) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
61 updateState(channelUID, openClosedType);
63 logger.trace(" updateChannel: {} = {}", channelUID, data);
65 OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
66 updateState(channelUID, onOffType);
68 logger.trace(" updateChannel: {} = {}", channelUID, onOffType);
73 public void handleCommand(ChannelUID channelUID, Command command) {
74 logger.trace("handleCommand(): Command Received - {} {}.", channelUID, command);
80 if (command instanceof RefreshType) {
81 // Refresh only if 2 seconds have passed from the last refresh
82 if (System.currentTimeMillis() - lastRefreshTime > 2000) {
83 cmd1 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
84 cmd2 = CaddxBindingConstants.ZONE_NAME_REQUEST;
85 data = String.format("%d", getZoneNumber() - 1);
89 lastRefreshTime = System.currentTimeMillis();
90 } else if (channelUID.getId().equals(CaddxBindingConstants.ZONE_BYPASSED)) {
91 cmd1 = CaddxBindingConstants.ZONE_BYPASSED;
92 cmd2 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
93 data = String.format("%d", getZoneNumber() - 1);
95 logger.debug("Unknown command {}", command);
99 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
100 if (bridgeHandler == null) {
103 bridgeHandler.sendCommand(cmd1, data);
104 bridgeHandler.sendCommand(cmd2, data);
108 public void caddxEventReceived(CaddxEvent event, Thing thing) {
109 logger.trace("caddxEventReceived(): Event Received - {}", event);
111 if (getThing().equals(thing)) {
112 CaddxMessage message = event.getCaddxMessage();
113 CaddxMessageType mt = message.getCaddxMessageType();
114 ChannelUID channelUID = null;
116 for (CaddxProperty p : mt.properties) {
117 logger.trace(" Checking property: {}", p.getName());
119 if (!p.getId().isEmpty()) {
120 String value = message.getPropertyById(p.getId());
121 channelUID = new ChannelUID(getThing().getUID(), p.getId());
122 updateChannel(channelUID, value);
124 logger.trace(" updateChannel: {} = {}", channelUID, value);
128 updateStatus(ThingStatus.ONLINE);
133 public Collection<Class<? extends ThingHandlerService>> getServices() {
134 return Collections.singleton(CaddxZoneActions.class);
137 public void bypass() {
138 String cmd = CaddxBindingConstants.ZONE_BYPASSED;
139 String data = String.format("%d", getZoneNumber() - 1);
141 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
142 if (bridgeHandler == null) {
145 bridgeHandler.sendCommand(cmd, data);