diff --git a/helper/alarms/alarms.ts b/helper/alarms/alarms.ts index 36686b19..464ac47b 100644 --- a/helper/alarms/alarms.ts +++ b/helper/alarms/alarms.ts @@ -172,7 +172,7 @@ const handlePutAlarmState = ( }; const buildAlarmMessage = (message: string) => { - const pos: {value: Position} = server.getSelfPath('navigation.position'); + const pos: { value: Position } = server.getSelfPath('navigation.position'); return { message: message, data: { diff --git a/helper/index.ts b/helper/index.ts index 437378cb..a137de24 100644 --- a/helper/index.ts +++ b/helper/index.ts @@ -41,6 +41,13 @@ const CONFIG_SCHEMA = { title: 'Enable Weather', description: ' ' }, + apiVersion: { + type: 'number', + title: 'API Version', + default: 2, + enum: [2, 3], + description: 'Note: v2 API not supported after April 2024!' + }, apiKey: { type: 'string', title: 'API Key', @@ -91,6 +98,10 @@ const CONFIG_UISCHEMA = { 'ui:title': ' ', 'ui:help': ' ' }, + apiVersion: { + 'ui:widget': 'select', + 'ui-help': ' ' + }, apiKey: { 'ui:disabled': false, 'ui-help': '' @@ -154,6 +165,7 @@ module.exports = (server: FreeboardHelperApp): OpenApiPlugin => { }, weather: { enable: false, + apiVersion: 2, apiKey: '', pollInterval: defaultPollInterval }, @@ -194,10 +206,12 @@ module.exports = (server: FreeboardHelperApp): OpenApiPlugin => { settings.weather = options.weather ?? { enable: false, + apiVersion: 2, apiKey: '', pollInterval: defaultPollInterval }; settings.weather.enable = options.weather.enable ?? false; + settings.weather.apiVersion = options.weather.apiVersion ?? 2; settings.weather.apiKey = options.weather.apiKey ?? ''; settings.weather.pollInterval = options.weather.pollInterval ?? defaultPollInterval; diff --git a/helper/weather/openweather.ts b/helper/weather/openweather.ts index e091104e..20bd2663 100644 --- a/helper/weather/openweather.ts +++ b/helper/weather/openweather.ts @@ -104,7 +104,9 @@ export class OpenWeather implements IWeatherService { } private getUrl(position: Position): string { - const api = 'https://api.openweathermap.org/data/2.5/onecall'; + const v2 = 'https://api.openweathermap.org/data/2.5/onecall'; + const v3 = 'https://api.openweathermap.org/data/3.0/onecall'; + const api = this.settings.apiVersion === 3 ? v3 : v2; if (!this.settings.apiKey || !position) { return ''; } else { diff --git a/helper/weather/weather-service.ts b/helper/weather/weather-service.ts index 8cf5780c..48f9ce78 100644 --- a/helper/weather/weather-service.ts +++ b/helper/weather/weather-service.ts @@ -11,6 +11,7 @@ import { Request, Response } from 'express'; export interface WEATHER_CONFIG { enable: boolean; + apiVersion: number; apiKey: string; pollInterval: number; } @@ -139,6 +140,7 @@ let server: FreeboardHelperApp; let pluginId: string; const wakeInterval = 60000; +let lastWake: number; // last wake time let lastFetch: number; // last successful fetch let fetchInterval = 3600000; // 1hr // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -165,9 +167,13 @@ export const initWeather = ( ) => { server = app; pluginId = id; - fetchInterval = config.pollInterval * 60000; + fetchInterval = (config.pollInterval ?? 60) * 60000; + if (isNaN(fetchInterval)) { + fetchInterval = 60 * 60000; + } server.debug(`*** Weather: settings: ${JSON.stringify(config)}`); + server.debug(`*** fetchInterval: ${fetchInterval}`); weatherService = new OpenWeather(config); weatherServiceName = 'openweather'; @@ -384,9 +390,24 @@ const fetchWeatherData = () => { //server.debug(JSON.stringify(data)); retryCount = 0; lastFetch = Date.now(); + lastWake = Date.now(); weatherData = data; timer = setInterval(() => { server.debug(`*** Weather: wake from sleep....poll provider.`); + const dt = Date.now() - lastWake; + // check for runaway timer + if (dt >= 50000) { + server.debug('Wake timer watchdog -> OK'); + server.debug(`*** Weather: Polling provider.`); + } else { + server.debug( + 'Wake timer watchdog -> NOT OK... Stopping wake timer!' + ); + server.debug(`Watch interval < 50 secs. (${dt / 1000} secs)`); + clearInterval(timer); + server.setPluginError('Weather watch timer error!'); + } + lastWake = Date.now(); fetchWeatherData(); }, wakeInterval); emitMeteoDeltas(); @@ -682,7 +703,8 @@ const buildObservationMetas = (pathRoot: string) => { metas.push({ path: `${pathRoot}.outside.horizontalVisibilityOverRange`, value: { - description: 'Visibilty distance is greater than the range of the measuring equipment.' + description: + 'Visibilty distance is greater than the range of the measuring equipment.' } }); metas.push({