mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
forecast
This commit is contained in:
parent
ebee80d10e
commit
95adc0aec1
@ -1,20 +1,23 @@
|
||||
{% if forecast %}
|
||||
<div class="normal medium">
|
||||
<table class="small">
|
||||
<table class="{{config.tableClass}}">
|
||||
{% for f in forecast %}
|
||||
<tr class="{% if config.colored %}colored{% endif %}">
|
||||
<td class="day">{{f.day}}</td>
|
||||
<td class="bright weather-icon"><span class="wi weathericon {{f.icon}}"></span></td>
|
||||
<tr {% if config.colored %}class="colored"{% endif %}>
|
||||
<td class="day">{{f.date.format('ddd')}}</td>
|
||||
<td class="bright weather-icon"><span class="wi weathericon wi-{{f.weatherType}}"></span></td>
|
||||
<td class="align-right bright max-temp">
|
||||
{{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>
|
||||
{% endif %}
|
||||
</tr>
|
||||
{% endfor %}
|
||||
</table>
|
||||
</div>
|
||||
{% else %}
|
||||
{{"LOADING" | translate}}
|
||||
{% endif %}
|
||||
|
@ -12,28 +12,22 @@
|
||||
WeatherProvider.register("openweathermap", {
|
||||
|
||||
// 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.
|
||||
providerName: "OpenWeatherMap",
|
||||
|
||||
// Overwrite the fetchCurrentWeather method.
|
||||
fetchCurrentWeather: function() {
|
||||
var apiVersion = "2.5"
|
||||
var apiBase = "http://api.openweathermap.org/data/"
|
||||
var weatherEndpoint = "weather"
|
||||
|
||||
var url = apiBase + apiVersion + "/" + weatherEndpoint + this.getParams()
|
||||
|
||||
this.fetchData(url)
|
||||
this.fetchData(this.getUrl())
|
||||
.then(data => {
|
||||
Log.log(data)
|
||||
|
||||
if (!data || !data.main || typeof data.main.temp === "undefined") {
|
||||
// Did not receive usable new data.
|
||||
// Maybe this needs a better check?
|
||||
return;
|
||||
}
|
||||
|
||||
this.setFetchedLocation(data.name + ', ' + data.sys.country)
|
||||
|
||||
var currentWeather = this.generateWeatherObjectFromCurrentWeather(data)
|
||||
this.setCurrentWeather(currentWeather)
|
||||
})
|
||||
@ -44,29 +38,33 @@ WeatherProvider.register("openweathermap", {
|
||||
|
||||
// Overwrite the fetchCurrentWeather method.
|
||||
fetchWeatherForecast: function() {
|
||||
|
||||
// I haven't yet implemented the real api call, so let's just generate some random data.
|
||||
|
||||
var forecast = []
|
||||
var today = moment()
|
||||
|
||||
for (var i = 0; i < 5; i++ ) {
|
||||
var weatherObject = new WeatherObject()
|
||||
|
||||
weatherObject.date = moment(today).add(i, "days")
|
||||
weatherObject.minTemperature = Math.random() * 10 + 10
|
||||
weatherObject.maxTemperature = Math.random() * 15 + 10
|
||||
|
||||
forecast.push(weatherObject)
|
||||
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;
|
||||
}
|
||||
|
||||
this.setFetchedLocation(data.city.name + ', ' + data.city.country)
|
||||
|
||||
var forecast = this.generateWeatherObjectsFromForecast(data.list)
|
||||
this.setWeatherForecast(forecast)
|
||||
})
|
||||
.catch(function(request) {
|
||||
Log.error("Could not load data ... ", request)
|
||||
})
|
||||
},
|
||||
|
||||
|
||||
|
||||
/** 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
|
||||
@ -74,19 +72,38 @@ WeatherProvider.register("openweathermap", {
|
||||
generateWeatherObjectFromCurrentWeather: function(currentWeatherData) {
|
||||
var currentWeather = new WeatherObject()
|
||||
|
||||
currentWeather.date = new Date
|
||||
|
||||
currentWeather.humidity = currentWeatherData.main.humidity ? parseFloat(currentWeatherData.main.humidity) : null
|
||||
currentWeather.temperature = currentWeatherData.main.temp ? parseFloat(currentWeatherData.main.temp) : null
|
||||
currentWeather.windSpeed = currentWeatherData.wind.speed ? parseFloat(currentWeatherData.wind.speed) : null
|
||||
currentWeather.windDirection = currentWeatherData.wind.deg ? currentWeatherData.wind.deg : null
|
||||
currentWeather.weatherType = currentWeatherData.weather[0].icon ? this.convertWeatherType(currentWeatherData.weather[0].icon) : null
|
||||
currentWeather.sunrise = currentWeatherData.sys.sunrise ? new Date(currentWeatherData.sys.sunrise * 1000) : null
|
||||
currentWeather.sunset = currentWeatherData.sys.sunset ? new Date(currentWeatherData.sys.sunset * 1000) : null
|
||||
currentWeather.humidity = currentWeatherData.main.humidity
|
||||
currentWeather.temperature = currentWeatherData.main.temp
|
||||
currentWeather.windSpeed = currentWeatherData.wind.speed
|
||||
currentWeather.windDirection = currentWeatherData.wind.deg
|
||||
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.weather[0].icon)
|
||||
currentWeather.sunrise = moment(currentWeatherData.sys.sunrise, "X")
|
||||
currentWeather.sunset = moment(currentWeatherData.sys.sunset, "X")
|
||||
|
||||
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.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user