2 * Copyright (c) 2010-2023 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.vitotronic.internal.handler;
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;
31 * The {@link VitotronicHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Stefan Andres - Initial contribution
37 public class VitotronicThingHandler extends BaseThingHandler {
39 private final Logger logger = LoggerFactory.getLogger(VitotronicThingHandler.class);
41 private VitotronicBridgeHandler bridgeHandler;
43 public VitotronicThingHandler(Thing thing) {
48 public void initialize() {
49 bridgeHandler = getBridgeHandler();
50 logger.debug("Thing Handler for {} started", getThing().getUID().getId());
51 registerVitotronicThingListener(bridgeHandler);
55 public void dispose() {
56 logger.debug("Thing Handler for {} stop", getThing().getUID().getId());
57 unregisterVitotronicThingListener(bridgeHandler);
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());
67 public void updateStatus(ThingStatus status) {
68 super.updateStatus(status);
71 private synchronized VitotronicBridgeHandler getBridgeHandler() {
72 Bridge bridge = getBridge();
74 logger.debug("Required bridge not defined for device {}.", getThing().getUID());
77 return getBridgeHandler(bridge);
81 private synchronized VitotronicBridgeHandler getBridgeHandler(Bridge bridge) {
82 VitotronicBridgeHandler bridgeHandler = null;
84 ThingHandler handler = bridge.getHandler();
85 if (handler instanceof VitotronicBridgeHandler) {
86 bridgeHandler = (VitotronicBridgeHandler) handler;
88 logger.debug("No available bridge handler found yet. Bridge: {} .", bridge.getUID());
94 private void registerVitotronicThingListener(VitotronicBridgeHandler bridgeHandler) {
95 if (bridgeHandler != null) {
96 bridgeHandler.registerVitotronicThingListener(this);
98 logger.debug("Can't register {} at bridge bridgeHandler is null.", this.getThing().getUID());
102 private void unregisterVitotronicThingListener(VitotronicBridgeHandler bridgeHandler) {
103 if (bridgeHandler != null) {
104 bridgeHandler.unregisterThingListener(this);
106 logger.debug("Can't unregister {} at bridge bridgeHandler is null.", this.getThing().getUID());
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);
117 logger.trace("Set {}:{}:{} = {}", getThing().getUID().getId(), channelId, channel.getAcceptedItemType(), value);
118 switch (channel.getAcceptedItemType()) {
120 this.updateState(channelId, new DecimalType(value));
123 this.updateState(channelId, new StringType(value));
126 if (value.toUpperCase().contains("ON")) {
127 this.updateState(channelId, OnOffType.ON);
129 this.updateState(channelId, OnOffType.OFF);
133 this.updateState(channelId, new DateTimeType(value));
136 logger.trace("Type '{}' for channel '{}' not implemented", channel.getAcceptedItemType(), channelId);
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();
147 channelList = channel.getUID().getId();