mirror of
https://github.com/MichMich/MagicMirror.git
synced 2026-04-24 14:57:15 +00:00
This migrates the Weather module from client-side fetching to use the server-side centralized HTTPFetcher (introduced in #4016), following the same pattern as the Calendar and Newsfeed modules. ## Motivation This brings consistent error handling and better maintainability and completes the refactoring effort to centralize HTTP error handling across all default modules. Migrating to server-side providers with HTTPFetcher brings: - **Centralized error handling**: Inherits smart retry strategies (401/403, 429, 5xx backoff) and timeout handling (30s) - **Consistency**: Same architecture as Calendar and Newsfeed modules - **Security**: Possibility to hide API keys/secrets from client-side - **Performance**: Reduced API calls in multi-client setups - one server fetch instead of one per client - **Enabling possible future features**: e.g. server-side caching, rate limit monitoring, and data sharing with third-party modules ## Changes - All 10 weather providers now use HTTPFetcher for server-side fetching - Consistent error handling like Calendar and Newsfeed modules ## Breaking Changes None. Existing configurations continue to work. ## Testing To ensure proper functionality, I obtained API keys and credentials for all providers that require them. I configured all 10 providers in a carousel setup and tested each one individually. Screenshots for each provider are attached below demonstrating their working state. I even requested developer access from the Tempest/WeatherFlow team to properly test this provider. **Comprehensive test coverage**: A major advantage of the server-side architecture is the ability to thoroughly test providers with unit tests using real API response snapshots. Don't be alarmed by the many lines added in this PR - they are primarily test files and real-data mocks that ensure provider reliability. ## Review Notes I know this is an enormous change - I've been working on this for quite some time. Unfortunately, breaking it into smaller incremental PRs wasn't feasible due to the interdependencies between providers and the shared architecture. Given the scope, it's nearly impossible to manually review every change. To ensure quality, I've used both CodeRabbit and GitHub Copilot to review the code multiple times in my fork, and both provided extensive and valuable feedback. Most importantly, my test setup with all 10 providers working successfully is very encouraging. ## Related Part of the HTTPFetcher migration #4016. ## Screenshots <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-06-54" src="https://github.com/user-attachments/assets/2139f4d2-2a9b-4e49-8d0a-e4436983ed6e" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-02" src="https://github.com/user-attachments/assets/880f7ce2-4e44-42d5-bfe4-5ce475cca7c2" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-07" src="https://github.com/user-attachments/assets/abd89933-fe03-40ab-8a7c-41ae1ff99255" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-12" src="https://github.com/user-attachments/assets/22225852-f0a9-4d33-87ab-0733ba30fad3" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-17" src="https://github.com/user-attachments/assets/7a7192a5-f237-4060-85d7-6f50b9bef5af" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-22" src="https://github.com/user-attachments/assets/df84d9f1-e531-4995-8da8-d6f2601b6a08" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-27" src="https://github.com/user-attachments/assets/4cf391ac-db43-4b52-95f4-f5eadc5ea34d" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-32" src="https://github.com/user-attachments/assets/8dd8e688-d47f-4815-87f6-7f2630f15d58" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-37" src="https://github.com/user-attachments/assets/ee84a8bc-6b35-405a-b311-88658d9268dd" /> <img width="1920" height="1080" alt="Ekrankopio de 2026-02-08 13-07-42" src="https://github.com/user-attachments/assets/f941f341-453f-4d4d-a8d9-6b9158eb2681" /> Provider "Weather API" added later: <img width="1910" height="1080" alt="Ekrankopio de 2026-02-15 19-39-06" src="https://github.com/user-attachments/assets/3f0c8ba3-105c-4f90-8b2e-3a1be543d3d2" />
134 lines
4.3 KiB
JavaScript
134 lines
4.3 KiB
JavaScript
/* global SunCalc, WeatherUtils */
|
|
|
|
/**
|
|
* @external Moment
|
|
*/
|
|
class WeatherObject {
|
|
|
|
/**
|
|
* Constructor for a WeatherObject
|
|
*/
|
|
constructor () {
|
|
this.date = null;
|
|
this.windSpeed = null;
|
|
this.windFromDirection = null;
|
|
this.sunrise = null;
|
|
this.sunset = null;
|
|
this.temperature = null;
|
|
this.minTemperature = null;
|
|
this.maxTemperature = null;
|
|
this.weatherType = null;
|
|
this.humidity = null;
|
|
this.precipitationAmount = null;
|
|
this.precipitationUnits = null;
|
|
this.precipitationProbability = null;
|
|
this.feelsLikeTemp = null;
|
|
}
|
|
|
|
cardinalWindDirection () {
|
|
if (this.windFromDirection > 11.25 && this.windFromDirection <= 33.75) {
|
|
return "NNE";
|
|
} else if (this.windFromDirection > 33.75 && this.windFromDirection <= 56.25) {
|
|
return "NE";
|
|
} else if (this.windFromDirection > 56.25 && this.windFromDirection <= 78.75) {
|
|
return "ENE";
|
|
} else if (this.windFromDirection > 78.75 && this.windFromDirection <= 101.25) {
|
|
return "E";
|
|
} else if (this.windFromDirection > 101.25 && this.windFromDirection <= 123.75) {
|
|
return "ESE";
|
|
} else if (this.windFromDirection > 123.75 && this.windFromDirection <= 146.25) {
|
|
return "SE";
|
|
} else if (this.windFromDirection > 146.25 && this.windFromDirection <= 168.75) {
|
|
return "SSE";
|
|
} else if (this.windFromDirection > 168.75 && this.windFromDirection <= 191.25) {
|
|
return "S";
|
|
} else if (this.windFromDirection > 191.25 && this.windFromDirection <= 213.75) {
|
|
return "SSW";
|
|
} else if (this.windFromDirection > 213.75 && this.windFromDirection <= 236.25) {
|
|
return "SW";
|
|
} else if (this.windFromDirection > 236.25 && this.windFromDirection <= 258.75) {
|
|
return "WSW";
|
|
} else if (this.windFromDirection > 258.75 && this.windFromDirection <= 281.25) {
|
|
return "W";
|
|
} else if (this.windFromDirection > 281.25 && this.windFromDirection <= 303.75) {
|
|
return "WNW";
|
|
} else if (this.windFromDirection > 303.75 && this.windFromDirection <= 326.25) {
|
|
return "NW";
|
|
} else if (this.windFromDirection > 326.25 && this.windFromDirection <= 348.75) {
|
|
return "NNW";
|
|
} else {
|
|
return "N";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Determines if the sun sets or rises next. Uses the current time and not
|
|
* the date from the weather-forecast.
|
|
* @param {Moment} date an optional date where you want to get the next
|
|
* action for. Useful only in tests, defaults to the current time.
|
|
* @returns {string|null} "sunset", "sunrise", or null if sun data unavailable
|
|
*/
|
|
nextSunAction (date = moment()) {
|
|
// Return null if sunrise/sunset data is unavailable
|
|
if (!this.sunrise || !this.sunset) {
|
|
return null;
|
|
}
|
|
return date.isBetween(this.sunrise, this.sunset) ? "sunset" : "sunrise";
|
|
}
|
|
|
|
feelsLike () {
|
|
if (this.feelsLikeTemp) {
|
|
return this.feelsLikeTemp;
|
|
}
|
|
return WeatherUtils.calculateFeelsLike(this.temperature, this.windSpeed, this.humidity);
|
|
}
|
|
|
|
/**
|
|
* Checks if the weatherObject is at dayTime.
|
|
* @returns {boolean} true if it is at dayTime
|
|
*/
|
|
isDayTime () {
|
|
// Default to daytime if sunrise/sunset data unavailable
|
|
if (!this.sunrise || !this.sunset) {
|
|
return true;
|
|
}
|
|
const now = !this.date ? moment() : this.date;
|
|
return now.isBetween(this.sunrise, this.sunset, undefined, "[]");
|
|
}
|
|
|
|
/**
|
|
* Update the sunrise / sunset time depending on the location. This can be
|
|
* used if your provider doesn't provide that data by itself. Then SunCalc
|
|
* is used here to calculate them according to the location.
|
|
* @param {number} lat latitude
|
|
* @param {number} lon longitude
|
|
*/
|
|
updateSunTime (lat, lon) {
|
|
const now = !this.date ? new Date() : this.date.toDate();
|
|
const times = SunCalc.getTimes(now, lat, lon);
|
|
this.sunrise = moment(times.sunrise);
|
|
this.sunset = moment(times.sunset);
|
|
}
|
|
|
|
/**
|
|
* Clone to simple object to prevent mutating and deprecation of legacy library.
|
|
*
|
|
* Before being handed to other modules, mutable values must be cloned safely.
|
|
* Especially 'moment' object is not immutable, so original 'date', 'sunrise', 'sunset' could be corrupted or changed by other modules.
|
|
* @returns {object} plained object clone of original weatherObject
|
|
*/
|
|
simpleClone () {
|
|
const toFlat = ["date", "sunrise", "sunset"];
|
|
let clone = { ...this };
|
|
for (const prop of toFlat) {
|
|
clone[prop] = clone?.[prop]?.valueOf() ?? clone?.[prop];
|
|
}
|
|
return clone;
|
|
}
|
|
}
|
|
|
|
/*************** DO NOT EDIT THE LINE BELOW ***************/
|
|
if (typeof module !== "undefined") {
|
|
module.exports = WeatherObject;
|
|
}
|