Skip to content
This repository has been archived by the owner on May 17, 2021. It is now read-only.

Commit

Permalink
[Weather] Add two new Provider APIXU and Weatherbit.
Browse files Browse the repository at this point in the history
Resolve merge conflicts from master after rebase.
  • Loading branch information
Joerg Mahmens committed Jan 5, 2019
1 parent 84aa57b commit 686f3d3
Show file tree
Hide file tree
Showing 17 changed files with 624 additions and 334 deletions.
6 changes: 5 additions & 1 deletion bundles/binding/org.openhab.binding.weather/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ The apikey for the different weather providers, at least one must be specified.
| apikey.Hamweather | `client_id` for [Hamweather](http://hamweather.com) |
| apikey2.Hamweather | `client_secret` for [Hamweather](http://hamweather.com) |
| apikey.Meteoblue | API key for [MeteoBlue](https://www.meteoblue.com/) |
| apikey.ApiXU | API key for [ApiXU](https://www.apixu.com/) |
| apikey.Weatherbit | API key for [Weatherbit](https://www.weatherbit.io/) |

### Location Configuration

Expand All @@ -76,7 +78,6 @@ Let's display the current temperature and humidity in Salzburg (AT).
services/weather.cfg

```
location.home.woeid=547826
location.home.provider=ForecastIo
location.home.language=de
location.home.updateInterval=10
Expand Down Expand Up @@ -302,6 +303,9 @@ Each provider sends different forecast days.
- WorldWeatherOnline: 5 days (0-4)
- Wunderground: 10 days (0-9)
- Hamweather: 5 days (0-4)
- Meteoblue: 10 days (0-9)
- ApiXU: 7 days (0-6)
- Weaterbit: 10 days (0-9)

**Note:** If you omit the forecast property, the *current* conditions are shown, if you specify forecast=0, the forecast for *today* is shown.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
*/
package org.openhab.binding.weather.internal.common;

import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import org.openhab.binding.weather.internal.model.ProviderName;
import org.openhab.binding.weather.internal.utils.PropertyResolver;

import org.osgi.service.cm.ConfigurationException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


/**
* Parses the config in openhab.cfg.
*
Expand All @@ -35,17 +39,20 @@
* #weather:apikey.Wunderground=
* #weather:apikey.Hamweather=
* #weather:apikey2.Hamweather=
* #weather:apikey.Meteoblue=
* #weather:apikey.ApiXU=
* #weather:apikey.Weatherbit=
*
* # location configuration, you can specify multiple locations
* #weather:location.<locationId1>.latitude=
* #weather:location.<locationId1>.longitude=
* #weather:location.<locationId1>.latitude=
* #weather:location.<locationId1>.longitude=
* #weather:location.<locationId1>.provider=
* #weather:location.<locationId1>.language=
* #weather:location.<locationId1>.updateInterval= (optional, defaults to 240)
* #weather:location.<locationId1>.units= (optional; defaults to "si")
*
* #weather:location.<locationId2>.latitude=
* #weather:location.<locationId2>.longitude=
* #weather:location.<locationId2>.latitude=
* #weather:location.<locationId2>.longitude=
* #weather:location.<locationId2>.provider=
* #weather:location.<locationId2>.language=
* #weather:location.<locationId2>.updateInterval= (optional, defaults to 240)
Expand All @@ -57,31 +64,34 @@
*/
public class WeatherConfig {
private static final Logger logger = LoggerFactory.getLogger(WeatherConfig.class);

private Map<ProviderName, ProviderConfig> providerConfigs = new HashMap<ProviderName, ProviderConfig>();
private Map<String, LocationConfig> locationConfigs = new HashMap<String, LocationConfig>();

private boolean valid;
private boolean parseCompleted;

/**
* Parses and validates the properties in openhab.cfg.
*/
public void parse(Dictionary<String, ?> properties) throws ConfigurationException {
public void parse(Dictionary<String, ?> properties)
throws ConfigurationException {
parseCompleted = false;
valid = false;

if (properties == null) {
parseCompleted = true;
logger.warn("No configuration found for the weather binding. Check openhab.cfg.");
logger.warn(
"No configuration found for the weather binding. Check openhab.cfg.");
throw new ConfigurationException("weather",
"No configuration found for the weather binding. Check openhab.cfg.");
"No configuration found for the weather binding. Check openhab.cfg.");
}

Enumeration<String> keys = properties.keys();

while (keys.hasMoreElements()) {
String key = keys.nextElement();

String value = StringUtils.trimToNull((String) properties.get(key));

if (StringUtils.startsWithIgnoreCase(key, "apikey")) {
parseApiKey(key, value);
} else if (StringUtils.startsWithIgnoreCase(key, "location")) {
Expand All @@ -93,26 +103,32 @@ public void parse(Dictionary<String, ?> properties) throws ConfigurationExceptio
for (LocationConfig lc : locationConfigs.values()) {
if (!lc.isValid()) {
parseCompleted = true;
logger.warn("Incomplete location config for locationId '{}'. Check openhab.cfg.", lc.getLocationId());
logger.warn("Incomplete location config for locationId '{}'. Check openhab.cfg.",
lc.getLocationId());
throw new ConfigurationException("weather",
"Incomplete location config for locationId '" + lc.getLocationId() + "'. Check openhab.cfg.");
"Incomplete location config for locationId '" +
lc.getLocationId() + "'. Check openhab.cfg.");
}

if (!providerConfigs.containsKey(lc.getProviderName())) {
parseCompleted = true;
logger.warn("No apikey found for provider '{}'. Check openhab.cfg.", lc.getProviderName());
logger.warn("No apikey found for provider '{}'. Check openhab.cfg.",
lc.getProviderName());
throw new ConfigurationException("weather",
"No apikey found for provider '" + lc.getProviderName() + "'. Check openhab.cfg.");
"No apikey found for provider '" + lc.getProviderName() +
"'. Check openhab.cfg.");
}
}

// check all ProviderConfigs
for (ProviderConfig pc : providerConfigs.values()) {
if (!pc.isValid()) {
parseCompleted = true;
logger.warn("Invalid apikey config for provider '{}'. Check openhab.cfg.", pc.getProviderName());
logger.warn("Invalid apikey config for provider '{}'. Check openhab.cfg.",
pc.getProviderName());
throw new ConfigurationException("weather",
"Invalid apikey config for provider '" + pc.getProviderName() + "'. Check openhab.cfg.");
"Invalid apikey config for provider '" +
pc.getProviderName() + "'. Check openhab.cfg.");
}
}

Expand All @@ -124,25 +140,32 @@ public void parse(Dictionary<String, ?> properties) throws ConfigurationExceptio
/**
* Parses the properties for a location config.
*/
private void parseLocation(String key, String value) throws ConfigurationException {
private void parseLocation(String key, String value)
throws ConfigurationException {
if (value == null) {
logger.warn("Weather location setting '{}' has no value. Check openhab.cfg.", key);
logger.warn("Weather location setting '{}' has no value. Check openhab.cfg.",
key);

return;
}

String locationId = StringUtils.substringBetween(key, ".");

if (StringUtils.isBlank(locationId)) {
logger.warn("Weather location setting '{}' is missing its location. Check openhab.cfg.", key);
logger.warn("Weather location setting '{}' is missing its location. Check openhab.cfg.",
key);
}

LocationConfig lc = locationConfigs.get(locationId);

if (lc == null) {
lc = new LocationConfig();
lc.setLocationId(locationId);
locationConfigs.put(locationId, lc);
}

String keyId = PropertyResolver.last(key);

if (StringUtils.equalsIgnoreCase(keyId, "provider")) {
lc.setProviderName(getProviderName(value));
} else if (StringUtils.equalsIgnoreCase(keyId, "updateInterval")) {
Expand All @@ -160,62 +183,77 @@ private void parseLocation(String key, String value) throws ConfigurationExcepti
} else if (StringUtils.equalsIgnoreCase(keyId, "units")) {
lc.setMeasurementUnits(value.toLowerCase());
} else {
logger.debug("Unknown weather configuration setting '{}'. Check openhab.cfg.", key);
logger.debug("Unknown weather configuration setting '{}'. Check openhab.cfg.",
key);
}
}

/**
* Parses the properties for a provider config.
*/
private void parseApiKey(String key, String value) throws ConfigurationException {
private void parseApiKey(String key, String value)
throws ConfigurationException {
if (value == null) {
logger.warn("Weather apikey setting '{}' has no value. Check openhab.cfg.", key);
logger.warn("Weather apikey setting '{}' has no value. Check openhab.cfg.",
key);

return;
}

String provider = PropertyResolver.last(key);
ProviderName providerName = getProviderName(provider);

ProviderConfig pConfig = providerConfigs.get(providerName);

if (pConfig == null) {
pConfig = new ProviderConfig();
pConfig.setProviderName(providerName);
providerConfigs.put(providerName, pConfig);
}

String keyId = PropertyResolver.first(key);

if (StringUtils.equalsIgnoreCase(keyId, "apikey")) {
pConfig.setApiKey(value);
} else if (StringUtils.equalsIgnoreCase(keyId, "apikey2")) {
pConfig.setApiKey2(value);
} else {
logger.warn("Unknown configuration key '{}'. Check openhab.cfg.", key);
logger.warn("Unknown configuration key '{}'. Check openhab.cfg.",
key);
}
}

/**
* Parse a double value from a string.
*/
private Double parseNumber(String key, String value) throws ConfigurationException {
private Double parseNumber(String key, String value)
throws ConfigurationException {
try {
return Double.parseDouble(value);
} catch (Exception ex) {
logger.warn("Parameter '{}' empty or in wrong format ('{}'). Check openhab.cfg.", key, value);
logger.warn("Parameter '{}' empty or in wrong format ('{}'). Check openhab.cfg.",
key, value);
throw new ConfigurationException("weather",
"Parameter '" + key + "' empty or in wrong format ('" + value + "'). Check openhab.cfg.");
"Parameter '" + key + "' empty or in wrong format ('" + value +
"'). Check openhab.cfg.");
}
}

/**
* Parse a ProviderName from a string.
*/
private ProviderName getProviderName(String name) throws ConfigurationException {
private ProviderName getProviderName(String name)
throws ConfigurationException {
ProviderName providerName = ProviderName.parse(name);

if (providerName == null) {
logger.warn("Provider with name '{}' not found. Check openhab.cfg.", name);
logger.warn("Provider with name '{}' not found. Check openhab.cfg.",
name);
throw new ConfigurationException("weather",
"Provider with name '" + name + "' not found. Check openhab.cfg.");
"Provider with name '" + name +
"' not found. Check openhab.cfg.");
}

return providerName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,27 @@
*/
package org.openhab.binding.weather.internal.converter.property;

import java.util.Calendar;

import org.openhab.binding.weather.internal.converter.Converter;
import org.openhab.binding.weather.internal.converter.ConverterType;

import java.util.Calendar;


/**
* Date converter for a unix timestamp.
*
* @author Gerhard Riegler
* @since 1.6.0
*/
public class UnixDateConverter implements Converter<Calendar> {

/**
* {@inheritDoc}
*/
@Override
public Calendar convert(String value) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(Long.valueOf(value) * 1000);
cal.setTimeInMillis((long) (Double.valueOf(value) * 1000));

return cal;
}

Expand All @@ -38,5 +39,4 @@ public Calendar convert(String value) {
public ConverterType getType() {
return ConverterType.UNIX_DATE;
}

}
Loading

0 comments on commit 686f3d3

Please sign in to comment.