in die DB nur alle 5min schreiben, deploy.sh dazu
This commit is contained in:
63
wetter.js
63
wetter.js
@@ -18,6 +18,7 @@ import { readArchiveSince, connectStation, fetchLoopData } from "./davis.js";
|
||||
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
||||
const DB_PATH = process.env.DB_PATH ?? path.join(__dirname, "wetter.db");
|
||||
const LOOP_INTERVAL_MS = Number(process.env.LOOP_INTERVAL_MS ?? 30_000);
|
||||
const DB_INTERVAL_MS = 5 * 60 * 1000; // alle 5 min in DB schreiben
|
||||
const POST_URL = process.env.POST_URL ?? null;
|
||||
|
||||
// ── Hilfsfunktionen ────────────────────────────────────────────────────────
|
||||
@@ -28,6 +29,40 @@ function log(msg) { console.log (`[${fmt24h(new Date())}] ${msg}`); }
|
||||
function warn(msg) { console.warn(`[${fmt24h(new Date())}] WARN ${msg}`); }
|
||||
function err(msg) { console.error(`[${fmt24h(new Date())}] ERROR ${msg}`); }
|
||||
|
||||
// ── 5-Minuten-Aggregation ──────────────────────────────────────────────────
|
||||
|
||||
function aggregateBuffer(buf) {
|
||||
const avg = (key) => {
|
||||
const vals = buf.map(r => r[key]).filter(v => v !== null && v !== undefined);
|
||||
if (!vals.length) return null;
|
||||
return +( vals.reduce((a, b) => a + b, 0) / vals.length ).toFixed(1);
|
||||
};
|
||||
const peak = (key) => {
|
||||
const vals = buf.map(r => r[key]).filter(v => v !== null && v !== undefined);
|
||||
return vals.length ? +Math.max(...vals).toFixed(1) : null;
|
||||
};
|
||||
const last = (key) => {
|
||||
for (let i = buf.length - 1; i >= 0; i--)
|
||||
if (buf[i][key] !== null && buf[i][key] !== undefined) return buf[i][key];
|
||||
return null;
|
||||
};
|
||||
return {
|
||||
time: buf[buf.length - 1].time,
|
||||
tempOut: avg('tempOut'),
|
||||
tempIn: avg('tempIn'),
|
||||
humOut: avg('humOut'),
|
||||
humIn: avg('humIn'),
|
||||
windAvg: avg('windAvg'),
|
||||
windGust: peak('windGust'), // Spitzenwert der Periode
|
||||
windDir: avg('windDir'),
|
||||
pressure: avg('pressure'),
|
||||
barTrend: last('barTrend'),
|
||||
forecast: last('forecast'),
|
||||
rain: last('rain'),
|
||||
rainRate: avg('rainRate'),
|
||||
};
|
||||
}
|
||||
|
||||
// ── Archiv nachladen ───────────────────────────────────────────────────────
|
||||
|
||||
async function catchUpArchive(db) {
|
||||
@@ -55,12 +90,24 @@ async function catchUpArchive(db) {
|
||||
|
||||
const inserted = insertRecords(db, records, "archive");
|
||||
log(`Archiv: ${inserted} neue Datensätze gespeichert (${records.length} empfangen).`);
|
||||
|
||||
if (POST_URL) {
|
||||
for (const r of records) {
|
||||
fetch(POST_URL, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(r),
|
||||
}).catch(e => warn("Archiv-POST fehlgeschlagen: " + e.message));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── LOOP-Schleife ──────────────────────────────────────────────────────────
|
||||
|
||||
async function runLoop(db) {
|
||||
let station = null;
|
||||
let station = null;
|
||||
const buffer = [];
|
||||
let lastDbWrite = Date.now();
|
||||
|
||||
async function connect() {
|
||||
station = await connectStation();
|
||||
@@ -70,7 +117,8 @@ async function runLoop(db) {
|
||||
async function tick() {
|
||||
try {
|
||||
const data = await fetchLoopData(station);
|
||||
insertRecord(db, data, "loop");
|
||||
buffer.push(data);
|
||||
|
||||
if (POST_URL) {
|
||||
fetch(POST_URL, {
|
||||
method: "POST",
|
||||
@@ -78,6 +126,17 @@ async function runLoop(db) {
|
||||
body: JSON.stringify(data),
|
||||
}).catch(e => warn("POST fehlgeschlagen: " + e.message));
|
||||
}
|
||||
|
||||
const now = Date.now();
|
||||
if (now - lastDbWrite >= DB_INTERVAL_MS) {
|
||||
const n = buffer.length;
|
||||
const agg = aggregateBuffer(buffer);
|
||||
buffer.length = 0;
|
||||
lastDbWrite = now;
|
||||
insertRecord(db, agg, "loop");
|
||||
log(`DB: 5-min-Mittel gespeichert (${n} Messwerte)`);
|
||||
}
|
||||
|
||||
log(
|
||||
`Außen: ${data.tempOut?.toFixed(1)}°C ` +
|
||||
`InnenT: ${data.tempIn?.toFixed(1)}°C ` +
|
||||
|
||||
Reference in New Issue
Block a user