]> git.basschouten.com Git - openhab-addons.git/blob
108cf1f0425d735ecc8c52c356cdc4f46ff3e8af
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.camera;
14
15 import static org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants.*;
16
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.boschshc.internal.devices.BoschSHCDeviceHandler;
21 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
22 import org.openhab.binding.boschshc.internal.services.cameranotification.CameraNotificationService;
23 import org.openhab.binding.boschshc.internal.services.cameranotification.CameraNotificationState;
24 import org.openhab.binding.boschshc.internal.services.cameranotification.dto.CameraNotificationServiceState;
25 import org.openhab.binding.boschshc.internal.services.privacymode.PrivacyModeService;
26 import org.openhab.binding.boschshc.internal.services.privacymode.PrivacyModeState;
27 import org.openhab.binding.boschshc.internal.services.privacymode.dto.PrivacyModeServiceState;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.types.Command;
32
33 /**
34  * Handler for security cameras.
35  * <p>
36  * This implementation handles services and commands that are common to all cameras, which are currently:
37  *
38  * <ul>
39  * <li><code>PrivacyMode</code> - Controls whether the camera records images</li>
40  * <li><code>CameraNotification</code> - Enables or disables notifications for the camera</li>
41  * </ul>
42  *
43  * <p>
44  * The Eyes outdoor camera advertises a <code>CameraLight</code> service, which unfortunately does not work properly.
45  * Valid states are <code>ON</code> and <code>OFF</code>.
46  * One of my two cameras returns <code>HTTP 204 (No Content)</code> when requesting the state.
47  * Once Bosch supports this service properly, a new subclass may be introduced for the Eyes outdoor camera.
48  *
49  * @author David Pace - Initial contribution
50  *
51  */
52 @NonNullByDefault
53 public class CameraHandler extends BoschSHCDeviceHandler {
54
55     private PrivacyModeService privacyModeService;
56     private CameraNotificationService cameraNotificationService;
57
58     public CameraHandler(Thing thing) {
59         super(thing);
60         this.privacyModeService = new PrivacyModeService();
61         this.cameraNotificationService = new CameraNotificationService();
62     }
63
64     @Override
65     protected void initializeServices() throws BoschSHCException {
66         super.initializeServices();
67
68         this.registerService(this.privacyModeService, this::updateChannels, List.of(CHANNEL_PRIVACY_MODE), true);
69         this.registerService(this.cameraNotificationService, this::updateChannels, List.of(CHANNEL_CAMERA_NOTIFICATION),
70                 true);
71     }
72
73     @Override
74     public void handleCommand(ChannelUID channelUID, Command command) {
75         super.handleCommand(channelUID, command);
76
77         switch (channelUID.getId()) {
78             case CHANNEL_PRIVACY_MODE:
79                 if (command instanceof OnOffType onOffCommand) {
80                     updatePrivacyModeState(onOffCommand);
81                 }
82                 break;
83
84             case CHANNEL_CAMERA_NOTIFICATION:
85                 if (command instanceof OnOffType onOffCommand) {
86                     updateCameraNotificationState(onOffCommand);
87                 }
88                 break;
89         }
90     }
91
92     private void updatePrivacyModeState(OnOffType command) {
93         PrivacyModeServiceState serviceState = new PrivacyModeServiceState();
94         serviceState.value = PrivacyModeState.from(command);
95         this.updateServiceState(this.privacyModeService, serviceState);
96     }
97
98     private void updateCameraNotificationState(OnOffType command) {
99         CameraNotificationServiceState serviceState = new CameraNotificationServiceState();
100         serviceState.value = CameraNotificationState.from(command);
101         this.updateServiceState(this.cameraNotificationService, serviceState);
102     }
103
104     private void updateChannels(PrivacyModeServiceState state) {
105         super.updateState(CHANNEL_PRIVACY_MODE, state.value.toOnOffType());
106     }
107
108     private void updateChannels(CameraNotificationServiceState state) {
109         super.updateState(CHANNEL_CAMERA_NOTIFICATION, state.value.toOnOffType());
110     }
111 }