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);
46 public ThingHandlerPartition(Thing thing) {
47 super(thing, CaddxThingType.PARTITION);
51 public void updateChannel(ChannelUID channelUID, String data) {
52 if (CaddxBindingConstants.PARTITION_SECONDARY_COMMAND.equals(channelUID.getId())) {
53 updateState(channelUID, new DecimalType(data));
55 OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
56 updateState(channelUID, onOffType);
61 public void handleCommand(ChannelUID channelUID, Command command) {
62 logger.debug("handleCommand(): Command Received - {} {}.", channelUID, command);
66 CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
67 if (bridgeHandler == null) {
71 if (command instanceof RefreshType) {
72 if (channelUID.getId().equals(CaddxBindingConstants.PARTITION_ARMED)) {
73 cmd = CaddxBindingConstants.PARTITION_STATUS_REQUEST;
74 data = String.format("%d", getPartitionNumber() - 1);
78 } else if (channelUID.getId().equals(CaddxBindingConstants.PARTITION_SECONDARY_COMMAND)) {
79 cmd = channelUID.getId();
80 data = String.format("%s,%d", command.toString(), (1 << getPartitionNumber() - 1));
82 logger.debug("Unknown command {}", command);
86 if (!data.startsWith("-")) {
87 bridgeHandler.sendCommand(cmd, data);
92 public void caddxEventReceived(CaddxEvent event, Thing thing) {
93 logger.trace("caddxEventReceived(): Event Received - {}", event);
95 if (getThing().equals(thing)) {
96 CaddxMessage message = event.getCaddxMessage();
97 CaddxMessageType mt = message.getCaddxMessageType();
98 ChannelUID channelUID = null;
100 for (CaddxProperty p : mt.properties) {
101 if (!p.getId().isEmpty()) {
102 String value = message.getPropertyById(p.getId());
103 channelUID = new ChannelUID(getThing().getUID(), p.getId());
104 updateChannel(channelUID, value);
110 channelUID = new ChannelUID(getThing().getUID(), CaddxBindingConstants.PARTITION_SECONDARY_COMMAND);
111 updateChannel(channelUID, value);
113 updateStatus(ThingStatus.ONLINE);