]> git.basschouten.com Git - openhab-addons.git/blob
9b63f121853012cb96eaa410c7dff77fbfb93908
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 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;
26
27 /**
28  * The {@link CameraChannelHelper} handles specific channels of cameras
29  *
30  * @author GaĆ«l L'hopital - Initial contribution
31  *
32  */
33 @NonNullByDefault
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;
40
41     public CameraChannelHelper() {
42         this(GROUP_CAM_STATUS, GROUP_CAM_LIVE);
43     }
44
45     protected CameraChannelHelper(String... providedGroups) {
46         super(providedGroups);
47     }
48
49     public void setUrls(String vpnUrl, @Nullable String localUrl) {
50         this.localUrl = localUrl;
51         this.vpnUrl = vpnUrl;
52         this.isLocal = localUrl != null;
53     }
54
55     public @Nullable String getLocalURL() {
56         return localUrl;
57     }
58
59     @Override
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());
64             switch (channelId) {
65                 case CHANNEL_MONITORING:
66                     return camera.getMonitoring();
67                 case CHANNEL_SD_CARD:
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);
81             }
82         }
83         return null;
84     }
85
86     private @Nullable String getLivePictureURL(boolean local, boolean isMonitoring) {
87         String url = local ? localUrl : vpnUrl;
88         if (!isMonitoring || (local && !isLocal) || url == null) {
89             return null;
90         }
91         return String.format("%s%s", url, LIVE_PICTURE);
92     }
93
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;
98         }
99         String finalQual = configQual != null ? configQual : "poor";
100         return toStringType("%s/live/%s", url, local ? String.format("files/%s/index.m3u8", finalQual) : "index.m3u8");
101     }
102 }