]> git.basschouten.com Git - openhab-addons.git/blob
26d33bed9690dcdfb900d614aa4bdd182fd551d2
[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.nest.internal.wwn.handler;
14
15 import static org.openhab.binding.nest.internal.wwn.WWNBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_FIRMWARE_VERSION;
17 import static org.openhab.core.types.RefreshType.REFRESH;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.nest.internal.wwn.dto.WWNCamera;
22 import org.openhab.binding.nest.internal.wwn.dto.WWNCameraEvent;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Handles all the updates to the camera as well as handling the commands that send updates to the WWN API.
35  *
36  * @author David Bennett - Initial contribution
37  * @author Wouter Born - Handle channel refresh command
38  */
39 @NonNullByDefault
40 public class WWNCameraHandler extends WWNBaseHandler<WWNCamera> {
41     private final Logger logger = LoggerFactory.getLogger(WWNCameraHandler.class);
42
43     public WWNCameraHandler(Thing thing) {
44         super(thing, WWNCamera.class);
45     }
46
47     @Override
48     protected State getChannelState(ChannelUID channelUID, WWNCamera camera) {
49         if (channelUID.getId().startsWith(CHANNEL_GROUP_CAMERA_PREFIX)) {
50             return getCameraChannelState(channelUID, camera);
51         } else if (channelUID.getId().startsWith(CHANNEL_GROUP_LAST_EVENT_PREFIX)) {
52             return getLastEventChannelState(channelUID, camera);
53         } else {
54             logger.error("Unsupported channelId '{}'", channelUID.getId());
55             return UnDefType.UNDEF;
56         }
57     }
58
59     protected State getCameraChannelState(ChannelUID channelUID, WWNCamera camera) {
60         switch (channelUID.getId()) {
61             case CHANNEL_CAMERA_APP_URL:
62                 return getAsStringTypeOrNull(camera.getAppUrl());
63             case CHANNEL_CAMERA_AUDIO_INPUT_ENABLED:
64                 return getAsOnOffTypeOrNull(camera.isAudioInputEnabled());
65             case CHANNEL_CAMERA_LAST_ONLINE_CHANGE:
66                 return getAsDateTimeTypeOrNull(camera.getLastIsOnlineChange());
67             case CHANNEL_CAMERA_PUBLIC_SHARE_ENABLED:
68                 return getAsOnOffTypeOrNull(camera.isPublicShareEnabled());
69             case CHANNEL_CAMERA_PUBLIC_SHARE_URL:
70                 return getAsStringTypeOrNull(camera.getPublicShareUrl());
71             case CHANNEL_CAMERA_SNAPSHOT_URL:
72                 return getAsStringTypeOrNull(camera.getSnapshotUrl());
73             case CHANNEL_CAMERA_STREAMING:
74                 return getAsOnOffTypeOrNull(camera.isStreaming());
75             case CHANNEL_CAMERA_VIDEO_HISTORY_ENABLED:
76                 return getAsOnOffTypeOrNull(camera.isVideoHistoryEnabled());
77             case CHANNEL_CAMERA_WEB_URL:
78                 return getAsStringTypeOrNull(camera.getWebUrl());
79             default:
80                 logger.error("Unsupported channelId '{}'", channelUID.getId());
81                 return UnDefType.UNDEF;
82         }
83     }
84
85     protected State getLastEventChannelState(ChannelUID channelUID, WWNCamera camera) {
86         WWNCameraEvent lastEvent = camera.getLastEvent();
87         if (lastEvent == null) {
88             return UnDefType.NULL;
89         }
90
91         switch (channelUID.getId()) {
92             case CHANNEL_LAST_EVENT_ACTIVITY_ZONES:
93                 return getAsStringTypeListOrNull(lastEvent.getActivityZones());
94             case CHANNEL_LAST_EVENT_ANIMATED_IMAGE_URL:
95                 return getAsStringTypeOrNull(lastEvent.getAnimatedImageUrl());
96             case CHANNEL_LAST_EVENT_APP_URL:
97                 return getAsStringTypeOrNull(lastEvent.getAppUrl());
98             case CHANNEL_LAST_EVENT_END_TIME:
99                 return getAsDateTimeTypeOrNull(lastEvent.getEndTime());
100             case CHANNEL_LAST_EVENT_HAS_MOTION:
101                 return getAsOnOffTypeOrNull(lastEvent.isHasMotion());
102             case CHANNEL_LAST_EVENT_HAS_PERSON:
103                 return getAsOnOffTypeOrNull(lastEvent.isHasPerson());
104             case CHANNEL_LAST_EVENT_HAS_SOUND:
105                 return getAsOnOffTypeOrNull(lastEvent.isHasSound());
106             case CHANNEL_LAST_EVENT_IMAGE_URL:
107                 return getAsStringTypeOrNull(lastEvent.getImageUrl());
108             case CHANNEL_LAST_EVENT_START_TIME:
109                 return getAsDateTimeTypeOrNull(lastEvent.getStartTime());
110             case CHANNEL_LAST_EVENT_URLS_EXPIRE_TIME:
111                 return getAsDateTimeTypeOrNull(lastEvent.getUrlsExpireTime());
112             case CHANNEL_LAST_EVENT_WEB_URL:
113                 return getAsStringTypeOrNull(lastEvent.getWebUrl());
114             default:
115                 logger.error("Unsupported channelId '{}'", channelUID.getId());
116                 return UnDefType.UNDEF;
117         }
118     }
119
120     @Override
121     public void handleCommand(ChannelUID channelUID, Command command) {
122         if (REFRESH.equals(command)) {
123             WWNCamera lastUpdate = getLastUpdate();
124             if (lastUpdate != null) {
125                 updateState(channelUID, getChannelState(channelUID, lastUpdate));
126             }
127         } else if (CHANNEL_CAMERA_STREAMING.equals(channelUID.getId())) {
128             // Change the mode.
129             if (command instanceof OnOffType) {
130                 // Set the mode to be the cmd value.
131                 addUpdateRequest("is_streaming", command == OnOffType.ON);
132             }
133         }
134     }
135
136     private void addUpdateRequest(String field, Object value) {
137         addUpdateRequest(NEST_CAMERA_UPDATE_PATH, field, value);
138     }
139
140     @Override
141     protected void update(@Nullable WWNCamera oldCamera, WWNCamera camera) {
142         logger.debug("Updating {}", getThing().getUID());
143
144         updateLinkedChannels(oldCamera, camera);
145         updateProperty(PROPERTY_FIRMWARE_VERSION, camera.getSoftwareVersion());
146
147         ThingStatus newStatus = camera.isOnline() == null ? ThingStatus.UNKNOWN
148                 : camera.isOnline() ? ThingStatus.ONLINE : ThingStatus.OFFLINE;
149         if (newStatus != thing.getStatus()) {
150             updateStatus(newStatus);
151         }
152     }
153 }