mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-06-27 19:53:36 +00:00
# Added Yr.no as a weather provider Yr.no is a free Norwegian weather service. The configuration is quite simple: ```js { weatherProvider: "yr", lat: 59.9171, lon: 10.7276, altitude: 30 } ``` The latitude and longitude cannot have more than 4 decimals, but that should be plenty. To quote yr: "There is no need to ask for weather forecasts with nanometer precision!". The altitude should be meters above sea level and defaults to 0. If `type` is set to `current` the symbol can display the next 1, 6 or 12 hours by setting `currentForecastHours` (default is 1). It states in [Getting started-guide](https://developer.yr.no/doc/GettingStarted/) that users of the API should cache the results and use the `Expires`-header to know when to ask for new data. By using the `If-Modified-Since`-header we can avoid downloading the same data over and over again. I chose not to override the `User-Agent`-header set in [`server.js`](https://github.com/MichMich/MagicMirror/blob/a328ce5/js/server.js#L97) even though it does not comply with [the terms of service](https://developer.yr.no/doc/TermsOfService/). It currently works with the default header, and by searching the web for MagicMirror the GitHub-repo should be easy to find without an explicit link. I also had to make some minor changes to `server.js` and `weatherprovider.js` to be able to send and return HTTP headers. To handle the HTTP 304 response without body I chose to return `undefined` so we easily can use the response as a condition: `if (response) ...`. The documentation for the API is available here: - [API Reference overview](https://api.met.no/weatherapi/) - [Locationforecast](https://api.met.no/weatherapi/locationforecast/2.0/) - Used to get the weather forecast - [Sunrise](https://api.met.no/weatherapi/sunrise/2.0/documentation) - used to find sunrise and sunset times Co-authored-by: Veeck <github@veeck.de>
172 lines
5.7 KiB
JavaScript
172 lines
5.7 KiB
JavaScript
/* global Class, performWebRequest */
|
|
|
|
/* MagicMirror²
|
|
* Module: Weather
|
|
*
|
|
* By Michael Teeuw https://michaelteeuw.nl
|
|
* MIT Licensed.
|
|
*
|
|
* This class is the blueprint for a weather provider.
|
|
*/
|
|
const WeatherProvider = Class.extend({
|
|
// Weather Provider Properties
|
|
providerName: null,
|
|
defaults: {},
|
|
|
|
// The following properties have accessor methods.
|
|
// Try to not access them directly.
|
|
currentWeatherObject: null,
|
|
weatherForecastArray: null,
|
|
weatherHourlyArray: null,
|
|
fetchedLocationName: null,
|
|
|
|
// The following properties will be set automatically.
|
|
// You do not need to overwrite these properties.
|
|
config: null,
|
|
delegate: null,
|
|
providerIdentifier: null,
|
|
|
|
// Weather Provider Methods
|
|
// 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;
|
|
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) {
|
|
this.config = config;
|
|
Log.info(`Weather provider: ${this.providerName} config set.`, this.config);
|
|
},
|
|
|
|
// Called when the weather provider is about to start.
|
|
start: function () {
|
|
Log.info(`Weather provider: ${this.providerName} started.`);
|
|
},
|
|
|
|
// This method should start the API request to fetch the current weather.
|
|
// This method should definitely be overwritten in the provider.
|
|
fetchCurrentWeather: function () {
|
|
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchCurrentWeather method.`);
|
|
},
|
|
|
|
// This method should start the API request to fetch the weather forecast.
|
|
// This method should definitely be overwritten in the provider.
|
|
fetchWeatherForecast: function () {
|
|
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherForecast method.`);
|
|
},
|
|
|
|
// This method should start the API request to fetch the weather hourly.
|
|
// This method should definitely be overwritten in the provider.
|
|
fetchWeatherHourly: function () {
|
|
Log.warn(`Weather provider: ${this.providerName} does not subclass the fetchWeatherHourly method.`);
|
|
},
|
|
|
|
// This returns a WeatherDay object for the current weather.
|
|
currentWeather: function () {
|
|
return this.currentWeatherObject;
|
|
},
|
|
|
|
// This returns an array of WeatherDay objects for the weather forecast.
|
|
weatherForecast: function () {
|
|
return this.weatherForecastArray;
|
|
},
|
|
|
|
// This returns an object containing WeatherDay object(s) depending on the type of call.
|
|
weatherHourly: function () {
|
|
return this.weatherHourlyArray;
|
|
},
|
|
|
|
// This returns the name of the fetched location or an empty string.
|
|
fetchedLocation: function () {
|
|
return this.fetchedLocationName || "";
|
|
},
|
|
|
|
// 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
|
|
this.currentWeatherObject = currentWeatherObject;
|
|
},
|
|
|
|
// Set the weatherForecastArray and notify the delegate that new information is available.
|
|
setWeatherForecast: function (weatherForecastArray) {
|
|
// We should check here if we are passing a WeatherDay
|
|
this.weatherForecastArray = weatherForecastArray;
|
|
},
|
|
|
|
// Set the weatherHourlyArray and notify the delegate that new information is available.
|
|
setWeatherHourly: function (weatherHourlyArray) {
|
|
this.weatherHourlyArray = weatherHourlyArray;
|
|
},
|
|
|
|
// Set the fetched location name.
|
|
setFetchedLocation: function (name) {
|
|
this.fetchedLocationName = name;
|
|
},
|
|
|
|
// Notify the delegate that new weather is available.
|
|
updateAvailable: function () {
|
|
this.delegate.updateAvailable(this);
|
|
},
|
|
|
|
/**
|
|
* A convenience function to make requests.
|
|
*
|
|
* @param {string} url the url to fetch from
|
|
* @param {string} type what contenttype to expect in the response, can be "json" or "xml"
|
|
* @param {Array.<{name: string, value:string}>} requestHeaders the HTTP headers to send
|
|
* @param {Array.<string>} expectedResponseHeaders the expected HTTP headers to recieve
|
|
* @returns {Promise} resolved when the fetch is done
|
|
*/
|
|
fetchData: async function (url, type = "json", requestHeaders = undefined, expectedResponseHeaders = undefined) {
|
|
const mockData = this.config.mockData;
|
|
if (mockData) {
|
|
const data = mockData.substring(1, mockData.length - 1);
|
|
return JSON.parse(data);
|
|
}
|
|
const useCorsProxy = typeof this.config.useCorsProxy !== "undefined" && this.config.useCorsProxy;
|
|
return performWebRequest(url, type, useCorsProxy, requestHeaders, expectedResponseHeaders);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Collection of registered weather providers.
|
|
*/
|
|
WeatherProvider.providers = [];
|
|
|
|
/**
|
|
* Static method to register a new weather provider.
|
|
*
|
|
* @param {string} providerIdentifier The name of the weather provider
|
|
* @param {object} providerDetails The details of the weather provider
|
|
*/
|
|
WeatherProvider.register = function (providerIdentifier, providerDetails) {
|
|
WeatherProvider.providers[providerIdentifier.toLowerCase()] = WeatherProvider.extend(providerDetails);
|
|
};
|
|
|
|
/**
|
|
* Static method to initialize a new weather provider.
|
|
*
|
|
* @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) {
|
|
providerIdentifier = providerIdentifier.toLowerCase();
|
|
|
|
const provider = new WeatherProvider.providers[providerIdentifier]();
|
|
const config = Object.assign({}, provider.defaults, delegate.config);
|
|
|
|
provider.delegate = delegate;
|
|
provider.setConfig(config);
|
|
|
|
provider.providerIdentifier = providerIdentifier;
|
|
if (!provider.providerName) {
|
|
provider.providerName = providerIdentifier;
|
|
}
|
|
|
|
return provider;
|
|
};
|