2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.boschshc.internal.devices.intrusion;
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;
23 import java.util.List;
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;
47 * Handler for the intrusion detection alarm system.
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>
59 * @author David Pace - Initial contribution
63 public class IntrusionDetectionHandler extends BoschSHCHandler {
65 private IntrusionDetectionSystemStateService intrusionDetectionSystemStateService;
66 private IntrusionDetectionControlStateService intrusionDetectionControlStateService;
67 private SurveillanceAlarmService surveillanceAlarmService;
68 private ArmActionService armActionService;
69 private DisarmActionService disarmActionService;
70 private MuteActionService muteActionService;
72 public IntrusionDetectionHandler(Thing 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();
83 public @Nullable String getBoschID() {
84 return BoschSHCBindingConstants.SERVICE_INTRUSION_DETECTION;
88 protected void initializeServices() throws BoschSHCException {
89 super.initializeServices();
91 this.registerService(intrusionDetectionSystemStateService, this::updateChannels,
92 List.of(CHANNEL_SYSTEM_AVAILABILITY, CHANNEL_ARMING_STATE, CHANNEL_ALARM_STATE,
93 CHANNEL_ACTIVE_CONFIGURATION_PROFILE),
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);
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));
111 private void updateChannels(IntrusionDetectionControlState controlState) {
112 super.updateState(CHANNEL_ARMING_STATE, new StringType(controlState.value.toString()));
115 private void updateChannels(SurveillanceAlarmState surveillanceAlarmState) {
116 super.updateState(CHANNEL_ALARM_STATE, new StringType(surveillanceAlarmState.value.toString()));
120 public void handleCommand(ChannelUID channelUID, Command command) {
121 super.handleCommand(channelUID, command);
123 switch (channelUID.getId()) {
124 case CHANNEL_ARM_ACTION:
125 if (command instanceof StringType stringCommand) {
126 armIntrusionDetectionSystem(stringCommand);
129 case CHANNEL_DISARM_ACTION:
130 if (command instanceof OnOffType onOffCommand) {
131 disarmIntrusionDetectionSystem(onOffCommand);
134 case CHANNEL_MUTE_ACTION:
135 if (command instanceof OnOffType onOffCommand) {
136 muteIntrusionDetectionSystem(onOffCommand);
142 private void armIntrusionDetectionSystem(StringType profileIdCommand) {
143 ArmActionRequest armActionRequest = new ArmActionRequest();
144 armActionRequest.profileId = profileIdCommand.toFullString();
145 postAction(armActionService, armActionRequest);
148 private void disarmIntrusionDetectionSystem(OnOffType command) {
149 if (command == OnOffType.ON) {
150 postAction(disarmActionService);
154 private void muteIntrusionDetectionSystem(OnOffType command) {
155 if (command == OnOffType.ON) {
156 postAction(muteActionService);