]> git.basschouten.com Git - openhab-addons.git/blob
15a674531eeb7651ea7df5c373f9e653ccba756a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.caddx.internal.handler;
14
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;
30
31 /**
32  * This is a class for handling a Partition type Thing.
33  *
34  * @author Georgios Moutsos - Initial contribution
35  */
36 @NonNullByDefault
37 public class ThingHandlerPartition extends CaddxBaseThingHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(ThingHandlerPartition.class);
40
41     /**
42      * Constructor.
43      *
44      * @param thing
45      */
46     public ThingHandlerPartition(Thing thing) {
47         super(thing, CaddxThingType.PARTITION);
48     }
49
50     @Override
51     public void updateChannel(ChannelUID channelUID, String data) {
52         if (CaddxBindingConstants.PARTITION_SECONDARY_COMMAND.equals(channelUID.getId())) {
53             updateState(channelUID, new DecimalType(data));
54         } else {
55             OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
56             updateState(channelUID, onOffType);
57         }
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         logger.debug("handleCommand(): Command Received - {} {}.", channelUID, command);
63
64         String cmd = null;
65         String data = null;
66         CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
67         if (bridgeHandler == null) {
68             return;
69         }
70
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);
75             } else {
76                 return;
77             }
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));
81         } else {
82             logger.debug("Unknown command {}", command);
83             return;
84         }
85
86         if (!data.startsWith("-")) {
87             bridgeHandler.sendCommand(cmd, data);
88         }
89     }
90
91     @Override
92     public void caddxEventReceived(CaddxEvent event, Thing thing) {
93         logger.trace("caddxEventReceived(): Event Received - {}", event);
94
95         if (getThing().equals(thing)) {
96             CaddxMessage message = event.getCaddxMessage();
97             CaddxMessageType mt = message.getCaddxMessageType();
98             ChannelUID channelUID = null;
99
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);
105                 }
106             }
107
108             // Reset the command
109             String value = "-1";
110             channelUID = new ChannelUID(getThing().getUID(), CaddxBindingConstants.PARTITION_SECONDARY_COMMAND);
111             updateChannel(channelUID, value);
112
113             updateStatus(ThingStatus.ONLINE);
114         }
115     }
116 }