2022-10-28 19:56:55 +02:00
|
|
|
/* 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
|
|
|
},
|
|
|
|
|
2023-12-25 08:17:11 +01:00
|
|
|
fetchCurrentWeather () {
|
2022-02-22 11:14:28 +01:00
|
|
|
this.fetchData(this.getUrl())
|
|
|
|
.then((data) => {
|
2022-10-24 19:41:34 +02:00
|
|
|
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;
|
2022-10-28 19:56:55 +02:00
|
|
|
currentWeather.windSpeed = WeatherUtils.convertWindToMs(data.current_conditions.wind_avg);
|
2023-01-21 22:40:08 +01:00
|
|
|
currentWeather.windFromDirection = data.current_conditions.wind_direction;
|
2022-02-22 11:14:28 +01:00
|
|
|
currentWeather.weatherType = data.forecast.daily[0].icon;
|
2022-10-16 23:37:50 +02:00
|
|
|
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
|
|
|
|
2023-12-25 08:17:11 +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) {
|
2022-10-24 19:41:34 +02:00
|
|
|
const weather = new WeatherObject();
|
2022-02-21 14:54:35 +01:00
|
|
|
|
2022-10-16 23:37:50 +02: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;
|
Tidy up precipitation (#3023)
Fixes #2953
This is an attempt to fix the issue with precipitation amount and
percentage mixup. I have created a separate
`precipitationPercentage`-variable where the probability of rain can be
stored.
The config options now has the old `showPrecipitationAmount` in addition
to a new setting: `showPrecipitationProbability` (shows the likelihood
of rain).
<details>
<summary>Examples</summary>
### Yr
I tested the Yr weather provider for a Norwegian city Bergen that has a
lot of rain. I have removed properties that are irrelevant for this demo
from the config-samples below.
Config:
```js
{
module: "weather",
config: {
weatherProvider: "yr",
type: "current",
showPrecipitationAmount: true,
showPrecipitationProbability: true
}
},
{
module: "weather",
config: {
weatherProvider: "yr",
type: "hourly",
showPrecipitationAmount: true,
showPrecipitationProbability: true
}
},
{
module: "weather",
config: {
weatherProvider: "yr",
type: "daily",
showPrecipitationAmount: true,
showPrecipitationProbability: true
}
}
```
Result:<br/>
<img width="444" alt="screenshot"
src="https://user-images.githubusercontent.com/34011212/216775423-4e37345c-f915-47e5-8551-7c544ebd24b1.png">
</details>
---------
Co-authored-by: Magnus Marthinsen <magmar@online.no>
Co-authored-by: Veeck <github@veeck.de>
2023-02-04 19:02:55 +01:00
|
|
|
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.
|
2023-12-25 08:17:11 +01:00
|
|
|
getUrl () {
|
2022-10-24 19:41:34 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|