]> git.basschouten.com Git - openhab-addons.git/blob
5d207abbd2bea65d0646b9de51c4877627a6daad
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.handler.channelhelper;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
17
18 import java.util.Set;
19
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;
28
29 /**
30  * The {@link CameraChannelHelper} handles specific channels of cameras
31  *
32  * @author GaĆ«l L'hopital - Initial contribution
33  *
34  */
35 @NonNullByDefault
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;
42
43     public CameraChannelHelper(Set<String> providedGroups) {
44         super(providedGroups);
45     }
46
47     public void setUrls(String vpnUrl, @Nullable String localUrl) {
48         this.localUrl = localUrl;
49         this.vpnUrl = vpnUrl;
50         this.isLocal = localUrl != null;
51     }
52
53     public @Nullable String getLocalURL() {
54         return localUrl;
55     }
56
57     @Override
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());
61             switch (channelId) {
62                 case CHANNEL_MONITORING:
63                     return camera.getMonitoring();
64                 case CHANNEL_SD_CARD:
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);
78             }
79         }
80         return null;
81     }
82
83     private @Nullable String getLivePictureURL(boolean local, boolean isMonitoring) {
84         String url = local ? localUrl : vpnUrl;
85         if (!isMonitoring || (local && !isLocal) || url == null) {
86             return null;
87         }
88         return String.format("%s%s", url, LIVE_PICTURE);
89     }
90
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;
95         }
96         String finalQual = configQual != null ? configQual : "poor";
97         return toStringType("%s/live/%s", url, local ? String.format("files/%s/index.m3u8", finalQual) : "index.m3u8");
98     }
99 }