This repository has been archived by the owner on May 7, 2020. It is now read-only.
/
MeteoBlueConfiguration.java
87 lines (75 loc) · 2.98 KB
/
MeteoBlueConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* Copyright (c) 2014,2018 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.smarthome.binding.meteoblue.internal;
import org.apache.commons.lang.StringUtils;
/**
* Model for the meteoblue binding configuration.
*
* @author Chris Carman - Initial contribution
*/
public class MeteoBlueConfiguration {
// default values
public static final int DEFAULT_REFRESH = 240;
// constants
public static final String SERVICETYPE_COMM = "Commercial";
public static final String SERVICETYPE_NONCOMM = "NonCommercial";
public static final String COMM_BASE_URL = "http://my.meteoblue.com/dataApi/dispatch.pl?type=json_7day_3h_firstday&";
public static final String NONCOMM_BASE_URL = "http://my.meteoblue.com/packages/basic-day?";
public static final String URL_MINIMAL_PARAMS = "apikey=#API_KEY#&lat=#LATITUDE#&lon=#LONGITUDE#";
// required parameters
// servicetype - either Commercial or NonCommercial
public String serviceType;
// location - lat., long., and alt. in a single string
public String location;
// optional parameters
// refresh - time period in minutes between pulls
public Integer refresh;
// latitude - the latitude of this location in degrees (-90 to 90)
public Double latitude;
// longitude - the longitude of this location in degrees (-180 to 180)
public Double longitude;
// altitude - the height above sea level of the location, in meters
public Double altitude;
// timeZone - the timezone of the location (see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
public String timeZone;
// returns the URL for the specified serviceType
public static String getURL(String serviceType) {
if (SERVICETYPE_COMM.equals(serviceType)) {
return COMM_BASE_URL + URL_MINIMAL_PARAMS + "#FORMAT_PARAMS#";
} else {
return NONCOMM_BASE_URL + URL_MINIMAL_PARAMS + "#FORMAT_PARAMS#";
}
}
public void parseLocation() {
String[] split = location.split(",");
String a1 = split.length > 0 ? split[0] : null;
String a2 = split.length > 1 ? split[1] : null;
String a3 = split.length > 2 ? split[2] : null;
if (!StringUtils.isBlank(a1)) {
latitude = tryGetDouble(a1);
}
if (!StringUtils.isBlank(a2)) {
longitude = tryGetDouble(a2);
}
if (!StringUtils.isBlank(a3)) {
altitude = tryGetDouble(a3);
}
}
private Double tryGetDouble(String toParse) {
try {
return Double.parseDouble(toParse);
} catch (NumberFormatException e) {
return null;
}
}
}