]> git.basschouten.com Git - openhab-addons.git/blob
6e2b4de590d83865a639033a1529a734359bc71d
[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.THING_TYPE_ZONE;
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.types.StateType;
24 import org.openhab.binding.satel.internal.types.ZoneControl;
25 import org.openhab.binding.satel.internal.types.ZoneState;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.types.Command;
31
32 /**
33  * The {@link SatelZoneHandler} is responsible for handling commands, which are
34  * sent to one of the channels of a zone.
35  *
36  * @author Krzysztof Goworek - Initial contribution
37  */
38 @NonNullByDefault
39 public class SatelZoneHandler extends WirelessChannelsHandler {
40
41     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_ZONE);
42
43     public SatelZoneHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     protected Optional<SatelCommand> convertCommand(ChannelUID channel, Command command) {
49         if (command instanceof OnOffType) {
50             final SatelBridgeHandler bridgeHandler = getBridgeHandler();
51             boolean switchOn = (command == OnOffType.ON);
52             StateType stateType = getStateType(channel.getId());
53             int size = bridgeHandler.getIntegraType().hasExtPayload() ? 32 : 16;
54             byte[] zones = getObjectBitset(size, getThingConfig().getId());
55             ZoneControl action = null;
56             switch ((ZoneState) stateType) {
57                 case BYPASS:
58                     action = switchOn ? ZoneControl.BYPASS : ZoneControl.UNBYPASS;
59                     break;
60                 case ISOLATE:
61                     action = switchOn ? ZoneControl.ISOLATE : null;
62                     break;
63                 default:
64                     // do nothing for other types of state
65                     break;
66             }
67
68             if (action != null) {
69                 return Optional.of(new ControlObjectCommand(action, zones, bridgeHandler.getUserCode(), scheduler));
70             }
71         }
72
73         return Optional.empty();
74     }
75
76     @Override
77     @SuppressWarnings("PMD.CompareObjectsWithEquals")
78     protected StateType getStateType(String channelId) {
79         StateType result = super.getStateType(channelId);
80         if (result == StateType.NONE) {
81             result = ZoneState.valueOf(channelId.toUpperCase());
82         }
83         return result;
84     }
85 }