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