2023-09-08 01:41:20 -04:00
|
|
|
/* global Class, WeatherObject */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wrapper class to enable overrides of currentOverrideWeatherObject.
|
|
|
|
*
|
|
|
|
* Sits between the weather.js module and the provider implementations to allow us to
|
|
|
|
* combine the incoming data from the CURRENT_WEATHER_OVERRIDE notification with the
|
|
|
|
* existing data received from the current api provider. If no notifications have
|
|
|
|
* been received then the api provider's data is used.
|
|
|
|
*
|
|
|
|
* The intent is to allow partial WeatherObjects from local sensors to augment or
|
|
|
|
* replace the WeatherObjects from the api providers.
|
|
|
|
*
|
|
|
|
* This class shares the signature of WeatherProvider, and passes any methods not
|
|
|
|
* concerning the current weather directly to the api provider implementation that
|
|
|
|
* is currently in use.
|
|
|
|
*/
|
|
|
|
const OverrideWrapper = Class.extend({
|
|
|
|
baseProvider: null,
|
|
|
|
providerName: "localWrapper",
|
|
|
|
notificationWeatherObject: null,
|
|
|
|
currentOverrideWeatherObject: null,
|
|
|
|
|
2023-12-25 08:17:11 +01:00
|
|
|
init (baseProvider) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider = baseProvider;
|
|
|
|
|
|
|
|
// Binding the scope of current weather functions so any fetchData calls with
|
|
|
|
// setCurrentWeather nested in them call this classes implementation instead
|
|
|
|
// of the provider's default
|
|
|
|
this.baseProvider.setCurrentWeather = this.setCurrentWeather.bind(this);
|
|
|
|
this.baseProvider.currentWeather = this.currentWeather.bind(this);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Unchanged Api Provider Methods */
|
|
|
|
|
2023-12-25 08:17:11 +01:00
|
|
|
setConfig (config) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.setConfig(config);
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
start () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.start();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
fetchCurrentWeather () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.fetchCurrentWeather();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
fetchWeatherForecast () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.fetchWeatherForecast();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
fetchWeatherHourly () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.fetchEatherHourly();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
weatherForecast () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.weatherForecast();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
weatherHourly () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.weatherHourly();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
fetchedLocation () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.fetchedLocation();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
setWeatherForecast (weatherForecastArray) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.setWeatherForecast(weatherForecastArray);
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
setWeatherHourly (weatherHourlyArray) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.setWeatherHourly(weatherHourlyArray);
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
setFetchedLocation (name) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.setFetchedLocation(name);
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
updateAvailable () {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.updateAvailable();
|
|
|
|
},
|
2023-12-25 08:17:11 +01:00
|
|
|
async fetchData (url, type = "json", requestHeaders = undefined, expectedResponseHeaders = undefined) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.baseProvider.fetchData(url, type, requestHeaders, expectedResponseHeaders);
|
|
|
|
},
|
|
|
|
|
|
|
|
/* Override Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override to return this scope's
|
|
|
|
* @returns {WeatherObject} The current weather object. May or may not contain overridden data.
|
|
|
|
*/
|
2023-12-25 08:17:11 +01:00
|
|
|
currentWeather () {
|
2023-09-08 01:41:20 -04:00
|
|
|
return this.currentOverrideWeatherObject;
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Override to combine the overrideWeatherObejct provided in the
|
|
|
|
* notificationReceived method with the currentOverrideWeatherObject provided by the
|
|
|
|
* api provider fetchData implementation.
|
|
|
|
* @param {WeatherObject} currentWeatherObject - the api provider weather object
|
|
|
|
*/
|
2023-12-25 08:17:11 +01:00
|
|
|
setCurrentWeather (currentWeatherObject) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.currentOverrideWeatherObject = Object.assign(currentWeatherObject, this.notificationWeatherObject);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the overrideWeatherObject, calls setCurrentWeather to combine it with
|
|
|
|
* the existing current weather object provided by the base provider, and signals
|
|
|
|
* that an update is ready.
|
|
|
|
* @param {WeatherObject} payload - the weather object received from the CURRENT_WEATHER_OVERRIDE
|
|
|
|
* notification. Represents information to augment the
|
|
|
|
* existing currentOverrideWeatherObject with.
|
|
|
|
*/
|
2023-12-25 08:17:11 +01:00
|
|
|
notificationReceived (payload) {
|
2023-09-08 01:41:20 -04:00
|
|
|
this.notificationWeatherObject = payload;
|
|
|
|
|
|
|
|
// setCurrentWeather combines the newly received notification weather with
|
|
|
|
// the existing weather object we return for current weather
|
|
|
|
this.setCurrentWeather(this.currentOverrideWeatherObject);
|
|
|
|
this.updateAvailable();
|
|
|
|
}
|
|
|
|
});
|