From 842727a851a2b60a2a51031349cc75b1213ccef5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Reinhard=20X=2E=20F=C3=BCrst?= Date: Sun, 26 Apr 2026 11:08:21 +0200 Subject: [PATCH] =?UTF-8?q?V=201.2.0.=20=20=20=20postData=20eingef=C3=BChr?= =?UTF-8?q?t=20(mit=20X-API-Key)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compose.yaml | 1 + package.json | 2 +- wetter.js | 34 +++++++++++++++++----------------- 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/compose.yaml b/compose.yaml index 3db79d4..1908e56 100644 --- a/compose.yaml +++ b/compose.yaml @@ -18,6 +18,7 @@ services: BAUD_RATE: ${BAUD_RATE:-19200} DB_PATH: /data/wetter.db LOOP_INTERVAL_MS: ${LOOP_INTERVAL_MS:-30000} + COLLECTOR_API_KEY: ${COLLECTOR_API_KEY} volumes: wetter_data: diff --git a/package.json b/package.json index ed243f0..ccaaf10 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wetter_1", - "version": "1.1.0", + "version": "1.2.0", "description": "", "license": "ISC", "author": "rxf", diff --git a/wetter.js b/wetter.js index 8d4bb74..3f7113d 100644 --- a/wetter.js +++ b/wetter.js @@ -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 DB_INTERVAL_MS = 5 * 60 * 1000; // alle 5 min in DB schreiben const POST_URL = process.env.POST_URL ?? null; +const COLLECTOR_API_KEY = process.env.COLLECTOR_API_KEY; // ── 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 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 ────────────────────────────────────────────────── function aggregateBuffer(buf) { @@ -91,15 +106,7 @@ 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)); - } - } + for (const r of records) postData(r); } // ── LOOP-Schleife ────────────────────────────────────────────────────────── @@ -119,14 +126,7 @@ async function runLoop(db) { const data = await fetchLoopData(station); buffer.push(data); - if (POST_URL) { - 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)); - } + postData(data); const now = Date.now(); if (now - lastDbWrite >= DB_INTERVAL_MS) {