V 1.2.0. postData eingeführt (mit X-API-Key)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "wetter_1",
|
||||
"version": "1.1.0",
|
||||
"version": "1.2.0",
|
||||
"description": "",
|
||||
"license": "ISC",
|
||||
"author": "rxf",
|
||||
|
||||
34
wetter.js
34
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) {
|
||||
|
||||
Reference in New Issue
Block a user