]> git.basschouten.com Git - openhab-addons.git/blob
a8c5a49c20533a95e2317cc2e042d9ddc8e73b9b
[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, CHANNEL_ACTIVE_CONFIGURATION_PROFILE));
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         super.updateState(CHANNEL_ACTIVE_CONFIGURATION_PROFILE, new StringType(controlState.activeProfile));
114     }
115
116     private void updateChannels(SurveillanceAlarmState surveillanceAlarmState) {
117         super.updateState(CHANNEL_ALARM_STATE, new StringType(surveillanceAlarmState.value.toString()));
118     }
119
120     @Override
121     public void handleCommand(ChannelUID channelUID, Command command) {
122         super.handleCommand(channelUID, command);
123
124         switch (channelUID.getId()) {
125             case CHANNEL_ARM_ACTION:
126                 if (command instanceof StringType stringCommand) {
127                     armIntrusionDetectionSystem(stringCommand);
128                 }
129                 break;
130             case CHANNEL_DISARM_ACTION:
131                 if (command instanceof OnOffType onOffCommand) {
132                     disarmIntrusionDetectionSystem(onOffCommand);
133                 }
134                 break;
135             case CHANNEL_MUTE_ACTION:
136                 if (command instanceof OnOffType onOffCommand) {
137                     muteIntrusionDetectionSystem(onOffCommand);
138                 }
139                 break;
140         }
141     }
142
143     private void armIntrusionDetectionSystem(StringType profileIdCommand) {
144         ArmActionRequest armActionRequest = new ArmActionRequest();
145         armActionRequest.profileId = profileIdCommand.toFullString();
146         postAction(armActionService, armActionRequest);
147     }
148
149     private void disarmIntrusionDetectionSystem(OnOffType command) {
150         if (command == OnOffType.ON) {
151             postAction(disarmActionService);
152         }
153     }
154
155     private void muteIntrusionDetectionSystem(OnOffType command) {
156         if (command == OnOffType.ON) {
157             postAction(muteActionService);
158         }
159     }
160 }