- Systeminformationen (IP, Subnetz, Gateway, NTP, etc.) werden nun über fetchSystemSettingsThunk geladen - loadWindowVariables.ts von systemSettings-Logik bereinigt - Aufruf erfolgt lokal in NetworkInfo.tsx statt global in _app.tsx - Verbesserte Struktur, reduzierte Netzwerklast, klarere Trennung der Zuständigkeiten
164 lines
4.5 KiB
TypeScript
164 lines
4.5 KiB
TypeScript
// /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",
|
|
"win_kueID",
|
|
"win_kuePSTmMinus96V",
|
|
"win_kueAlarm1",
|
|
"win_kueAlarm2",
|
|
"win_kueIso",
|
|
"win_kueResidence",
|
|
"win_kueCableBreak",
|
|
"win_kueGroundFault",
|
|
"win_kueLimit1",
|
|
"win_kueLimit2Low",
|
|
"win_kueDelay1",
|
|
"win_kueLoopInterval",
|
|
"win_kueVersion",
|
|
"win_tdrAtten",
|
|
"win_tdrPulse",
|
|
"win_tdrSpeed",
|
|
"win_tdrAmp",
|
|
"win_tdrTrigger",
|
|
"win_tdrLocation",
|
|
"win_tdrActive",
|
|
"win_kueOverflow",
|
|
"win_tdrLast",
|
|
"win_appVersion",
|
|
"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<void> => {
|
|
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);
|
|
});
|
|
};
|
|
|
|
// ✅ Lade alle Skripte nacheinander
|
|
scripts
|
|
.reduce(
|
|
(promise, script) => promise.then(() => loadScript(script)),
|
|
Promise.resolve()
|
|
)
|
|
.then(() => {
|
|
const win = window as unknown as CustomWindow;
|
|
|
|
// ✅ Erstelle ein Objekt mit allen geladenen Variablen
|
|
const variablesObj: Record<string, any> = requiredVars.reduce(
|
|
(acc, variable) => {
|
|
if (win[variable] !== undefined) {
|
|
acc[variable.replace("win_", "")] = win[variable];
|
|
}
|
|
return acc;
|
|
},
|
|
{}
|
|
);
|
|
|
|
// ✅ Redux mit Systemvariablen aktualisieren
|
|
|
|
resolve(variablesObj);
|
|
})
|
|
.catch((error) => {
|
|
console.error("Fehler beim Laden eines Skripts:", error);
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|
|
|
|
// ✅ 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,
|
|
});
|
|
}
|
|
};
|