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.

weatherprovider.js 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* global Class */
  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 weather provider.
  9. */
  10. const WeatherProvider = Class.extend({
  11. // Weather Provider Properties
  12. providerName: null,
  13. defaults: {},
  14. // The following properties have accessor methods.
  15. // Try to not access them directly.
  16. currentWeatherObject: null,
  17. weatherForecastArray: null,
  18. weatherHourlyArray: null,
  19. fetchedLocationName: null,
  20. // The following properties will be set automatically.
  21. // You do not need to overwrite these properties.
  22. config: null,
  23. delegate: null,
  24. providerIdentifier: null,
  25. // Weather Provider Methods
  26. // All the following methods can be overwritten, although most are good as they are.
  27. // Called when a weather provider is initialized.
  28. init: function (config) {
  29. this.config = config;
  30. Log.info(`Weather provider: ${this.providerName} initialized.`);
  31. },
  32. // Called to set the config, this config is the same as the weather module's config.
  33. setConfig: function (config) {
  34. this.config = config;
  35. Log.info(`Weather provider: ${this.providerName} config set.`, this.config);
  36. },
  37. // Called when the weather provider is about to start.
  38. start: function () {
  39. Log.info(`Weather provider: ${this.providerName} started.`);
  40. },
  41. // This method should start the API request to fetch the current weather.
  42. // This method should definitely be overwritten in the provider.
  43. fetchCurrentWeather: function () {
  44. Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchCurrentWeather method.`);
  45. },
  46. // This method should start the API request to fetch the weather forecast.
  47. // This method should definitely be overwritten in the provider.
  48. fetchWeatherForecast: function () {
  49. Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherForecast method.`);
  50. },
  51. // This method should start the API request to fetch the weather hourly.
  52. // This method should definitely be overwritten in the provider.
  53. fetchWeatherHourly: function () {
  54. Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherHourly method.`);
  55. },
  56. // This returns a WeatherDay object for the current weather.
  57. currentWeather: function () {
  58. return this.currentWeatherObject;
  59. },
  60. // This returns an array of WeatherDay objects for the weather forecast.
  61. weatherForecast: function () {
  62. return this.weatherForecastArray;
  63. },
  64. // This returns an object containing WeatherDay object(s) depending on the type of call.
  65. weatherHourly: function () {
  66. return this.weatherHourlyArray;
  67. },
  68. // This returns the name of the fetched location or an empty string.
  69. fetchedLocation: function () {
  70. return this.fetchedLocationName || "";
  71. },
  72. // Set the currentWeather and notify the delegate that new information is available.
  73. setCurrentWeather: function (currentWeatherObject) {
  74. // We should check here if we are passing a WeatherDay
  75. this.currentWeatherObject = currentWeatherObject;
  76. },
  77. // Set the weatherForecastArray and notify the delegate that new information is available.
  78. setWeatherForecast: function (weatherForecastArray) {
  79. // We should check here if we are passing a WeatherDay
  80. this.weatherForecastArray = weatherForecastArray;
  81. },
  82. // Set the weatherHourlyArray and notify the delegate that new information is available.
  83. setWeatherHourly: function (weatherHourlyArray) {
  84. this.weatherHourlyArray = weatherHourlyArray;
  85. },
  86. // Set the fetched location name.
  87. setFetchedLocation: function (name) {
  88. this.fetchedLocationName = name;
  89. },
  90. // Notify the delegate that new weather is available.
  91. updateAvailable: function () {
  92. this.delegate.updateAvailable(this);
  93. },
  94. // A convenience function to make requests. It returns a promise.
  95. fetchData: function (url, method = "GET", data = null) {
  96. return new Promise(function (resolve, reject) {
  97. const request = new XMLHttpRequest();
  98. request.open(method, url, true);
  99. request.onreadystatechange = function () {
  100. if (this.readyState === 4) {
  101. if (this.status === 200) {
  102. resolve(JSON.parse(this.response));
  103. } else {
  104. reject(request);
  105. }
  106. }
  107. };
  108. request.send();
  109. });
  110. }
  111. });
  112. /**
  113. * Collection of registered weather providers.
  114. */
  115. WeatherProvider.providers = [];
  116. /**
  117. * Static method to register a new weather provider.
  118. *
  119. * @param {string} providerIdentifier The name of the weather provider
  120. * @param {object} providerDetails The details of the weather provider
  121. */
  122. WeatherProvider.register = function (providerIdentifier, providerDetails) {
  123. WeatherProvider.providers[providerIdentifier.toLowerCase()] = WeatherProvider.extend(providerDetails);
  124. };
  125. /**
  126. * Static method to initialize a new weather provider.
  127. *
  128. * @param {string} providerIdentifier The name of the weather provider
  129. * @param {object} delegate The weather module
  130. * @returns {object} The new weather provider
  131. */
  132. WeatherProvider.initialize = function (providerIdentifier, delegate) {
  133. providerIdentifier = providerIdentifier.toLowerCase();
  134. const provider = new WeatherProvider.providers[providerIdentifier]();
  135. const config = Object.assign({}, provider.defaults, delegate.config);
  136. provider.delegate = delegate;
  137. provider.setConfig(config);
  138. provider.providerIdentifier = providerIdentifier;
  139. if (!provider.providerName) {
  140. provider.providerName = providerIdentifier;
  141. }
  142. return provider;
  143. };