// /pages/api/cpl/getDgitalInputsHandler.ts import fs from "fs"; import path from "path"; import type { NextApiRequest, NextApiResponse } from "next"; export default function handler(req: NextApiRequest, res: NextApiResponse) { try { const mode = process.env.NEXT_PUBLIC_CPL_MODE; if (mode === "json") { const filePath = path.join( process.cwd(), "mocks", "api", "SERVICE", "digitalInputsMockData.json" ); const fileContent = fs.readFileSync(filePath, "utf-8"); const json = JSON.parse(fileContent); return res.status(200).json(json); } if (mode === "jsmock") { const jsPath = path.join( process.cwd(), "mocks/device-cgi-simulator/SERVICE/digitalInputsMockData.js" ); const fileContent = fs.readFileSync(jsPath, "utf-8"); const extractArray = (name: string) => { const match = fileContent.match( new RegExp(`var\\s+${name}\\s*=\\s*\\[([\\s\\S]*?)\\];`) ); if (!match) throw new Error(`Feld ${name} nicht gefunden`); return match[1] .split(",") .map((s) => s.trim().replace(/^["']|["']$/g, "")); }; const data = { win_de_state: extractArray("win_de_state").map(Number), win_de_invert: extractArray("win_de_invert").map(Number), win_de_counter: extractArray("win_de_counter").map(Number), win_de_time_filter: extractArray("win_de_time_filter").map(Number), win_de_weighting: extractArray("win_de_weighting").map(Number), win_de_counter_active: extractArray("win_de_counter_active").map( Number ), win_de_offline: extractArray("win_de_offline").map(Number), win_de_label: extractArray("win_de_label"), }; return res.status(200).json(data); } return res.status(400).json({ error: "Ungültiger Modus" }); } catch (error) { console.error("❌ Fehler beim Parsen der digitalen Eingänge:", error); return res .status(500) .json({ error: "Fehler beim Parsen der digitalen Eingänge" }); } }