Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

weatherobject.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* global SunCalc */
  2. /* Magic Mirror
  3. * Module: Weather
  4. *
  5. * By Michael Teeuw https://michaelteeuw.nl
  6. * MIT Licensed.
  7. *
  8. * This class is the blueprint for a day which includes weather information.
  9. *
  10. * Currently this is focused on the information which is necessary for the current weather.
  11. * As soon as we start implementing the forecast, mode properties will be added.
  12. */
  13. class WeatherObject {
  14. /**
  15. * Constructor for a WeatherObject
  16. *
  17. * @param {string} units what units to use, "imperial" or "metric"
  18. * @param {string} tempUnits what tempunits to use
  19. * @param {string} windUnits what windunits to use
  20. * @param {boolean} useKmh use kmh if true, mps if false
  21. */
  22. constructor(units, tempUnits, windUnits, useKmh) {
  23. this.units = units;
  24. this.tempUnits = tempUnits;
  25. this.windUnits = windUnits;
  26. this.useKmh = useKmh;
  27. this.date = null;
  28. this.windSpeed = null;
  29. this.windDirection = null;
  30. this.sunrise = null;
  31. this.sunset = null;
  32. this.temperature = null;
  33. this.minTemperature = null;
  34. this.maxTemperature = null;
  35. this.weatherType = null;
  36. this.humidity = null;
  37. this.rain = null;
  38. this.snow = null;
  39. this.precipitation = null;
  40. this.precipitationUnits = null;
  41. this.feelsLikeTemp = null;
  42. }
  43. cardinalWindDirection() {
  44. if (this.windDirection > 11.25 && this.windDirection <= 33.75) {
  45. return "NNE";
  46. } else if (this.windDirection > 33.75 && this.windDirection <= 56.25) {
  47. return "NE";
  48. } else if (this.windDirection > 56.25 && this.windDirection <= 78.75) {
  49. return "ENE";
  50. } else if (this.windDirection > 78.75 && this.windDirection <= 101.25) {
  51. return "E";
  52. } else if (this.windDirection > 101.25 && this.windDirection <= 123.75) {
  53. return "ESE";
  54. } else if (this.windDirection > 123.75 && this.windDirection <= 146.25) {
  55. return "SE";
  56. } else if (this.windDirection > 146.25 && this.windDirection <= 168.75) {
  57. return "SSE";
  58. } else if (this.windDirection > 168.75 && this.windDirection <= 191.25) {
  59. return "S";
  60. } else if (this.windDirection > 191.25 && this.windDirection <= 213.75) {
  61. return "SSW";
  62. } else if (this.windDirection > 213.75 && this.windDirection <= 236.25) {
  63. return "SW";
  64. } else if (this.windDirection > 236.25 && this.windDirection <= 258.75) {
  65. return "WSW";
  66. } else if (this.windDirection > 258.75 && this.windDirection <= 281.25) {
  67. return "W";
  68. } else if (this.windDirection > 281.25 && this.windDirection <= 303.75) {
  69. return "WNW";
  70. } else if (this.windDirection > 303.75 && this.windDirection <= 326.25) {
  71. return "NW";
  72. } else if (this.windDirection > 326.25 && this.windDirection <= 348.75) {
  73. return "NNW";
  74. } else {
  75. return "N";
  76. }
  77. }
  78. beaufortWindSpeed() {
  79. const windInKmh = this.windUnits === "imperial" ? this.windSpeed * 1.609344 : this.useKmh ? this.windSpeed : (this.windSpeed * 60 * 60) / 1000;
  80. const speeds = [1, 5, 11, 19, 28, 38, 49, 61, 74, 88, 102, 117, 1000];
  81. for (const [index, speed] of speeds.entries()) {
  82. if (speed > windInKmh) {
  83. return index;
  84. }
  85. }
  86. return 12;
  87. }
  88. kmhWindSpeed() {
  89. return this.windUnits === "imperial" ? this.windSpeed * 1.609344 : (this.windSpeed * 60 * 60) / 1000;
  90. }
  91. nextSunAction() {
  92. return moment().isBetween(this.sunrise, this.sunset) ? "sunset" : "sunrise";
  93. }
  94. feelsLike() {
  95. if (this.feelsLikeTemp) {
  96. return this.feelsLikeTemp;
  97. }
  98. const windInMph = this.windUnits === "imperial" ? this.windSpeed : this.windSpeed * 2.23694;
  99. const tempInF = this.tempUnits === "imperial" ? this.temperature : (this.temperature * 9) / 5 + 32;
  100. let feelsLike = tempInF;
  101. if (windInMph > 3 && tempInF < 50) {
  102. feelsLike = Math.round(35.74 + 0.6215 * tempInF - 35.75 * Math.pow(windInMph, 0.16) + 0.4275 * tempInF * Math.pow(windInMph, 0.16));
  103. } else if (tempInF > 80 && this.humidity > 40) {
  104. feelsLike =
  105. -42.379 +
  106. 2.04901523 * tempInF +
  107. 10.14333127 * this.humidity -
  108. 0.22475541 * tempInF * this.humidity -
  109. 6.83783 * Math.pow(10, -3) * tempInF * tempInF -
  110. 5.481717 * Math.pow(10, -2) * this.humidity * this.humidity +
  111. 1.22874 * Math.pow(10, -3) * tempInF * tempInF * this.humidity +
  112. 8.5282 * Math.pow(10, -4) * tempInF * this.humidity * this.humidity -
  113. 1.99 * Math.pow(10, -6) * tempInF * tempInF * this.humidity * this.humidity;
  114. }
  115. return this.tempUnits === "imperial" ? feelsLike : ((feelsLike - 32) * 5) / 9;
  116. }
  117. /**
  118. * Checks if the weatherObject is at dayTime.
  119. *
  120. * @returns {boolean} true if it is at dayTime
  121. */
  122. isDayTime() {
  123. return this.date.isBetween(this.sunrise, this.sunset, undefined, "[]");
  124. }
  125. /**
  126. * Update the sunrise / sunset time depending on the location. This can be
  127. * used if your provider doesnt provide that data by itself. Then SunCalc
  128. * is used here to calculate them according to the location.
  129. *
  130. * @param {number} lat latitude
  131. * @param {number} lon longitude
  132. */
  133. updateSunTime(lat, lon) {
  134. let now = !this.date ? new Date() : this.date.toDate();
  135. let times = SunCalc.getTimes(now, lat, lon);
  136. this.sunrise = moment(times.sunrise, "X");
  137. this.sunset = moment(times.sunset, "X");
  138. }
  139. }
  140. /*************** DO NOT EDIT THE LINE BELOW ***************/
  141. if (typeof module !== "undefined") {
  142. module.exports = WeatherObject;
  143. }