78 lines
2.4 KiB
JavaScript
Raw Normal View History

/* global WeatherProvider, WeatherObject, WeatherUtils */
2022-02-21 14:54:35 +01:00
/* 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
},
fetchCurrentWeather () {
2022-02-22 11:14:28 +01:00
this.fetchData(this.getUrl())
.then((data) => {
const currentWeather = new WeatherObject();
2022-02-22 11:14:28 +01:00
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 = WeatherUtils.convertWindToMs(data.current_conditions.wind_avg);
currentWeather.windFromDirection = data.current_conditions.wind_direction;
2022-02-22 11:14:28 +01:00
currentWeather.weatherType = data.forecast.daily[0].icon;
currentWeather.sunrise = moment.unix(data.forecast.daily[0].sunrise);
currentWeather.sunset = moment.unix(data.forecast.daily[0].sunset);
2022-02-22 11:14:28 +01:00
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
fetchWeatherForecast () {
2022-02-22 11:14:28 +01:00
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();
2022-02-21 14:54:35 +01:00
weather.date = moment.unix(forecast.day_start_local);
2022-02-22 11:14:28 +01:00
weather.minTemperature = forecast.air_temp_low;
weather.maxTemperature = forecast.air_temp_high;
weather.precipitationProbability = forecast.precip_probability;
2022-02-22 11:14:28 +01:00
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}&units_temp=c&units_wind=kph&units_pressure=mb&units_precip=mm&units_distance=km&token=${this.config.token}`;
2022-02-22 11:14:28 +01:00
}
});