From c8619f96dede4d702258caa13496c71365ca7ad9 Mon Sep 17 00:00:00 2001 From: ISA Date: Wed, 26 Mar 2025 15:37:11 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20fetchKueData=20mit=20vollst=C3=A4ndiger?= =?UTF-8?q?=20Extraktion=20aus=20kueData.js=20ersetzt=20loadWindowVariable?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dynamisches Laden von kueData.js nur bei Bedarf - Alle benötigten Variablen wie kueAlarm1, kueCableBreak etc. extrahiert - Fehleranzeige in KabelModulStatus funktioniert wieder korrekt - loadWindowVariables.ts für KUE vollständig ersetzt --- .../main/uebersicht/Baugruppentraeger.tsx | 13 ++- .../modulesStatus/KabelModulStatus.tsx | 10 ++- config/webVersion.ts | 2 +- .../windowVariables/useLoadWindowVariables.ts | 67 -------------- pages/einstellungen.tsx | 41 +-------- redux/slices/kueDataSlice.ts | 60 ++++++------- services/fetchKueData.ts | 87 ++++++++++++------- 7 files changed, 104 insertions(+), 176 deletions(-) delete mode 100644 hooks/windowVariables/useLoadWindowVariables.ts diff --git a/components/main/uebersicht/Baugruppentraeger.tsx b/components/main/uebersicht/Baugruppentraeger.tsx index 7a07423..a04cb43 100644 --- a/components/main/uebersicht/Baugruppentraeger.tsx +++ b/components/main/uebersicht/Baugruppentraeger.tsx @@ -1,11 +1,13 @@ "use client"; // components/main/uebersicht/Baugruppentraeger.tsx -import React, { useMemo } from "react"; +import React, { useMemo, useEffect } from "react"; import { useSelector } from "react-redux"; import { useRouter } from "next/navigation"; -import { RootState } from "../../../redux/store"; +import { RootState, useAppDispatch } from "../../../redux/store"; import KabelModulStatus from "./modulesStatus/KabelModulStatus"; +import { fetchKueDataThunk } from "../../../redux/thunks/fetchKueDataThunk"; const Baugruppentraeger: React.FC = () => { + const dispatch = useAppDispatch(); const router = useRouter(); // useRouter für Navigation hinzufügen // Redux-Variablen direkt hier abrufen @@ -16,7 +18,7 @@ const Baugruppentraeger: React.FC = () => { kueAlarm1, kueAlarm2, kueGroundFault, - } = useSelector((state: RootState) => state.variables); + } = useSelector((state: RootState) => state.kueData); // `kueOnline` sicherstellen, dass es nur Zahlen enthält const kueOnline = useMemo( @@ -79,6 +81,11 @@ const Baugruppentraeger: React.FC = () => { ); } + //-------------------------------------------- + useEffect(() => { + dispatch(fetchKueDataThunk()); + }, [dispatch]); + //-------------------------------------------- return <>{baugruppen}; }; diff --git a/components/main/uebersicht/modulesStatus/KabelModulStatus.tsx b/components/main/uebersicht/modulesStatus/KabelModulStatus.tsx index 492fb56..c0a82e8 100644 --- a/components/main/uebersicht/modulesStatus/KabelModulStatus.tsx +++ b/components/main/uebersicht/modulesStatus/KabelModulStatus.tsx @@ -31,10 +31,12 @@ const KabelModulStatus: React.FC = ({ } // Status nur prüfen, wenn der Slot aktiv ist (kueOnline für den Slot ist 1) - const isCableBreak = kueCableBreak[slot - 1] === 1; - const isAlarm1 = kueAlarm1[slot - 1] === 1; - const isAlarm2 = kueAlarm2[slot - 1] === 1; - const groundFault = kueGroundFault[slot - 1] === 1; + const isCableBreak = + Array.isArray(kueCableBreak) && kueCableBreak[slot - 1] === 1; + const isAlarm1 = Array.isArray(kueAlarm1) && kueAlarm1[slot - 1] === 1; + const isAlarm2 = Array.isArray(kueAlarm2) && kueAlarm2[slot - 1] === 1; + const groundFault = + Array.isArray(kueGroundFault) && kueGroundFault[slot - 1] === 1; return (
diff --git a/config/webVersion.ts b/config/webVersion.ts index 0cd0a75..989a182 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.168"; +const webVersion = "1.6.169"; export default webVersion; diff --git a/hooks/windowVariables/useLoadWindowVariables.ts b/hooks/windowVariables/useLoadWindowVariables.ts deleted file mode 100644 index f03b3f1..0000000 --- a/hooks/windowVariables/useLoadWindowVariables.ts +++ /dev/null @@ -1,67 +0,0 @@ -// hooks/windowvariables/useLoadWindowVariables.ts -import { useEffect } from "react"; -import { useDispatch } from "react-redux"; -import { setDigitalOutputs } from "../../redux/slices/digitalOutputsSlice"; - -const requiredVars: string[] = ["win_da_state", "win_da_bezeichnung"]; - -const scripts: string[] = [ - "da.js", - "de.js", - "ae.js", - "kueData.js", - "Start.js", - "System.js", - "opcua.js", -]; - -const loadScript = (src: string): Promise => { - return new Promise((resolve, reject) => { - const script = document.createElement("script"); - const environment = process.env.NEXT_PUBLIC_NODE_ENV || "production"; - script.src = - environment === "production" - ? `/CPL?/CPL/SERVICE/${src}` - : `/CPLmockData/SERVICE/${src}`; - script.async = true; - script.onload = () => resolve(); - script.onerror = () => reject(new Error(`Script load error: ${src}`)); - document.head.appendChild(script); - }); -}; - -export const useLoadWindowVariables = () => { - const dispatch = useDispatch(); - - useEffect(() => { - const loadVariables = async () => { - try { - await loadScript("da.js"); // Lade `da.js` zuerst - for (const script of scripts) { - if (script !== "da.js") await loadScript(script); - } - - console.log("Laden abgeschlossen. Jetzt werden Variablen geprüft."); - console.log("win_da_state:", window.win_da_state); - console.log("win_da_bezeichnung:", window.win_da_bezeichnung); - - if (window.win_da_state && window.win_da_bezeichnung) { - const digitalOutputs = window.win_da_state.map( - (status: number, index: number) => ({ - id: index + 1, - label: window.win_da_bezeichnung[index] || `Ausgang ${index + 1}`, - status: status === 1, - }) - ); - - console.log("Dispatching digitalOutputs:", digitalOutputs); - dispatch(setDigitalOutputs(digitalOutputs)); - } - } catch (error) { - console.error("Fehler beim Laden der Skripte:", error); - } - }; - - loadVariables(); - }, [dispatch]); -}; diff --git a/pages/einstellungen.tsx b/pages/einstellungen.tsx index 1860b39..15851bc 100644 --- a/pages/einstellungen.tsx +++ b/pages/einstellungen.tsx @@ -1,7 +1,7 @@ +// /pages/einstellungen.tsx import React, { useState, useEffect } from "react"; import { useAppDispatch } from "../redux/store"; -import { setSystemSettings } from "../redux/slices/systemSettingsSlice"; -import { loadWindowVariables } from "../utils/loadWindowVariables"; +import { fetchSystemSettingsThunk } from "../redux/thunks/fetchSystemSettingsThunk"; import GeneralSettings from "../components/main/settingsPageComponents/GeneralSettings"; import OPCUAInterfaceSettings from "../components/main/settingsPageComponents/OPCUAInterfaceSettings"; @@ -10,42 +10,7 @@ export default function Settings() { const dispatch = useAppDispatch(); useEffect(() => { - const loadSettings = async () => { - const vars = await loadWindowVariables(); - if (!vars) return; - - const { - deviceName, - mac1, - ip, - subnet, - gateway, - cplInternalTimestamp, - ntp1, - ntp2, - ntp3, - ntpTimezone, - ntpActive, - } = vars; - - dispatch( - setSystemSettings({ - deviceName, - mac1, - ip, - subnet, - gateway, - cplInternalTimestamp, - ntp1, - ntp2, - ntp3, - ntpTimezone, - ntpActive, - }) - ); - }; - - loadSettings(); + dispatch(fetchSystemSettingsThunk()); }, [dispatch]); return ( diff --git a/redux/slices/kueDataSlice.ts b/redux/slices/kueDataSlice.ts index f0bb403..c8287f9 100644 --- a/redux/slices/kueDataSlice.ts +++ b/redux/slices/kueDataSlice.ts @@ -3,39 +3,39 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; interface KueDataState { - kueOnline: boolean; - kueID: string | null; - pstMinus96V: number | null; - alarm1: number | null; - alarm2: number | null; - iso: number | null; - residence: number | null; - cableBreak: number | null; - groundFault: number | null; - limit1: number | null; - limit2Low: number | null; - delay1: number | null; - loopInterval: number | null; - kueVersion: string | null; - overflow: number | null; + kueOnline: number[]; + kueID: string[]; + pstMinus96V: number[]; + alarm1: number[]; + alarm2: number[]; + iso: number[]; + residence: number[]; + cableBreak: number[]; + groundFault: number[]; + limit1: number[]; + limit2Low: number[]; + delay1: number[]; + loopInterval: number[]; + kueVersion: number[]; + overflow: number[]; } const initialState: KueDataState = { - kueOnline: false, - kueID: null, - pstMinus96V: null, - alarm1: null, - alarm2: null, - iso: null, - residence: null, - cableBreak: null, - groundFault: null, - limit1: null, - limit2Low: null, - delay1: null, - loopInterval: null, - kueVersion: null, - overflow: null, + kueOnline: [], + kueID: [], + pstMinus96V: [], + alarm1: [], + alarm2: [], + iso: [], + residence: [], + cableBreak: [], + groundFault: [], + limit1: [], + limit2Low: [], + delay1: [], + loopInterval: [], + kueVersion: [], + overflow: [], }; const kueDataSlice = createSlice({ diff --git a/services/fetchKueData.ts b/services/fetchKueData.ts index 28d4e80..741c1fc 100644 --- a/services/fetchKueData.ts +++ b/services/fetchKueData.ts @@ -1,40 +1,61 @@ -// ✅ Service: /services/fetchKueData.ts +// /services/fetchKueData.ts export const fetchKueData = async () => { - if (typeof window === "undefined") return null; + try { + if (typeof window === "undefined") return null; - // ✅ kueData.js nur bei Bedarf nachladen (Pfad abhängig von Umgebung) - const scriptSrc = - process.env.NODE_ENV === "production" - ? "/CPL?/CPL/SERVICE/kueData.js" - : "/CPLmockData/SERVICE/kueData.js"; + // ✅ Script dynamisch laden + await new Promise((resolve, reject) => { + const script = document.createElement("script"); + const env = process.env.NEXT_PUBLIC_NODE_ENV; + script.src = + env === "production" + ? "/CPL?/CPL/SERVICE/kueData.js" + : "/CPLmockData/SERVICE/kueData.js"; + script.async = true; + script.onload = () => resolve(); + script.onerror = () => reject("Fehler beim Laden von kueData.js"); + document.body.appendChild(script); + }); - await new Promise((resolve, reject) => { - const script = document.createElement("script"); - script.src = scriptSrc; - script.async = true; - script.onload = () => resolve(); - script.onerror = () => reject("❌ Fehler beim Laden von kueData.js"); - document.body.appendChild(script); - }); + const win = window as any; - const win = window as any; + // ✅ Alle benötigten Variablen extrahieren + const keys = [ + "kueOnline", + "kueID", + "kuePSTmMinus96V", + "kueAlarm1", + "kueAlarm2", + "kueIso", + "kueResidence", + "kueCableBreak", + "kueGroundFault", + "kueLimit1", + "kueLimit2Low", + "kueDelay1", + "kueLoopInterval", + "kueVersion", + "tdrAtten", + "tdrPulse", + "tdrSpeed", + "tdrAmp", + "tdrTrigger", + "tdrLocation", + "tdrActive", + "kueOverflow", + "tdrLast", + ]; - return { - kueOnline: win.win_kueOnline ?? false, - kueID: win.win_kueID ?? null, - pstMinus96V: win.win_kuePSTmMinus96V ?? null, - alarm1: win.win_kueAlarm1 ?? null, - alarm2: win.win_kueAlarm2 ?? null, - iso: win.win_kueIso ?? null, - residence: win.win_kueResidence ?? null, - cableBreak: win.win_kueCableBreak ?? null, - groundFault: win.win_kueGroundFault ?? null, - limit1: win.win_kueLimit1 ?? null, - limit2Low: win.win_kueLimit2Low ?? null, - delay1: win.win_kueDelay1 ?? null, - loopInterval: win.win_kueLoopInterval ?? null, - kueVersion: win.win_kueVersion ?? null, - overflow: win.win_kueOverflow ?? null, - }; + const result: Record = {}; + for (const key of keys) { + const winKey = `win_${key}`; + result[key] = win[winKey] ?? []; + } + + return result; + } catch (error) { + console.error("❌ Fehler beim Laden der KUE-Daten:", error); + return null; + } };