Files
CPLv4.0/pages/api/cpl/getDigitalOutputsHandler.ts
ISA 887e7b4992 feat: zentrale API für json und jsmock + Diagramm-Doku aktualisiert
- Neue API /api/cpl/getDigitalOutputsHandler.ts implementiert (vereint json + jsmock)
- fetchDigitalOutputsService.ts auf zentrale API umgestellt (weniger Code, klarere Struktur)
- Nur production-Modus lädt weiterhin Skript /CPL?/CPL/SERVICE/digitalOutputs.js
- README_digitalOutputs.md überarbeitet:
  - Diagrammtyp („flowchart“, Datenflussdiagramm) explizit benannt
  - API-Endpunkte konsolidiert dargestellt
- CHANGELOG.md um neue API-Struktur und Dokumentationsänderung ergänzt
2025-06-19 10:48:18 +02:00

48 lines
1.4 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// /pages/api/cpl/getDigitalOutputsJsonHandler.ts
import { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import fs from "fs/promises";
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const mode = process.env.NEXT_PUBLIC_CPL_MODE ?? "json";
if (mode === "json") {
// Lese JSON-Datei z.B. digitalOutputsMockData.json
const filePath = path.join(
process.cwd(),
"mocks/api/SERVICE/digitalOutputsMockData.json"
);
const content = await fs.readFile(filePath, "utf-8");
const data = JSON.parse(content);
return res.status(200).json(data);
}
if (mode === "jsmock") {
// Lese Datei und extrahiere window-Variablen aus .js-Datei
const filePath = path.join(
process.cwd(),
"mocks/device-cgi-simulator/SERVICE/digitalOutputsMockData.js"
);
const fileContent = await fs.readFile(filePath, "utf-8");
const stateMatch = fileContent.match(/win_da_state\s*=\s*\[([^\]]*)\]/);
const labelMatch = fileContent.match(
/win_da_bezeichnung\s*=\s*\[([^\]]*)\]/
);
const win_da_state =
stateMatch?.[1]?.split(",").map((x) => parseInt(x.trim())) || [];
const win_da_bezeichnung =
labelMatch?.[1]?.split(",").map((x) => x.trim().replace(/["']/g, "")) ||
[];
return res.status(200).json({ win_da_state, win_da_bezeichnung });
}
return res.status(400).json({ error: "Unsupported mode" });
}