WIP: update dititale Ausgänge Mock datei
This commit is contained in:
@@ -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 React, { useState } from "react";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { RootState } from "../../../../redux/store";
|
||||||
|
|
||||||
export default function OutputModal({
|
export default function OutputModal({
|
||||||
selectedOutput,
|
selectedOutput,
|
||||||
@@ -10,25 +12,45 @@ export default function OutputModal({
|
|||||||
closeOutputModal: () => void;
|
closeOutputModal: () => void;
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
}) {
|
}) {
|
||||||
if (!isOpen || !selectedOutput) return null;
|
const allOutputs = useSelector(
|
||||||
|
(state: RootState) => state.digitalOutputsSlice.outputs
|
||||||
|
);
|
||||||
|
|
||||||
const [label, setLabel] = useState(selectedOutput.label || "");
|
const [label, setLabel] = useState(selectedOutput.label || "");
|
||||||
const [status, setStatus] = useState(selectedOutput.status || false);
|
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 = () => {
|
if (!isOpen || !selectedOutput) return null;
|
||||||
// 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,
|
|
||||||
});
|
|
||||||
|
|
||||||
// Optional: Fake-Aufruf an CGI-Endpoint
|
const handleSave = async () => {
|
||||||
// location.href = `CPL?Service/ausgaenge.ACP&DA${selectedOutput.id}=${status ? 1 : 0}`;
|
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 (
|
return (
|
||||||
@@ -75,18 +97,22 @@ export default function OutputModal({
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{errorMsg && <p className="text-red-600 text-sm mb-2">{errorMsg}</p>}
|
||||||
|
|
||||||
<div className="flex justify-end gap-2 mt-6">
|
<div className="flex justify-end gap-2 mt-6">
|
||||||
<button
|
<button
|
||||||
onClick={closeOutputModal}
|
onClick={closeOutputModal}
|
||||||
|
disabled={isSaving}
|
||||||
className="px-4 py-2 rounded bg-gray-300 hover:bg-gray-400"
|
className="px-4 py-2 rounded bg-gray-300 hover:bg-gray-400"
|
||||||
>
|
>
|
||||||
Abbrechen
|
Abbrechen
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
onClick={handleSave}
|
onClick={handleSave}
|
||||||
|
disabled={isSaving}
|
||||||
className="px-4 py-2 rounded bg-blue-600 hover:bg-blue-700 text-white"
|
className="px-4 py-2 rounded bg-blue-600 hover:bg-blue-700 text-white"
|
||||||
>
|
>
|
||||||
Speichern
|
{isSaving ? "Speichern..." : "Speichern"}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -6,5 +6,5 @@
|
|||||||
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
const webVersion = "1.6.331";
|
const webVersion = "1.6.332";
|
||||||
export default webVersion;
|
export default webVersion;
|
||||||
|
|||||||
46
pages/api/cpl/updateDigitalOutputs.ts
Normal file
46
pages/api/cpl/updateDigitalOutputs.ts
Normal file
@@ -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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
|
// /public/CPL/SERVICE/da.js
|
||||||
|
|
||||||
var win_da_state=[<%=DES80%>,<%=DES81%>,<%=DES82%>,<%=DES83%>];
|
var win_da_state=[<%=DES80%>,<%=DES81%>,<%=DES82%>,<%=DES83%>];
|
||||||
|
|
||||||
var win_da_bezeichnung=["Augang1","Ausgang2","Ausgang3","Ausgang4"]; // weil es gibt noch kein Platzhalter
|
var win_da_bezeichnung=["Augang1","Ausgang2","Ausgang3","Ausgang4"]; // weil es gibt noch kein Platzhalter
|
||||||
|
|||||||
Reference in New Issue
Block a user