]> git.basschouten.com Git - openhab-addons.git/blob
c7e25f777f6b47351a48e0dd9e89264cb87d26d1
[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     private long lastRefreshTime = 0;
41
42     public ThingHandlerPartition(Thing thing) {
43         super(thing, CaddxThingType.PARTITION);
44     }
45
46     @Override
47     public void updateChannel(ChannelUID channelUID, String data) {
48         if (CaddxBindingConstants.PARTITION_SECONDARY_COMMAND.equals(channelUID.getId())) {
49             updateState(channelUID, new DecimalType(data));
50         } else {
51             OnOffType onOffType = ("true".equals(data)) ? OnOffType.ON : OnOffType.OFF;
52             updateState(channelUID, onOffType);
53         }
54     }
55
56     @Override
57     public void handleCommand(ChannelUID channelUID, Command command) {
58         logger.debug("handleCommand(): Command Received - {} {}.", channelUID, command);
59
60         String cmd = null;
61         String data = null;
62         CaddxBridgeHandler bridgeHandler = getCaddxBridgeHandler();
63         if (bridgeHandler == null) {
64             return;
65         }
66
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);
72             } else {
73                 return;
74             }
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));
79         } else {
80             logger.debug("Unknown command {}", command);
81             return;
82         }
83
84         if (!data.startsWith("-")) {
85             bridgeHandler.sendCommand(cmd, data);
86         }
87     }
88
89     @Override
90     public void caddxEventReceived(CaddxEvent event, Thing thing) {
91         logger.trace("caddxEventReceived(): Event Received - {}", event);
92
93         if (getThing().equals(thing)) {
94             CaddxMessage message = event.getCaddxMessage();
95             CaddxMessageType mt = message.getCaddxMessageType();
96             ChannelUID channelUID = null;
97
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);
103                 }
104             }
105
106             // Reset the command
107             String value = "-1";
108             channelUID = new ChannelUID(getThing().getUID(), CaddxBindingConstants.PARTITION_SECONDARY_COMMAND);
109             updateChannel(channelUID, value);
110
111             updateStatus(ThingStatus.ONLINE);
112         }
113     }
114 }