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 super(GROUP_CAM_STATUS, GROUP_CAM_LIVE);
45 public void setUrls(String vpnUrl, @Nullable String localUrl) {
46 this.localUrl = localUrl;
48 this.isLocal = localUrl != null;
51 public @Nullable String getLocalURL() {
56 protected @Nullable State internalGetProperty(String channelId, NAThing naThing, Configuration config) {
57 if (naThing instanceof HomeStatusModule) {
58 HomeStatusModule camera = (HomeStatusModule) naThing;
59 boolean isMonitoring = OnOffType.ON.equals(camera.getMonitoring());
61 case CHANNEL_MONITORING:
62 return camera.getMonitoring();
64 return toStringType(camera.getSdStatus());
65 case CHANNEL_ALIM_STATUS:
66 return toStringType(camera.getAlimStatus());
67 case CHANNEL_LIVEPICTURE_VPN_URL:
68 return toStringType(getLivePictureURL(false, isMonitoring));
69 case CHANNEL_LIVEPICTURE_LOCAL_URL:
70 return toStringType(getLivePictureURL(true, isMonitoring));
71 case CHANNEL_LIVEPICTURE:
72 return toRawType(getLivePictureURL(isLocal, isMonitoring));
73 case CHANNEL_LIVESTREAM_VPN_URL:
74 return getLiveStreamURL(false, (String) config.get(QUALITY_CONF_ENTRY), isMonitoring);
75 case CHANNEL_LIVESTREAM_LOCAL_URL:
76 return getLiveStreamURL(true, (String) config.get(QUALITY_CONF_ENTRY), isMonitoring);
82 private @Nullable String getLivePictureURL(boolean local, boolean isMonitoring) {
83 String url = local ? localUrl : vpnUrl;
84 if (!isMonitoring || (local && !isLocal) || url == null) {
87 return String.format("%s%s", url, LIVE_PICTURE);
90 private State getLiveStreamURL(boolean local, @Nullable String configQual, boolean isMonitoring) {
91 String url = local ? localUrl : vpnUrl;
92 if (!isMonitoring || (local && !isLocal) || url == null) {
93 return UnDefType.NULL;
95 String finalQual = configQual != null ? configQual : "poor";
96 return toStringType("%s/live/%s", url, local ? String.format("files/%s/index.m3u8", finalQual) : "index.m3u8");