2 * Copyright (c) 2010-2024 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.netatmo.internal.handler.capability;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.Objects;
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.core.UriBuilderException;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.AlimentationStatus;
29 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.SdCardStatus;
30 import org.openhab.binding.netatmo.internal.api.dto.HomeDataPerson;
31 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
32 import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule;
33 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
34 import org.openhab.binding.netatmo.internal.api.dto.WebhookEvent;
35 import org.openhab.binding.netatmo.internal.deserialization.NAObjectMap;
36 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
37 import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper;
38 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
39 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
40 import org.openhab.core.config.core.Configuration;
41 import org.openhab.core.library.types.OnOffType;
42 import org.openhab.core.thing.ChannelUID;
43 import org.openhab.core.types.Command;
44 import org.openhab.core.types.RefreshType;
45 import org.openhab.core.types.State;
46 import org.openhab.core.types.StateOption;
47 import org.openhab.core.types.UnDefType;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
52 * {@link CameraCapability} give to handle Welcome Camera specifics
54 * @author Gaƫl L'hopital - Initial contribution
58 public class CameraCapability extends HomeSecurityThingCapability {
59 private static final String IP_ADDRESS = "ipAddress";
61 private final Logger logger = LoggerFactory.getLogger(CameraCapability.class);
63 private final CameraChannelHelper cameraHelper;
64 private final ChannelUID personChannelUID;
66 protected @Nullable String localUrl;
67 protected @Nullable String vpnUrl;
68 private boolean hasSubEventGroup;
69 private boolean hasLastEventGroup;
71 public CameraCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider,
72 List<ChannelHelper> channelHelpers) {
73 super(handler, descriptionProvider, channelHelpers);
74 this.personChannelUID = new ChannelUID(thingUID, GROUP_LAST_EVENT, CHANNEL_EVENT_PERSON_ID);
75 this.cameraHelper = (CameraChannelHelper) channelHelpers.stream().filter(c -> c instanceof CameraChannelHelper)
76 .findFirst().orElseThrow(() -> new IllegalArgumentException(
77 "CameraCapability must find a CameraChannelHelper, please file a bug report."));
81 public void initialize() {
82 hasSubEventGroup = !thing.getChannelsOfGroup(GROUP_SUB_EVENT).isEmpty();
83 hasLastEventGroup = !thing.getChannelsOfGroup(GROUP_LAST_EVENT).isEmpty();
87 public void updateHomeStatusModule(HomeStatusModule newData) {
88 super.updateHomeStatusModule(newData);
89 // Per documentation vpn_url expires every 3 hours and on camera reboot. So useless to reping it if not changed
90 String newVpnUrl = newData.getVpnUrl();
91 if (newVpnUrl != null && !newVpnUrl.equals(vpnUrl)) {
92 // This will also decrease the number of requests emitted toward Netatmo API.
93 localUrl = newData.isLocal() ? ping(newVpnUrl) : null;
94 logger.debug("localUrl set to {} for camera {}", localUrl, thingUID);
95 cameraHelper.setUrls(newVpnUrl, localUrl);
96 eventHelper.setUrls(newVpnUrl, localUrl);
99 if (!SdCardStatus.SD_CARD_WORKING.equals(newData.getSdStatus())
100 || !AlimentationStatus.ALIM_CORRECT_POWER.equals(newData.getAlimStatus())) {
101 statusReason = "%s, %s".formatted(newData.getSdStatus(), newData.getAlimStatus());
106 protected void updateWebhookEvent(WebhookEvent event) {
107 super.updateWebhookEvent(event);
109 if (hasSubEventGroup) {
110 updateSubGroup(event, GROUP_SUB_EVENT);
113 if (hasLastEventGroup) {
114 updateSubGroup(event, GROUP_LAST_EVENT);
117 // The channel should get triggered at last (after super and sub methods), because this allows rules to access
118 // the new updated data from the other channels.
119 final String eventType = event.getEventType().name();
120 handler.recurseUpToHomeHandler(handler)
121 .ifPresent(homeHandler -> homeHandler.triggerChannel(CHANNEL_HOME_EVENT, eventType));
122 handler.triggerChannel(CHANNEL_HOME_EVENT, eventType);
125 private void updateSubGroup(WebhookEvent event, String group) {
126 handler.updateState(group, CHANNEL_EVENT_TYPE, toStringType(event.getEventType()));
127 handler.updateState(group, CHANNEL_EVENT_TIME, toDateTimeType(event.getTime()));
128 handler.updateState(group, CHANNEL_EVENT_SNAPSHOT, toRawType(event.getSnapshotUrl()));
129 handler.updateState(group, CHANNEL_EVENT_SNAPSHOT_URL, toStringType(event.getSnapshotUrl()));
130 handler.updateState(group, CHANNEL_EVENT_VIGNETTE, toRawType(event.getVignetteUrl()));
131 handler.updateState(group, CHANNEL_EVENT_VIGNETTE_URL, toStringType(event.getVignetteUrl()));
132 handler.updateState(group, CHANNEL_EVENT_SUBTYPE,
133 Objects.requireNonNull(event.getSubTypeDescription().map(d -> toStringType(d)).orElse(UnDefType.NULL)));
134 final String message = event.getName();
135 handler.updateState(group, CHANNEL_EVENT_MESSAGE,
136 message == null || message.isBlank() ? UnDefType.NULL : toStringType(message));
137 State personId = event.getPersons().isEmpty() ? UnDefType.NULL
138 : toStringType(event.getPersons().values().iterator().next().getId());
139 handler.updateState(personChannelUID, personId);
143 public void handleCommand(String channelName, Command command) {
144 if (command instanceof OnOffType && CHANNEL_MONITORING.equals(channelName)) {
145 getSecurityCapability().ifPresent(cap -> cap.changeStatus(localUrl, OnOffType.ON.equals(command)));
146 } else if (command instanceof RefreshType && CHANNEL_LIVEPICTURE.equals(channelName)) {
147 handler.updateState(GROUP_CAM_LIVE, CHANNEL_LIVEPICTURE,
148 toRawType(cameraHelper.getLivePictureURL(localUrl != null, true)));
150 super.handleCommand(channelName, command);
155 protected void beforeNewData() {
156 super.beforeNewData();
157 getSecurityCapability().ifPresent(cap -> {
158 NAObjectMap<HomeDataPerson> persons = cap.getPersons();
159 descriptionProvider.setStateOptions(personChannelUID,
160 persons.values().stream().map(p -> new StateOption(p.getId(), p.getName())).toList());
165 public List<NAObject> updateReadings() {
166 List<NAObject> result = new ArrayList<>();
167 getSecurityCapability().ifPresent(cap -> {
168 HomeEvent event = cap.getDeviceLastEvent(handler.getId(), moduleType.apiName);
171 result.addAll(event.getSubEvents());
177 public @Nullable String ping(String vpnUrl) {
178 return getSecurityCapability().map(cap -> {
179 UriBuilder builder = UriBuilder.fromPath(cap.ping(vpnUrl));
180 URI apiLocalUrl = null;
182 apiLocalUrl = builder.build();
183 if (apiLocalUrl.getHost().startsWith("169.254.")) {
184 logger.warn("Suspicious local IP address received: {}", apiLocalUrl);
185 Configuration config = handler.getThing().getConfiguration();
186 if (config.containsKey(IP_ADDRESS)) {
187 String provided = (String) config.get(IP_ADDRESS);
188 apiLocalUrl = builder.host(provided).build();
189 logger.info("Using {} as local url for '{}'", apiLocalUrl, thingUID);
191 logger.debug("No alternative ip Address provided, keeping API answer");
194 } catch (UriBuilderException e) { // Crashed at first URI build
195 logger.warn("API returned a badly formatted local url address for '{}': {}", thingUID, e.getMessage());
196 } catch (IllegalArgumentException e) {
197 logger.warn("Invalid fallback address provided in configuration for '{}' keeping API answer: {}",
198 thingUID, e.getMessage());
200 return apiLocalUrl != null ? apiLocalUrl.toString() : null;