darksky forecast and darksky current weather fixes

This commit is contained in:
fewieden 2018-12-27 17:56:34 +01:00
parent 95adc0aec1
commit 0ed2ba0183

View File

@ -13,14 +13,12 @@ WeatherProvider.register("darksky", {
// Set the name of the provider.
// Not strictly required, but helps for debugging.
providerName: "Dark Sky",
// Implement fetchCurrentWeather.
fetchCurrentWeather: function() {
// Create a URL from the config and base URL.
var url = `https://api.darksky.net/forecast/${this.config.apiKey}/${this.config.latLong}`;
// Run the request.
this.fetchData(url).then(data => {
this.fetchData(this.getUrl())
.then(data => {
Log.log(data);
if(!data || !data.main || typeof data.main.temp === "undefined") {
if(!data || !data.currently || typeof data.currently.temperature === "undefined") {
// No usable data?
return;
}
@ -30,32 +28,61 @@ WeatherProvider.register("darksky", {
Log.error("Could not load data!", request);
});
},
fetchWeatherForecast: function() {
// Also, fake 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 => {
Log.log(data);
if(!data || !data.daily || !data.daily.data.length) {
// No usable data?
return;
}
this.setWeatherForecast();
var forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
this.setWeatherForecast(forecast);
}).catch(function(request) {
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.
generateWeatherDayFromCurrentWeather: function(currentWeatherData) {
var currentWeather = new WeatherObject();
currentWeather.date = new Date();
currentWeather.date = moment();
currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity);
currentWeather.temperature = parseFloat(currentWeatherData.currently.temperature);
currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed);
currentWeather.windDirection = currentWeatherData.currently.windBearing;
currentWeather.weatherType = this.currentWeatherType(currentWeatherData.currently.icon);
currentWeather.sunrise = new Date(currentWeatherData.daily.data[0].sunriseTime);
currentWeather.sunset = new Date(currentWeatherData.daily.data[0].sunsetTime);
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");
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.
convertWeatherType: function(weatherType) {
var weatherTypes = {