mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
darksky forecast and darksky current weather fixes
This commit is contained in:
parent
95adc0aec1
commit
0ed2ba0183
@ -13,49 +13,76 @@ WeatherProvider.register("darksky", {
|
|||||||
// Set the name of the provider.
|
// Set the name of the provider.
|
||||||
// Not strictly required, but helps for debugging.
|
// Not strictly required, but helps for debugging.
|
||||||
providerName: "Dark Sky",
|
providerName: "Dark Sky",
|
||||||
// Implement fetchCurrentWeather.
|
|
||||||
fetchCurrentWeather: function() {
|
fetchCurrentWeather: function() {
|
||||||
// Create a URL from the config and base URL.
|
this.fetchData(this.getUrl())
|
||||||
var url = `https://api.darksky.net/forecast/${this.config.apiKey}/${this.config.latLong}`;
|
.then(data => {
|
||||||
// Run the request.
|
Log.log(data);
|
||||||
this.fetchData(url).then(data => {
|
if(!data || !data.currently || typeof data.currently.temperature === "undefined") {
|
||||||
Log.log(data);
|
// No usable data?
|
||||||
if(!data || !data.main || typeof data.main.temp === "undefined") {
|
return;
|
||||||
// No usable data?
|
}
|
||||||
return;
|
var currentWeather = this.generateWeatherDayFromCurrentWeather(data);
|
||||||
}
|
this.setCurrentWeather(currentWeather);
|
||||||
var currentWeather = this.generateWeatherDayFromCurrentWeather(data);
|
}).catch(function(request) {
|
||||||
this.setCurrentWeather(currentWeather);
|
Log.error("Could not load data!", request);
|
||||||
}).catch(function(request) {
|
});
|
||||||
Log.error("Could not load data!", request);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
|
|
||||||
fetchWeatherForecast: function() {
|
fetchWeatherForecast: function() {
|
||||||
// Also, fake data.
|
this.fetchData(this.getUrl())
|
||||||
var forecast = [];
|
.then(data => {
|
||||||
var today = moment();
|
Log.log(data);
|
||||||
for(var i = 0; i < 5; i++) {
|
if(!data || !data.daily || !data.daily.data.length) {
|
||||||
var weatherObject = new WeatherObject();
|
// No usable data?
|
||||||
weatherObject.date = moment(today).add(i, "days");
|
return;
|
||||||
weatherObject.minTemperature = Math.random() * 10 + 10;
|
}
|
||||||
weatherObject.maxTemperature = Math.random() * 15 + 10;
|
var forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
|
||||||
forecast.push(weatherObject);
|
this.setWeatherForecast(forecast);
|
||||||
}
|
}).catch(function(request) {
|
||||||
this.setWeatherForecast();
|
Log.error("Could not load data!", request);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
// Create a URL from the config and base URL.
|
||||||
|
getUrl: function() {
|
||||||
|
return `https://cors-anywhere.herokuapp.com/https://api.darksky.net/forecast/${this.config.apiKey}/${this.config.lat},${this.config.lon}`;
|
||||||
|
},
|
||||||
|
|
||||||
// Implement WeatherDay generator.
|
// Implement WeatherDay generator.
|
||||||
generateWeatherDayFromCurrentWeather: function(currentWeatherData) {
|
generateWeatherDayFromCurrentWeather: function(currentWeatherData) {
|
||||||
var currentWeather = new WeatherObject();
|
var currentWeather = new WeatherObject();
|
||||||
currentWeather.date = new Date();
|
|
||||||
|
currentWeather.date = moment();
|
||||||
currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity);
|
currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity);
|
||||||
currentWeather.temperature = parseFloat(currentWeatherData.currently.temperature);
|
currentWeather.temperature = parseFloat(currentWeatherData.currently.temperature);
|
||||||
currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed);
|
currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed);
|
||||||
currentWeather.windDirection = currentWeatherData.currently.windBearing;
|
currentWeather.windDirection = currentWeatherData.currently.windBearing;
|
||||||
currentWeather.weatherType = this.currentWeatherType(currentWeatherData.currently.icon);
|
currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon);
|
||||||
currentWeather.sunrise = new Date(currentWeatherData.daily.data[0].sunriseTime);
|
currentWeather.sunrise = moment(currentWeatherData.daily.data[0].sunriseTime, "X");
|
||||||
currentWeather.sunset = new Date(currentWeatherData.daily.data[0].sunsetTime);
|
currentWeather.sunset = moment(currentWeatherData.daily.data[0].sunsetTime, "X");
|
||||||
|
|
||||||
return currentWeather;
|
return currentWeather;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
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
|
||||||
|
},
|
||||||
|
|
||||||
// Map icons from Dark Sky to our icons.
|
// Map icons from Dark Sky to our icons.
|
||||||
convertWeatherType: function(weatherType) {
|
convertWeatherType: function(weatherType) {
|
||||||
var weatherTypes = {
|
var weatherTypes = {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user