Files
signalk-plugin/plugin/routes/forecasts.js
2026-03-11 15:25:03 +01:00

64 lines
2.2 KiB
JavaScript

const { getForecast, getSeaConditions } = require("../api_models/openmeteo.js");
/**
* Registers forecast-related routes.
* @param {Object} router - Route wrapper with get/post methods
* @param {Object} app - SignalK app instance
*/
function registerForecastRoutes(router, app) {
// Get current forecast data directly from OpenMeteo
router.get("/forecasts/data", async (req, res) => {
try {
const position = app.getSelfPath('navigation.position')?.value;
if (!position?.latitude || !position?.longitude) {
return res.status(503).json({ error: "Position not available" });
}
const mode = req.query.mode || 'both';
const [forecastData, wavesData] = await Promise.all([
getForecast(position, { mode }),
getSeaConditions(position, { mode })
]);
res.status(200).json({
forecast: forecastData,
sea: wavesData
});
} catch (error) {
console.error('[MEB] Error in /meb/forecasts/data:', error);
res.status(500).json({ error: error.message });
}
});
// Force update: fetch fresh hourly data from OpenMeteo
router.post("/forecasts/update", async (req, res) => {
try {
const position = app.getSelfPath('navigation.position')?.value;
if (!position?.latitude || !position?.longitude) {
return res.status(503).json({ error: "Position not available" });
}
const [forecastData, wavesData] = await Promise.all([
getForecast(position, { mode: 'both' }),
getSeaConditions(position, { mode: 'both' })
]);
if (!forecastData?.hourly || !wavesData?.hourly) {
return res.status(500).json({ error: "Hourly data not available from API" });
}
res.status(200).json({
forecast: forecastData,
sea: wavesData
});
} catch (error) {
console.error('[MEB] Error in /meb/forecasts/update:', error);
res.status(500).json({ error: error.message });
}
});
}
module.exports = registerForecastRoutes;