Skip to content

Commit

Permalink
Add OpenWeather 3.0 support.
Browse files Browse the repository at this point in the history
  • Loading branch information
panaaj committed Mar 30, 2024
1 parent 6b0ddd9 commit c8b531f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion helper/alarms/alarms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
14 changes: 14 additions & 0 deletions helper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -91,6 +98,10 @@ const CONFIG_UISCHEMA = {
'ui:title': ' ',
'ui:help': ' '
},
apiVersion: {
'ui:widget': 'select',
'ui-help': ' '
},
apiKey: {
'ui:disabled': false,
'ui-help': ''
Expand Down Expand Up @@ -154,6 +165,7 @@ module.exports = (server: FreeboardHelperApp): OpenApiPlugin => {
},
weather: {
enable: false,
apiVersion: 2,
apiKey: '',
pollInterval: defaultPollInterval
},
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion helper/weather/openweather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
26 changes: 24 additions & 2 deletions helper/weather/weather-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Request, Response } from 'express';

export interface WEATHER_CONFIG {
enable: boolean;
apiVersion: number;
apiKey: string;
pollInterval: number;
}
Expand Down Expand Up @@ -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
Expand All @@ -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';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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({
Expand Down

0 comments on commit c8b531f

Please sign in to comment.