From f3bd3ccc781da5520c317210372138302edb8ecd Mon Sep 17 00:00:00 2001 From: Ismail Ali Date: Thu, 1 May 2025 18:49:19 +0200 Subject: [PATCH] =?UTF-8?q?WIP:=20update=20dititale=20Ausg=C3=A4nge=20Mock?= =?UTF-8?q?=20datei?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/einausgaenge/modals/OutputModal.tsx | 56 ++++++++++++++----- config/webVersion.ts | 2 +- pages/api/cpl/updateDigitalOutputs.ts | 46 +++++++++++++++ public/CPL/SERVICE/da.js | 3 +- 4 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 pages/api/cpl/updateDigitalOutputs.ts diff --git a/components/main/einausgaenge/modals/OutputModal.tsx b/components/main/einausgaenge/modals/OutputModal.tsx index 36e8753..fd7538f 100644 --- a/components/main/einausgaenge/modals/OutputModal.tsx +++ b/components/main/einausgaenge/modals/OutputModal.tsx @@ -1,5 +1,7 @@ -"use client"; // /compoenents/main/einausgaenge/modals/OutputModal.tsx +"use client"; // /components/main/einausgaenge/modals/OutputModal.tsx import React, { useState } from "react"; +import { useSelector } from "react-redux"; +import { RootState } from "../../../../redux/store"; export default function OutputModal({ selectedOutput, @@ -10,25 +12,45 @@ export default function OutputModal({ closeOutputModal: () => void; isOpen: boolean; }) { - if (!isOpen || !selectedOutput) return null; + const allOutputs = useSelector( + (state: RootState) => state.digitalOutputsSlice.outputs + ); const [label, setLabel] = useState(selectedOutput.label || ""); const [status, setStatus] = useState(selectedOutput.status || false); - const [timer, setTimer] = useState(0); // Optional: Sekunden für temporäres Einschalten + const [timer, setTimer] = useState(0); + const [isSaving, setIsSaving] = useState(false); + const [errorMsg, setErrorMsg] = useState(""); - const handleSave = () => { - // TODO: Ersetze dies durch echten API-Call (z. B. per fetch) - console.log("🔧 Neue Einstellungen:", { - id: selectedOutput.id, - label, - status, - timer: timer > 0 ? timer : null, - }); + if (!isOpen || !selectedOutput) return null; - // Optional: Fake-Aufruf an CGI-Endpoint - // location.href = `CPL?Service/ausgaenge.ACP&DA${selectedOutput.id}=${status ? 1 : 0}`; + const handleSave = async () => { + setIsSaving(true); + setErrorMsg(""); - closeOutputModal(); + const updatedOutputs = allOutputs.map((output) => + output.id === selectedOutput.id ? { ...output, label, status } : output + ); + + try { + const res = await fetch("/api/cpl/updateDigitalOutputs", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ outputs: updatedOutputs }), + }); + + if (!res.ok) { + const err = await res.json(); + setErrorMsg(err?.error || "Fehler beim Speichern."); + } else { + console.log("✅ Änderungen gespeichert"); + closeOutputModal(); + } + } catch (err) { + setErrorMsg("❌ Netzwerkfehler beim Speichern."); + } finally { + setIsSaving(false); + } }; return ( @@ -75,18 +97,22 @@ export default function OutputModal({ /> + {errorMsg &&

{errorMsg}

} +
diff --git a/config/webVersion.ts b/config/webVersion.ts index 4e12319..f89a7cd 100644 --- a/config/webVersion.ts +++ b/config/webVersion.ts @@ -6,5 +6,5 @@ 2: Patch oder Hotfix (Bugfixes oder kleine Änderungen). */ -const webVersion = "1.6.331"; +const webVersion = "1.6.332"; export default webVersion; diff --git a/pages/api/cpl/updateDigitalOutputs.ts b/pages/api/cpl/updateDigitalOutputs.ts new file mode 100644 index 0000000..2461bbe --- /dev/null +++ b/pages/api/cpl/updateDigitalOutputs.ts @@ -0,0 +1,46 @@ +// /pages/api/cpl/updateDigitalOutputs.ts + +import { NextApiRequest, NextApiResponse } from "next"; +import path from "path"; +import fs from "fs/promises"; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + if (req.method !== "POST") { + return res.status(405).json({ error: "Method not allowed" }); + } + + const filePath = path.join( + process.cwd(), + "apiMockData", + "SERVICE", + "digitaleAusgaengeMockData.js" + ); + + try { + const { outputs } = req.body; + + if (!Array.isArray(outputs) || outputs.length !== 4) { + return res + .status(400) + .json({ error: "Ungültiges Datenformat (4 Einträge erwartet)" }); + } + + const stateArray = outputs.map((o) => (o.status ? 1 : 0)).join(", "); + const labelArray = outputs.map((o) => `"${o.label}"`).join(", "); + + const fileContent = `win_da_state = [${stateArray}];\nwin_da_bezeichnung = [${labelArray}];\n`; + + await fs.writeFile(filePath, fileContent, "utf-8"); + + return res + .status(200) + .json({ message: "Mockdaten erfolgreich gespeichert." }); + } catch (error) { + return res + .status(500) + .json({ error: "Speichern fehlgeschlagen", detail: error }); + } +} diff --git a/public/CPL/SERVICE/da.js b/public/CPL/SERVICE/da.js index 151ce9d..f1c4210 100644 --- a/public/CPL/SERVICE/da.js +++ b/public/CPL/SERVICE/da.js @@ -1,5 +1,4 @@ - - +// /public/CPL/SERVICE/da.js var win_da_state=[<%=DES80%>,<%=DES81%>,<%=DES82%>,<%=DES83%>]; var win_da_bezeichnung=["Augang1","Ausgang2","Ausgang3","Ausgang4"]; // weil es gibt noch kein Platzhalter