- 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
48 lines
1.4 KiB
TypeScript
48 lines
1.4 KiB
TypeScript
// /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" });
|
||
}
|