mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 03:39:55 +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>
143 lines
3.9 KiB
JavaScript
143 lines
3.9 KiB
JavaScript
/* MagicMirror²
|
|
* Weather Util Methods
|
|
*
|
|
* By Rejas
|
|
* MIT Licensed.
|
|
*/
|
|
const WeatherUtils = {
|
|
/**
|
|
* Convert wind (from m/s) to beaufort scale
|
|
* @param {number} speedInMS the windspeed you want to convert
|
|
* @returns {number} the speed in beaufort
|
|
*/
|
|
beaufortWindSpeed(speedInMS) {
|
|
const windInKmh = this.convertWind(speedInMS, "kmh");
|
|
const speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
|
|
for (const [index, speed] of speeds.entries()) {
|
|
if (speed > windInKmh) {
|
|
return index;
|
|
}
|
|
}
|
|
return 12;
|
|
},
|
|
|
|
/**
|
|
* Convert a value in a given unit to a string with a converted
|
|
* value and a postfix matching the output unit system.
|
|
* @param {number} value - The value to convert.
|
|
* @param {string} valueUnit - The unit the values has. Default is mm.
|
|
* @param {string} outputUnit - The unit system (imperial/metric) the return value should have.
|
|
* @returns {string} - A string with tha value and a unit postfix.
|
|
*/
|
|
convertPrecipitationUnit(value, valueUnit, outputUnit) {
|
|
if (valueUnit === "%") return `${value.toFixed(0)} ${valueUnit}`;
|
|
|
|
let convertedValue = value;
|
|
let conversionUnit = valueUnit;
|
|
if (outputUnit === "imperial") {
|
|
if (valueUnit && valueUnit.toLowerCase() === "cm") convertedValue = convertedValue * 0.3937007874;
|
|
else convertedValue = convertedValue * 0.03937007874;
|
|
conversionUnit = "in";
|
|
} else {
|
|
conversionUnit = valueUnit ? valueUnit : "mm";
|
|
}
|
|
|
|
return `${convertedValue.toFixed(2)} ${conversionUnit}`;
|
|
},
|
|
|
|
/**
|
|
* Convert temp (from degrees C) into imperial or metric unit depending on
|
|
* your config
|
|
* @param {number} tempInC the temperature in celsius you want to convert
|
|
* @param {string} unit can be 'imperial' or 'metric'
|
|
* @returns {number} the converted temperature
|
|
*/
|
|
convertTemp(tempInC, unit) {
|
|
return unit === "imperial" ? tempInC * 1.8 + 32 : tempInC;
|
|
},
|
|
|
|
/**
|
|
* Convert wind speed into another unit.
|
|
* @param {number} windInMS the windspeed in meter/sec you want to convert
|
|
* @param {string} unit can be 'beaufort', 'kmh', 'knots, 'imperial' (mph)
|
|
* or 'metric' (mps)
|
|
* @returns {number} the converted windspeed
|
|
*/
|
|
convertWind(windInMS, unit) {
|
|
switch (unit) {
|
|
case "beaufort":
|
|
return this.beaufortWindSpeed(windInMS);
|
|
case "kmh":
|
|
return (windInMS * 3600) / 1000;
|
|
case "knots":
|
|
return windInMS * 1.943844;
|
|
case "imperial":
|
|
return windInMS * 2.2369362920544;
|
|
case "metric":
|
|
default:
|
|
return windInMS;
|
|
}
|
|
},
|
|
|
|
/*
|
|
* Convert the wind direction cardinal to value
|
|
*/
|
|
convertWindDirection(windDirection) {
|
|
const windCardinals = {
|
|
N: 0,
|
|
NNE: 22,
|
|
NE: 45,
|
|
ENE: 67,
|
|
E: 90,
|
|
ESE: 112,
|
|
SE: 135,
|
|
SSE: 157,
|
|
S: 180,
|
|
SSW: 202,
|
|
SW: 225,
|
|
WSW: 247,
|
|
W: 270,
|
|
WNW: 292,
|
|
NW: 315,
|
|
NNW: 337
|
|
};
|
|
|
|
return windCardinals.hasOwnProperty(windDirection) ? windCardinals[windDirection] : null;
|
|
},
|
|
|
|
convertWindToMetric(mph) {
|
|
return mph / 2.2369362920544;
|
|
},
|
|
|
|
convertWindToMs(kmh) {
|
|
return kmh * 0.27777777777778;
|
|
},
|
|
|
|
calculateFeelsLike(temperature, windSpeed, humidity) {
|
|
const windInMph = this.convertWind(windSpeed, "imperial");
|
|
const tempInF = this.convertTemp(temperature, "imperial");
|
|
let feelsLike = tempInF;
|
|
|
|
if (windInMph > 3 && tempInF < 50) {
|
|
feelsLike = Math.round(35.74 + 0.6215 * tempInF - 35.75 * Math.pow(windInMph, 0.16) + 0.4275 * tempInF * Math.pow(windInMph, 0.16));
|
|
} else if (tempInF > 80 && humidity > 40) {
|
|
feelsLike =
|
|
-42.379 +
|
|
2.04901523 * tempInF +
|
|
10.14333127 * humidity -
|
|
0.22475541 * tempInF * humidity -
|
|
6.83783 * Math.pow(10, -3) * tempInF * tempInF -
|
|
5.481717 * Math.pow(10, -2) * humidity * humidity +
|
|
1.22874 * Math.pow(10, -3) * tempInF * tempInF * humidity +
|
|
8.5282 * Math.pow(10, -4) * tempInF * humidity * humidity -
|
|
1.99 * Math.pow(10, -6) * tempInF * tempInF * humidity * humidity;
|
|
}
|
|
|
|
return ((feelsLike - 32) * 5) / 9;
|
|
}
|
|
};
|
|
|
|
if (typeof module !== "undefined") {
|
|
module.exports = WeatherUtils;
|
|
}
|