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.urtsi.internal.handler;
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;
30 * The {@link RtsDeviceHandler} is responsible for handling commands, which are
31 * sent to one of the channels.
33 * @author Oliver Libutzki - Initial contribution
35 public class RtsDeviceHandler extends BaseThingHandler {
37 public RtsDeviceHandler(Thing thing) {
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) {
48 rtsCommand = RtsCommand.UP;
51 rtsCommand = RtsCommand.DOWN;
56 } else if (command instanceof StopMoveType) {
57 switch ((StopMoveType) command) {
59 rtsCommand = RtsCommand.STOP;
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(),
71 if (executedSuccessfully && command instanceof State) {
72 updateState(channelUID, (State) command);
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.");
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);