]> git.basschouten.com Git - openhab-addons.git/blob
06eb5f0a1c6316548f38bdfb7058b92034fbe4d8
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.boschshc.internal.devices.BoschSHCHandler;
25 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
26 import org.openhab.binding.boschshc.internal.services.cameranotification.CameraNotificationService;
27 import org.openhab.binding.boschshc.internal.services.cameranotification.CameraNotificationState;
28 import org.openhab.binding.boschshc.internal.services.cameranotification.dto.CameraNotificationServiceState;
29 import org.openhab.binding.boschshc.internal.services.privacymode.PrivacyModeService;
30 import org.openhab.binding.boschshc.internal.services.privacymode.PrivacyModeState;
31 import org.openhab.binding.boschshc.internal.services.privacymode.dto.PrivacyModeServiceState;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.types.Command;
36
37 /**
38  * Handler for security cameras.
39  * <p>
40  * This implementation handles services and commands that are common to all cameras, which are currently:
41  * 
42  * <ul>
43  * <li><code>PrivacyMode</code> - Controls whether the camera records images</li>
44  * <li><code>CameraNotification</code> - Enables or disables notifications for the camera</li>
45  * </ul>
46  * 
47  * <p>
48  * The Eyes outdoor camera advertises a <code>CameraLight</code> service, which unfortunately does not work properly.
49  * Valid states are <code>ON</code> and <code>OFF</code>.
50  * One of my two cameras returns <code>HTTP 204 (No Content)</code> when requesting the state.
51  * Once Bosch supports this service properly, a new subclass may be introduced for the Eyes outdoor camera.
52  * 
53  * @author David Pace - Initial contribution
54  *
55  */
56 @NonNullByDefault
57 public class CameraHandler extends BoschSHCHandler {
58
59     private PrivacyModeService privacyModeService;
60     private CameraNotificationService cameraNotificationService;
61
62     public CameraHandler(Thing thing) {
63         super(thing);
64         this.privacyModeService = new PrivacyModeService();
65         this.cameraNotificationService = new CameraNotificationService();
66     }
67
68     @Override
69     protected void initializeServices() throws BoschSHCException {
70         super.initializeServices();
71
72         this.registerService(this.privacyModeService, this::updateChannels, List.of(CHANNEL_PRIVACY_MODE));
73         this.registerService(this.cameraNotificationService, this::updateChannels,
74                 List.of(CHANNEL_CAMERA_NOTIFICATION));
75     }
76
77     @Override
78     public void initialize() {
79         super.initialize();
80         requestInitialStates();
81     }
82
83     /**
84      * Requests the initial states for relevant services.
85      * <p>
86      * If this is not done, items associated with the corresponding channels with stay in an uninitialized state
87      * (<code>null</code>).
88      * This in turn leads to events not being fired properly when switches are used in the UI.
89      * <p>
90      * Unfortunately the long poll results do not contain camera-related updates, so this is the current approach
91      * to get the initial states.
92      */
93     private void requestInitialStates() {
94         requestInitialPrivacyState();
95         requestInitialNotificationState();
96     }
97
98     private void requestInitialPrivacyState() {
99         try {
100             @Nullable
101             PrivacyModeServiceState serviceState = privacyModeService.getState();
102             if (serviceState != null) {
103                 super.updateState(CHANNEL_PRIVACY_MODE, serviceState.value.toOnOffType());
104             }
105         } catch (InterruptedException e) {
106             Thread.currentThread().interrupt();
107             logger.debug("Could not retrieve the initial privacy state of camera {}", getBoschID());
108         } catch (TimeoutException | ExecutionException | BoschSHCException e) {
109             logger.debug("Could not retrieve the initial privacy state of camera {}", getBoschID());
110         }
111     }
112
113     private void requestInitialNotificationState() {
114         try {
115             @Nullable
116             CameraNotificationServiceState serviceState = cameraNotificationService.getState();
117             if (serviceState != null) {
118                 super.updateState(CHANNEL_CAMERA_NOTIFICATION, serviceState.value.toOnOffType());
119             }
120         } catch (InterruptedException e) {
121             Thread.currentThread().interrupt();
122             logger.debug("Could not retrieve the initial notification state of camera {}", getBoschID());
123         } catch (TimeoutException | ExecutionException | BoschSHCException e) {
124             logger.debug("Could not retrieve the initial notification state of camera {}", getBoschID());
125         }
126     }
127
128     @Override
129     public void handleCommand(ChannelUID channelUID, Command command) {
130         super.handleCommand(channelUID, command);
131
132         switch (channelUID.getId()) {
133             case CHANNEL_PRIVACY_MODE:
134                 if (command instanceof OnOffType) {
135                     updatePrivacyModeState((OnOffType) command);
136                 }
137                 break;
138
139             case CHANNEL_CAMERA_NOTIFICATION:
140                 if (command instanceof OnOffType) {
141                     updateCameraNotificationState((OnOffType) command);
142                 }
143                 break;
144         }
145     }
146
147     private void updatePrivacyModeState(OnOffType command) {
148         PrivacyModeServiceState serviceState = new PrivacyModeServiceState();
149         serviceState.value = PrivacyModeState.from(command);
150         this.updateServiceState(this.privacyModeService, serviceState);
151     }
152
153     private void updateCameraNotificationState(OnOffType command) {
154         CameraNotificationServiceState serviceState = new CameraNotificationServiceState();
155         serviceState.value = CameraNotificationState.from(command);
156         this.updateServiceState(this.cameraNotificationService, serviceState);
157     }
158
159     private void updateChannels(PrivacyModeServiceState state) {
160         super.updateState(CHANNEL_PRIVACY_MODE, state.value.toOnOffType());
161     }
162
163     private void updateChannels(CameraNotificationServiceState state) {
164         super.updateState(CHANNEL_CAMERA_NOTIFICATION, state.value.toOnOffType());
165     }
166 }