test: WebSocket-Datenprüfung erfolgreich – JSON wird bei Änderung gespeichert

- Webservice-Antworten werden nun korrekt mit vorherigen Daten verglichen
- Bei Änderungen wird automatisch eine neue JSON-Datei unter /debug erstellt
- Keine Datei-Erstellung bei identischen Antworten
- Funktion `saveJsonIfChanged` erfolgreich in utils/websocket ausgelagert
This commit is contained in:
Ismail Ali
2025-06-08 08:52:45 +02:00
parent a8978034d2
commit db54cc7cca
3 changed files with 48 additions and 21 deletions

View File

@@ -1,2 +1,2 @@
// /config/appVersion
export const APP_VERSION = "1.1.245";
export const APP_VERSION = "1.1.246";

View File

@@ -3,6 +3,7 @@ const next = require("next");
const { Server } = require("socket.io");
const fs = require("fs");
const path = require("path");
const { saveJsonIfChanged } = require("./utils/websocket/saveJsonIfChanged");
const fetch = (...args) => import("node-fetch").then(({ default: fetch }) => fetch(...args));
const dev = process.env.NODE_ENV !== "production";
@@ -47,28 +48,22 @@ app.prepare().then(() => {
const json = JSON.parse(text);
statis = json?.Statis || [];
console.log("📡 Webservice-Daten empfangen");
//------------------------------------
// Änderung prüfen
const hasChanged = saveJsonIfChanged(json, lastStatis);
if (hasChanged) {
// socket.emit("gisLinesStatusUpdated", statis);
console.log("✅ Änderung erkannt und gesendet");
} else {
console.log("🔁 Keine Änderung festgestellt");
}
//------------------------------------
return;
}
const hasChanged =
statis.length !== lastStatis.length ||
statis.some((entry, index) => {
const prev = lastStatis[index];
return (
!prev ||
entry.Modul !== prev.Modul ||
entry.ModulName !== prev.ModulName ||
entry.Value !== prev.Value ||
entry.Level !== prev.Level
);
});
if (hasChanged) {
lastStatis = statis;
socket.emit("gisLinesStatusUpdated", statis);
console.log("✅ Änderung erkannt und gesendet");
} else {
console.log("🔁 Keine Änderung festgestellt");
}
// Nur bei Mockdaten: direkt senden
socket.emit("gisLinesStatusUpdated", statis);
} catch (error) {
console.error("❌ Fehler beim Datenabruf:", error.message);
}

View File

@@ -0,0 +1,32 @@
const fs = require("fs");
const path = require("path");
function saveJsonIfChanged(newJson, lastStatisRef) {
const newStatis = newJson?.Statis || [];
const hasChanged =
newStatis.length !== lastStatisRef.length ||
newStatis.some((entry, index) => {
const prev = lastStatisRef[index];
return (
!prev ||
entry.Modul !== prev.Modul ||
entry.ModulName !== prev.ModulName ||
entry.Value !== prev.Value ||
entry.Level !== prev.Level
);
});
if (hasChanged) {
lastStatisRef.splice(0, lastStatisRef.length, ...newStatis);
const outputPath = path.join(process.cwd(), "debug", `webserviceDump-${Date.now()}.json`);
fs.mkdirSync(path.dirname(outputPath), { recursive: true });
fs.writeFileSync(outputPath, JSON.stringify(newJson, null, 2), "utf-8");
console.log("📝 Webservice-Daten wurden gespeichert:", outputPath);
}
return hasChanged;
}
module.exports = { saveJsonIfChanged };