110 lines
2.9 KiB
JavaScript
Raw Normal View History

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-26 11:09:41 +01:00
imperial: {
temp: "f",
wind: "mph",
pressure: "hpa",
precip: "in",
distance: "mi"
},
metric: {
temp: "c",
wind: "kph",
pressure: "mb",
precip: "mm",
distance: "km"
}
2022-02-21 14:54:35 +01:00
},
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.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
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
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.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() {
2022-02-26 11:09:41 +01:00
return (
this.config.apiBase +
"better_forecast?station_id=" +
this.config.stationid +
"&units_temp=" +
this.units[this.config.units].temp +
"&units_wind=" +
this.units[this.config.units].wind +
"&units_pressure=" +
this.units[this.config.units].pressure +
"&units_precip=" +
this.units[this.config.units].precip +
"&units_distance=" +
this.units[this.config.units].distance +
"&token=" +
this.config.token
);
2022-02-22 11:14:28 +01:00
}
});