2 * Copyright (c) 2010-2022 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.camera;
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;
18 import java.util.List;
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
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;
38 * Handler for security cameras.
40 * This implementation handles services and commands that are common to all cameras, which are currently:
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>
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.
53 * @author David Pace - Initial contribution
57 public class CameraHandler extends BoschSHCHandler {
59 private PrivacyModeService privacyModeService;
60 private CameraNotificationService cameraNotificationService;
62 public CameraHandler(Thing thing) {
64 this.privacyModeService = new PrivacyModeService();
65 this.cameraNotificationService = new CameraNotificationService();
69 protected void initializeServices() throws BoschSHCException {
70 super.initializeServices();
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));
78 public void initialize() {
80 requestInitialStates();
84 * Requests the initial states for relevant services.
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.
90 * Unfortunately the long poll results do not contain camera-related updates, so this is the current approach
91 * to get the initial states.
93 private void requestInitialStates() {
94 requestInitialPrivacyState();
95 requestInitialNotificationState();
98 private void requestInitialPrivacyState() {
101 PrivacyModeServiceState serviceState = privacyModeService.getState();
102 if (serviceState != null) {
103 super.updateState(CHANNEL_PRIVACY_MODE, serviceState.value.toOnOffType());
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());
113 private void requestInitialNotificationState() {
116 CameraNotificationServiceState serviceState = cameraNotificationService.getState();
117 if (serviceState != null) {
118 super.updateState(CHANNEL_CAMERA_NOTIFICATION, serviceState.value.toOnOffType());
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());
129 public void handleCommand(ChannelUID channelUID, Command command) {
130 super.handleCommand(channelUID, command);
132 switch (channelUID.getId()) {
133 case CHANNEL_PRIVACY_MODE:
134 if (command instanceof OnOffType) {
135 updatePrivacyModeState((OnOffType) command);
139 case CHANNEL_CAMERA_NOTIFICATION:
140 if (command instanceof OnOffType) {
141 updateCameraNotificationState((OnOffType) command);
147 private void updatePrivacyModeState(OnOffType command) {
148 PrivacyModeServiceState serviceState = new PrivacyModeServiceState();
149 serviceState.value = PrivacyModeState.from(command);
150 this.updateServiceState(this.privacyModeService, serviceState);
153 private void updateCameraNotificationState(OnOffType command) {
154 CameraNotificationServiceState serviceState = new CameraNotificationServiceState();
155 serviceState.value = CameraNotificationState.from(command);
156 this.updateServiceState(this.cameraNotificationService, serviceState);
159 private void updateChannels(PrivacyModeServiceState state) {
160 super.updateState(CHANNEL_PRIVACY_MODE, state.value.toOnOffType());
163 private void updateChannels(CameraNotificationServiceState state) {
164 super.updateState(CHANNEL_CAMERA_NOTIFICATION, state.value.toOnOffType());