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