]> git.basschouten.com Git - openhab-addons.git/blob
dda33d81776bfb42697c8705e0dc48fe8bb50778
[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.elerotransmitterstick.internal.handler;
14
15 import static org.openhab.binding.elerotransmitterstick.internal.EleroTransmitterStickBindingConstants.*;
16
17 import java.util.List;
18
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;
36
37 /**
38  * The {@link EleroChannelHandler} is responsible for handling commands, which are
39  * sent to one of the channels.
40  *
41  * @author Volker Bier - Initial contribution
42  */
43 public class EleroChannelHandler extends BaseThingHandler implements StatusListener {
44     private final Logger logger = LoggerFactory.getLogger(EleroChannelHandler.class);
45
46     protected Integer channelId;
47     protected EleroTransmitterStickHandler bridge;
48
49     public EleroChannelHandler(Thing thing) {
50         super(thing);
51     }
52
53     @Override
54     public void initialize() {
55         bridge = (EleroTransmitterStickHandler) getBridge().getHandler();
56
57         channelId = getConfig().as(EleroChannelConfig.class).channelId;
58         bridge.addStatusListener(channelId, this);
59
60         if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
61             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
62         } else {
63             updateStatus(ThingStatus.UNKNOWN);
64         }
65     }
66
67     @Override
68     public void dispose() {
69         if (bridge != null) {
70             bridge.removeStatusListener(channelId, this);
71         }
72     }
73
74     @Override
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);
80         } else {
81             updateStatus(ThingStatus.UNKNOWN);
82         }
83     }
84
85     @Override
86     public void handleCommand(ChannelUID channelUID, Command command) {
87         logger.debug("Received command {} for channel {}", command, channelUID);
88
89         if (channelUID.getIdWithoutGroup().equals(CONTROL_CHANNEL)) {
90             if (command == UpDownType.UP) {
91                 bridge.getStick().sendCommand(CommandType.UP, List.of(channelId));
92             } else if (command == UpDownType.DOWN) {
93                 bridge.getStick().sendCommand(CommandType.DOWN, List.of(channelId));
94             } else if (command == StopMoveType.STOP) {
95                 bridge.getStick().sendCommand(CommandType.STOP, List.of(channelId));
96             } else if (command instanceof PercentType pt) {
97                 CommandType cmd = CommandType.getForPercent(pt.intValue());
98                 if (cmd != null) {
99                     bridge.getStick().sendCommand(cmd, List.of(channelId));
100                 } else {
101                     logger.debug("Unhandled command {}.", command);
102                 }
103             } else if (command == RefreshType.REFRESH) {
104                 bridge.getStick().requestUpdate(List.of(channelId));
105             }
106         }
107     }
108
109     @Override
110     public void statusChanged(int channelId, ResponseStatus status) {
111         logger.debug("Received updated state {} for thing {}", status, getThing().getUID().toString());
112
113         updateState(STATUS_CHANNEL, new StringType(status.toString()));
114
115         int percentage = ResponseStatus.getPercentageFor(status);
116         if (percentage != -1) {
117             updateState(CONTROL_CHANNEL, new PercentType(percentage));
118         }
119
120         updateStatus(ThingStatus.ONLINE);
121     }
122 }