2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.meteoblue.internal;
15 import org.apache.commons.lang.StringUtils;
18 * Model for the meteoblue binding configuration.
20 * @author Chris Carman - Initial contribution
22 public class MeteoBlueConfiguration {
25 public static final int DEFAULT_REFRESH = 240;
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#";
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;
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;
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#";
57 return NONCOMM_BASE_URL + URL_MINIMAL_PARAMS + "#FORMAT_PARAMS#";
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;
67 if (!StringUtils.isBlank(a1)) {
68 latitude = tryGetDouble(a1);
71 if (!StringUtils.isBlank(a2)) {
72 longitude = tryGetDouble(a2);
75 if (!StringUtils.isBlank(a3)) {
76 altitude = tryGetDouble(a3);
80 private Double tryGetDouble(String toParse) {
82 return Double.parseDouble(toParse);
83 } catch (NumberFormatException e) {