MagicMirror/modules/default/weather/weatherprovider.js

152 lines
4.6 KiB
JavaScript
Raw Normal View History

/* global Class */
/* Magic Mirror
* Module: Weather
*
2020-04-28 23:05:28 +02:00
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.
2017-10-18 13:38:56 +02:00
*
* This class is the blueprint for a weather provider.
*/
var WeatherProvider = Class.extend({
// Weather Provider Properties
providerName: null,
2020-07-30 12:58:35 +02:00
// The following properties have accessor methods.
// Try to not access them directly.
currentWeatherObject: null,
weatherForecastArray: null,
2018-12-27 17:13:06 +01:00
fetchedLocationName: null,
2019-06-05 09:46:59 +02:00
// The following properties will be set automatically.
// You do not need to overwrite these properties.
config: null,
delegate: null,
providerIdentifier: null,
// Weather Provider Methods
2019-06-05 09:46:59 +02:00
// All the following methods can be overwritten, although most are good as they are.
// Called when a weather provider is initialized.
init: function (config) {
this.config = config;
2018-12-27 19:37:02 +01:00
Log.info(`Weather provider: ${this.providerName} initialized.`);
},
// Called to set the config, this config is the same as the weather module's config.
setConfig: function (config) {
2018-12-27 19:37:02 +01:00
this.config = config;
Log.info(`Weather provider: ${this.providerName} config set.`, this.config);
},
// Called when the weather provider is about to start.
start: function () {
2018-12-27 19:37:02 +01:00
Log.info(`Weather provider: ${this.providerName} started.`);
},
// This method should start the API request to fetch the current weather.
2019-06-05 09:46:59 +02:00
// This method should definitely be overwritten in the provider.
fetchCurrentWeather: function () {
2018-12-27 19:37:02 +01:00
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchCurrentWeather method.`);
},
// This method should start the API request to fetch the weather forecast.
2019-06-05 09:46:59 +02:00
// This method should definitely be overwritten in the provider.
fetchWeatherForecast: function () {
2018-12-27 19:37:02 +01:00
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherForecast method.`);
},
// This returns a WeatherDay object for the current weather.
currentWeather: function () {
2018-12-27 19:37:02 +01:00
return this.currentWeatherObject;
},
// This returns an array of WeatherDay objects for the weather forecast.
weatherForecast: function () {
2018-12-27 19:37:02 +01:00
return this.weatherForecastArray;
},
// This returns the name of the fetched location or an empty string.
fetchedLocation: function () {
2018-12-27 19:37:02 +01:00
return this.fetchedLocationName || "";
2018-12-27 17:13:06 +01:00
},
2017-09-22 13:26:44 +02:00
// Set the currentWeather and notify the delegate that new information is available.
setCurrentWeather: function (currentWeatherObject) {
// We should check here if we are passing a WeatherDay
2018-12-27 19:37:02 +01:00
this.currentWeatherObject = currentWeatherObject;
},
2017-09-22 13:26:44 +02:00
// Set the weatherForecastArray and notify the delegate that new information is available.
setWeatherForecast: function (weatherForecastArray) {
2017-09-22 13:26:44 +02:00
// We should check here if we are passing a WeatherDay
2018-12-27 19:37:02 +01:00
this.weatherForecastArray = weatherForecastArray;
2017-09-22 13:26:44 +02:00
},
// Set the fetched location name.
setFetchedLocation: function (name) {
2018-12-27 19:37:02 +01:00
this.fetchedLocationName = name;
2018-12-27 17:13:06 +01:00
},
// Notify the delegate that new weather is available.
updateAvailable: function () {
2018-12-27 19:37:02 +01:00
this.delegate.updateAvailable(this);
},
2019-06-05 09:46:59 +02:00
// A convenience function to make requests. It returns a promise.
fetchData: function (url, method = "GET", data = null) {
return new Promise(function (resolve, reject) {
var request = new XMLHttpRequest();
request.open(method, url, true);
request.onreadystatechange = function () {
if (this.readyState === 4) {
if (this.status === 200) {
resolve(JSON.parse(this.response));
} else {
2019-06-05 10:23:58 +02:00
reject(request);
}
}
};
request.send();
2019-06-05 10:23:58 +02:00
});
}
});
/**
* Collection of registered weather providers.
*/
2018-12-27 19:37:02 +01:00
WeatherProvider.providers = [];
/**
* Static method to register a new weather provider.
*
2020-07-30 12:58:35 +02:00
* @param {string} providerIdentifier The name of the weather provider
* @param {object} providerDetails The details of the weather provider
*/
WeatherProvider.register = function (providerIdentifier, providerDetails) {
2018-12-27 19:37:02 +01:00
WeatherProvider.providers[providerIdentifier.toLowerCase()] = WeatherProvider.extend(providerDetails);
};
/**
* Static method to initialize a new weather provider.
*
2020-07-30 12:58:35 +02:00
* @param {string} providerIdentifier The name of the weather provider
* @param {object} delegate The weather module
* @returns {object} The new weather provider
*/
WeatherProvider.initialize = function (providerIdentifier, delegate) {
2018-12-27 19:37:02 +01:00
providerIdentifier = providerIdentifier.toLowerCase();
2018-12-27 19:37:02 +01:00
var provider = new WeatherProvider.providers[providerIdentifier]();
2018-12-27 19:37:02 +01:00
provider.delegate = delegate;
provider.setConfig(delegate.config);
provider.providerIdentifier = providerIdentifier;
if (!provider.providerName) {
2018-12-27 19:37:02 +01:00
provider.providerName = providerIdentifier;
}
return provider;
2018-12-27 19:37:02 +01:00
};