Files
CPLv4.0/pages/api/cpl/updateAnalogInputsSettingsHandler.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

66 lines
2.0 KiB
TypeScript

// /pages/api/cpl/updateAnalogInputsSettingsHandler.ts
import { NextApiRequest, NextApiResponse } from "next";
import fs from "fs";
import path from "path";
export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Method not allowed" });
}
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
const updates = req.body.updates;
if (!Array.isArray(updates)) {
return res.status(400).json({ error: "Missing or invalid updates array" });
}
if (mode === "json") {
const filePath = path.join(
process.cwd(),
"mocks/api/SERVICE/analogInputsMockData.json"
);
const content = fs.readFileSync(filePath, "utf-8");
const data = JSON.parse(content);
for (const update of updates) {
const { key, index, value } = update;
if (Array.isArray(data[key]) && index < data[key].length) {
data[key][index] = value;
}
}
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), "utf-8");
return res.status(200).json({ success: true });
}
if (mode === "jsSimulatedProd") {
const filePath = path.join(
process.cwd(),
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js"
);
let content = fs.readFileSync(filePath, "utf-8");
for (const update of updates) {
const { key, index, value } = update;
const regex = new RegExp(`var\\s+${key}\\s*=\\s*\\[([\\s\\S]*?)\\];`);
const match = content.match(regex);
if (!match) continue;
const items = match[1].split(",").map((s) => s.trim());
if (index >= items.length) continue;
const isString = typeof value === "string";
items[index] = isString ? `"${value}"` : `${value}`;
const updatedLine = `var ${key} = [${items.join(", ")}];`;
content = content.replace(regex, updatedLine);
}
fs.writeFileSync(filePath, content, "utf-8");
return res.status(200).json({ success: true });
}
return res.status(400).json({ error: "Unsupported mode" });
}