This commit is contained in:
fewieden 2018-12-27 17:14:03 +01:00
parent ebee80d10e
commit 95adc0aec1
2 changed files with 70 additions and 50 deletions

View File

@ -1,20 +1,23 @@
{% if forecast %} {% if forecast %}
<div class="normal medium"> <table class="{{config.tableClass}}">
<table class="small"> {% for f in forecast %}
{% for f in forecast %} <tr {% if config.colored %}class="colored"{% endif %}>
<tr class="{% if config.colored %}colored{% endif %}"> <td class="day">{{f.date.format('ddd')}}</td>
<td class="day">{{f.day}}</td> <td class="bright weather-icon"><span class="wi weathericon wi-{{f.weatherType}}"></span></td>
<td class="bright weather-icon"><span class="wi weathericon {{f.icon}}"></span></td> <td class="align-right bright max-temp">
<td class="align-right bright max-temp"> {{f.maxTemperature | roundValue | unit("temperature")}}
{{f.maxTemperature | roundValue | unit("temperature")}} </td>
<td class="align-right min-temp">
{{f.minTemperature | roundValue | unit("temperature")}}
</td>
{% if config.showRainAmount %}
<td class="align-right bright rain">
{{f.rain | formatRain}}
</td> </td>
<td class="align-right min-temp"> {% endif %}
{{f.minTemperature | roundValue | unit("temperature")}} </tr>
</td> {% endfor %}
</tr> </table>
{% endfor %}
</table>
</div>
{% else %} {% else %}
{{"LOADING" | translate}} {{"LOADING" | translate}}
{% endif %} {% endif %}

View File

@ -12,28 +12,22 @@
WeatherProvider.register("openweathermap", { WeatherProvider.register("openweathermap", {
// Set the name of the provider. // Set the name of the provider.
// This isn't strictly nessecery, since it will fallback to the provider identifier // This isn't strictly necessary, since it will fallback to the provider identifier
// But for debugging (and future alerts) it would be nice to have the real name. // But for debugging (and future alerts) it would be nice to have the real name.
providerName: "OpenWeatherMap", providerName: "OpenWeatherMap",
// Overwrite the fetchCurrentWeather method. // Overwrite the fetchCurrentWeather method.
fetchCurrentWeather: function() { fetchCurrentWeather: function() {
var apiVersion = "2.5" this.fetchData(this.getUrl())
var apiBase = "http://api.openweathermap.org/data/"
var weatherEndpoint = "weather"
var url = apiBase + apiVersion + "/" + weatherEndpoint + this.getParams()
this.fetchData(url)
.then(data => { .then(data => {
Log.log(data)
if (!data || !data.main || typeof data.main.temp === "undefined") { if (!data || !data.main || typeof data.main.temp === "undefined") {
// Did not receive usable new data. // Did not receive usable new data.
// Maybe this needs a better check? // Maybe this needs a better check?
return; return;
} }
this.setFetchedLocation(data.name + ', ' + data.sys.country)
var currentWeather = this.generateWeatherObjectFromCurrentWeather(data) var currentWeather = this.generateWeatherObjectFromCurrentWeather(data)
this.setCurrentWeather(currentWeather) this.setCurrentWeather(currentWeather)
}) })
@ -44,29 +38,33 @@ WeatherProvider.register("openweathermap", {
// Overwrite the fetchCurrentWeather method. // Overwrite the fetchCurrentWeather method.
fetchWeatherForecast: function() { fetchWeatherForecast: function() {
this.fetchData(this.getUrl())
.then(data => {
if (!data || !data.list || !data.list.length) {
// Did not receive usable new data.
// Maybe this needs a better check?
return;
}
// I haven't yet implemented the real api call, so let's just generate some random data. this.setFetchedLocation(data.city.name + ', ' + data.city.country)
var forecast = [] var forecast = this.generateWeatherObjectsFromForecast(data.list)
var today = moment() this.setWeatherForecast(forecast)
})
for (var i = 0; i < 5; i++ ) { .catch(function(request) {
var weatherObject = new WeatherObject() Log.error("Could not load data ... ", request)
})
weatherObject.date = moment(today).add(i, "days")
weatherObject.minTemperature = Math.random() * 10 + 10
weatherObject.maxTemperature = Math.random() * 15 + 10
forecast.push(weatherObject)
}
this.setWeatherForecast(forecast)
}, },
/** OpenWeatherMap Specific Methods - These are not part of the default provider methods */ /** OpenWeatherMap Specific Methods - These are not part of the default provider methods */
/*
* Gets the complete url for the request
*/
getUrl: function() {
return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams()
},
/* /*
* Generate a WeatherObject based on currentWeatherInformation * Generate a WeatherObject based on currentWeatherInformation
@ -74,19 +72,38 @@ WeatherProvider.register("openweathermap", {
generateWeatherObjectFromCurrentWeather: function(currentWeatherData) { generateWeatherObjectFromCurrentWeather: function(currentWeatherData) {
var currentWeather = new WeatherObject() var currentWeather = new WeatherObject()
currentWeather.date = new Date currentWeather.humidity = currentWeatherData.main.humidity
currentWeather.temperature = currentWeatherData.main.temp
currentWeather.humidity = currentWeatherData.main.humidity ? parseFloat(currentWeatherData.main.humidity) : null currentWeather.windSpeed = currentWeatherData.wind.speed
currentWeather.temperature = currentWeatherData.main.temp ? parseFloat(currentWeatherData.main.temp) : null currentWeather.windDirection = currentWeatherData.wind.deg
currentWeather.windSpeed = currentWeatherData.wind.speed ? parseFloat(currentWeatherData.wind.speed) : null currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon)
currentWeather.windDirection = currentWeatherData.wind.deg ? currentWeatherData.wind.deg : null currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X")
currentWeather.weatherType = currentWeatherData.weather[0].icon ? this.convertWeatherType(currentWeatherData.weather[0].icon) : null currentWeather.sunset = moment(currentWeatherData.sys.sunset, "X")
currentWeather.sunrise = currentWeatherData.sys.sunrise ? new Date(currentWeatherData.sys.sunrise * 1000) : null
currentWeather.sunset = currentWeatherData.sys.sunset ? new Date(currentWeatherData.sys.sunset * 1000) : null
return currentWeather return currentWeather
}, },
/*
* Generate WeatherObjects based on forecast information
*/
generateWeatherObjectsFromForecast: function(forecasts) {
var days = []
for (var forecast of forecasts) {
var weather = new WeatherObject()
weather.date = moment(forecast.dt, "X")
weather.minTemperature = forecast.temp.min
weather.maxTemperature = forecast.temp.max
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon)
weather.rain = forecast.rain
days.push(weather)
}
return days
},
/* /*
* Convert the OpenWeatherMap icons to a more usable name. * Convert the OpenWeatherMap icons to a more usable name.
*/ */