24h forecasting
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
import {queryHourly, getCoords, queryCurrent} from './influx.mjs';
|
import {queryHourly, getCoords, queryCurrent} from './influx.mjs';
|
||||||
import { getCelestialForecast } from './celestial.mjs';
|
import { getCelestialForecast } from './celestial.mjs';
|
||||||
|
|
||||||
let cache = { ts: 0, slots: [], summary: null }
|
export let lastForecast = { ts: 0, slots: [], summary: null }
|
||||||
const TTL = 10 * 60 * 1000
|
const TTL = 10 * 60 * 1000
|
||||||
|
|
||||||
// ── Physics Helpers ───────────────────────────────────────────────────────────
|
// ── Physics Helpers ───────────────────────────────────────────────────────────
|
||||||
@@ -224,14 +224,14 @@ function summarise(slots) {
|
|||||||
|
|
||||||
export async function refreshForecast() {
|
export async function refreshForecast() {
|
||||||
const sensors = await queryCurrent().catch(() => null)
|
const sensors = await queryCurrent().catch(() => null)
|
||||||
if (!sensors) return cache
|
if (!sensors) return lastForecast
|
||||||
const slots = await get24HourForecast(sensors).catch(() => null)
|
const slots = await get24HourForecast(sensors).catch(() => null)
|
||||||
if (!slots) return cache
|
if (!slots) return lastForecast
|
||||||
cache = { ts: Date.now(), slots, summary: summarise(slots) }
|
lastForecast = { ts: Date.now(), slots, summary: summarise(slots) }
|
||||||
return cache
|
return lastForecast
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function ensureForecast() {
|
export async function ensureForecast() {
|
||||||
if (Date.now() - cache.ts > TTL) await refreshForecast()
|
if (Date.now() - lastForecast.ts > TTL) await refreshForecast()
|
||||||
return cache
|
return lastForecast
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +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 {refreshForecast} from './forecast.mjs';
|
import {lastForecast, refreshForecast} from './forecast.mjs';
|
||||||
import {cache} from 'express/lib/application.js';
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const DIR = dirname(fileURLToPath(import.meta.url));
|
const DIR = dirname(fileURLToPath(import.meta.url));
|
||||||
@@ -186,7 +185,7 @@ let ready = false
|
|||||||
while (!ready) {
|
while (!ready) {
|
||||||
try {
|
try {
|
||||||
await refreshForecast()
|
await refreshForecast()
|
||||||
ready = cache.slots.length > 0
|
ready = lastForecast.slots.length > 0
|
||||||
} catch {}
|
} catch {}
|
||||||
if (!ready) await new Promise(r => setTimeout(r, 5000))
|
if (!ready) await new Promise(r => setTimeout(r, 5000))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user