]> git.basschouten.com Git - openhab-addons.git/blob
9b164232bf726e46bfb1c80d45b70a7a7d071032
[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.urtsi.internal.handler;
14
15 import org.openhab.binding.urtsi.internal.UrtsiBindingConstants;
16 import org.openhab.binding.urtsi.internal.config.RtsDeviceConfig;
17 import org.openhab.binding.urtsi.internal.mapping.UrtsiChannelMapping;
18 import org.openhab.core.library.types.StopMoveType;
19 import org.openhab.core.library.types.UpDownType;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
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.openhab.core.types.State;
28
29 /**
30  * The {@link RtsDeviceHandler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author Oliver Libutzki - Initial contribution
34  */
35 public class RtsDeviceHandler extends BaseThingHandler {
36
37     public RtsDeviceHandler(Thing thing) {
38         super(thing);
39     }
40
41     @Override
42     public void handleCommand(ChannelUID channelUID, Command command) {
43         if (channelUID.getId().equals(UrtsiBindingConstants.POSITION)) {
44             RtsCommand rtsCommand = null;
45             if (command instanceof UpDownType) {
46                 switch ((UpDownType) command) {
47                     case UP:
48                         rtsCommand = RtsCommand.UP;
49                         break;
50                     case DOWN:
51                         rtsCommand = RtsCommand.DOWN;
52                         break;
53                     default:
54                         break;
55                 }
56             } else if (command instanceof StopMoveType) {
57                 switch ((StopMoveType) command) {
58                     case STOP:
59                         rtsCommand = RtsCommand.STOP;
60                         break;
61                     default:
62                         break;
63                 }
64             }
65             if (rtsCommand != null) {
66                 // We delegate the execution to the bridge handler
67                 ThingHandler bridgeHandler = getBridge().getHandler();
68                 if (bridgeHandler instanceof UrtsiDeviceHandler) {
69                     boolean executedSuccessfully = ((UrtsiDeviceHandler) bridgeHandler).executeRtsCommand(getThing(),
70                             rtsCommand);
71                     if (executedSuccessfully && command instanceof State) {
72                         updateState(channelUID, (State) command);
73                     }
74                 }
75             }
76         }
77     }
78
79     @Override
80     public void initialize() {
81         RtsDeviceConfig rtsDeviceConfig = getConfigAs(RtsDeviceConfig.class);
82         String mappedChannel = UrtsiChannelMapping.getMappedChannel(rtsDeviceConfig.channel);
83         if (mappedChannel == null) {
84             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
85                     "The channel '" + rtsDeviceConfig.channel + "' is invalid.");
86         } else {
87             // Just use the status of the bridge as we do not have any information if there a RTS device listening at
88             // the configured channel
89             ThingStatus bridgeStatus = getBridge().getStatus();
90             updateStatus(bridgeStatus);
91
92         }
93     }
94 }