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:
@@ -124,9 +124,8 @@ var win_tdrLocation = [
|
|||||||
0, 0, 0, 0, 0, 0,
|
0, 0, 0, 0, 0, 0,
|
||||||
];
|
];
|
||||||
var win_tdrActive = [
|
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,
|
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
|
||||||
1, 1, 1, 1, 1, 1,
|
];;
|
||||||
];
|
|
||||||
var win_tdrLast = [
|
var win_tdrLast = [
|
||||||
"2024-10-17 07:51:54:000",
|
"2024-10-17 07:51:54:000",
|
||||||
"2024-09-30 08:38:50:000",
|
"2024-09-30 08:38:50:000",
|
||||||
|
|||||||
@@ -60,18 +60,34 @@ export default function TdrEinstellung({ slot }: Props) {
|
|||||||
const newState = !tdrActive;
|
const newState = !tdrActive;
|
||||||
setTdrActive(newState);
|
setTdrActive(newState);
|
||||||
|
|
||||||
const url = `${
|
const isDev = window.location.hostname === "localhost";
|
||||||
window.location.origin
|
const slotParam = `KTX${slot}=${newState ? 1 : 0}`;
|
||||||
}/CPL?/kabelueberwachung.html&KTX${slot}=${newState ? 1 : 0}`;
|
|
||||||
fetch(url)
|
if (isDev) {
|
||||||
.then((res) => {
|
fetch(
|
||||||
if (!res.ok) throw new Error("TDR-Befehl fehlgeschlagen");
|
`/api/cpl/updateTdrSettingsDataAPIHandler?slot=${slot}&value=${
|
||||||
console.log("TDR aktiviert/deaktiviert:", res.status);
|
newState ? 1 : 0
|
||||||
})
|
}`
|
||||||
.catch((err) => {
|
)
|
||||||
console.error("Fehler beim TDR-Befehl:", err);
|
.then((res) => res.json())
|
||||||
alert("Fehler beim Umschalten der TDR-Funktion.");
|
.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 (
|
return (
|
||||||
|
|||||||
46
pages/api/cpl/updateTdrSettingsDataAPIHandler.ts
Normal file
46
pages/api/cpl/updateTdrSettingsDataAPIHandler.ts
Normal 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." });
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user