2017-09-21 20:06:42 -04:00
|
|
|
/* global WeatherProvider, WeatherDay */
|
|
|
|
|
|
|
|
/* Magic Mirror
|
|
|
|
* Module: Weather
|
|
|
|
* Provider: Dark Sky
|
|
|
|
*
|
|
|
|
* By Nicholas Hubbard https://github.com/nhubbard
|
|
|
|
* MIT Licensed
|
|
|
|
*
|
|
|
|
* This class is a provider for Dark Sky.
|
|
|
|
*/
|
|
|
|
WeatherProvider.register("darksky", {
|
|
|
|
// Set the name of the provider.
|
|
|
|
// Not strictly required, but helps for debugging.
|
|
|
|
providerName: "Dark Sky",
|
2018-12-27 17:56:34 +01:00
|
|
|
|
2018-12-28 19:39:00 +01:00
|
|
|
units: {
|
|
|
|
imperial: 'us',
|
|
|
|
metric: 'ca'
|
|
|
|
},
|
|
|
|
|
2017-09-21 20:06:42 -04:00
|
|
|
fetchCurrentWeather: function() {
|
2018-12-27 17:56:34 +01:00
|
|
|
this.fetchData(this.getUrl())
|
|
|
|
.then(data => {
|
|
|
|
if(!data || !data.currently || typeof data.currently.temperature === "undefined") {
|
|
|
|
// No usable data?
|
|
|
|
return;
|
|
|
|
}
|
2018-12-27 19:37:02 +01:00
|
|
|
|
2018-12-27 17:56:34 +01:00
|
|
|
var currentWeather = this.generateWeatherDayFromCurrentWeather(data);
|
|
|
|
this.setCurrentWeather(currentWeather);
|
|
|
|
}).catch(function(request) {
|
2018-12-27 19:37:02 +01:00
|
|
|
Log.error("Could not load data ... ", request);
|
2018-12-27 17:56:34 +01:00
|
|
|
});
|
2017-09-21 20:06:42 -04:00
|
|
|
},
|
2018-12-27 17:56:34 +01:00
|
|
|
|
2017-09-29 10:11:46 -04:00
|
|
|
fetchWeatherForecast: function() {
|
2018-12-27 17:56:34 +01:00
|
|
|
this.fetchData(this.getUrl())
|
|
|
|
.then(data => {
|
|
|
|
if(!data || !data.daily || !data.daily.data.length) {
|
|
|
|
// No usable data?
|
|
|
|
return;
|
|
|
|
}
|
2018-12-27 19:37:02 +01:00
|
|
|
|
2018-12-27 17:56:34 +01:00
|
|
|
var forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
|
|
|
|
this.setWeatherForecast(forecast);
|
|
|
|
}).catch(function(request) {
|
2018-12-27 19:37:02 +01:00
|
|
|
Log.error("Could not load data ... ", request);
|
2018-12-27 17:56:34 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Create a URL from the config and base URL.
|
|
|
|
getUrl: function() {
|
2018-12-28 19:39:00 +01:00
|
|
|
var units = this.units[this.config.units] || 'auto';
|
|
|
|
return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`;
|
2017-09-29 10:11:46 -04:00
|
|
|
},
|
2018-12-27 17:56:34 +01:00
|
|
|
|
2017-09-21 20:06:42 -04:00
|
|
|
// Implement WeatherDay generator.
|
|
|
|
generateWeatherDayFromCurrentWeather: function(currentWeatherData) {
|
2017-09-29 10:11:46 -04:00
|
|
|
var currentWeather = new WeatherObject();
|
2018-12-27 17:56:34 +01:00
|
|
|
|
|
|
|
currentWeather.date = moment();
|
2017-09-21 20:06:42 -04:00
|
|
|
currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity);
|
|
|
|
currentWeather.temperature = parseFloat(currentWeatherData.currently.temperature);
|
|
|
|
currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed);
|
|
|
|
currentWeather.windDirection = currentWeatherData.currently.windBearing;
|
2018-12-27 17:56:34 +01:00
|
|
|
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon);
|
|
|
|
currentWeather.sunrise = moment(currentWeatherData.daily.data[0].sunriseTime, "X");
|
|
|
|
currentWeather.sunset = moment(currentWeatherData.daily.data[0].sunsetTime, "X");
|
|
|
|
|
2017-09-21 20:06:42 -04:00
|
|
|
return currentWeather;
|
|
|
|
},
|
2018-12-27 17:56:34 +01:00
|
|
|
|
|
|
|
generateWeatherObjectsFromForecast: function(forecasts) {
|
|
|
|
var days = [];
|
|
|
|
|
|
|
|
for (var forecast of forecasts) {
|
|
|
|
var weather = new WeatherObject();
|
|
|
|
|
|
|
|
weather.date = moment(forecast.time, "X");
|
|
|
|
weather.minTemperature = forecast.temperatureMin;
|
|
|
|
weather.maxTemperature = forecast.temperatureMax;
|
|
|
|
weather.weatherType = this.convertWeatherType(forecast.icon);
|
|
|
|
weather.rain = forecast.precipAccumulation;
|
|
|
|
|
|
|
|
days.push(weather)
|
|
|
|
}
|
|
|
|
|
|
|
|
return days
|
|
|
|
},
|
|
|
|
|
2017-09-21 20:06:42 -04:00
|
|
|
// Map icons from Dark Sky to our icons.
|
|
|
|
convertWeatherType: function(weatherType) {
|
|
|
|
var weatherTypes = {
|
|
|
|
"clear-day": "day-sunny",
|
|
|
|
"clear-night": "night-clear",
|
|
|
|
"rain": "rain",
|
|
|
|
"snow": "snow",
|
|
|
|
"sleet": "snow",
|
|
|
|
"wind": "wind",
|
|
|
|
"fog": "fog",
|
|
|
|
"cloudy": "cloudy",
|
|
|
|
"partly-cloudy-day": "day-cloudy",
|
|
|
|
"partly-cloudy-night": "night-cloudy"
|
|
|
|
};
|
|
|
|
return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
|
|
|
|
}
|
2018-12-27 19:37:02 +01:00
|
|
|
});
|