2023-01-15 11:12:55 +01:00
|
|
|
/* global WeatherProvider, WeatherObject */
|
|
|
|
|
|
|
|
/* MagicMirror²
|
|
|
|
* Module: Weather
|
|
|
|
* Provider: Pirate Weather
|
|
|
|
*
|
|
|
|
* Written by Nicholas Hubbard https://github.com/nhubbard for formerly Dark Sky Provider
|
|
|
|
* Modified by Karsten Hassel for Pirate Weather
|
|
|
|
* MIT Licensed
|
|
|
|
*
|
|
|
|
* This class is a provider for Pirate Weather, it is a replacement for Dark Sky (same api).
|
|
|
|
*/
|
|
|
|
WeatherProvider.register("pirateweather", {
|
|
|
|
// Set the name of the provider.
|
|
|
|
// Not strictly required, but helps for debugging.
|
|
|
|
providerName: "pirateweather",
|
|
|
|
|
|
|
|
// Set the default config properties that is specific to this provider
|
|
|
|
defaults: {
|
|
|
|
useCorsProxy: true,
|
|
|
|
apiBase: "https://api.pirateweather.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);
|
2023-01-21 22:40:08 +01:00
|
|
|
currentWeather.windFromDirection = currentWeatherData.currently.windBearing;
|
2023-01-15 11:12:55 +01:00
|
|
|
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;
|
|
|
|
weather.rain = 0;
|
|
|
|
|
|
|
|
let precip = 0;
|
|
|
|
if (forecast.hasOwnProperty("precipAccumulation")) {
|
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
|
|
|
precip = forecast.precipAccumulation * 10;
|
2023-01-15 11:12:55 +01:00
|
|
|
}
|
|
|
|
|
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.precipitationAmount = precip;
|
2023-01-15 11:12:55 +01:00
|
|
|
if (forecast.hasOwnProperty("precipType")) {
|
|
|
|
if (forecast.precipType === "snow") {
|
|
|
|
weather.snow = precip;
|
|
|
|
} else {
|
|
|
|
weather.rain = precip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
days.push(weather);
|
|
|
|
}
|
|
|
|
|
|
|
|
return days;
|
|
|
|
},
|
|
|
|
|
|
|
|
// Map icons from Pirate Weather 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;
|
|
|
|
}
|
|
|
|
});
|