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.satel.internal.handler;
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
17 import java.util.Optional;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.satel.internal.command.ControlObjectCommand;
22 import org.openhab.binding.satel.internal.command.SatelCommand;
23 import org.openhab.binding.satel.internal.event.IntegraStateEvent;
24 import org.openhab.binding.satel.internal.types.OutputControl;
25 import org.openhab.binding.satel.internal.types.OutputState;
26 import org.openhab.binding.satel.internal.types.StateType;
27 import org.openhab.core.library.types.StopMoveType;
28 import org.openhab.core.library.types.UpDownType;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.types.Command;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link SatelShutterHandler} is responsible for handling commands, which are
39 * sent to one of the channels of a shutter device.
41 * @author Krzysztof Goworek - Initial contribution
44 public class SatelShutterHandler extends SatelStateThingHandler {
46 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SHUTTER);
48 private final Logger logger = LoggerFactory.getLogger(SatelShutterHandler.class);
50 public SatelShutterHandler(Thing thing) {
55 public void incomingEvent(IntegraStateEvent event) {
56 logger.trace("Handling incoming event: {}", event);
57 if (getThingConfig().isCommandOnly() || !event.hasDataForState(OutputState.STATE)) {
60 Channel channel = getThing().getChannel(CHANNEL_SHUTTER_STATE);
61 if (channel != null) {
62 int upBitNbr = getThingConfig().getUpId() - 1;
63 int downBitNbr = getThingConfig().getDownId() - 1;
64 if (event.isSet(OutputState.STATE, upBitNbr)) {
65 if (!event.isSet(OutputState.STATE, downBitNbr)) {
66 updateState(channel.getUID(), UpDownType.UP);
68 } else if (event.isSet(OutputState.STATE, downBitNbr)) {
69 updateState(channel.getUID(), UpDownType.DOWN);
75 protected StateType getStateType(String channelId) {
76 return CHANNEL_SHUTTER_STATE.equals(channelId) ? OutputState.STATE : StateType.NONE;
80 protected Optional<SatelCommand> convertCommand(ChannelUID channel, Command command) {
81 ControlObjectCommand result = null;
82 if (CHANNEL_SHUTTER_STATE.equals(channel.getId())) {
83 final SatelBridgeHandler bridgeHandler = getBridgeHandler();
84 int cmdBytes = bridgeHandler.getIntegraType().hasExtPayload() ? 32 : 16;
85 if (command == UpDownType.UP) {
86 byte[] outputs = getObjectBitset(cmdBytes, getThingConfig().getUpId());
87 result = new ControlObjectCommand(OutputControl.ON, outputs, bridgeHandler.getUserCode(), scheduler);
88 } else if (command == UpDownType.DOWN) {
89 byte[] outputs = getObjectBitset(cmdBytes, getThingConfig().getDownId());
90 result = new ControlObjectCommand(OutputControl.ON, outputs, bridgeHandler.getUserCode(), scheduler);
91 } else if (command == StopMoveType.STOP) {
92 byte[] outputs = getObjectBitset(cmdBytes, getThingConfig().getUpId(), getThingConfig().getDownId());
93 result = new ControlObjectCommand(OutputControl.OFF, outputs, bridgeHandler.getUserCode(), scheduler);
97 return result == null ? Optional.empty() : Optional.of(result);