Files
CPLv4.0/pages/api/cpl/updateDigitalOutputsHandler.ts
Ismail Ali 041bc3e23e feat: jsSimulatedProd-Modus für analoge & digitale Eingänge implementiert
- neuen Modus `jsSimulatedProd` eingeführt für realitätsnahe Simulation auf Basis echter Produktionsdaten
- analoge Eingänge: analogInputsMockData.js eingebunden und dynamisch per Script geladen
- digitale Eingänge: digitalInputsMockData.js eingebunden mit window-Variablen (z. B. win_de_state, win_de_label etc.)
- fetchAnalogInputsService.ts und fetchDigitalInputsService.ts angepasst zur Modusprüfung und Script-Auswertung
- getAnalogInputsHandler.ts und getDigitalInputsHandler.ts geben im jsSimulatedProd-Modus JavaScript-Dateien aus
- .env.development setzt `NEXT_PUBLIC_CPL_MODE=jsSimulatedProd`
2025-06-22 08:42:49 +02:00

76 lines
2.1 KiB
TypeScript

// /pages/api/cpl/updateDigitalOutputshandler.ts
import fs from "fs";
import path from "path";
export default function handler(req, res) {
if (req.method !== "POST") {
return res.status(405).json({ message: "Nur POST erlaubt" });
}
const { outputs } = req.body;
if (!Array.isArray(outputs)) {
return res.status(400).json({ error: "Ungültige Datenstruktur" });
}
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
const jsonData = {
win_da_state: outputs.map((o) => (o.status ? 1 : 0)),
win_da_bezeichnung: outputs.map((o) => o.label),
};
let filePath = "";
if (mode === "json") {
filePath = path.join(
process.cwd(),
"mocks",
"api",
"SERVICE",
"digitalOutputsMockData.json"
);
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, JSON.stringify(jsonData, null, 2), "utf-8");
console.log("✅ Datei (JSON) geschrieben:", filePath);
res.status(200).json({ success: true });
} catch (err) {
console.error("❌ Fehler beim Schreiben der JSON-Datei:", err);
res.status(500).json({ error: "Fehler beim Schreiben der Datei" });
}
} else if (mode === "jsSimulatedProd") {
filePath = path.join(
process.cwd(),
"mocks",
"device-cgi-simulator",
"SERVICE",
"digitalOutputsMockData.js"
);
const jsContent =
`win_da_state = [${jsonData.win_da_state.join(", ")}];\n` +
`win_da_bezeichnung = [${jsonData.win_da_bezeichnung
.map((s) => `"${s}"`)
.join(", ")}];\n`;
try {
fs.mkdirSync(path.dirname(filePath), { recursive: true });
fs.writeFileSync(filePath, jsContent, "utf-8");
console.log("✅ Datei (JS-Mock) geschrieben:", filePath);
res.status(200).json({ success: true });
} catch (err) {
console.error("❌ Fehler beim Schreiben der JS-Datei:", err);
res.status(500).json({ error: "Fehler beim Schreiben der JS-Datei" });
}
} else {
res
.status(403)
.json({ error: "In production ist Schreiben nicht erlaubt" });
}
}