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.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
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.types.Command;
27 import org.openhab.core.types.RefreshType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * This is a class for handling a Partition type Thing.
34 * @author Georgios Moutsos - Initial contribution
37 public class ThingHandlerPartition extends CaddxBaseThingHandler {
39 private final Logger logger = LoggerFactory.getLogger(ThingHandlerPartition.class);
40 private long lastRefreshTime = 0;
42 public ThingHandlerPartition(Thing thing) {
43 super(thing, CaddxThingType.PARTITION);
47 public void updateChannel(ChannelUID channelUID, String data) {
48 if (CaddxBindingConstants.PARTITION_SECONDARY_COMMAND.equals(channelUID.getId())) {
49 updateState(channelUID, new DecimalType(data));
51 OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
52 updateState(channelUID, onOffType);
57 public void handleCommand(ChannelUID channelUID, Command command) {
58 logger.debug("handleCommand(): Command Received - {} {}.", channelUID, command);
62 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
63 if (bridgeHandler == null) {
67 if (command instanceof RefreshType) {
68 // Refresh only if 2 seconds have passed from the last refresh
69 if (System.currentTimeMillis() - lastRefreshTime > 2000) {
70 cmd = CaddxBindingConstants.PARTITION_STATUS_REQUEST;
71 data = String.format("%d", getPartitionNumber() - 1);
75 lastRefreshTime = System.currentTimeMillis();
76 } else if (channelUID.getId().equals(CaddxBindingConstants.PARTITION_SECONDARY_COMMAND)) {
77 cmd = channelUID.getId();
78 data = String.format("%s,%d", command.toString(), (1 << getPartitionNumber() - 1));
80 logger.debug("Unknown command {}", command);
84 if (!data.startsWith("-")) {
85 bridgeHandler.sendCommand(cmd, data);
90 public void caddxEventReceived(CaddxEvent event, Thing thing) {
91 logger.trace("caddxEventReceived(): Event Received - {}", event);
93 if (getThing().equals(thing)) {
94 CaddxMessage message = event.getCaddxMessage();
95 CaddxMessageType mt = message.getCaddxMessageType();
96 ChannelUID channelUID = null;
98 for (CaddxProperty p : mt.properties) {
99 if (!p.getId().isEmpty()) {
100 String value = message.getPropertyById(p.getId());
101 channelUID = new ChannelUID(getThing().getUID(), p.getId());
102 updateChannel(channelUID, value);
108 channelUID = new ChannelUID(getThing().getUID(), CaddxBindingConstants.PARTITION_SECONDARY_COMMAND);
109 updateChannel(channelUID, value);
111 updateStatus(ThingStatus.ONLINE);