]> git.basschouten.com Git - openhab-addons.git/blob
b96c9ff5efc81392d3a4eb25da36913abe1ecbc4
[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.satel.internal.handler;
14
15 import static org.openhab.binding.satel.internal.SatelBindingConstants.*;
16
17 import java.util.Optional;
18 import java.util.Set;
19
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;
36
37 /**
38  * The {@link SatelShutterHandler} is responsible for handling commands, which are
39  * sent to one of the channels of a shutter device.
40  *
41  * @author Krzysztof Goworek - Initial contribution
42  */
43 @NonNullByDefault
44 public class SatelShutterHandler extends SatelStateThingHandler {
45
46     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_SHUTTER);
47
48     private final Logger logger = LoggerFactory.getLogger(SatelShutterHandler.class);
49
50     public SatelShutterHandler(Thing thing) {
51         super(thing);
52     }
53
54     @Override
55     public void incomingEvent(IntegraStateEvent event) {
56         logger.trace("Handling incoming event: {}", event);
57         if (getThingConfig().isCommandOnly() || !event.hasDataForState(OutputState.STATE)) {
58             return;
59         }
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);
67                 }
68             } else if (event.isSet(OutputState.STATE, downBitNbr)) {
69                 updateState(channel.getUID(), UpDownType.DOWN);
70             }
71         }
72     }
73
74     @Override
75     protected StateType getStateType(String channelId) {
76         return CHANNEL_SHUTTER_STATE.equals(channelId) ? OutputState.STATE : StateType.NONE;
77     }
78
79     @Override
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);
94             }
95         }
96
97         return result == null ? Optional.empty() : Optional.of(result);
98     }
99 }