fix: digital Inputs Modal

This commit is contained in:
ISA
2025-06-20 10:53:22 +02:00
parent 3cadee04a8
commit b233694fed
11 changed files with 616 additions and 144 deletions

View File

@@ -26,12 +26,13 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
"mocks/device-cgi-simulator/SERVICE/digitalInputsMockData.js"
);
const fileContent = fs.readFileSync(jsPath, "utf-8");
//console.log("📡 JSMOCK-Daten geladen fileContent:", fileContent);
const extractArray = (name: string) => {
const extractArray = (label: string) => {
const match = fileContent.match(
new RegExp(`var\\s+${name}\\s*=\\s*\\[([\\s\\S]*?)\\];`)
new RegExp(`var\\s+${label}\\s*=\\s*\\[([\\s\\S]*?)\\];`)
);
if (!match) throw new Error(`Feld ${name} nicht gefunden`);
if (!match) throw new Error(`Feld ${label} nicht gefunden`);
return match[1]
.split(",")
.map((s) => s.trim().replace(/^["']|["']$/g, ""));
@@ -49,7 +50,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
win_de_offline: extractArray("win_de_offline").map(Number),
win_de_label: extractArray("win_de_label"),
};
console.log("📡 JSMOCK-Daten geladen:", data);
//console.log("📡 JSMOCK-Daten geladen in api in jsmock:", data);
return res.status(200).json(data);
}

View File

@@ -3,15 +3,36 @@ import type { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import fs from "fs";
// WICHTIG: Der Mock-Datenpfad
const mockFilePath = path.join(
process.cwd(),
"apiMockData",
"SERVICE",
"digitalInputsMockData.js"
);
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
// Funktion zum Parsen des JS-Datei-Inhalts in ein eval-fähiges Objekt
// 1⃣ Variable vorab definieren
let mockFilePath: string | null = null;
if (mode === "json") {
mockFilePath = path.join(
process.cwd(),
"mocks",
"api",
"SERVICE",
"digitalInputsMockData.json"
);
}
if (mode === "jsmock") {
mockFilePath = path.join(
process.cwd(),
"mocks",
"device-cgi-simulator",
"SERVICE",
"digitalInputsMockData.js"
);
}
// 2⃣ Sicherstellen, dass ein Modus aktiv ist
if (!mockFilePath) {
throw new Error(`❌ Kein gültiger Modus in .env gesetzt: ${mode}`);
}
// Funktion zum Parsen bei jsmock
function extractMockData(raw: string) {
const context = {};
const func = new Function(
@@ -44,32 +65,43 @@ export default async function handler(
}
try {
const { id, name, invertierung, filterzeit, gewichtung, zaehlerAktiv } =
req.body;
const {
id,
label,
invert,
timeFilter,
weighting,
zaehlerAktiv,
eingangOffline,
} = req.body;
if (typeof id !== "number" || id < 1 || id > 32) {
return res.status(400).json({ error: "Ungültige ID (132 erlaubt)" });
}
const rawContent = fs.readFileSync(mockFilePath, "utf-8");
const data = extractMockData(rawContent);
const rawContent = fs.readFileSync(mockFilePath!, "utf-8");
// Update der Daten
if (typeof name === "string") data.win_de_label[id - 1] = name;
if (typeof invertierung === "number")
data.win_de_invert[id - 1] = invertierung;
if (typeof filterzeit === "number")
data.win_de_time_filter[id - 1] = filterzeit;
if (typeof gewichtung === "number")
data.win_de_weighting[id - 1] = gewichtung;
// 3⃣ JSON vs JS Verarbeitung
const data =
mode === "json" ? JSON.parse(rawContent) : extractMockData(rawContent);
// 4⃣ Aktualisieren der Felder
if (typeof label === "string") data.win_de_label[id - 1] = label;
if (typeof invert === "number") data.win_de_invert[id - 1] = invert;
if (typeof timeFilter === "number")
data.win_de_time_filter[id - 1] = timeFilter;
if (typeof weighting === "number")
data.win_de_weighting[id - 1] = weighting;
if (typeof zaehlerAktiv === "number")
data.win_de_counter_active[id - 1] = zaehlerAktiv;
if (typeof eingangOffline === "number")
data.win_de_offline[id - 1] = eingangOffline;
if (typeof invertierung === "number")
data.win_de_invert[id - 1] = invertierung;
// Erzeuge neuen Dateiinhalt
const newContent = `
// 5⃣ Speichern
if (mode === "json") {
fs.writeFileSync(mockFilePath!, JSON.stringify(data, null, 2), "utf-8");
} else {
const newContent = `
// auto-generated from update API
var win_de_state = ${JSON.stringify(data.win_de_state, null, 2)};
var win_de_invert = ${JSON.stringify(data.win_de_invert, null, 2)};
@@ -77,19 +109,25 @@ var win_de_counter = ${JSON.stringify(data.win_de_counter, null, 2)};
var win_de_time_filter = ${JSON.stringify(data.win_de_time_filter, null, 2)};
var win_de_weighting = ${JSON.stringify(data.win_de_weighting, null, 2)};
var win_de_counter_active = ${JSON.stringify(
data.win_de_counter_active,
null,
2
)};
data.win_de_counter_active,
null,
2
)};
var win_de_offline = ${JSON.stringify(data.win_de_offline, null, 2)};
var win_de_label = ${JSON.stringify(data.win_de_label, null, 2)};
`;
fs.writeFileSync(mockFilePath!, newContent, "utf-8");
}
fs.writeFileSync(mockFilePath, newContent, "utf-8");
return res
.status(200)
.json({ message: "Update erfolgreich", id, name, invertierung });
return res.status(200).json({
message: `Update erfolgreich für ID ${id}`,
id,
label,
invert,
timeFilter,
weighting,
eingangOffline,
});
} catch (err: any) {
console.error("Fehler beim Schreiben:", err);
return res.status(500).json({ error: "Update fehlgeschlagen" });