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