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