]> git.basschouten.com Git - openhab-addons.git/blob
d8b61aea192e0048f15e46b464ec52de4268e49b
[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.boschshc.internal.devices.intrusion;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_ACTIVE_CONFIGURATION_PROFILE;
16 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_ALARM_STATE;
17 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_ARMING_STATE;
18 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_ARM_ACTION;
19 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_DISARM_ACTION;
20 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_MUTE_ACTION;
21 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.CHANNEL_SYSTEM_AVAILABILITY;
22
23 import java.util.List;
24
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
28 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
29 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
30 import org.openhab.binding.boschshc.internal.services.intrusion.IntrusionDetectionControlStateService;
31 import org.openhab.binding.boschshc.internal.services.intrusion.IntrusionDetectionSystemStateService;
32 import org.openhab.binding.boschshc.internal.services.intrusion.SurveillanceAlarmService;
33 import org.openhab.binding.boschshc.internal.services.intrusion.actions.arm.ArmActionService;
34 import org.openhab.binding.boschshc.internal.services.intrusion.actions.arm.dto.ArmActionRequest;
35 import org.openhab.binding.boschshc.internal.services.intrusion.actions.disarm.DisarmActionService;
36 import org.openhab.binding.boschshc.internal.services.intrusion.actions.mute.MuteActionService;
37 import org.openhab.binding.boschshc.internal.services.intrusion.dto.IntrusionDetectionControlState;
38 import org.openhab.binding.boschshc.internal.services.intrusion.dto.IntrusionDetectionSystemState;
39 import org.openhab.binding.boschshc.internal.services.intrusion.dto.SurveillanceAlarmState;
40 import org.openhab.core.library.types.OnOffType;
41 import org.openhab.core.library.types.StringType;
42 import org.openhab.core.thing.ChannelUID;
43 import org.openhab.core.thing.Thing;
44 import org.openhab.core.types.Command;
45
46 /**
47  * Handler for the intrusion detection alarm system.
48  * <p>
49  * It supports
50  * <ul>
51  * <li>Obtaining the current intrusion detection system state</li>
52  * <li>Receiving updates related to the detection control state</li>
53  * <li>Receiving updates related to surveillance alarm events</li>
54  * <li>Arming the system</li>
55  * <li>Disarming the system</li>
56  * <li>Muting the alarm</li>
57  * </ul>
58  * 
59  * @author David Pace - Initial contribution
60  *
61  */
62 @NonNullByDefault
63 public class IntrusionDetectionHandler extends BoschSHCHandler {
64
65     private IntrusionDetectionSystemStateService intrusionDetectionSystemStateService;
66     private IntrusionDetectionControlStateService intrusionDetectionControlStateService;
67     private SurveillanceAlarmService surveillanceAlarmService;
68     private ArmActionService armActionService;
69     private DisarmActionService disarmActionService;
70     private MuteActionService muteActionService;
71
72     public IntrusionDetectionHandler(Thing thing) {
73         super(thing);
74         this.intrusionDetectionSystemStateService = new IntrusionDetectionSystemStateService();
75         this.intrusionDetectionControlStateService = new IntrusionDetectionControlStateService();
76         this.surveillanceAlarmService = new SurveillanceAlarmService();
77         this.armActionService = new ArmActionService();
78         this.disarmActionService = new DisarmActionService();
79         this.muteActionService = new MuteActionService();
80     }
81
82     @Override
83     public @Nullable String getBoschID() {
84         return BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION;
85     }
86
87     @Override
88     protected void initializeServices() throws BoschSHCException {
89         super.initializeServices();
90
91         this.registerService(intrusionDetectionSystemStateService, this::updateChannels,
92                 List.of(CHANNEL_SYSTEM_AVAILABILITY, CHANNEL_ARMING_STATE, CHANNEL_ALARM_STATE,
93                         CHANNEL_ACTIVE_CONFIGURATION_PROFILE),
94                 true);
95         this.registerService(intrusionDetectionControlStateService, this::updateChannels,
96                 List.of(CHANNEL_ARMING_STATE));
97         this.registerService(surveillanceAlarmService, this::updateChannels, List.of(CHANNEL_ALARM_STATE));
98         this.registerStatelessService(armActionService);
99         this.registerStatelessService(disarmActionService);
100         this.registerStatelessService(muteActionService);
101     }
102
103     private void updateChannels(IntrusionDetectionSystemState systemState) {
104         super.updateState(CHANNEL_SYSTEM_AVAILABILITY, OnOffType.from(systemState.systemAvailability.available));
105         super.updateState(CHANNEL_ARMING_STATE, new StringType(systemState.armingState.state.toString()));
106         super.updateState(CHANNEL_ALARM_STATE, new StringType(systemState.alarmState.value.toString()));
107         super.updateState(CHANNEL_ACTIVE_CONFIGURATION_PROFILE,
108                 new StringType(systemState.activeConfigurationProfile.profileId));
109     }
110
111     private void updateChannels(IntrusionDetectionControlState controlState) {
112         super.updateState(CHANNEL_ARMING_STATE, new StringType(controlState.value.toString()));
113     }
114
115     private void updateChannels(SurveillanceAlarmState surveillanceAlarmState) {
116         super.updateState(CHANNEL_ALARM_STATE, new StringType(surveillanceAlarmState.value.toString()));
117     }
118
119     @Override
120     public void handleCommand(ChannelUID channelUID, Command command) {
121         super.handleCommand(channelUID, command);
122
123         switch (channelUID.getId()) {
124             case CHANNEL_ARM_ACTION:
125                 if (command instanceof StringType) {
126                     armIntrusionDetectionSystem((StringType) command);
127                 }
128                 break;
129             case CHANNEL_DISARM_ACTION:
130                 if (command instanceof OnOffType) {
131                     disarmIntrusionDetectionSystem((OnOffType) command);
132                 }
133                 break;
134             case CHANNEL_MUTE_ACTION:
135                 if (command instanceof OnOffType) {
136                     muteIntrusionDetectionSystem((OnOffType) command);
137                 }
138                 break;
139         }
140     }
141
142     private void armIntrusionDetectionSystem(StringType profileIdCommand) {
143         ArmActionRequest armActionRequest = new ArmActionRequest();
144         armActionRequest.profileId = profileIdCommand.toFullString();
145         postAction(armActionService, armActionRequest);
146     }
147
148     private void disarmIntrusionDetectionSystem(OnOffType command) {
149         if (command == OnOffType.ON) {
150             postAction(disarmActionService);
151         }
152     }
153
154     private void muteIntrusionDetectionSystem(OnOffType command) {
155         if (command == OnOffType.ON) {
156             postAction(muteActionService);
157         }
158     }
159 }