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