From db67ba070926b4b6fda1564cfad166610c163021 Mon Sep 17 00:00:00 2001 From: ISA Date: Wed, 26 Mar 2025 12:11:59 +0100 Subject: [PATCH] =?UTF-8?q?refactor:=20digitale=20Ausg=C3=A4nge=20=C3=BCbe?= =?UTF-8?q?r=20eigenen=20Service=20und=20Redux=20Thunk=20laden?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - neue Datei fetchDigitalOutputs.ts liest win_da_state und win_da_bezeichnung aus window - fetchDigitalOutputsThunk.ts verwendet den Service und befüllt Redux Slice - entfernt alte Logik aus loadWindowVariables.ts - verbessert Performance und Struktur, lädt Ausgänge nur bei Bedarf --- config/webVersion.ts | 2 +- pages/einausgaenge.tsx | 10 ++++ redux/thunks/fetchDigitalOutputsThunk.ts | 13 +++++ services/fetchDigitalOutputs.ts | 26 +++++++++ utils/loadWindowVariables.ts | 69 ------------------------ 5 files changed, 50 insertions(+), 70 deletions(-) create mode 100644 redux/thunks/fetchDigitalOutputsThunk.ts create mode 100644 services/fetchDigitalOutputs.ts diff --git a/config/webVersion.ts b/config/webVersion.ts index 7eba3b5..556a936 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.166"; +const webVersion = "1.6.167"; export default webVersion; diff --git a/pages/einausgaenge.tsx b/pages/einausgaenge.tsx index 00a4b17..281e244 100644 --- a/pages/einausgaenge.tsx +++ b/pages/einausgaenge.tsx @@ -12,6 +12,7 @@ import { useDispatch } from "react-redux"; import { AppDispatch } from "../redux/store"; import { setDigitalOutputs } from "../redux/slices/digitalOutputsSlice"; import { fetchDigitaleEingaengeThunk } from "../redux/thunks/fetchDigitaleEingaengeThunk"; +import { fetchDigitalOutputsThunk } from "../redux/thunks/fetchDigitalOutputsThunk"; const EinAusgaenge: React.FC = () => { const dispatch = useDispatch(); const { digitalOutputs, isLoading: isLoadingOutputs } = useDigitalOutputs(); @@ -74,6 +75,15 @@ const EinAusgaenge: React.FC = () => { return () => clearInterval(interval); } }, [dispatch]); + //--------------------------------------------------------- + useEffect(() => { + dispatch(fetchDigitalOutputsThunk()); + const interval = setInterval(() => { + dispatch(fetchDigitalOutputsThunk()); + }, 10000); + return () => clearInterval(interval); + }, [dispatch]); + //--------------------------------------------------------- return ( diff --git a/redux/thunks/fetchDigitalOutputsThunk.ts b/redux/thunks/fetchDigitalOutputsThunk.ts new file mode 100644 index 0000000..d568d86 --- /dev/null +++ b/redux/thunks/fetchDigitalOutputsThunk.ts @@ -0,0 +1,13 @@ +// ✅ Thunk: /redux/thunks/fetchDigitalOutputsThunk.ts + +import { createAsyncThunk } from "@reduxjs/toolkit"; +import { fetchDigitalOutputs } from "../../services/fetchDigitalOutputs"; +import { setDigitalOutputs } from "../slices/digitalOutputsSlice"; + +export const fetchDigitalOutputsThunk = createAsyncThunk( + "digitalOutputs/fetch", + async (_, { dispatch }) => { + const outputs = await fetchDigitalOutputs(); + dispatch(setDigitalOutputs(outputs)); + } +); diff --git a/services/fetchDigitalOutputs.ts b/services/fetchDigitalOutputs.ts new file mode 100644 index 0000000..7a6f0b8 --- /dev/null +++ b/services/fetchDigitalOutputs.ts @@ -0,0 +1,26 @@ +// ✅ Service: /services/fetchDigitalOutputs.ts + +export const fetchDigitalOutputs = async () => { + const win = window as any; + + const state = win.win_da_state; + const labels = win.win_da_bezeichnung; + + if ( + Array.isArray(state) && + Array.isArray(labels) && + state.length === labels.length + ) { + return state.map((status: number, index: number) => ({ + id: index + 1, + label: labels[index] || `Ausgang ${index + 1}`, + status: status === 1, + })); + } else { + console.warn("⚠️ Digitale Ausgänge unvollständig oder inkonsistent:", { + state, + labels, + }); + return []; + } +}; diff --git a/utils/loadWindowVariables.ts b/utils/loadWindowVariables.ts index 5c1333e..e9d55b3 100644 --- a/utils/loadWindowVariables.ts +++ b/utils/loadWindowVariables.ts @@ -1,52 +1,14 @@ // /utils/loadWindowVariables.ts -import store from "../redux/store"; -import { setSystemSettings } from "../redux/slices/systemSettingsSlice"; -import { - toggleOpcUaServer, - setOpcUaEncryption, - setOpcUaZustand, - setOpcUaActiveClientCount, - setOpcUaNodesetName, - addOpcUaUser, - removeOpcUaUser, -} from "../redux/slices/opcuaSettingsSlice"; -import { setDigitalOutputs } from "../redux/slices/digitalOutputsSlice"; // ✅ Interface für `window`-Objekt zur TypeScript-Sicherheit interface CustomWindow extends Window { [key: string]: any; } -// ✅ Interface für System-Einstellungen im Redux-Store -interface SystemSettings { - deviceName: string; - mac1: string; - ip: string; - subnet: string; - gateway: string; - cplInternalTimestamp: string; - ntp1: string; - ntp2: string; - ntp3: string; - ntpTimezone: string; - ntpActive: boolean; -} - -// ✅ Interface für OPC-UA Einstellungen im Redux-Store -interface OpcUaSettings { - isEnabled: boolean; - encryption: string; - opcUaZustand: string; - opcUaActiveClientCount: number; - opcUaNodesetName: string; - users: { id: number; username: string; password: string }[]; -} - // ✅ Hauptfunktion zum Laden von `window`-Variablen export async function loadWindowVariables(): Promise> { return new Promise((resolve, reject) => { const requiredVars: string[] = [ - "win_de_state", "win_counter", "win_flutter", "win_kueOnline", @@ -73,8 +35,6 @@ export async function loadWindowVariables(): Promise> { "win_kueOverflow", "win_tdrLast", "win_appVersion", - "win_da_state", - "win_da_bezeichnung", ]; const scripts: string[] = [ @@ -132,32 +92,3 @@ export async function loadWindowVariables(): Promise> { }); }); } - -// ✅ Funktion zum Speichern der digitalen Ausgänge in Redux -const loadAndStoreDigitalOutputs = (win: CustomWindow) => { - console.log("Prüfe digitale Ausgänge in window:"); - console.log("win_da_state:", win.win_da_state); - console.log("win_da_bezeichnung:", win.win_da_bezeichnung); - - if ( - Array.isArray(win.win_da_state) && - Array.isArray(win.win_da_bezeichnung) && - win.win_da_state.length === win.win_da_bezeichnung.length - ) { - const digitalOutputs = win.win_da_state.map( - (status: number, index: number) => ({ - id: index + 1, - label: win.win_da_bezeichnung[index] || `Ausgang ${index + 1}`, - status: status === 1, // Status in Boolean umwandeln - }) - ); - - console.log("Dispatching digitalOutputs:", digitalOutputs); - store.dispatch(setDigitalOutputs(digitalOutputs)); - } else { - console.warn("Digitale Ausgänge konnten nicht geladen werden:", { - da_state: win.win_da_state, - da_bezeichnung: win.win_da_bezeichnung, - }); - } -};