2022-02-21 14:54:35 +01:00
|
|
|
/* global WeatherProvider, WeatherObject */
|
|
|
|
|
|
|
|
/* MagicMirror²
|
|
|
|
* Module: Weather
|
2022-02-22 11:12:17 +01:00
|
|
|
* Provider: Weatherflow
|
2022-02-21 14:54:35 +01:00
|
|
|
*
|
|
|
|
* By Tobias Dreyem https://github.com/10bias
|
|
|
|
* MIT Licensed
|
|
|
|
*
|
|
|
|
* This class is a provider for Weatherflow.
|
|
|
|
* Note that the Weatherflow API does not provide snowfall.
|
|
|
|
*/
|
|
|
|
|
|
|
|
WeatherProvider.register("weatherflow", {
|
|
|
|
// Set the name of the provider.
|
|
|
|
// Not strictly required, but helps for debugging
|
2022-02-22 11:14:28 +01:00
|
|
|
providerName: "WeatherFlow",
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
// Set the default config properties that is specific to this provider
|
|
|
|
defaults: {
|
2022-02-21 14:54:35 +01:00
|
|
|
apiBase: "https://swd.weatherflow.com/swd/rest/",
|
|
|
|
token: "",
|
2022-02-22 11:14:28 +01:00
|
|
|
stationid: ""
|
2022-02-21 14:54:35 +01:00
|
|
|
},
|
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
units: {
|
2022-02-21 14:54:35 +01:00
|
|
|
imperial: "us",
|
|
|
|
metric: "si"
|
|
|
|
},
|
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
fetchCurrentWeather() {
|
|
|
|
this.fetchData(this.getUrl())
|
|
|
|
.then((data) => {
|
|
|
|
const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
|
|
|
currentWeather.date = moment();
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
currentWeather.humidity = data.current_conditions.relative_humidity;
|
|
|
|
currentWeather.temperature = data.current_conditions.air_temperature;
|
|
|
|
currentWeather.windSpeed = data.current_conditions.wind_avg;
|
|
|
|
currentWeather.windDirection = data.current_conditions.wind_direction;
|
|
|
|
currentWeather.weatherType = data.forecast.daily[0].icon;
|
|
|
|
(currentWeather.sunrise = moment(data.forecast.daily[0].sunrise)), "X";
|
|
|
|
currentWeather.sunset = moment(data.forecast.daily[0].sunset, "X");
|
|
|
|
this.setCurrentWeather(currentWeather);
|
|
|
|
})
|
|
|
|
.catch(function (request) {
|
2022-02-21 14:54:35 +01:00
|
|
|
Log.error("Could not load data ... ", request);
|
|
|
|
})
|
|
|
|
.finally(() => this.updateAvailable());
|
2022-02-22 11:14:28 +01:00
|
|
|
},
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
fetchWeatherForecast() {
|
|
|
|
this.fetchData(this.getUrl())
|
|
|
|
.then((data) => {
|
|
|
|
const days = [];
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
for (const forecast of data.forecast.daily) {
|
|
|
|
const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
weather.date = moment(forecast.day_start_local, "X");
|
|
|
|
weather.minTemperature = forecast.air_temp_low;
|
|
|
|
weather.maxTemperature = forecast.air_temp_high;
|
|
|
|
weather.weatherType = forecast.icon;
|
|
|
|
weather.snow = 0;
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
days.push(weather);
|
|
|
|
}
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
this.setWeatherForecast(days);
|
|
|
|
})
|
|
|
|
.catch(function (request) {
|
|
|
|
Log.error("Could not load data ... ", request);
|
|
|
|
})
|
|
|
|
.finally(() => this.updateAvailable());
|
|
|
|
},
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-02-22 11:14:28 +01:00
|
|
|
// Create a URL from the config and base URL.
|
|
|
|
getUrl() {
|
|
|
|
return this.config.apiBase + "better_forecast?station_id=" + this.config.stationid + "&token=" + this.config.token;
|
|
|
|
}
|
|
|
|
});
|