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.

weatherbit.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* global WeatherProvider, WeatherObject */
  2. /* Magic Mirror
  3. * Module: Weather
  4. * Provider: Weatherbit
  5. *
  6. * By Andrew Pometti
  7. * MIT Licensed
  8. *
  9. * This class is a provider for Weatherbit, based on Nicholas Hubbard's class
  10. * for Dark Sky & Vince Peri's class for Weather.gov.
  11. */
  12. WeatherProvider.register("weatherbit", {
  13. // Set the name of the provider.
  14. // Not strictly required, but helps for debugging.
  15. providerName: "Weatherbit",
  16. // Set the default config properties that is specific to this provider
  17. defaults: {
  18. apiBase: "https://api.weatherbit.io/v2.0",
  19. weatherEndpoint: "/current",
  20. apiKey: "",
  21. lat: 0,
  22. lon: 0
  23. },
  24. units: {
  25. imperial: "I",
  26. metric: "M"
  27. },
  28. fetchedLocation: function () {
  29. return this.fetchedLocationName || "";
  30. },
  31. fetchCurrentWeather() {
  32. this.fetchData(this.getUrl())
  33. .then((data) => {
  34. if (!data || !data.data[0] || typeof data.data[0].temp === "undefined") {
  35. // No usable data?
  36. return;
  37. }
  38. const currentWeather = this.generateWeatherDayFromCurrentWeather(data);
  39. this.setCurrentWeather(currentWeather);
  40. })
  41. .catch(function (request) {
  42. Log.error("Could not load data ... ", request);
  43. })
  44. .finally(() => this.updateAvailable());
  45. },
  46. fetchWeatherForecast() {
  47. this.fetchData(this.getUrl())
  48. .then((data) => {
  49. if (!data || !data.data) {
  50. // No usable data?
  51. return;
  52. }
  53. const forecast = this.generateWeatherObjectsFromForecast(data.data);
  54. this.setWeatherForecast(forecast);
  55. this.fetchedLocationName = data.city_name + ", " + data.state_code;
  56. })
  57. .catch(function (request) {
  58. Log.error("Could not load data ... ", request);
  59. })
  60. .finally(() => this.updateAvailable());
  61. },
  62. // Create a URL from the config and base URL.
  63. getUrl() {
  64. const units = this.units[this.config.units] || "auto";
  65. return `${this.config.apiBase}${this.config.weatherEndpoint}?lat=${this.config.lat}&lon=${this.config.lon}&units=${units}&key=${this.config.apiKey}`;
  66. },
  67. // Implement WeatherDay generator.
  68. generateWeatherDayFromCurrentWeather(currentWeatherData) {
  69. //Calculate TZ Offset and invert to convert Sunrise/Sunset times to Local
  70. const d = new Date();
  71. let tzOffset = d.getTimezoneOffset();
  72. tzOffset = tzOffset * -1;
  73. const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
  74. currentWeather.date = moment(currentWeatherData.data[0].ts, "X");
  75. currentWeather.humidity = parseFloat(currentWeatherData.data[0].rh);
  76. currentWeather.temperature = parseFloat(currentWeatherData.data[0].temp);
  77. currentWeather.windSpeed = parseFloat(currentWeatherData.data[0].wind_spd);
  78. currentWeather.windDirection = currentWeatherData.data[0].wind_dir;
  79. currentWeather.weatherType = this.convertWeatherType(currentWeatherData.data[0].weather.icon);
  80. currentWeather.sunrise = moment(currentWeatherData.data[0].sunrise, "HH:mm").add(tzOffset, "m");
  81. currentWeather.sunset = moment(currentWeatherData.data[0].sunset, "HH:mm").add(tzOffset, "m");
  82. this.fetchedLocationName = currentWeatherData.data[0].city_name + ", " + currentWeatherData.data[0].state_code;
  83. return currentWeather;
  84. },
  85. generateWeatherObjectsFromForecast(forecasts) {
  86. const days = [];
  87. for (const forecast of forecasts) {
  88. const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits);
  89. weather.date = moment(forecast.datetime, "YYYY-MM-DD");
  90. weather.minTemperature = forecast.min_temp;
  91. weather.maxTemperature = forecast.max_temp;
  92. weather.precipitation = forecast.precip;
  93. weather.weatherType = this.convertWeatherType(forecast.weather.icon);
  94. days.push(weather);
  95. }
  96. return days;
  97. },
  98. // Map icons from Dark Sky to our icons.
  99. convertWeatherType(weatherType) {
  100. const weatherTypes = {
  101. t01d: "day-thunderstorm",
  102. t01n: "night-alt-thunderstorm",
  103. t02d: "day-thunderstorm",
  104. t02n: "night-alt-thunderstorm",
  105. t03d: "thunderstorm",
  106. t03n: "thunderstorm",
  107. t04d: "day-thunderstorm",
  108. t04n: "night-alt-thunderstorm",
  109. t05d: "day-sleet-storm",
  110. t05n: "night-alt-sleet-storm",
  111. d01d: "day-sprinkle",
  112. d01n: "night-alt-sprinkle",
  113. d02d: "day-sprinkle",
  114. d02n: "night-alt-sprinkle",
  115. d03d: "day-shower",
  116. d03n: "night-alt-shower",
  117. r01d: "day-shower",
  118. r01n: "night-alt-shower",
  119. r02d: "day-rain",
  120. r02n: "night-alt-rain",
  121. r03d: "day-rain",
  122. r03n: "night-alt-rain",
  123. r04d: "day-sprinkle",
  124. r04n: "night-alt-sprinkle",
  125. r05d: "day-shower",
  126. r05n: "night-alt-shower",
  127. r06d: "day-shower",
  128. r06n: "night-alt-shower",
  129. f01d: "day-sleet",
  130. f01n: "night-alt-sleet",
  131. s01d: "day-snow",
  132. s01n: "night-alt-snow",
  133. s02d: "day-snow-wind",
  134. s02n: "night-alt-snow-wind",
  135. s03d: "snowflake-cold",
  136. s03n: "snowflake-cold",
  137. s04d: "day-rain-mix",
  138. s04n: "night-alt-rain-mix",
  139. s05d: "day-sleet",
  140. s05n: "night-alt-sleet",
  141. s06d: "day-snow",
  142. s06n: "night-alt-snow",
  143. a01d: "day-haze",
  144. a01n: "dust",
  145. a02d: "smoke",
  146. a02n: "smoke",
  147. a03d: "day-haze",
  148. a03n: "dust",
  149. a04d: "dust",
  150. a04n: "dust",
  151. a05d: "day-fog",
  152. a05n: "night-fog",
  153. a06d: "fog",
  154. a06n: "fog",
  155. c01d: "day-sunny",
  156. c01n: "night-clear",
  157. c02d: "day-sunny-overcast",
  158. c02n: "night-alt-partly-cloudy",
  159. c03d: "day-cloudy",
  160. c03n: "night-alt-cloudy",
  161. c04d: "cloudy",
  162. c04n: "cloudy",
  163. u00d: "rain-mix",
  164. u00n: "rain-mix"
  165. };
  166. return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
  167. }
  168. });