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