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.

darksky.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* global WeatherProvider, WeatherObject */
  2. /* Magic Mirror
  3. * Module: Weather
  4. * Provider: Dark Sky
  5. *
  6. * By Nicholas Hubbard https://github.com/nhubbard
  7. * MIT Licensed
  8. *
  9. * This class is a provider for Dark Sky.
  10. * Note that the Dark Sky API does not provide rainfall. Instead it provides
  11. * snowfall and precipitation probability
  12. */
  13. WeatherProvider.register("darksky", {
  14. // Set the name of the provider.
  15. // Not strictly required, but helps for debugging.
  16. providerName: "Dark Sky",
  17. // Set the default config properties that is specific to this provider
  18. defaults: {
  19. apiBase: "https://cors-anywhere.herokuapp.com/https://api.darksky.net",
  20. weatherEndpoint: "/forecast",
  21. apiKey: "",
  22. lat: 0,
  23. lon: 0
  24. },
  25. units: {
  26. imperial: "us",
  27. metric: "si"
  28. },
  29. fetchCurrentWeather() {
  30. this.fetchData(this.getUrl())
  31. .then((data) => {
  32. if (!data || !data.currently || typeof data.currently.temperature === "undefined") {
  33. // No usable data?
  34. return;
  35. }
  36. const currentWeather = this.generateWeatherDayFromCurrentWeather(data);
  37. this.setCurrentWeather(currentWeather);
  38. })
  39. .catch(function (request) {
  40. Log.error("Could not load data ... ", request);
  41. })
  42. .finally(() => this.updateAvailable());
  43. },
  44. fetchWeatherForecast() {
  45. this.fetchData(this.getUrl())
  46. .then((data) => {
  47. if (!data || !data.daily || !data.daily.data.length) {
  48. // No usable data?
  49. return;
  50. }
  51. const forecast = this.generateWeatherObjectsFromForecast(data.daily.data);
  52. this.setWeatherForecast(forecast);
  53. })
  54. .catch(function (request) {
  55. Log.error("Could not load data ... ", request);
  56. })
  57. .finally(() => this.updateAvailable());
  58. },
  59. // Create a URL from the config and base URL.
  60. getUrl() {
  61. const units = this.units[this.config.units] || "auto";
  62. return `${this.config.apiBase}${this.config.weatherEndpoint}/${this.config.apiKey}/${this.config.lat},${this.config.lon}?units=${units}&lang=${this.config.lang}`;
  63. },
  64. // Implement WeatherDay generator.
  65. generateWeatherDayFromCurrentWeather(currentWeatherData) {
  66. const currentWeather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
  67. currentWeather.date = moment();
  68. currentWeather.humidity = parseFloat(currentWeatherData.currently.humidity);
  69. currentWeather.temperature = parseFloat(currentWeatherData.currently.temperature);
  70. currentWeather.windSpeed = parseFloat(currentWeatherData.currently.windSpeed);
  71. currentWeather.windDirection = currentWeatherData.currently.windBearing;
  72. currentWeather.weatherType = this.convertWeatherType(currentWeatherData.currently.icon);
  73. currentWeather.sunrise = moment(currentWeatherData.daily.data[0].sunriseTime, "X");
  74. currentWeather.sunset = moment(currentWeatherData.daily.data[0].sunsetTime, "X");
  75. return currentWeather;
  76. },
  77. generateWeatherObjectsFromForecast(forecasts) {
  78. const days = [];
  79. for (const forecast of forecasts) {
  80. const weather = new WeatherObject(this.config.units, this.config.tempUnits, this.config.windUnits, this.config.useKmh);
  81. weather.date = moment(forecast.time, "X");
  82. weather.minTemperature = forecast.temperatureMin;
  83. weather.maxTemperature = forecast.temperatureMax;
  84. weather.weatherType = this.convertWeatherType(forecast.icon);
  85. weather.snow = 0;
  86. // The API will return centimeters if units is 'si' and will return inches for 'us'
  87. // Note that the Dark Sky API does not provide rainfall.
  88. // Instead it provides snowfall and precipitation probability
  89. if (forecast.hasOwnProperty("precipAccumulation")) {
  90. if (this.config.units === "imperial" && !isNaN(forecast.precipAccumulation)) {
  91. weather.snow = forecast.precipAccumulation;
  92. } else if (!isNaN(forecast.precipAccumulation)) {
  93. weather.snow = forecast.precipAccumulation * 10;
  94. }
  95. }
  96. weather.precipitation = weather.snow;
  97. days.push(weather);
  98. }
  99. return days;
  100. },
  101. // Map icons from Dark Sky to our icons.
  102. convertWeatherType(weatherType) {
  103. const weatherTypes = {
  104. "clear-day": "day-sunny",
  105. "clear-night": "night-clear",
  106. rain: "rain",
  107. snow: "snow",
  108. sleet: "snow",
  109. wind: "wind",
  110. fog: "fog",
  111. cloudy: "cloudy",
  112. "partly-cloudy-day": "day-cloudy",
  113. "partly-cloudy-night": "night-cloudy"
  114. };
  115. return weatherTypes.hasOwnProperty(weatherType) ? weatherTypes[weatherType] : null;
  116. }
  117. });