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 upDownCommand) {
46 switch (upDownCommand) {
48 rtsCommand = RtsCommand.UP;
51 rtsCommand = RtsCommand.DOWN;
56 } else if (command instanceof StopMoveType stopMoveCommand) {
57 switch (stopMoveCommand) {
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 deviceHandler) {
69 boolean executedSuccessfully = deviceHandler.executeRtsCommand(getThing(), rtsCommand);
70 if (executedSuccessfully && command instanceof State state) {
71 updateState(channelUID, state);
79 public void initialize() {
80 RtsDeviceConfig rtsDeviceConfig = getConfigAs(RtsDeviceConfig.class);
81 String mappedChannel = UrtsiChannelMapping.getMappedChannel(rtsDeviceConfig.channel);
82 if (mappedChannel == null) {
83 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
84 "The channel '" + rtsDeviceConfig.channel + "' is invalid.");
86 // Just use the status of the bridge as we do not have any information if there a RTS device listening at
87 // the configured channel
88 ThingStatus bridgeStatus = getBridge().getStatus();
89 updateStatus(bridgeStatus);