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

136 lines
3.6 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.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// /pages/api/cpl/updateDigitalInputs.ts
import type { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import fs from "fs";
const mode = process.env.NEXT_PUBLIC_CPL_MODE;
// 1⃣ Variable vorab definieren
let mockFilePath: string | null = null;
if (mode === "json") {
mockFilePath = path.join(
process.cwd(),
"mocks",
"api",
"SERVICE",
"digitalInputsMockData.json"
);
}
if (mode === "jsSimulatedProd") {
mockFilePath = path.join(
process.cwd(),
"mocks",
"device-cgi-simulator",
"SERVICE",
"digitalInputsMockData.js"
);
}
// 2⃣ Sicherstellen, dass ein Modus aktiv ist
if (!mockFilePath) {
throw new Error(`❌ Kein gültiger Modus in .env gesetzt: ${mode}`);
}
// Funktion zum Parsen bei jsSimulatedProd
function extractMockData(raw: string) {
const context = {};
const func = new Function(
"context",
`
with (context) {
${raw}
return {
win_de_label,
win_de_invert,
win_de_state,
win_de_counter,
win_de_time_filter,
win_de_weighting,
win_de_counter_active,
win_de_offline
};
}
`
);
return func({});
}
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== "POST") {
return res.status(405).json({ error: "Only POST supported" });
}
try {
const {
id,
label,
invert,
timeFilter,
weighting,
zaehlerAktiv,
eingangOffline,
} = req.body;
if (typeof id !== "number" || id < 1 || id > 32) {
return res.status(400).json({ error: "Ungültige ID (132 erlaubt)" });
}
const rawContent = fs.readFileSync(mockFilePath!, "utf-8");
// 3⃣ JSON vs JS Verarbeitung
const data =
mode === "json" ? JSON.parse(rawContent) : extractMockData(rawContent);
// 4⃣ Aktualisieren der Felder
if (typeof label === "string") data.win_de_label[id - 1] = label;
if (typeof invert === "number") data.win_de_invert[id - 1] = invert;
if (typeof timeFilter === "number")
data.win_de_time_filter[id - 1] = timeFilter;
if (typeof weighting === "number")
data.win_de_weighting[id - 1] = weighting;
if (typeof zaehlerAktiv === "number")
data.win_de_counter_active[id - 1] = zaehlerAktiv;
if (typeof eingangOffline === "number")
data.win_de_offline[id - 1] = eingangOffline;
// 5⃣ Speichern
if (mode === "json") {
fs.writeFileSync(mockFilePath!, JSON.stringify(data, null, 2), "utf-8");
} else {
const newContent = `
// auto-generated from update API
var win_de_state = ${JSON.stringify(data.win_de_state, null, 2)};
var win_de_invert = ${JSON.stringify(data.win_de_invert, null, 2)};
var win_de_counter = ${JSON.stringify(data.win_de_counter, null, 2)};
var win_de_time_filter = ${JSON.stringify(data.win_de_time_filter, null, 2)};
var win_de_weighting = ${JSON.stringify(data.win_de_weighting, null, 2)};
var win_de_counter_active = ${JSON.stringify(
data.win_de_counter_active,
null,
2
)};
var win_de_offline = ${JSON.stringify(data.win_de_offline, null, 2)};
var win_de_label = ${JSON.stringify(data.win_de_label, null, 2)};
`;
fs.writeFileSync(mockFilePath!, newContent, "utf-8");
}
return res.status(200).json({
message: `Update erfolgreich für ID ${id}`,
id,
label,
invert,
timeFilter,
weighting,
eingangOffline,
});
} catch (err: any) {
console.error("Fehler beim Schreiben:", err);
return res.status(500).json({ error: "Update fehlgeschlagen" });
}
}