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`
This commit is contained in:
@@ -18,51 +18,17 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return res.status(200).json(data);
|
||||
}
|
||||
|
||||
if (mode === "jsmock") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js"
|
||||
if (mode === "jsSimulatedProd") {
|
||||
const analogInputsScript = fs.readFileSync(
|
||||
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js",
|
||||
"utf-8"
|
||||
);
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
function extractArray(name: string): any[] {
|
||||
const match = content.match(
|
||||
new RegExp(`var\\s+${name}\\s*=\\s*\\[([\\s\\S]*?)\\];`)
|
||||
);
|
||||
if (!match) return [];
|
||||
return match[1]
|
||||
.split(",")
|
||||
.map((s) => s.trim().replace(/^["']|["']$/g, ""))
|
||||
.slice(0, 8);
|
||||
}
|
||||
|
||||
const responseData = {
|
||||
win_analogInputsValues: extractArray("win_analogInputsValues").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsLabels: extractArray("win_analogInputsLabels"),
|
||||
win_analogInputsOffset: extractArray("win_analogInputsOffset").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsFactor: extractArray("win_analogInputsFactor").map(
|
||||
Number
|
||||
),
|
||||
win_analogInputsLoggerIntervall: extractArray(
|
||||
"win_analogInputsLoggerIntervall"
|
||||
).map(Number),
|
||||
win_analogInputsUnits: extractArray("win_analogInputsUnits"),
|
||||
win_analogInputsWeighting: extractArray(
|
||||
"win_analogInputsWeighting"
|
||||
).map(Number),
|
||||
};
|
||||
//console.log("Analog Inputs Mock Data:", responseData);
|
||||
|
||||
return res.status(200).json(responseData);
|
||||
res.setHeader("Content-Type", "application/javascript");
|
||||
res.status(200).send(analogInputsScript);
|
||||
return;
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Unsupported mode" });
|
||||
} catch (error) {
|
||||
console.error("❌ Fehler beim Lesen der analogen Eingänge:", error);
|
||||
return res.status(500).json({ error: "Fehler beim Lesen der Datei" });
|
||||
console.error(error);
|
||||
res.status(500).json({ error: "Internal Server Error" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,39 +20,14 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return res.status(200).json(json);
|
||||
}
|
||||
|
||||
if (mode === "jsmock") {
|
||||
const jsPath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/device-cgi-simulator/SERVICE/digitalInputsMockData.js"
|
||||
if (mode === "jsSimulatedProd") {
|
||||
const analogInputsScript = fs.readFileSync(
|
||||
"mocks/device-cgi-simulator/SERVICE/digitalInputsMockData.js",
|
||||
"utf-8"
|
||||
);
|
||||
const fileContent = fs.readFileSync(jsPath, "utf-8");
|
||||
//console.log("📡 JSMOCK-Daten geladen fileContent:", fileContent);
|
||||
|
||||
const extractArray = (label: string) => {
|
||||
const match = fileContent.match(
|
||||
new RegExp(`var\\s+${label}\\s*=\\s*\\[([\\s\\S]*?)\\];`)
|
||||
);
|
||||
if (!match) throw new Error(`Feld ${label} 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"),
|
||||
};
|
||||
//console.log("📡 JSMOCK-Daten geladen in api in jsmock:", data);
|
||||
|
||||
return res.status(200).json(data);
|
||||
res.setHeader("Content-Type", "application/javascript");
|
||||
res.status(200).send(analogInputsScript);
|
||||
return;
|
||||
}
|
||||
|
||||
return res.status(400).json({ error: "Ungültiger Modus" });
|
||||
|
||||
@@ -21,7 +21,7 @@ export default async function handler(
|
||||
return res.status(200).json(data);
|
||||
}
|
||||
|
||||
if (mode === "jsmock") {
|
||||
if (mode === "jsSimulatedProd") {
|
||||
// Lese Datei und extrahiere window-Variablen aus .js-Datei
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
|
||||
@@ -34,7 +34,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||
return res.status(200).json({ success: true });
|
||||
}
|
||||
|
||||
if (mode === "jsmock") {
|
||||
if (mode === "jsSimulatedProd") {
|
||||
const filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks/device-cgi-simulator/SERVICE/analogInputsMockData.js"
|
||||
|
||||
@@ -17,7 +17,7 @@ if (mode === "json") {
|
||||
"digitalInputsMockData.json"
|
||||
);
|
||||
}
|
||||
if (mode === "jsmock") {
|
||||
if (mode === "jsSimulatedProd") {
|
||||
mockFilePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks",
|
||||
@@ -32,7 +32,7 @@ if (!mockFilePath) {
|
||||
throw new Error(`❌ Kein gültiger Modus in .env gesetzt: ${mode}`);
|
||||
}
|
||||
|
||||
// Funktion zum Parsen bei jsmock
|
||||
// Funktion zum Parsen bei jsSimulatedProd
|
||||
function extractMockData(raw: string) {
|
||||
const context = {};
|
||||
const func = new Function(
|
||||
|
||||
@@ -42,7 +42,7 @@ export default function handler(req, res) {
|
||||
console.error("❌ Fehler beim Schreiben der JSON-Datei:", err);
|
||||
res.status(500).json({ error: "Fehler beim Schreiben der Datei" });
|
||||
}
|
||||
} else if (mode === "jsmock") {
|
||||
} else if (mode === "jsSimulatedProd") {
|
||||
filePath = path.join(
|
||||
process.cwd(),
|
||||
"mocks",
|
||||
|
||||
Reference in New Issue
Block a user