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.elerotransmitterstick.internal.handler;
15 import static org.openhab.binding.elerotransmitterstick.internal.EleroTransmitterStickBindingConstants.*;
17 import java.util.Collections;
19 import org.openhab.binding.elerotransmitterstick.internal.config.EleroChannelConfig;
20 import org.openhab.binding.elerotransmitterstick.internal.stick.CommandType;
21 import org.openhab.binding.elerotransmitterstick.internal.stick.ResponseStatus;
22 import org.openhab.core.library.types.PercentType;
23 import org.openhab.core.library.types.StopMoveType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.library.types.UpDownType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.ThingStatusInfo;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link EleroChannelHandler} is responsible for handling commands, which are
39 * sent to one of the channels.
41 * @author Volker Bier - Initial contribution
43 public class EleroChannelHandler extends BaseThingHandler implements StatusListener {
44 private final Logger logger = LoggerFactory.getLogger(EleroChannelHandler.class);
46 protected Integer channelId;
47 protected EleroTransmitterStickHandler bridge;
49 public EleroChannelHandler(Thing thing) {
54 public void initialize() {
55 bridge = (EleroTransmitterStickHandler) getBridge().getHandler();
57 channelId = getConfig().as(EleroChannelConfig.class).channelId;
58 bridge.addStatusListener(channelId, this);
60 if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
61 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
63 updateStatus(ThingStatus.UNKNOWN);
68 public void dispose() {
70 bridge.removeStatusListener(channelId, this);
75 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
76 if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
77 logger.debug("Bridge for Elero channel handler for thing {} ({}) changed status to {}",
78 getThing().getLabel(), getThing().getUID(), bridgeStatusInfo.getStatus().toString());
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
81 updateStatus(ThingStatus.UNKNOWN);
86 public void handleCommand(ChannelUID channelUID, Command command) {
87 logger.debug("Received command {} for channel {}", command, channelUID);
89 if (channelUID.getIdWithoutGroup().equals(CONTROL_CHANNEL)) {
90 if (command == UpDownType.UP) {
91 bridge.getStick().sendCommand(CommandType.UP, Collections.singletonList(channelId));
92 } else if (command == UpDownType.DOWN) {
93 bridge.getStick().sendCommand(CommandType.DOWN, Collections.singletonList(channelId));
94 } else if (command == StopMoveType.STOP) {
95 bridge.getStick().sendCommand(CommandType.STOP, Collections.singletonList(channelId));
96 } else if (command instanceof PercentType) {
97 CommandType cmd = CommandType.getForPercent(((PercentType) command).intValue());
99 bridge.getStick().sendCommand(cmd, Collections.singletonList(channelId));
101 logger.debug("Unhandled command {}.", command);
103 } else if (command == RefreshType.REFRESH) {
104 bridge.getStick().requestUpdate(Collections.singletonList(channelId));
110 public void statusChanged(int channelId, ResponseStatus status) {
111 logger.debug("Received updated state {} for thing {}", status, getThing().getUID().toString());
113 updateState(STATUS_CHANNEL, new StringType(status.toString()));
115 int percentage = ResponseStatus.getPercentageFor(status);
116 if (percentage != -1) {
117 updateState(CONTROL_CHANNEL, new PercentType(percentage));
120 updateStatus(ThingStatus.ONLINE);