Graph fixes
This commit is contained in:
@@ -2,10 +2,17 @@ import {queryHourly, getCoords, queryCurrent} from './influx.mjs';
|
||||
import {getCelestialCurrent} from './celestial.mjs';
|
||||
import {getWeatherCondition} from './openweather.mjs';
|
||||
import {localDateStr} from './config.mjs';
|
||||
import {uvLabel} from './openmeteo.mjs';
|
||||
|
||||
export let lastForecast = { ts: 0, slots: [], summary: null }
|
||||
export const forecastTTL = 5 * 60 * 60_000
|
||||
|
||||
function estimateUV(sunElevation, cloudFraction) {
|
||||
if (sunElevation <= 0) return 0;
|
||||
const clearSkyUV = Math.sin((sunElevation * Math.PI) / 180) * 12;
|
||||
return Math.round(clearSkyUV * (1 - cloudFraction * 0.75) * 10) / 10;
|
||||
}
|
||||
|
||||
function linearTrend(values) {
|
||||
const n = values.length;
|
||||
if (n < 2) return 0;
|
||||
@@ -117,6 +124,7 @@ export async function get24HourForecast(currentSensors) {
|
||||
const temperature = Math.round(bgTemp * 10) / 10;
|
||||
const humidity = forecastHumidity(seedAbsHum, temperature);
|
||||
const wind_speed = forecastWind(seedWind, pressureTrend);
|
||||
const uv_index = estimateUV(celestial.sun_elevation ?? 0, clouds);
|
||||
|
||||
const celestial = getCelestialCurrent(coords.latitude, coords.longitude, time);
|
||||
const snapshot = {
|
||||
@@ -131,6 +139,8 @@ export async function get24HourForecast(currentSensors) {
|
||||
wind_speed,
|
||||
clouds,
|
||||
precipitation_chance: Math.round(weather.precipChance * (1 - i * 0.02) * 100),
|
||||
uv_index,
|
||||
uv_index_label: uvLabel(uv_index),
|
||||
...celestial
|
||||
};
|
||||
|
||||
|
||||
@@ -80,7 +80,7 @@ function absoluteHumidity(tempC, humidity) {
|
||||
return Math.round((humidity / 100 * svp * 2165) / (tempC + 273.15) * 1000) / 1000
|
||||
}
|
||||
|
||||
function uvLabel(uv) {
|
||||
export function uvLabel(uv) {
|
||||
if (uv < 3) return 'Low'
|
||||
if (uv < 6) return 'Moderate'
|
||||
if (uv < 8) return 'High'
|
||||
|
||||
@@ -56,15 +56,13 @@ app.get('/api/current', async (req, res) => {
|
||||
const space = getCelestialCurrent(coords.latitude, coords.longitude)
|
||||
const condition = getWeatherCondition(Object.assign(sensors, space))
|
||||
|
||||
console.time();
|
||||
const forecast = await getForecast();
|
||||
console.timeEnd();
|
||||
console.log(forecast);
|
||||
const merged = {
|
||||
...(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,
|
||||
|
||||
Reference in New Issue
Block a user