2 * Copyright (c) 2010-2023 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.*;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.AlimentationStatus;
25 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.SdCardStatus;
26 import org.openhab.binding.netatmo.internal.api.dto.HomeDataPerson;
27 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
28 import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule;
29 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
30 import org.openhab.binding.netatmo.internal.api.dto.WebhookEvent;
31 import org.openhab.binding.netatmo.internal.deserialization.NAObjectMap;
32 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
33 import org.openhab.binding.netatmo.internal.handler.channelhelper.CameraChannelHelper;
34 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
35 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.thing.ChannelUID;
38 import org.openhab.core.thing.ThingUID;
39 import org.openhab.core.types.Command;
40 import org.openhab.core.types.StateOption;
41 import org.openhab.core.types.UnDefType;
44 * {@link CameraCapability} give to handle Welcome Camera specifics
46 * @author Gaƫl L'hopital - Initial contribution
50 public class CameraCapability extends HomeSecurityThingCapability {
51 private final CameraChannelHelper cameraHelper;
52 private final ChannelUID personChannelUID;
54 protected @Nullable String localUrl;
55 protected @Nullable String vpnUrl;
57 public CameraCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider,
58 List<ChannelHelper> channelHelpers) {
59 super(handler, descriptionProvider, channelHelpers);
60 this.personChannelUID = new ChannelUID(thing.getUID(), GROUP_LAST_EVENT, CHANNEL_EVENT_PERSON_ID);
61 this.cameraHelper = (CameraChannelHelper) channelHelpers.stream().filter(c -> c instanceof CameraChannelHelper)
62 .findFirst().orElseThrow(() -> new IllegalArgumentException(
63 "CameraCapability must find a CameraChannelHelper, please file a bug report."));
67 public void updateHomeStatusModule(HomeStatusModule newData) {
68 super.updateHomeStatusModule(newData);
69 // Per documentation vpn_url expires every 3 hours and on camera reboot. So useless to reping it if not changed
70 String newVpnUrl = newData.getVpnUrl();
71 if (newVpnUrl != null && !newVpnUrl.equals(vpnUrl)) {
72 // This will also decrease the number of requests emitted toward Netatmo API.
73 localUrl = newData.isLocal() ? getSecurityCapability().map(cap -> cap.ping(newVpnUrl)).orElse(null) : null;
74 cameraHelper.setUrls(newVpnUrl, localUrl);
75 eventHelper.setUrls(newVpnUrl, localUrl);
78 if (!SdCardStatus.SD_CARD_WORKING.equals(newData.getSdStatus())
79 || !AlimentationStatus.ALIM_CORRECT_POWER.equals(newData.getAlimStatus())) {
80 statusReason = String.format("%s, %s", newData.getSdStatus(), newData.getAlimStatus());
85 protected void updateWebhookEvent(WebhookEvent event) {
86 super.updateWebhookEvent(event);
88 final ThingUID thingUid = handler.getThing().getUID();
89 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_TYPE),
90 toStringType(event.getEventType()));
91 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_TIME),
92 toDateTimeType(event.getTime()));
93 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_SNAPSHOT),
94 toRawType(event.getSnapshotUrl()));
95 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_SNAPSHOT_URL),
96 toStringType(event.getSnapshotUrl()));
97 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_VIGNETTE),
98 toRawType(event.getVignetteUrl()));
99 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_VIGNETTE_URL),
100 toStringType(event.getVignetteUrl()));
102 final String message = event.getName();
103 handler.updateState(new ChannelUID(thingUid, GROUP_SUB_EVENT, CHANNEL_EVENT_MESSAGE),
104 message == null || message.isBlank() ? UnDefType.NULL : toStringType(message));
106 // The channel should get triggered at last (after super and sub methods), because this allows rules to access
107 // the new updated data from the other channels.
108 final String eventType = event.getEventType().name();
109 handler.recurseUpToHomeHandler(handler)
110 .ifPresent(homeHandler -> homeHandler.triggerChannel(CHANNEL_HOME_EVENT, eventType));
111 handler.triggerChannel(CHANNEL_HOME_EVENT, eventType);
115 public void handleCommand(String channelName, Command command) {
116 if (command instanceof OnOffType && CHANNEL_MONITORING.equals(channelName)) {
117 getSecurityCapability().ifPresent(cap -> cap.changeStatus(localUrl, OnOffType.ON.equals(command)));
119 super.handleCommand(channelName, command);
124 protected void beforeNewData() {
125 super.beforeNewData();
126 getSecurityCapability().ifPresent(cap -> {
127 NAObjectMap<HomeDataPerson> persons = cap.getPersons();
128 descriptionProvider.setStateOptions(personChannelUID, persons.values().stream()
129 .map(p -> new StateOption(p.getId(), p.getName())).collect(Collectors.toList()));
134 public List<NAObject> updateReadings() {
135 List<NAObject> result = new ArrayList<>();
136 getSecurityCapability().ifPresent(cap -> {
137 HomeEvent event = cap.getDeviceLastEvent(handler.getId(), moduleType.apiName);
140 result.addAll(event.getSubevents());