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