feat: API zum gezielten Überschreiben einzelner KUE-Mock-Werte erstellt
- nur betroffene window.win_*[slot] Werte werden ersetzt - gesamte Datei bleibt erhalten - handleSave übermittelt nur geänderte Felder und Slot
This commit is contained in:
@@ -11,36 +11,36 @@ export default async function handler(
|
||||
return res.status(405).json({ error: "Method not allowed" });
|
||||
}
|
||||
|
||||
const { data } = req.body;
|
||||
const { slot, changes } = req.body;
|
||||
|
||||
if (!data) {
|
||||
return res.status(400).json({ error: "No data provided" });
|
||||
if (slot === undefined || !changes || typeof changes !== "object") {
|
||||
return res.status(400).json({ error: "Missing slot or changes" });
|
||||
}
|
||||
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"apiMockData",
|
||||
"SERVICE",
|
||||
"kueDataMockData.js"
|
||||
"kabelueberwachungMockData.js"
|
||||
);
|
||||
|
||||
try {
|
||||
const fileContent =
|
||||
`window.win_kueID = ${JSON.stringify(data.kueID)};\n` +
|
||||
`window.win_kuePSTmMinus96V = ${JSON.stringify(
|
||||
data.kueBezeichnungen
|
||||
)};\n` +
|
||||
`window.win_kueLimit1 = ${JSON.stringify(data.isolationsgrenzwerte)};\n` +
|
||||
`window.win_kueDelay1 = ${JSON.stringify(data.verzoegerung)};\n` +
|
||||
`window.win_kueLimit2Low = ${JSON.stringify(
|
||||
data.untereSchleifenGrenzwerte
|
||||
)};\n` +
|
||||
`window.win_kueLimit2High = ${JSON.stringify(
|
||||
data.obereSchleifenGrenzwerte
|
||||
)};\n` +
|
||||
`window.win_kueLoopInterval = ${JSON.stringify(
|
||||
data.schleifenintervall
|
||||
)};\n`;
|
||||
let fileContent = await fs.readFile(filePath, "utf-8");
|
||||
|
||||
for (const [varName, value] of Object.entries(changes)) {
|
||||
const regex = new RegExp(
|
||||
`(var\\s+${varName}\\s*=\\s*\\[)([^\\]]*)(\\])`,
|
||||
"m"
|
||||
);
|
||||
|
||||
const match = fileContent.match(regex);
|
||||
if (match) {
|
||||
const values = match[2].split(",").map((v) => v.trim());
|
||||
values[slot] = JSON.stringify(value); // korrektes Format: z. B. `"FTZ_2"` oder `10.5`
|
||||
const updatedArray = `${match[1]} ${values.join(", ")} ${match[3]}`;
|
||||
fileContent = fileContent.replace(regex, updatedArray);
|
||||
}
|
||||
}
|
||||
|
||||
await fs.writeFile(filePath, fileContent, "utf-8");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user