]> git.basschouten.com Git - openhab-addons.git/blob
e8190cf082a7dea502022bb8ad2cb5b507b201a9
[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_PARTITION;
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.PartitionControl;
24 import org.openhab.binding.satel.internal.types.PartitionState;
25 import org.openhab.binding.satel.internal.types.StateType;
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 SatelPartitionHandler} is responsible for handling commands, which are
34  * sent to one of the channels of a partition device.
35  *
36  * @author Krzysztof Goworek - Initial contribution
37  */
38 @NonNullByDefault
39 public class SatelPartitionHandler extends SatelStateThingHandler {
40
41     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_PARTITION);
42
43     public SatelPartitionHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     protected StateType getStateType(String channelId) {
49         return PartitionState.valueOf(channelId.toUpperCase());
50     }
51
52     @Override
53     protected Optional<SatelCommand> convertCommand(ChannelUID channel, Command command) {
54         if (command instanceof OnOffType) {
55             boolean switchOn = (command == OnOffType.ON);
56             StateType stateType = getStateType(channel.getId());
57             byte[] partitions = getObjectBitset(4, getThingConfig().getId());
58             boolean forceArm = getThingConfig().isForceArmingEnabled();
59             PartitionControl action = null;
60             switch ((PartitionState) stateType) {
61                 // clear alarms on OFF command
62                 case ALARM:
63                 case ALARM_MEMORY:
64                 case FIRE_ALARM:
65                 case FIRE_ALARM_MEMORY:
66                 case VERIFIED_ALARMS:
67                 case WARNING_ALARMS:
68                     action = switchOn ? null : PartitionControl.CLEAR_ALARM;
69                     break;
70
71                 // arm or disarm, depending on command
72                 case ARMED:
73                 case REALLY_ARMED:
74                     action = switchOn ? (forceArm ? PartitionControl.FORCE_ARM_MODE_0 : PartitionControl.ARM_MODE_0)
75                             : PartitionControl.DISARM;
76                     break;
77                 case ARMED_MODE_1:
78                     action = switchOn ? (forceArm ? PartitionControl.FORCE_ARM_MODE_1 : PartitionControl.ARM_MODE_1)
79                             : PartitionControl.DISARM;
80                     break;
81                 case ARMED_MODE_2:
82                     action = switchOn ? (forceArm ? PartitionControl.FORCE_ARM_MODE_2 : PartitionControl.ARM_MODE_2)
83                             : PartitionControl.DISARM;
84                     break;
85                 case ARMED_MODE_3:
86                     action = switchOn ? (forceArm ? PartitionControl.FORCE_ARM_MODE_3 : PartitionControl.ARM_MODE_3)
87                             : PartitionControl.DISARM;
88                     break;
89
90                 // do nothing for other types of state
91                 default:
92                     break;
93             }
94
95             if (action != null) {
96                 return Optional
97                         .of(new ControlObjectCommand(action, partitions, getBridgeHandler().getUserCode(), scheduler));
98             }
99         }
100
101         return Optional.empty();
102     }
103 }