2022-10-28 19:56:55 +02:00
|
|
|
/* global SunCalc, WeatherUtils */
|
2021-08-31 21:41:27 +02:00
|
|
|
|
2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2017-09-22 12:26:47 +02:00
|
|
|
* Module: Weather
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2017-09-22 12:26:47 +02:00
|
|
|
* MIT Licensed.
|
2017-10-18 13:38:56 +02:00
|
|
|
*
|
2017-09-22 12:26:47 +02:00
|
|
|
* This class is the blueprint for a day which includes weather information.
|
2020-04-21 10:41:21 +02:00
|
|
|
*
|
|
|
|
* Currently this is focused on the information which is necessary for the current weather.
|
|
|
|
* As soon as we start implementing the forecast, mode properties will be added.
|
2017-09-22 12:26:47 +02:00
|
|
|
*/
|
2023-01-26 19:43:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @external Moment
|
|
|
|
*/
|
2017-09-22 12:26:47 +02:00
|
|
|
class WeatherObject {
|
2021-08-31 23:26:06 +02:00
|
|
|
/**
|
2021-09-02 20:35:43 +02:00
|
|
|
* Constructor for a WeatherObject
|
2021-08-31 23:26:06 +02:00
|
|
|
*/
|
2022-10-24 19:41:34 +02:00
|
|
|
constructor() {
|
2018-12-27 19:37:02 +01:00
|
|
|
this.date = null;
|
|
|
|
this.windSpeed = null;
|
2023-01-21 22:40:08 +01:00
|
|
|
this.windFromDirection = null;
|
2018-12-27 19:37:02 +01:00
|
|
|
this.sunrise = null;
|
|
|
|
this.sunset = null;
|
|
|
|
this.temperature = null;
|
|
|
|
this.minTemperature = null;
|
|
|
|
this.maxTemperature = null;
|
|
|
|
this.weatherType = null;
|
|
|
|
this.humidity = null;
|
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
|
|
|
this.precipitationAmount = null;
|
2021-05-13 10:56:30 -04:00
|
|
|
this.precipitationUnits = null;
|
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
|
|
|
this.precipitationProbability = null;
|
2019-04-03 15:19:32 +01:00
|
|
|
this.feelsLikeTemp = null;
|
2017-09-22 12:26:47 +02:00
|
|
|
}
|
|
|
|
|
2018-12-30 14:14:17 +01:00
|
|
|
cardinalWindDirection() {
|
2023-01-21 22:40:08 +01:00
|
|
|
if (this.windFromDirection > 11.25 && this.windFromDirection <= 33.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "NNE";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 33.75 && this.windFromDirection <= 56.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "NE";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 56.25 && this.windFromDirection <= 78.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "ENE";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 78.75 && this.windFromDirection <= 101.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "E";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 101.25 && this.windFromDirection <= 123.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "ESE";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 123.75 && this.windFromDirection <= 146.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "SE";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 146.25 && this.windFromDirection <= 168.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "SSE";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 168.75 && this.windFromDirection <= 191.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "S";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 191.25 && this.windFromDirection <= 213.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "SSW";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 213.75 && this.windFromDirection <= 236.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "SW";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 236.25 && this.windFromDirection <= 258.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "WSW";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 258.75 && this.windFromDirection <= 281.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "W";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 281.25 && this.windFromDirection <= 303.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "WNW";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 303.75 && this.windFromDirection <= 326.25) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "NW";
|
2023-01-21 22:40:08 +01:00
|
|
|
} else if (this.windFromDirection > 326.25 && this.windFromDirection <= 348.75) {
|
2017-09-22 12:26:47 +02:00
|
|
|
return "NNW";
|
|
|
|
} else {
|
|
|
|
return "N";
|
|
|
|
}
|
|
|
|
}
|
2017-10-18 13:38:56 +02:00
|
|
|
|
2023-01-26 19:43:34 +01:00
|
|
|
/**
|
|
|
|
* Determines if the sun sets or rises next. Uses the current time and not
|
|
|
|
* the date from the weather-forecast.
|
|
|
|
* @param {Moment} date an optional date where you want to get the next
|
|
|
|
* action for. Useful only in tests, defaults to the current time.
|
|
|
|
* @returns {string} "sunset" or "sunrise"
|
|
|
|
*/
|
|
|
|
nextSunAction(date = moment()) {
|
|
|
|
return date.isBetween(this.sunrise, this.sunset) ? "sunset" : "sunrise";
|
2017-10-18 13:38:56 +02:00
|
|
|
}
|
2018-12-30 14:17:13 +01:00
|
|
|
|
|
|
|
feelsLike() {
|
2020-04-20 22:16:23 +02:00
|
|
|
if (this.feelsLikeTemp) {
|
|
|
|
return this.feelsLikeTemp;
|
2019-04-03 15:19:32 +01:00
|
|
|
}
|
2023-01-26 19:43:34 +01:00
|
|
|
return WeatherUtils.calculateFeelsLike(this.temperature, this.windSpeed, this.humidity);
|
2018-12-30 14:17:13 +01:00
|
|
|
}
|
2021-08-31 21:41:27 +02:00
|
|
|
|
2021-08-31 21:55:43 +02:00
|
|
|
/**
|
|
|
|
* Checks if the weatherObject is at dayTime.
|
2022-04-26 14:57:47 +02:00
|
|
|
* @returns {boolean} true if it is at dayTime
|
2021-08-31 21:55:43 +02:00
|
|
|
*/
|
|
|
|
isDayTime() {
|
2023-01-26 19:43:34 +01:00
|
|
|
const now = !this.date ? moment() : this.date;
|
|
|
|
return now.isBetween(this.sunrise, this.sunset, undefined, "[]");
|
2021-08-31 21:55:43 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 21:41:27 +02:00
|
|
|
/**
|
2021-09-04 12:25:43 +02:00
|
|
|
* Update the sunrise / sunset time depending on the location. This can be
|
2022-10-06 19:44:16 +02:00
|
|
|
* used if your provider doesn't provide that data by itself. Then SunCalc
|
2021-09-04 12:25:43 +02:00
|
|
|
* is used here to calculate them according to the location.
|
2021-08-31 21:41:27 +02:00
|
|
|
* @param {number} lat latitude
|
|
|
|
* @param {number} lon longitude
|
|
|
|
*/
|
|
|
|
updateSunTime(lat, lon) {
|
2022-10-06 19:44:16 +02:00
|
|
|
const now = !this.date ? new Date() : this.date.toDate();
|
|
|
|
const times = SunCalc.getTimes(now, lat, lon);
|
2022-10-16 23:37:50 +02:00
|
|
|
this.sunrise = moment(times.sunrise);
|
|
|
|
this.sunset = moment(times.sunset);
|
2021-08-31 21:41:27 +02:00
|
|
|
}
|
2022-04-26 13:55:39 +02:00
|
|
|
|
|
|
|
/**
|
2022-04-26 17:37:23 +02:00
|
|
|
* Clone to simple object to prevent mutating and deprecation of legacy library.
|
2022-04-26 13:55:39 +02:00
|
|
|
*
|
2022-04-26 17:37:23 +02:00
|
|
|
* Before being handed to other modules, mutable values must be cloned safely.
|
|
|
|
* Especially 'moment' object is not immutable, so original 'date', 'sunrise', 'sunset' could be corrupted or changed by other modules.
|
|
|
|
* @returns {object} plained object clone of original weatherObject
|
2022-04-26 13:55:39 +02:00
|
|
|
*/
|
|
|
|
simpleClone() {
|
|
|
|
const toFlat = ["date", "sunrise", "sunset"];
|
|
|
|
let clone = { ...this };
|
|
|
|
for (const prop of toFlat) {
|
|
|
|
clone[prop] = clone?.[prop]?.valueOf() ?? clone?.[prop];
|
|
|
|
}
|
|
|
|
return clone;
|
|
|
|
}
|
2018-12-27 19:37:02 +01:00
|
|
|
}
|
2021-08-31 23:26:06 +02:00
|
|
|
|
|
|
|
/*************** DO NOT EDIT THE LINE BELOW ***************/
|
|
|
|
if (typeof module !== "undefined") {
|
|
|
|
module.exports = WeatherObject;
|
|
|
|
}
|