101 lines
2.8 KiB
TypeScript
101 lines
2.8 KiB
TypeScript
// /utils/loadWindowVariables.ts
|
|
|
|
// ✅ Interface für `window`-Objekt zur TypeScript-Sicherheit
|
|
interface CustomWindow extends Window {
|
|
[key: string]: any;
|
|
}
|
|
|
|
// ✅ Hauptfunktion zum Laden von `window`-Variablen
|
|
export async function loadWindowVariables(): Promise<Record<string, any>> {
|
|
return new Promise((resolve, reject) => {
|
|
const requiredVars: string[] = [
|
|
"win_deviceName",
|
|
"win_mac1",
|
|
"win_ip",
|
|
"win_subnet",
|
|
"win_gateway",
|
|
"win_cplInternalTimestamp",
|
|
"win_ntp1",
|
|
"win_ntp2",
|
|
"win_ntp3",
|
|
"win_systemZeit",
|
|
"win_ntpTimezone",
|
|
"win_ntpActive",
|
|
"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[] = ["system.js", "kueData.js"];
|
|
|
|
// ✅ Erkenne Umgebung anhand von `window.location.hostname`
|
|
const isDev = window.location.hostname === "localhost";
|
|
|
|
const loadScript = (src: string): Promise<void> => {
|
|
return new Promise((resolve, reject) => {
|
|
const script = document.createElement("script");
|
|
script.src = isDev
|
|
? `/CPLmockData/SERVICE/${src}` // Entwicklungsumgebung
|
|
: `/CPL?/CPL/SERVICE/${src}`; // Produktionsumgebung
|
|
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;
|
|
},
|
|
{}
|
|
);
|
|
|
|
resolve(variablesObj);
|
|
})
|
|
.catch((error) => {
|
|
console.error("❌ Fehler beim Laden eines Skripts:", error);
|
|
reject(error);
|
|
});
|
|
});
|
|
}
|