mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
Signed-off-by: naveen <172697+naveensrinivasan@users.noreply.github.com> Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: Karsten Hassel <hassel@gmx.de> Co-authored-by: Malte Hallström <46646495+SkySails@users.noreply.github.com> Co-authored-by: Veeck <github@veeck.de> Co-authored-by: veeck <michael@veeck.de> Co-authored-by: dWoolridge <dwoolridge@charter.net> Co-authored-by: Johan <jojjepersson@yahoo.se> Co-authored-by: Dario Mratovich <dario_mratovich@hotmail.com> Co-authored-by: Dario Mratovich <dario.mratovich@outlook.com> Co-authored-by: Magnus <34011212+MagMar94@users.noreply.github.com> Co-authored-by: Naveen <172697+naveensrinivasan@users.noreply.github.com> Co-authored-by: buxxi <buxxi@omfilm.net> Co-authored-by: Thomas Hirschberger <47733292+Tom-Hirschberger@users.noreply.github.com> Co-authored-by: Kristjan ESPERANTO <35647502+KristjanESPERANTO@users.noreply.github.com> Co-authored-by: Andrés Vanegas Jiménez <142350+angeldeejay@users.noreply.github.com> Co-authored-by: Dave Child <dave@addedbytes.com> Co-authored-by: grenagit <46225780+grenagit@users.noreply.github.com> Co-authored-by: Grena <grena@grenabox.fr> Co-authored-by: Magnus Marthinsen <magmar@online.no> Co-authored-by: Patrick <psieg@users.noreply.github.com> Co-authored-by: Piotr Rajnisz <56397164+rajniszp@users.noreply.github.com> Co-authored-by: Suthep Yonphimai <tomzt@users.noreply.github.com> Co-authored-by: CarJem Generations (Carter Wallace) <cwallacecs@gmail.com> Co-authored-by: Nicholas Fogal <nfogal.misc@gmail.com> Co-authored-by: JakeBinney <126349119+JakeBinney@users.noreply.github.com> Co-authored-by: OWL4C <124401812+OWL4C@users.noreply.github.com> Co-authored-by: Oscar Björkman <17575446+oscarb@users.noreply.github.com> Co-authored-by: Ismar Slomic <ismar@slomic.no> Co-authored-by: Jørgen Veum-Wahlberg <jorgen.wahlberg@amedia.no> Co-authored-by: Eddie Hung <6740044+eddiehung@users.noreply.github.com> Co-authored-by: Bugsounet - Cédric <github@bugsounet.fr> Co-authored-by: bugsounet <bugsounet@bugsounet.fr> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
137 lines
4.4 KiB
JavaScript
137 lines
4.4 KiB
JavaScript
/* global SunCalc, WeatherUtils */
|
|
|
|
/* MagicMirror²
|
|
* Module: Weather
|
|
*
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
|
* MIT Licensed.
|
|
*
|
|
* This class is the blueprint for a day which includes weather information.
|
|
*
|
|
* 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.
|
|
*/
|
|
|
|
/**
|
|
* @external Moment
|
|
*/
|
|
class WeatherObject {
|
|
/**
|
|
* Constructor for a WeatherObject
|
|
*/
|
|
constructor() {
|
|
this.date = null;
|
|
this.windSpeed = null;
|
|
this.windFromDirection = null;
|
|
this.sunrise = null;
|
|
this.sunset = null;
|
|
this.temperature = null;
|
|
this.minTemperature = null;
|
|
this.maxTemperature = null;
|
|
this.weatherType = null;
|
|
this.humidity = null;
|
|
this.precipitationAmount = null;
|
|
this.precipitationUnits = null;
|
|
this.precipitationProbability = null;
|
|
this.feelsLikeTemp = null;
|
|
}
|
|
|
|
cardinalWindDirection() {
|
|
if (this.windFromDirection > 11.25 && this.windFromDirection <= 33.75) {
|
|
return "NNE";
|
|
} else if (this.windFromDirection > 33.75 && this.windFromDirection <= 56.25) {
|
|
return "NE";
|
|
} else if (this.windFromDirection > 56.25 && this.windFromDirection <= 78.75) {
|
|
return "ENE";
|
|
} else if (this.windFromDirection > 78.75 && this.windFromDirection <= 101.25) {
|
|
return "E";
|
|
} else if (this.windFromDirection > 101.25 && this.windFromDirection <= 123.75) {
|
|
return "ESE";
|
|
} else if (this.windFromDirection > 123.75 && this.windFromDirection <= 146.25) {
|
|
return "SE";
|
|
} else if (this.windFromDirection > 146.25 && this.windFromDirection <= 168.75) {
|
|
return "SSE";
|
|
} else if (this.windFromDirection > 168.75 && this.windFromDirection <= 191.25) {
|
|
return "S";
|
|
} else if (this.windFromDirection > 191.25 && this.windFromDirection <= 213.75) {
|
|
return "SSW";
|
|
} else if (this.windFromDirection > 213.75 && this.windFromDirection <= 236.25) {
|
|
return "SW";
|
|
} else if (this.windFromDirection > 236.25 && this.windFromDirection <= 258.75) {
|
|
return "WSW";
|
|
} else if (this.windFromDirection > 258.75 && this.windFromDirection <= 281.25) {
|
|
return "W";
|
|
} else if (this.windFromDirection > 281.25 && this.windFromDirection <= 303.75) {
|
|
return "WNW";
|
|
} else if (this.windFromDirection > 303.75 && this.windFromDirection <= 326.25) {
|
|
return "NW";
|
|
} else if (this.windFromDirection > 326.25 && this.windFromDirection <= 348.75) {
|
|
return "NNW";
|
|
} else {
|
|
return "N";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 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";
|
|
}
|
|
|
|
feelsLike() {
|
|
if (this.feelsLikeTemp) {
|
|
return this.feelsLikeTemp;
|
|
}
|
|
return WeatherUtils.calculateFeelsLike(this.temperature, this.windSpeed, this.humidity);
|
|
}
|
|
|
|
/**
|
|
* Checks if the weatherObject is at dayTime.
|
|
* @returns {boolean} true if it is at dayTime
|
|
*/
|
|
isDayTime() {
|
|
const now = !this.date ? moment() : this.date;
|
|
return now.isBetween(this.sunrise, this.sunset, undefined, "[]");
|
|
}
|
|
|
|
/**
|
|
* Update the sunrise / sunset time depending on the location. This can be
|
|
* used if your provider doesn't provide that data by itself. Then SunCalc
|
|
* is used here to calculate them according to the location.
|
|
* @param {number} lat latitude
|
|
* @param {number} lon longitude
|
|
*/
|
|
updateSunTime(lat, lon) {
|
|
const now = !this.date ? new Date() : this.date.toDate();
|
|
const times = SunCalc.getTimes(now, lat, lon);
|
|
this.sunrise = moment(times.sunrise);
|
|
this.sunset = moment(times.sunset);
|
|
}
|
|
|
|
/**
|
|
* Clone to simple object to prevent mutating and deprecation of legacy library.
|
|
*
|
|
* 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
|
|
*/
|
|
simpleClone() {
|
|
const toFlat = ["date", "sunrise", "sunset"];
|
|
let clone = { ...this };
|
|
for (const prop of toFlat) {
|
|
clone[prop] = clone?.[prop]?.valueOf() ?? clone?.[prop];
|
|
}
|
|
return clone;
|
|
}
|
|
}
|
|
|
|
/*************** DO NOT EDIT THE LINE BELOW ***************/
|
|
if (typeof module !== "undefined") {
|
|
module.exports = WeatherObject;
|
|
}
|