Fixing current forecast
This commit is contained in:
@@ -231,11 +231,13 @@ function summarise(slots) {
|
||||
}
|
||||
}
|
||||
|
||||
export async function refreshForecast() {
|
||||
const sensors = await queryCurrent().catch(() => null)
|
||||
if (!sensors) return lastForecast
|
||||
const slots = await get24HourForecast(sensors).catch(() => null)
|
||||
if (!slots) return lastForecast
|
||||
lastForecast = { ts: Date.now(), slots, summary: summarise(slots) }
|
||||
export async function getForecast() {
|
||||
if(Date.now() - lastForecast.ts >= TTL) {
|
||||
const sensors = await queryCurrent().catch(() => null)
|
||||
if(!sensors) return lastForecast
|
||||
const slots = await get24HourForecast(sensors).catch(() => null)
|
||||
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, refreshForecast} from './forecast.mjs';
|
||||
import {lastForecast, getForecast} from './forecast.mjs';
|
||||
import {dailyWeather, hourlyWeather} from './openmeteo.mjs';
|
||||
|
||||
const app = express();
|
||||
@@ -45,13 +45,14 @@ app.get('/api/current', async (req, res) => {
|
||||
const condition = getWeatherCondition(Object.assign(sensors, space))
|
||||
|
||||
// Merge forecast summary fields — sensor readings take priority
|
||||
console.log(lastForecast?.summary);
|
||||
const forecast = getForecast();
|
||||
console.log(forecast?.summary);
|
||||
const merged = {
|
||||
...(lastForecast?.summary ? {
|
||||
precipitation_chance: lastForecast.summary.precipitation_chance,
|
||||
temperature_max: lastForecast.summary.temperature_max,
|
||||
temperature_min: lastForecast.summary.temperature_min,
|
||||
uv_index_max: lastForecast.summary.uv_index,
|
||||
...(forecast?.summary ? {
|
||||
precipitation_chance: forecast.summary.precipitation_chance,
|
||||
temperature_max: forecast.summary.temperature_max,
|
||||
temperature_min: forecast.summary.temperature_min,
|
||||
uv_index_max: forecast.summary.uv_index,
|
||||
} : {}),
|
||||
...condition,
|
||||
...sensors,
|
||||
@@ -181,8 +182,8 @@ app.get('*', (req, res) => {
|
||||
// ── Start ─────────────────────────────────────────────────────────────────────
|
||||
const c = cfg();
|
||||
|
||||
refreshForecast()
|
||||
setInterval(refreshForecast, 10 * 60 * 1000)
|
||||
setTimeout(getForecast, 2000)
|
||||
setInterval(getForecast, 10 * 60 * 1000)
|
||||
|
||||
app.listen(c.PORT, () => {
|
||||
console.log(`🌦 Weather API — http://localhost:${c.PORT}`)
|
||||
|
||||
Reference in New Issue
Block a user