265 lines
7.6 KiB
JavaScript
Raw Normal View History

/* global WeatherProvider, WeatherObject */
/* Magic Mirror
* Module: Weather
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
2019-01-05 17:16:19 +01:00
*
* This class is the blueprint for a weather provider.
*/
WeatherProvider.register("openweathermap", {
// Set the name of the provider.
2018-12-27 17:14:03 +01:00
// 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() {
2018-12-27 17:14:03 +01:00
this.fetchData(this.getUrl())
.then(data => {
if (!data || !data.main || typeof data.main.temp === "undefined") {
// Did not receive usable new data.
// Maybe this needs a better check?
return;
}
2018-12-27 19:37:02 +01:00
this.setFetchedLocation(`${data.name}, ${data.sys.country}`);
2018-12-27 17:14:03 +01:00
const currentWeather = this.generateWeatherObjectFromCurrentWeather(data);
2018-12-27 19:37:02 +01:00
this.setCurrentWeather(currentWeather);
})
.catch(function(request) {
2018-12-27 19:37:02 +01:00
Log.error("Could not load data ... ", request);
})
},
2017-09-22 13:26:44 +02:00
// Overwrite the fetchCurrentWeather method.
fetchWeatherForecast() {
2018-12-27 17:14:03 +01:00
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;
}
2017-09-22 13:26:44 +02:00
2018-12-27 19:37:02 +01:00
this.setFetchedLocation(`${data.city.name}, ${data.city.country}`);
2017-09-22 13:26:44 +02:00
const forecast = this.generateWeatherObjectsFromForecast(data.list);
2018-12-27 19:37:02 +01:00
this.setWeatherForecast(forecast);
2018-12-27 17:14:03 +01:00
})
.catch(function(request) {
2018-12-27 19:37:02 +01:00
Log.error("Could not load data ... ", request);
2018-12-27 17:14:03 +01:00
})
2017-09-22 13:26:44 +02:00
},
/** OpenWeatherMap Specific Methods - These are not part of the default provider methods */
2018-12-27 17:14:03 +01:00
/*
* Gets the complete url for the request
*/
getUrl() {
2018-12-27 19:37:02 +01:00
return this.config.apiBase + this.config.apiVersion + this.config.weatherEndpoint + this.getParams();
2018-12-27 17:14:03 +01:00
},
2019-01-05 17:16:19 +01:00
/*
* Generate a WeatherObject based on currentWeatherInformation
*/
generateWeatherObjectFromCurrentWeather(currentWeatherData) {
const currentWeather = new WeatherObject(this.config.units);
2018-12-27 19:37:02 +01:00
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");
2018-12-27 19:37:02 +01:00
return currentWeather;
},
2018-12-27 17:14:03 +01:00
/*
* Generate WeatherObjects based on forecast information
*/
generateWeatherObjectsFromForecast(forecasts) {
2019-01-05 17:16:19 +01:00
if (this.config.weatherEndpoint == "/forecast") {
return this.fetchForecastHourly(forecasts);
} else if (this.config.weatherEndpoint == "/forecast/daily") {
return this.fetchForecastDaily(forecasts);
}
// if weatherEndpoint does not match forecast or forecast/daily, what should be returned?
const days = [new WeatherObject(this.config.units)];
return days;
},
2019-01-05 17:16:19 +01:00
/*
* fetch forecast information for 3-hourly forecast (available for free subscription).
*/
fetchForecastHourly(forecasts) {
2019-01-04 12:13:39 +01:00
// initial variable declaration
const days = [];
2019-01-04 12:13:39 +01:00
// variables for temperature range and rain
var minTemp = [];
var maxTemp = [];
var rain = 0;
2019-01-04 12:13:39 +01:00
// variable for date
let date = "";
var weather = new WeatherObject(this.config.units);
2019-01-05 17:16:19 +01:00
for (const forecast of forecasts) {
2019-01-05 17:16:19 +01:00
if (date === moment(forecast.dt, "X").format("YYYY-MM-DD")) {
2019-01-04 12:13:39 +01:00
// the same day as before
// add values from forecast to corresponding variables
minTemp.push(forecast.main.temp_min);
maxTemp.push(forecast.main.temp_max);
2019-01-05 17:16:19 +01:00
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) {
rain += forecast.rain["3h"] / 25.4;
} else if (!isNaN(forecast.rain["3h"])){
rain += forecast.rain["3h"];
} else {
rain += 0;
}
} else {
rain += 0;
}
} else {
2019-01-04 12:13:39 +01:00
// a new day
// calculate minimum/maximum temperature, specify rain amount
weather.minTemperature = Math.min.apply(null, minTemp);
weather.maxTemperature = Math.max.apply(null, maxTemp);
weather.rain = rain;
2019-01-04 12:13:39 +01:00
// push weather information to days array
days.push(weather);
2019-01-04 12:13:39 +01:00
// create new weather-object
weather = new WeatherObject(this.config.units);
2019-01-05 17:16:19 +01:00
minTemp = [];
maxTemp = [];
rain = 0;
2019-01-05 17:16:19 +01:00
2019-01-04 12:13:39 +01:00
// set new date
date = moment(forecast.dt, "X").format("YYYY-MM-DD");
2019-01-05 17:16:19 +01:00
2019-01-04 12:13:39 +01:00
// specify date
weather.date = moment(forecast.dt, "X");
2019-01-05 17:16:19 +01:00
2019-01-04 12:13:39 +01:00
// select weather type by first forecast value of a day, is this reasonable?
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
2019-01-05 17:16:19 +01:00
2019-01-04 12:13:39 +01:00
// add values from first forecast of this day to corresponding variables
minTemp.push(forecast.main.temp_min);
maxTemp.push(forecast.main.temp_max);
2019-01-05 17:16:19 +01:00
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain["3h"])) {
rain += forecast.rain["3h"] / 25.4;
} else if (!isNaN(forecast.rain["3h"])){
rain += forecast.rain["3h"];
} else {
rain += 0;
}
} else {
rain += 0;
}
}
2018-12-27 17:14:03 +01:00
}
return days.slice(1);
2018-12-27 17:14:03 +01:00
},
2019-01-05 17:16:19 +01:00
/*
* fetch forecast information for daily forecast (available for paid subscription or old apiKey).
*/
fetchForecastDaily(forecasts) {
// initial variable declaration
const days = [];
2018-12-27 17:14:03 +01:00
for (const forecast of forecasts) {
const weather = new WeatherObject(this.config.units);
weather.date = moment(forecast.dt, "X");
weather.minTemperature = forecast.temp.min;
weather.maxTemperature = forecast.temp.max;
weather.weatherType = this.convertWeatherType(forecast.weather[0].icon);
2019-01-05 17:16:19 +01:00
// forecast.rain not available if amount is zero
if (forecast.hasOwnProperty("rain")) {
if (this.config.units === "imperial" && !isNaN(forecast.rain)) {
weather.rain = forecast.rain / 25.4;
2019-01-05 16:56:47 +01:00
} else if (!isNaN(forecast.rain)){
weather.rain = forecast.rain;
} else {
weather.rain = 0;
}
} else {
weather.rain = 0;
}
days.push(weather);
}
return days;
},
2019-01-05 17:16:19 +01:00
/*
* Convert the OpenWeatherMap icons to a more usable name.
*/
convertWeatherType(weatherType) {
const weatherTypes = {
"01d": "day-sunny",
"02d": "day-cloudy",
"03d": "cloudy",
"04d": "cloudy-windy",
"09d": "showers",
"10d": "rain",
"11d": "thunderstorm",
"13d": "snow",
"50d": "fog",
"01n": "night-clear",
"02n": "night-cloudy",
"03n": "night-cloudy",
"04n": "night-cloudy",
"09n": "night-showers",
"10n": "night-rain",
"11n": "night-thunderstorm",
"13n": "night-snow",
"50n": "night-alt-cloudy-windy"
2018-12-27 19:37:02 +01:00
};
2018-12-27 19:37:02 +01:00
return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
},
/* getParams(compliments)
* Generates an url with api parameters based on the config.
*
* return String - URL params.
*/
getParams() {
let params = "?";
if(this.config.locationID) {
params += "id=" + this.config.locationID;
} else if(this.config.location) {
params += "q=" + this.config.location;
} else if (this.firstEvent && this.firstEvent.geo) {
2018-12-27 19:37:02 +01:00
params += "lat=" + this.firstEvent.geo.lat + "&lon=" + this.firstEvent.geo.lon;
} else if (this.firstEvent && this.firstEvent.location) {
params += "q=" + this.firstEvent.location;
} else {
this.hide(this.config.animationSpeed, {lockString:this.identifier});
return;
}
params += "&units=" + this.config.units;
params += "&lang=" + this.config.lang;
params += "&APPID=" + this.config.apiKey;
return params;
2018-12-27 19:37:02 +01:00
}
});