]> git.basschouten.com Git - openhab-addons.git/blob
259cc477933bb701d56c1e7b17a04f8442cd2058
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.meteoblue.internal;
14
15 /**
16  * Model for the meteoblue binding configuration.
17  *
18  * @author Chris Carman - Initial contribution
19  */
20 public class MeteoBlueConfiguration {
21
22     // default values
23     public static final int DEFAULT_REFRESH = 240;
24
25     // constants
26     public static final String SERVICETYPE_COMM = "Commercial";
27     public static final String SERVICETYPE_NONCOMM = "NonCommercial";
28     public static final String COMM_BASE_URL = "http://my.meteoblue.com/dataApi/dispatch.pl?type=json_7day_3h_firstday&";
29     public static final String NONCOMM_BASE_URL = "http://my.meteoblue.com/packages/basic-day?";
30     public static final String URL_MINIMAL_PARAMS = "apikey=#API_KEY#&lat=#LATITUDE#&lon=#LONGITUDE#";
31
32     // required parameters
33     // servicetype - either Commercial or NonCommercial
34     public String serviceType;
35     // location - lat., long., and alt. in a single string
36     public String location;
37
38     // optional parameters
39     // refresh - time period in minutes between pulls
40     public Integer refresh;
41     // latitude - the latitude of this location in degrees (-90 to 90)
42     public Double latitude;
43     // longitude - the longitude of this location in degrees (-180 to 180)
44     public Double longitude;
45     // altitude - the height above sea level of the location, in meters
46     public Double altitude;
47     // timeZone - the timezone of the location (see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
48     public String timeZone;
49
50     // returns the URL for the specified serviceType
51     public static String getURL(String serviceType) {
52         if (SERVICETYPE_COMM.equals(serviceType)) {
53             return COMM_BASE_URL + URL_MINIMAL_PARAMS + "#FORMAT_PARAMS#";
54         } else {
55             return NONCOMM_BASE_URL + URL_MINIMAL_PARAMS + "#FORMAT_PARAMS#";
56         }
57     }
58
59     public void parseLocation() {
60         String[] split = location.split(",");
61         String a1 = split.length > 0 ? split[0] : null;
62         String a2 = split.length > 1 ? split[1] : null;
63         String a3 = split.length > 2 ? split[2] : null;
64
65         if (a1 != null && !a1.isBlank()) {
66             latitude = tryGetDouble(a1);
67         }
68
69         if (a2 != null && !a2.isBlank()) {
70             longitude = tryGetDouble(a2);
71         }
72
73         if (a3 != null && !a3.isBlank()) {
74             altitude = tryGetDouble(a3);
75         }
76     }
77
78     private Double tryGetDouble(String toParse) {
79         try {
80             return Double.parseDouble(toParse);
81         } catch (NumberFormatException e) {
82             return null;
83         }
84     }
85 }