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