V 1.2.0. postData eingeführt (mit X-API-Key)

This commit is contained in:
2026-04-26 11:08:21 +02:00
parent 6ae153ce10
commit 842727a851
3 changed files with 19 additions and 18 deletions

View File

@@ -18,6 +18,7 @@ services:
BAUD_RATE: ${BAUD_RATE:-19200} BAUD_RATE: ${BAUD_RATE:-19200}
DB_PATH: /data/wetter.db DB_PATH: /data/wetter.db
LOOP_INTERVAL_MS: ${LOOP_INTERVAL_MS:-30000} LOOP_INTERVAL_MS: ${LOOP_INTERVAL_MS:-30000}
COLLECTOR_API_KEY: ${COLLECTOR_API_KEY}
volumes: volumes:
wetter_data: wetter_data:

View File

@@ -1,6 +1,6 @@
{ {
"name": "wetter_1", "name": "wetter_1",
"version": "1.1.0", "version": "1.2.0",
"description": "", "description": "",
"license": "ISC", "license": "ISC",
"author": "rxf", "author": "rxf",

View File

@@ -20,6 +20,7 @@ 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 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 DB_INTERVAL_MS = 5 * 60 * 1000; // alle 5 min in DB schreiben
const POST_URL = process.env.POST_URL ?? null; const POST_URL = process.env.POST_URL ?? null;
const COLLECTOR_API_KEY = process.env.COLLECTOR_API_KEY;
// ── Hilfsfunktionen ──────────────────────────────────────────────────────── // ── Hilfsfunktionen ────────────────────────────────────────────────────────
@@ -29,6 +30,20 @@ function log(msg) { console.log (`[${fmt24h(new Date())}] ${msg}`); }
function warn(msg) { console.warn(`[${fmt24h(new Date())}] WARN ${msg}`); } function warn(msg) { console.warn(`[${fmt24h(new Date())}] WARN ${msg}`); }
function err(msg) { console.error(`[${fmt24h(new Date())}] ERROR ${msg}`); } function err(msg) { console.error(`[${fmt24h(new Date())}] ERROR ${msg}`); }
function postData(data) {
if (!POST_URL) return;
const headers = { "Content-Type": "application/json" };
if (COLLECTOR_API_KEY) headers["X-API-Key"] = COLLECTOR_API_KEY;
fetch(POST_URL, { method: "POST", headers, body: JSON.stringify(data) })
.then(async res => {
if (!res.ok) {
const text = await res.text().catch(() => "");
warn(`POST ${res.status} ${res.statusText}: ${text.slice(0, 200)}`);
}
})
.catch(e => warn("POST fehlgeschlagen: " + e.message));
}
// ── 5-Minuten-Aggregation ────────────────────────────────────────────────── // ── 5-Minuten-Aggregation ──────────────────────────────────────────────────
function aggregateBuffer(buf) { function aggregateBuffer(buf) {
@@ -91,15 +106,7 @@ async function catchUpArchive(db) {
const inserted = insertRecords(db, records, "archive"); const inserted = insertRecords(db, records, "archive");
log(`Archiv: ${inserted} neue Datensätze gespeichert (${records.length} empfangen).`); log(`Archiv: ${inserted} neue Datensätze gespeichert (${records.length} empfangen).`);
if (POST_URL) { for (const r of records) postData(r);
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 ────────────────────────────────────────────────────────── // ── LOOP-Schleife ──────────────────────────────────────────────────────────
@@ -119,14 +126,7 @@ async function runLoop(db) {
const data = await fetchLoopData(station); const data = await fetchLoopData(station);
buffer.push(data); buffer.push(data);
if (POST_URL) { postData(data);
console.log(`Poste zu ${POST_URL}`)
fetch(POST_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
}).catch(e => warn("POST fehlgeschlagen: " + e.message));
}
const now = Date.now(); const now = Date.now();
if (now - lastDbWrite >= DB_INTERVAL_MS) { if (now - lastDbWrite >= DB_INTERVAL_MS) {