mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 11:50:00 +00:00
So finally I think this refactorin is ready to be reviewed :-) DONE: - [x] Removed all conversion functions for wind and temperature from specific weatherproviders - [x] Use internally only metric units: celsius for temperature, meters per seconds for wind - [x] Convert temp and wind into the configured units when displaying data on the UI - [x] look how beaufort calculation uses metrics, added knots as new windunit - [x] add more e2e tests Checked providers: - [x] Darksky - [x] EnvCanada - [x] OpenWeatherMap - [x] SMHI provider - [x] UK Met Office - [x] UK Met Office DataHub - [x] WeatherBit - [x] WeatherFlow - [x] WeatherGov TODO in different tickets: - check weatherproviders for usage of weatherEndpoint (as seen in https://github.com/MichMich/MagicMirror-Documentation/issues/131) -> see #2926 - cleanup precipations -> #2953 Co-authored-by: veeck <michael@veeck.de>
133 lines
4.0 KiB
JavaScript
133 lines
4.0 KiB
JavaScript
/* global WeatherProvider, WeatherObject */
|
|
|
|
/* MagicMirror²
|
|
* Module: Weather
|
|
* Provider: Dark Sky
|
|
*
|
|
* By Nicholas Hubbard https://github.com/nhubbard
|
|
* MIT Licensed
|
|
*
|
|
* This class is a provider for Dark Sky.
|
|
* Note that the Dark Sky API does not provide rainfall. Instead it provides
|
|
* snowfall and precipitation probability
|
|
*/
|
|
WeatherProvider.register("darksky", {
|
|
// Set the name of the provider.
|
|
// Not strictly required, but helps for debugging.
|
|
providerName: "Dark Sky",
|
|
|
|
// Set the default config properties that is specific to this provider
|
|
defaults: {
|
|
useCorsProxy: true,
|
|
apiBase: "https://api.darksky.net",
|
|
weatherEndpoint: "/forecast",
|
|
apiKey: "",
|
|
lat: 0,
|
|
lon: 0
|
|
},
|
|
|
|
fetchCurrentWeather() {
|
|
this.fetchData(this.getUrl())
|
|
.then((data) => {
|
|
if (!data || !data.currently || typeof data.currently.temperature === "undefined") {
|
|
// No usable data?
|
|
return;
|
|
}
|
|
|
|
const currentWeather = this.generateWeatherDayFromCurrentWeather(data);
|
|
this.setCurrentWeather(currentWeather);
|
|
})
|
|
.catch(function (request) {
|
|
Log.error("Could not load data ... ", request);
|
|
})
|
|
.finally(() => this.updateAvailable());
|
|
},
|
|
|
|
fetchWeatherForecast() {
|
|
this.fetchData(this.getUrl())
|
|
.then((data) => {
|
|
if (!data || !data.daily || !data.daily.data.length) {
|
|
// No usable data?
|
|
return;
|
|
}
|
|
|
|
const forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
|
|
this.setWeatherForecast(forecast);
|
|
})
|
|
.catch(function (request) {
|
|
Log.error("Could not load data ... ", request);
|
|
})
|
|
.finally(() => this.updateAvailable());
|
|
},
|
|
|
|
// Create a URL from the config and base URL.
|
|
getUrl() {
|
|
return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=si&lang=${this.config.lang}`;
|
|
},
|
|
|
|
// Implement WeatherDay generator.
|
|
generateWeatherDayFromCurrentWeather(currentWeatherData) {
|
|
const currentWeather = new WeatherObject();
|
|
|
|
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.convertWeatherType(currentWeatherData.currently.icon);
|
|
currentWeather.sunrise = moment.unix(currentWeatherData.daily.data[0].sunriseTime);
|
|
currentWeather.sunset = moment.unix(currentWeatherData.daily.data[0].sunsetTime);
|
|
|
|
return currentWeather;
|
|
},
|
|
|
|
generateWeatherObjectsFromForecast(forecasts) {
|
|
const days = [];
|
|
|
|
for (const forecast of forecasts) {
|
|
const weather = new WeatherObject();
|
|
|
|
weather.date = moment.unix(forecast.time);
|
|
weather.minTemperature = forecast.temperatureMin;
|
|
weather.maxTemperature = forecast.temperatureMax;
|
|
weather.weatherType = this.convertWeatherType(forecast.icon);
|
|
weather.snow = 0;
|
|
|
|
// The API will return centimeters if units is 'si' and will return inches for 'us'
|
|
// Note that the Dark Sky API does not provide rainfall.
|
|
// Instead it provides snowfall and precipitation probability
|
|
if (forecast.hasOwnProperty("precipAccumulation")) {
|
|
if (this.config.units === "imperial" && !isNaN(forecast.precipAccumulation)) {
|
|
weather.snow = forecast.precipAccumulation;
|
|
} else if (!isNaN(forecast.precipAccumulation)) {
|
|
weather.snow = forecast.precipAccumulation * 10;
|
|
}
|
|
}
|
|
|
|
weather.precipitation = weather.snow;
|
|
|
|
days.push(weather);
|
|
}
|
|
|
|
return days;
|
|
},
|
|
|
|
// Map icons from Dark Sky to our icons.
|
|
convertWeatherType(weatherType) {
|
|
const 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;
|
|
}
|
|
});
|