Fixing current forecast

This commit is contained in:
2026-06-24 16:36:20 -04:00
parent d20ef97cf1
commit 83c2e655f6
2 changed files with 7 additions and 13 deletions

View File

@@ -4,7 +4,7 @@ import { getCelestialForecast } from './celestial.mjs';
import {getWeatherCondition} from './openweather.mjs'; import {getWeatherCondition} from './openweather.mjs';
export let lastForecast = { ts: 0, slots: [], summary: null } export let lastForecast = { ts: 0, slots: [], summary: null }
const TTL = 10 * 60 * 1000 export const forecastTTL = 5 * 60 * 60_000
// ── Physics Helpers ─────────────────────────────────────────────────────────── // ── Physics Helpers ───────────────────────────────────────────────────────────
@@ -234,17 +234,11 @@ function summarise(slots) {
} }
export async function getForecast() { export async function getForecast() {
if(Date.now() - lastForecast.ts < TTL) return lastForecast; if(Date.now() - lastForecast.ts < forecastTTL) return lastForecast;
const sensors = await queryCurrent().catch(() => null) const sensors = await queryCurrent().catch(() => null)
if(!sensors) { if(!sensors) return lastForecast
console.log('no sensors, bailing');
return lastForecast
}
const slots = await get24HourForecast(sensors).catch(() => null) const slots = await get24HourForecast(sensors).catch(() => null)
if(!slots) { if(!slots) return lastForecast
console.log('no slots, bailing')
return lastForecast
}
lastForecast = { ts: Date.now(), slots, summary: summarise(slots) } lastForecast = { ts: Date.now(), slots, summary: summarise(slots) }
return lastForecast return lastForecast
} }

View File

@@ -10,7 +10,7 @@ import {spec} from './spec.mjs';
import {existsSync} from 'fs'; import {existsSync} from 'fs';
import {getAirTraffic, getAirTrafficHistory, getShapes} from './airtraffic.mjs'; import {getAirTraffic, getAirTrafficHistory, getShapes} from './airtraffic.mjs';
import {getWeatherCondition} from './openweather.mjs'; import {getWeatherCondition} from './openweather.mjs';
import {lastForecast, getForecast} from './forecast.mjs'; import {lastForecast, getForecast, forecastTTL} from './forecast.mjs';
import {dailyWeather, hourlyWeather} from './openmeteo.mjs'; import {dailyWeather, hourlyWeather} from './openmeteo.mjs';
const app = express(); const app = express();
@@ -182,8 +182,8 @@ app.get('*', (req, res) => {
// ── Start ───────────────────────────────────────────────────────────────────── // ── Start ─────────────────────────────────────────────────────────────────────
const c = cfg(); const c = cfg();
setTimeout(getForecast, 2000) setTimeout(getForecast, 1)
setInterval(getForecast, 10 * 60 * 1000) setInterval(getForecast, forecastTTL)
app.listen(c.PORT, () => { app.listen(c.PORT, () => {
console.log(`🌦 Weather API — http://localhost:${c.PORT}`) console.log(`🌦 Weather API — http://localhost:${c.PORT}`)