UNPKG

signalk-data-age-watchdog

Version:

Signal K plugin to monitor the freshness (age) of timestamped data from any specified data paths. Triggers visual/sound notifications if data becomes stale.

83 lines (71 loc) 2.24 kB
module.exports = (app) => { let intervalId; const plugin = { id: 'signalk-data-age-watchdog', name: 'Data Age Watchdog', description: 'Computes the age of data from multiple paths and triggers alerts if data is old.', start: (settings) => { app.debug('data age watchdog plugin started'); intervalId = setInterval(() => { if (!Array.isArray(settings.paths)) return; settings.paths.forEach(path => { const outputPath = path + '.dataAge'; const value = app.getSelfPath(path); const timestamp = value?.timestamp; if (!timestamp) return; const ageSeconds = (Date.now() - new Date(timestamp).getTime()) / 1000; app.handleMessage('plugin-data-age-watchdog', { context: 'vessels.self', updates: [{ source: { label: 'data-age-watchdog' }, timestamp: new Date().toISOString(), values: [{ path: outputPath, value: ageSeconds }] }] }); if (ageSeconds > (settings?.maxAgeSeconds || 60)) { app.handleMessage('notifications', { updates: [{ values: [{ path: `notifications.${outputPath}.dataStale`, value: { state: 'alarm', method: ['visual', 'sound'], message: `Data is stale on ${path}: ${Math.round(ageSeconds)} seconds old` } }] }] }); } }); }, 1000); }, stop: () => { app.debug('data-age watchdog plugin stopped'); if (intervalId) clearInterval(intervalId); }, schema: () => ({ type: 'object', properties: { paths: { type: 'array', title: 'Paths to monitor', items: { type: 'string' }, minItems: 1, default: ['navigation.position'] }, maxAgeSeconds: { type: 'integer', title: 'Maximum allowed data age (seconds)', default: 60, minimum: 1 } } }) }; return plugin; };