feat(dev): API zum Umschalten der TDR-Aktivierung in Mock-Datei eingebunden

- Neuer Endpunkt: /api/cpl/updateTdrSettingsDataAPIHandler
- Aktualisiert win_tdrActive[slot] in Mockdatei
- Entwicklung vollständig ohne Hardware möglich
This commit is contained in:
ISA
2025-04-30 15:05:19 +02:00
parent f59fd0d773
commit 40777f212b
3 changed files with 76 additions and 15 deletions

View File

@@ -124,9 +124,8 @@ var win_tdrLocation = [
0, 0, 0, 0, 0, 0,
];
var win_tdrActive = [
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1,
];
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
];;
var win_tdrLast = [
"2024-10-17 07:51:54:000",
"2024-09-30 08:38:50:000",

View File

@@ -60,18 +60,34 @@ export default function TdrEinstellung({ slot }: Props) {
const newState = !tdrActive;
setTdrActive(newState);
const url = `${
window.location.origin
}/CPL?/kabelueberwachung.html&KTX${slot}=${newState ? 1 : 0}`;
fetch(url)
.then((res) => {
if (!res.ok) throw new Error("TDR-Befehl fehlgeschlagen");
console.log("TDR aktiviert/deaktiviert:", res.status);
})
.catch((err) => {
console.error("Fehler beim TDR-Befehl:", err);
alert("Fehler beim Umschalten der TDR-Funktion.");
});
const isDev = window.location.hostname === "localhost";
const slotParam = `KTX${slot}=${newState ? 1 : 0}`;
if (isDev) {
fetch(
`/api/cpl/updateTdrSettingsDataAPIHandler?slot=${slot}&value=${
newState ? 1 : 0
}`
)
.then((res) => res.json())
.then((data) => {
console.log("Mock-Status aktualisiert:", data);
})
.catch((err) => {
console.error("Fehler in DEV-API:", err);
});
} else {
const url = `${window.location.origin}/CPL?/kabelueberwachung.html&${slotParam}`;
fetch(url)
.then((res) => {
if (!res.ok) throw new Error("TDR-Befehl fehlgeschlagen");
console.log("TDR aktiviert/deaktiviert:", res.status);
})
.catch((err) => {
console.error("Fehler beim TDR-Befehl:", err);
alert("Fehler beim Umschalten der TDR-Funktion.");
});
}
};
return (

View File

@@ -0,0 +1,46 @@
// /pages/api/cpl/updateTdrSettingsDataAPIHandler.ts
import path from "path";
import fs from "fs/promises";
export default async function handler(req, res) {
const { slot, value } = req.query;
if (slot === undefined || value === undefined) {
return res.status(400).json({ error: "Missing slot or value" });
}
const key = "win_tdrActive";
const filePath = path.join(
process.cwd(),
"apiMockData/SERVICE/kabelueberwachungMockData.js"
);
try {
const fileContent = await fs.readFile(filePath, "utf-8");
const regex = new RegExp(`(var\\s+${key}\\s*=\\s*\\[)([^\\]]*)(\\])`);
const match = fileContent.match(regex);
if (!match) {
return res
.status(404)
.json({ error: `Key "${key}" not found in mock data.` });
}
const values = match[2].split(",").map((v) => v.trim());
values[Number(slot)] = value;
// Optional: Entferne leere Einträge
while (values.length > 0 && !values[values.length - 1]) values.pop();
const newArray = `var ${key} = [\n ${values.join(", ")}\n];`;
const updated = fileContent.replace(regex, newArray);
await fs.writeFile(filePath, updated, "utf-8");
return res.status(200).json({ success: true, slot, value });
} catch (err) {
console.error("API error:", err);
return res.status(500).json({ error: "Internal server error." });
}
}