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.channelhelper;
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.netatmo.internal.api.dto.HomeStatusModule;
23 import org.openhab.binding.netatmo.internal.api.dto.NAThing;
24 import org.openhab.core.config.core.Configuration;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
30 * The {@link CameraChannelHelper} handles specific channels of cameras
32 * @author Gaƫl L'hopital - Initial contribution
36 public class CameraChannelHelper extends ChannelHelper {
37 private static final String QUALITY_CONF_ENTRY = "quality";
38 private static final String LIVE_PICTURE = "/live/snapshot_720.jpg";
39 private boolean isLocal;
40 private @Nullable String vpnUrl;
41 private @Nullable String localUrl;
43 public CameraChannelHelper(Set<String> providedGroups) {
44 super(providedGroups);
47 public void setUrls(String vpnUrl, @Nullable String localUrl) {
48 this.localUrl = localUrl;
50 this.isLocal = localUrl != null;
53 public @Nullable String getLocalURL() {
58 protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
59 if (naThing instanceof HomeStatusModule camera) {
60 boolean isMonitoring = OnOffType.ON.equals(camera.getMonitoring());
62 case CHANNEL_MONITORING:
63 return camera.getMonitoring();
65 return toStringType(camera.getSdStatus());
66 case CHANNEL_ALIM_STATUS:
67 return toStringType(camera.getAlimStatus());
68 case CHANNEL_LIVEPICTURE_VPN_URL:
69 return toStringType(getLivePictureURL(false, isMonitoring));
70 case CHANNEL_LIVEPICTURE_LOCAL_URL:
71 return toStringType(getLivePictureURL(true, isMonitoring));
72 case CHANNEL_LIVEPICTURE:
73 return toRawType(getLivePictureURL(isLocal, isMonitoring));
74 case CHANNEL_LIVESTREAM_VPN_URL:
75 return getLiveStreamURL(false, (String) config.get(QUALITY_CONF_ENTRY), isMonitoring);
76 case CHANNEL_LIVESTREAM_LOCAL_URL:
77 return getLiveStreamURL(true, (String) config.get(QUALITY_CONF_ENTRY), isMonitoring);
83 private @Nullable String getLivePictureURL(boolean local, boolean isMonitoring) {
84 String url = local ? localUrl : vpnUrl;
85 if (!isMonitoring || (local && !isLocal) || url == null) {
88 return String.format("%s%s", url, LIVE_PICTURE);
91 private State getLiveStreamURL(boolean local, @Nullable String configQual, boolean isMonitoring) {
92 String url = local ? localUrl : vpnUrl;
93 if (!isMonitoring || (local && !isLocal) || url == null) {
94 return UnDefType.NULL;
96 String finalQual = configQual != null ? configQual : "poor";
97 return toStringType("%s/live/%s", url, local ? String.format("files/%s/index.m3u8", finalQual) : "index.m3u8");