Refactor code structure for improved readability and maintainability

This commit is contained in:
jhartworks
2026-09-16 12:41:24 +02:00
parent bfce688f6a
commit 4d8b07fc68
22 changed files with 2670 additions and 96 deletions
+2 -2
View File
@@ -1,9 +1,9 @@
FROM node:20-alpine
FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
RUN npm ci
COPY src ./src
RUN mkdir -p /app/data
+2433
View File
File diff suppressed because it is too large Load Diff
+17 -13
View File
@@ -1,15 +1,19 @@
{
"name": "seltd-collector",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"start": "node src/index.js"
},
"dependencies": {
"mysql2": "^3.12.0",
"node-opcua": "latest",
"node-bacnet": "latest",
"knx": "latest"
}
"name": "seltd-collector",
"version": "1.0.0",
"private": true,
"type": "module",
"scripts": {
"start": "node src/index.js",
"check": "node --check src/index.js"
},
"dependencies": {
"mysql2": "^3.12.0",
"node-opcua": "2.184.4",
"node-bacnet": "0.2.4",
"knx": "2.5.4"
},
"overrides": {
"binary-parser": "2.3.0"
}
}
+28 -13
View File
@@ -1,5 +1,5 @@
import { createServer } from "node:http";
import { mkdirSync, readFileSync, writeFileSync, existsSync } from "node:fs";
import { mkdirSync, readFileSync, writeFileSync, renameSync, unlinkSync, existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { Buffer } from "node:buffer";
@@ -15,6 +15,7 @@ const bacnetCollectorUrl = String(process.env.BACNET_COLLECTOR_URL || "").replac
const LOCAL_POINT_LIMIT = 2000;
let transactionId = 1;
let polling = false;
let lastKnownGoodStore = null;
const db = mysql.createPool({
host: process.env.DB_HOST || "localhost",
@@ -38,27 +39,41 @@ function baseStore() {
};
}
function normalizeStore(parsed) {
return {
...baseStore(),
...parsed,
scans: { ...baseStore().scans, ...(parsed.scans || {}) },
sources: Array.isArray(parsed.sources) ? parsed.sources : [],
datapoints: Array.isArray(parsed.datapoints) ? parsed.datapoints : [],
};
}
function readStore() {
if (!existsSync(storeFile)) {
return baseStore();
return structuredClone(lastKnownGoodStore || baseStore());
}
try {
const parsed = JSON.parse(readFileSync(storeFile, "utf8"));
return {
...baseStore(),
...parsed,
scans: { ...baseStore().scans, ...(parsed.scans || {}) },
sources: Array.isArray(parsed.sources) ? parsed.sources : [],
datapoints: Array.isArray(parsed.datapoints) ? parsed.datapoints : [],
};
} catch {
return baseStore();
const store = normalizeStore(JSON.parse(readFileSync(storeFile, "utf8")));
lastKnownGoodStore = store;
return structuredClone(store);
} catch (error) {
console.warn("Quellenkonfiguration konnte nicht gelesen werden, letzter gültiger Stand wird verwendet:", error.message);
return structuredClone(lastKnownGoodStore || baseStore());
}
}
function writeStore(payload) {
writeFileSync(storeFile, JSON.stringify(payload, null, 2), "utf8");
const temporaryFile = `${storeFile}.${process.pid}.${Date.now()}.tmp`;
try {
writeFileSync(temporaryFile, JSON.stringify(payload, null, 2), "utf8");
renameSync(temporaryFile, storeFile);
lastKnownGoodStore = normalizeStore(payload);
} catch (error) {
try { unlinkSync(temporaryFile); } catch {}
throw error;
}
}
function touchStore(mutator) {