]> git.basschouten.com Git - openhab-addons.git/blob
2f8947f88466c33a94933b14368dc15f580cef1b
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.vitotronic.internal.handler;
14
15 import org.openhab.core.library.types.DateTimeType;
16 import org.openhab.core.library.types.DecimalType;
17 import org.openhab.core.library.types.OnOffType;
18 import org.openhab.core.library.types.StringType;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.Channel;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.binding.BaseThingHandler;
25 import org.openhab.core.thing.binding.ThingHandler;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link VitotronicThingHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Stefan Andres - Initial contribution
35  */
36
37 public class VitotronicThingHandler extends BaseThingHandler {
38
39     private final Logger logger = LoggerFactory.getLogger(VitotronicThingHandler.class);
40
41     private VitotronicBridgeHandler bridgeHandler;
42
43     public VitotronicThingHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void initialize() {
49         bridgeHandler = getBridgeHandler();
50         logger.debug("Thing Handler for {} started", getThing().getUID().getId());
51         registerVitotronicThingListener(bridgeHandler);
52     }
53
54     @Override
55     public void dispose() {
56         logger.debug("Thing Handler for {} stop", getThing().getUID().getId());
57         unregisterVitotronicThingListener(bridgeHandler);
58     }
59
60     @Override
61     public void handleCommand(ChannelUID channelUID, Command command) {
62         logger.trace("Handle command for channel '{}' command '{}'", channelUID.getId(), command);
63         bridgeHandler.updateChannel(getThing().getUID().getId(), channelUID.getId(), command.toString());
64     }
65
66     @Override
67     public void updateStatus(ThingStatus status) {
68         super.updateStatus(status);
69     }
70
71     private synchronized VitotronicBridgeHandler getBridgeHandler() {
72         Bridge bridge = getBridge();
73         if (bridge == null) {
74             logger.debug("Required bridge not defined for device {}.", getThing().getUID());
75             return null;
76         } else {
77             return getBridgeHandler(bridge);
78         }
79     }
80
81     private synchronized VitotronicBridgeHandler getBridgeHandler(Bridge bridge) {
82         VitotronicBridgeHandler bridgeHandler = null;
83
84         ThingHandler handler = bridge.getHandler();
85         if (handler instanceof VitotronicBridgeHandler vitotronicBridgeHandler) {
86             bridgeHandler = vitotronicBridgeHandler;
87         } else {
88             logger.debug("No available bridge handler found yet. Bridge: {} .", bridge.getUID());
89             bridgeHandler = null;
90         }
91         return bridgeHandler;
92     }
93
94     private void registerVitotronicThingListener(VitotronicBridgeHandler bridgeHandler) {
95         if (bridgeHandler != null) {
96             bridgeHandler.registerVitotronicThingListener(this);
97         } else {
98             logger.debug("Can't register {} at bridge bridgeHandler is null.", this.getThing().getUID());
99         }
100     }
101
102     private void unregisterVitotronicThingListener(VitotronicBridgeHandler bridgeHandler) {
103         if (bridgeHandler != null) {
104             bridgeHandler.unregisterThingListener(this);
105         } else {
106             logger.debug("Can't unregister {} at bridge bridgeHandler is null.", this.getThing().getUID());
107         }
108     }
109
110     public void setChannelValue(String channelId, String value) {
111         Channel channel = getThing().getChannel(channelId);
112         if (channel == null) {
113             logger.trace("Cannel '{}:{}' not implemented", getThing().getUID().getId(), channelId);
114             return;
115         }
116
117         logger.trace("Set {}:{}:{} = {}", getThing().getUID().getId(), channelId, channel.getAcceptedItemType(), value);
118         switch (channel.getAcceptedItemType()) {
119             case "Number":
120                 this.updateState(channelId, new DecimalType(value));
121                 break;
122             case "String":
123                 this.updateState(channelId, new StringType(value));
124                 break;
125             case "Switch":
126                 if (value.toUpperCase().contains("ON")) {
127                     this.updateState(channelId, OnOffType.ON);
128                 } else {
129                     this.updateState(channelId, OnOffType.OFF);
130                 }
131                 break;
132             case "DateTime":
133                 this.updateState(channelId, new DateTimeType(value));
134                 break;
135             default:
136                 logger.trace("Type '{}' for channel '{}' not implemented", channel.getAcceptedItemType(), channelId);
137         }
138     }
139
140     public String getActiveChannelListAsString() {
141         String channelList = "";
142         for (Channel channel : getThing().getChannels()) {
143             if (isLinked(channel.getUID().getId())) {
144                 if (channelList.length() > 0) {
145                     channelList = channelList + "," + channel.getUID().getId();
146                 } else {
147                     channelList = channel.getUID().getId();
148                 }
149             }
150         }
151         return channelList;
152     }
153 }