Fixing current forecast
This commit is contained in:
@@ -4,7 +4,7 @@ import { getCelestialForecast } from './celestial.mjs';
|
||||
import {getWeatherCondition} from './openweather.mjs';
|
||||
|
||||
export let lastForecast = { ts: 0, slots: [], summary: null }
|
||||
const TTL = 10 * 60 * 1000
|
||||
export const forecastTTL = 5 * 60 * 60_000
|
||||
|
||||
// ── Physics Helpers ───────────────────────────────────────────────────────────
|
||||
|
||||
@@ -234,17 +234,11 @@ function summarise(slots) {
|
||||
}
|
||||
|
||||
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)
|
||||
if(!sensors) {
|
||||
console.log('no sensors, bailing');
|
||||
return lastForecast
|
||||
}
|
||||
if(!sensors) return lastForecast
|
||||
const slots = await get24HourForecast(sensors).catch(() => null)
|
||||
if(!slots) {
|
||||
console.log('no slots, bailing')
|
||||
return lastForecast
|
||||
}
|
||||
if(!slots) return lastForecast
|
||||
lastForecast = { ts: Date.now(), slots, summary: summarise(slots) }
|
||||
return lastForecast
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import {spec} from './spec.mjs';
|
||||
import {existsSync} from 'fs';
|
||||
import {getAirTraffic, getAirTrafficHistory, getShapes} from './airtraffic.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';
|
||||
|
||||
const app = express();
|
||||
@@ -182,8 +182,8 @@ app.get('*', (req, res) => {
|
||||
// ── Start ─────────────────────────────────────────────────────────────────────
|
||||
const c = cfg();
|
||||
|
||||
setTimeout(getForecast, 2000)
|
||||
setInterval(getForecast, 10 * 60 * 1000)
|
||||
setTimeout(getForecast, 1)
|
||||
setInterval(getForecast, forecastTTL)
|
||||
|
||||
app.listen(c.PORT, () => {
|
||||
console.log(`🌦 Weather API — http://localhost:${c.PORT}`)
|
||||
|
||||
Reference in New Issue
Block a user