Compare commits
4 Commits
842727a851
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 921fbf5cc5 | |||
| 8ae6734b56 | |||
| 0efec3b0cd | |||
| 4798e5f9c9 |
Generated
+2
-2
@@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "wetter_1",
|
"name": "wetter_1",
|
||||||
"version": "1.0.1",
|
"version": "1.2.1",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "wetter_1",
|
"name": "wetter_1",
|
||||||
"version": "1.0.1",
|
"version": "1.2.1",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"better-sqlite3": "^12.9.0",
|
"better-sqlite3": "^12.9.0",
|
||||||
|
|||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "wetter_1",
|
"name": "wetter_1",
|
||||||
"version": "1.2.0",
|
"version": "1.2.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"author": "rxf",
|
"author": "rxf",
|
||||||
|
|||||||
@@ -30,18 +30,30 @@ 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) {
|
async function postData(data, source) {
|
||||||
if (!POST_URL) return;
|
if (!POST_URL) return;
|
||||||
const headers = { "Content-Type": "application/json" };
|
const headers = { "Content-Type": "application/json" };
|
||||||
if (COLLECTOR_API_KEY) headers["X-API-Key"] = COLLECTOR_API_KEY;
|
if (COLLECTOR_API_KEY) headers["X-API-Key"] = COLLECTOR_API_KEY;
|
||||||
fetch(POST_URL, { method: "POST", headers, body: JSON.stringify(data) })
|
const body = JSON.stringify({ ...data, source });
|
||||||
.then(async res => {
|
|
||||||
if (!res.ok) {
|
for (let attempt = 1; attempt <= 5; attempt++) {
|
||||||
|
let res;
|
||||||
|
try {
|
||||||
|
res = await fetch(POST_URL, { method: "POST", headers, body });
|
||||||
|
} catch (e) {
|
||||||
|
warn("POST fehlgeschlagen: " + e.message);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (res.ok) return;
|
||||||
|
if (res.status === 429 && attempt < 5) {
|
||||||
|
const delay = Number(res.headers.get("Retry-After") ?? 2) * 1000;
|
||||||
|
await new Promise(r => setTimeout(r, delay));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
const text = await res.text().catch(() => "");
|
const text = await res.text().catch(() => "");
|
||||||
warn(`POST ${res.status} ${res.statusText}: ${text.slice(0, 200)}`);
|
warn(`POST ${res.status} ${res.statusText}: ${text.slice(0, 200)}`);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
})
|
|
||||||
.catch(e => warn("POST fehlgeschlagen: " + e.message));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── 5-Minuten-Aggregation ──────────────────────────────────────────────────
|
// ── 5-Minuten-Aggregation ──────────────────────────────────────────────────
|
||||||
@@ -106,7 +118,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).`);
|
||||||
|
|
||||||
for (const r of records) postData(r);
|
for (const r of records) await postData(r, "archive");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── LOOP-Schleife ──────────────────────────────────────────────────────────
|
// ── LOOP-Schleife ──────────────────────────────────────────────────────────
|
||||||
@@ -126,7 +138,7 @@ async function runLoop(db) {
|
|||||||
const data = await fetchLoopData(station);
|
const data = await fetchLoopData(station);
|
||||||
buffer.push(data);
|
buffer.push(data);
|
||||||
|
|
||||||
postData(data);
|
postData(data, "loop");
|
||||||
|
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
if (now - lastDbWrite >= DB_INTERVAL_MS) {
|
if (now - lastDbWrite >= DB_INTERVAL_MS) {
|
||||||
|
|||||||
Reference in New Issue
Block a user