Digitale Eingänge Update mit Mock daten in der Entwicklungsumgebung

This commit is contained in:
ISA
2025-04-24 11:11:04 +02:00
parent eddf293ce2
commit 0b63579d56
7 changed files with 365 additions and 72 deletions

View File

@@ -0,0 +1,87 @@
// /pages/api/cpl/updateDigitaleEingaenge.ts
import type { NextApiRequest, NextApiResponse } from "next";
import path from "path";
import fs from "fs";
// WICHTIG: Der Mock-Datenpfad
const mockFilePath = path.join(
process.cwd(),
"apiMockData",
"SERVICE",
"digitaleEingaengeMockData.js"
);
// Funktion zum Parsen des JS-Datei-Inhalts in ein eval-fähiges Objekt
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, name, invertierung } = 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");
const data = extractMockData(rawContent);
// Update der Daten
if (typeof name === "string") data.win_de_label[id - 1] = name;
if (typeof invertierung === "number")
data.win_de_invert[id - 1] = invertierung;
// Erzeuge neuen Dateiinhalt
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", id, name, invertierung });
} catch (err: any) {
console.error("Fehler beim Schreiben:", err);
return res.status(500).json({ error: "Update fehlgeschlagen" });
}
}

View File

@@ -1,4 +1,5 @@
"use client";
// /pages/digitalInputs.tsx
import React, { useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { AppDispatch, RootState } from "../redux/store";