2 * Copyright (c) 2010-2020 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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.caddx.internal.CaddxBindingConstants;
17 import org.openhab.binding.caddx.internal.CaddxEvent;
18 import org.openhab.binding.caddx.internal.CaddxMessage;
19 import org.openhab.binding.caddx.internal.CaddxMessageType;
20 import org.openhab.binding.caddx.internal.CaddxProperty;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.library.types.OpenClosedType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * This is a class for handling a Zone type Thing.
35 * @author Georgios Moutsos - Initial contribution
38 public class ThingHandlerZone extends CaddxBaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(ThingHandlerZone.class);
41 private long lastRefreshTime = 0;
43 public ThingHandlerZone(Thing thing) {
44 super(thing, CaddxThingType.ZONE);
48 public void updateChannel(ChannelUID channelUID, String data) {
49 if (channelUID.getId().equals(CaddxBindingConstants.ZONE_NAME)) {
50 getThing().setLabel(data);
51 updateState(channelUID, new StringType(data));
53 logger.trace(" updateChannel: {} = {}", channelUID, data);
54 } else if (channelUID.getId().equals(CaddxBindingConstants.ZONE_FAULTED)) {
55 OpenClosedType openClosedType = ("true".equals(data)) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
56 updateState(channelUID, openClosedType);
58 logger.trace(" updateChannel: {} = {}", channelUID, data);
60 OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
61 updateState(channelUID, onOffType);
63 logger.trace(" updateChannel: {} = {}", channelUID, onOffType);
68 public void handleCommand(ChannelUID channelUID, Command command) {
69 logger.trace("handleCommand(): Command Received - {} {}.", channelUID, command);
75 if (command instanceof RefreshType) {
76 // Refresh only if 2 seconds have passed from the last refresh
77 if (System.currentTimeMillis() - lastRefreshTime > 2000) {
78 cmd1 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
79 cmd2 = CaddxBindingConstants.ZONE_NAME_REQUEST;
80 data = String.format("%d", getZoneNumber() - 1);
84 lastRefreshTime = System.currentTimeMillis();
85 } else if (channelUID.getId().equals(CaddxBindingConstants.ZONE_BYPASSED)) {
86 cmd1 = CaddxBindingConstants.ZONE_BYPASSED;
87 cmd2 = CaddxBindingConstants.ZONE_STATUS_REQUEST;
88 data = String.format("%d", getZoneNumber() - 1);
90 logger.debug("Unknown command {}", command);
94 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
95 if (bridgeHandler == null) {
98 bridgeHandler.sendCommand(cmd1, data);
99 bridgeHandler.sendCommand(cmd2, data);
103 public void caddxEventReceived(CaddxEvent event, Thing thing) {
104 logger.trace("caddxEventReceived(): Event Received - {}", event);
106 if (getThing().equals(thing)) {
107 CaddxMessage message = event.getCaddxMessage();
108 CaddxMessageType mt = message.getCaddxMessageType();
109 ChannelUID channelUID = null;
111 for (CaddxProperty p : mt.properties) {
112 logger.trace(" Checking property: {}", p.getName());
114 if (!p.getId().isEmpty()) {
115 String value = message.getPropertyById(p.getId());
116 channelUID = new ChannelUID(getThing().getUID(), p.getId());
117 updateChannel(channelUID, value);
119 logger.trace(" updateChannel: {} = {}", channelUID, value);
123 updateStatus(ThingStatus.ONLINE);