refactor: digitale Ausgänge über eigenen Service und Redux Thunk laden

- 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
This commit is contained in:
ISA
2025-03-26 12:11:59 +01:00
parent 8a4764a372
commit db67ba0709
5 changed files with 50 additions and 70 deletions

View File

@@ -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<Record<string, any>> {
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<Record<string, any>> {
"win_kueOverflow",
"win_tdrLast",
"win_appVersion",
"win_da_state",
"win_da_bezeichnung",
];
const scripts: string[] = [
@@ -132,32 +92,3 @@ export async function loadWindowVariables(): Promise<Record<string, any>> {
});
});
}
// ✅ 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,
});
}
};