Forecast fixes
This commit is contained in:
@@ -186,16 +186,6 @@ function moonriseMoonset(lat, lon, date = new Date()) {
|
||||
return {moonrise: moonrise, moonset: moonset};
|
||||
}
|
||||
|
||||
function nextSolsticeEquinox(date = new Date()) {
|
||||
const y = date.getFullYear();
|
||||
return {
|
||||
summer_solstice: new Date(Date.UTC(y, 5, 21)),
|
||||
winter_solstice: new Date(Date.UTC(y, 11, 21)),
|
||||
vernal_equinox: new Date(Date.UTC(y, 2, 20)),
|
||||
autumnal_equinox: new Date(Date.UTC(y + 1, 2, 20)),
|
||||
};
|
||||
}
|
||||
|
||||
export function getCelestialCurrent(lat, lon, date = new Date()) {
|
||||
const data = {
|
||||
...sunPosition(lat, lon, date),
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// forecast.mjs
|
||||
import {queryHourly, getCoords, queryCurrent} from './influx.mjs';
|
||||
import {getCelestialCurrent, getCelestialForecast} from './celestial.mjs';
|
||||
import {getCelestialCurrent} from './celestial.mjs';
|
||||
import {getWeatherCondition} from './openweather.mjs';
|
||||
|
||||
export let lastForecast = { ts: 0, slots: [], summary: null }
|
||||
|
||||
@@ -102,40 +102,25 @@ app.get('/api/hourly', async (req, res) => {
|
||||
|
||||
app.get('/api/daily', async (req, res) => {
|
||||
const { fields } = req.query
|
||||
const now = new Date()
|
||||
const now = new Date()
|
||||
now.setHours(0, 0, 0, 0)
|
||||
const next = new Date(now); next.setDate(now.getDate() + 1)
|
||||
|
||||
const next = new Date(now); next.setDate(now.getDate() + 1)
|
||||
const start = req.query.start ? new Date(req.query.start) : now
|
||||
const end = req.query.end ? new Date(req.query.end) : next
|
||||
const coords = await getCoords()
|
||||
|
||||
// Does the requested window overlap with today (the 24h forecast window)?
|
||||
const todayStr = now
|
||||
const usePhysics = lastForecast.summary && start <= next && end >= now
|
||||
|
||||
// Past influx history (days before today)
|
||||
const [sensorResult] = await Promise.allSettled([
|
||||
start < now ? queryDaily(start, now) : Promise.resolve([])
|
||||
])
|
||||
const todayStr = now.toISOString().slice(0, 10) // ✅ string for comparison
|
||||
const usePhysics = lastForecast.summary && start <= now && end > now // ✅ only when today is in window
|
||||
const [sensorResult] = await Promise.allSettled([start < now ? queryDaily(start, now) : Promise.resolve([])])
|
||||
const history = sensorResult.status === 'fulfilled' ? sensorResult.value : []
|
||||
|
||||
// Future meteo (days after today)
|
||||
const meteoStart = end > next ? next : null
|
||||
const [meteoResult] = await Promise.allSettled([
|
||||
meteoStart && end > next ? dailyWeather(coords.latitude, coords.longitude, next, end) : Promise.resolve([])
|
||||
])
|
||||
// ✅ meteo starts from whichever is later: next or start
|
||||
const meteoFrom = new Date(Math.max(next.getTime(), start.getTime()))
|
||||
const [meteoResult] = await Promise.allSettled([end > next ? dailyWeather(coords.latitude, coords.longitude, meteoFrom, end) : Promise.resolve([])])
|
||||
const meteo = meteoResult.status === 'fulfilled' ? meteoResult.value : []
|
||||
|
||||
// Splice in physics summary for today, skip any meteo row that duplicates it
|
||||
const todaySlot = usePhysics ? [lastForecast.summary] : []
|
||||
const meteoClean = meteo.filter(r => r.time !== todayStr)
|
||||
|
||||
const daily = getCelestialForecast(
|
||||
coords.latitude, coords.longitude,
|
||||
[...history, ...todaySlot, ...meteoClean]
|
||||
)
|
||||
const meteoClean = meteo.filter(r => String(r.time).slice(0, 10) !== todayStr) // ✅ safe string compare
|
||||
|
||||
const daily = getCelestialForecast(coords.latitude, coords.longitude, [...history, ...todaySlot, ...meteoClean])
|
||||
res.json(filterArr(daily, fields))
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user