2017-09-21 16:38:18 +02:00
|
|
|
/* global Class */
|
|
|
|
|
2022-01-26 23:09:26 +01:00
|
|
|
/* MagicMirror²
|
2017-09-21 16:38:18 +02:00
|
|
|
* Module: Weather
|
|
|
|
*
|
2020-04-28 23:05:28 +02:00
|
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
2017-09-21 16:38:18 +02:00
|
|
|
* MIT Licensed.
|
2017-10-18 13:38:56 +02:00
|
|
|
*
|
2017-09-21 16:38:18 +02:00
|
|
|
* This class is the blueprint for a weather provider.
|
|
|
|
*/
|
2021-04-18 14:53:15 +02:00
|
|
|
const WeatherProvider = Class.extend({
|
2017-09-21 16:38:18 +02:00
|
|
|
// Weather Provider Properties
|
|
|
|
providerName: null,
|
2021-01-23 11:21:56 +01:00
|
|
|
defaults: {},
|
2017-09-21 16:38:18 +02:00
|
|
|
|
2020-07-30 12:58:35 +02:00
|
|
|
// The following properties have accessor methods.
|
2017-09-21 16:38:18 +02:00
|
|
|
// Try to not access them directly.
|
2017-09-22 12:26:47 +02:00
|
|
|
currentWeatherObject: null,
|
2017-09-21 16:38:18 +02:00
|
|
|
weatherForecastArray: null,
|
2021-01-23 10:45:55 +01:00
|
|
|
weatherHourlyArray: null,
|
2018-12-27 17:13:06 +01:00
|
|
|
fetchedLocationName: null,
|
2017-09-21 16:38:18 +02:00
|
|
|
|
2019-06-05 09:46:59 +02:00
|
|
|
// The following properties will be set automatically.
|
2017-09-21 16:38:18 +02:00
|
|
|
// 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.
|
2017-09-21 16:38:18 +02:00
|
|
|
|
|
|
|
// Called when a weather provider is initialized.
|
2020-05-11 22:22:32 +02:00
|
|
|
init: function (config) {
|
2017-09-21 16:38:18 +02:00
|
|
|
this.config = config;
|
2018-12-27 19:37:02 +01:00
|
|
|
Log.info(`Weather provider: ${this.providerName} initialized.`);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Called to set the config, this config is the same as the weather module's config.
|
2020-05-11 22:22:32 +02:00
|
|
|
setConfig: function (config) {
|
2018-12-27 19:37:02 +01:00
|
|
|
this.config = config;
|
|
|
|
Log.info(`Weather provider: ${this.providerName} config set.`, this.config);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// Called when the weather provider is about to start.
|
2020-05-11 22:22:32 +02:00
|
|
|
start: function () {
|
2018-12-27 19:37:02 +01:00
|
|
|
Log.info(`Weather provider: ${this.providerName} started.`);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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.
|
2020-05-11 22:22:32 +02:00
|
|
|
fetchCurrentWeather: function () {
|
2018-12-27 19:37:02 +01:00
|
|
|
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchCurrentWeather method.`);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// 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.
|
2020-05-11 22:22:32 +02:00
|
|
|
fetchWeatherForecast: function () {
|
2018-12-27 19:37:02 +01:00
|
|
|
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherForecast method.`);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
2021-01-23 10:45:55 +01:00
|
|
|
// This method should start the API request to fetch the weather hourly.
|
2020-06-30 02:40:41 -04:00
|
|
|
// This method should definitely be overwritten in the provider.
|
2021-01-23 10:45:55 +01:00
|
|
|
fetchWeatherHourly: function () {
|
|
|
|
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherHourly method.`);
|
2020-06-30 02:40:41 -04:00
|
|
|
},
|
|
|
|
|
2017-09-21 16:38:18 +02:00
|
|
|
// This returns a WeatherDay object for the current weather.
|
2020-05-11 22:22:32 +02:00
|
|
|
currentWeather: function () {
|
2018-12-27 19:37:02 +01:00
|
|
|
return this.currentWeatherObject;
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
// This returns an array of WeatherDay objects for the weather forecast.
|
2020-05-11 22:22:32 +02:00
|
|
|
weatherForecast: function () {
|
2018-12-27 19:37:02 +01:00
|
|
|
return this.weatherForecastArray;
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
2020-06-30 02:40:41 -04:00
|
|
|
// This returns an object containing WeatherDay object(s) depending on the type of call.
|
2021-01-23 10:45:55 +01:00
|
|
|
weatherHourly: function () {
|
|
|
|
return this.weatherHourlyArray;
|
2020-06-30 02:40:41 -04:00
|
|
|
},
|
|
|
|
|
2018-12-30 20:46:25 +01:00
|
|
|
// This returns the name of the fetched location or an empty string.
|
2020-05-11 22:22:32 +02:00
|
|
|
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.
|
2020-05-11 22:22:32 +02:00
|
|
|
setCurrentWeather: function (currentWeatherObject) {
|
2017-09-21 16:38:18 +02:00
|
|
|
// We should check here if we are passing a WeatherDay
|
2018-12-27 19:37:02 +01:00
|
|
|
this.currentWeatherObject = currentWeatherObject;
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
2017-09-22 13:26:44 +02:00
|
|
|
// Set the weatherForecastArray and notify the delegate that new information is available.
|
2020-05-11 22:22:32 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2021-01-23 10:45:55 +01:00
|
|
|
// Set the weatherHourlyArray and notify the delegate that new information is available.
|
2021-01-23 11:40:02 +01:00
|
|
|
setWeatherHourly: function (weatherHourlyArray) {
|
2021-01-23 10:45:55 +01:00
|
|
|
this.weatherHourlyArray = weatherHourlyArray;
|
2020-06-30 02:40:41 -04:00
|
|
|
},
|
|
|
|
|
2018-12-30 20:46:25 +01:00
|
|
|
// Set the fetched location name.
|
2020-05-11 22:22:32 +02:00
|
|
|
setFetchedLocation: function (name) {
|
2018-12-27 19:37:02 +01:00
|
|
|
this.fetchedLocationName = name;
|
2018-12-27 17:13:06 +01:00
|
|
|
},
|
|
|
|
|
2017-09-21 16:38:18 +02:00
|
|
|
// Notify the delegate that new weather is available.
|
2020-05-11 22:22:32 +02:00
|
|
|
updateAvailable: function () {
|
2018-12-27 19:37:02 +01:00
|
|
|
this.delegate.updateAvailable(this);
|
2017-09-21 16:38:18 +02:00
|
|
|
},
|
|
|
|
|
2019-06-05 09:46:59 +02:00
|
|
|
// A convenience function to make requests. It returns a promise.
|
2020-05-11 22:22:32 +02:00
|
|
|
fetchData: function (url, method = "GET", data = null) {
|
2021-10-16 00:05:12 +02:00
|
|
|
const getData = function (mockData) {
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
if (mockData) {
|
|
|
|
let data = mockData;
|
|
|
|
data = data.substring(1, data.length - 1);
|
|
|
|
resolve(JSON.parse(data));
|
|
|
|
} else {
|
|
|
|
const 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 {
|
|
|
|
reject(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
request.send();
|
2017-09-21 16:38:18 +02:00
|
|
|
}
|
2021-10-16 00:05:12 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
return getData(this.config.mockData);
|
2017-09-21 16:38:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Collection of registered weather providers.
|
|
|
|
*/
|
2018-12-27 19:37:02 +01:00
|
|
|
WeatherProvider.providers = [];
|
2017-09-21 16:38:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static method to register a new weather provider.
|
2020-07-27 14:24:30 +02:00
|
|
|
*
|
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
|
2017-09-21 16:38:18 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
WeatherProvider.register = function (providerIdentifier, providerDetails) {
|
2018-12-27 19:37:02 +01:00
|
|
|
WeatherProvider.providers[providerIdentifier.toLowerCase()] = WeatherProvider.extend(providerDetails);
|
|
|
|
};
|
2017-09-21 16:38:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Static method to initialize a new weather provider.
|
2020-07-27 14:24:30 +02:00
|
|
|
*
|
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
|
2017-09-21 16:38:18 +02:00
|
|
|
*/
|
2020-05-11 22:22:32 +02:00
|
|
|
WeatherProvider.initialize = function (providerIdentifier, delegate) {
|
2018-12-27 19:37:02 +01:00
|
|
|
providerIdentifier = providerIdentifier.toLowerCase();
|
2017-09-21 16:38:18 +02:00
|
|
|
|
2021-01-23 11:21:56 +01:00
|
|
|
const provider = new WeatherProvider.providers[providerIdentifier]();
|
|
|
|
const config = Object.assign({}, provider.defaults, delegate.config);
|
2017-09-21 16:38:18 +02:00
|
|
|
|
2018-12-27 19:37:02 +01:00
|
|
|
provider.delegate = delegate;
|
2021-01-23 11:21:56 +01:00
|
|
|
provider.setConfig(config);
|
2017-09-21 16:38:18 +02:00
|
|
|
|
|
|
|
provider.providerIdentifier = providerIdentifier;
|
|
|
|
if (!provider.providerName) {
|
2018-12-27 19:37:02 +01:00
|
|
|
provider.providerName = providerIdentifier;
|
2017-09-21 16:38:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return provider;
|
2018-12-27 19:37:02 +01:00
|
|
|
};
|