feat: Externalize script loading logic to improve code modularity and reusability
- Moved the logic for loading window variables from the server into a new utility function `loadWindowVariables.js`. - Updated `Header` and `Dashboard` components to use the new utility function for fetching and setting window variables. - Improved code readability and maintainability by centralizing the script loading process.
This commit is contained in:
81
utils/loadWindowVariables.js
Normal file
81
utils/loadWindowVariables.js
Normal file
@@ -0,0 +1,81 @@
|
||||
// utils/loadWindowVariables.js
|
||||
|
||||
export async function loadWindowVariables(apiUrl) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// Funktion zum Laden eines Skripts und Setzen der `window`-Variablen
|
||||
const loadScript = (src) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const script = document.createElement("script");
|
||||
script.src = `${apiUrl}/CPL?${src}`;
|
||||
script.async = true;
|
||||
script.onload = () => {
|
||||
resolve();
|
||||
};
|
||||
script.onerror = (error) => {
|
||||
reject(error);
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
};
|
||||
|
||||
// Lade das Skript, das alle Variablen enthält
|
||||
loadScript("last20Messages.acp")
|
||||
.then(() => {
|
||||
// Prüfen, ob alle Variablen verfügbar sind und sie in die Konsole ausgeben
|
||||
if (window.last20Messages) {
|
||||
console.log("Systemvariablen geladen:", {
|
||||
// last20Messages.acp
|
||||
last20Messages: window.last20Messages,
|
||||
// System.acp Variablen
|
||||
deviceName: window.deviceName,
|
||||
mac1: window.mac1,
|
||||
mac2: window.mac2,
|
||||
ip: window.ip,
|
||||
subnet: window.subnet,
|
||||
gateway: window.gateway,
|
||||
datetime: window.datetime,
|
||||
// de.acp Variablen
|
||||
de: window.de,
|
||||
counter: window.counter,
|
||||
flutter: window.flutter,
|
||||
// kueConfig.acp Variablen
|
||||
kueOnline: window.kueOnline,
|
||||
kueID: window.kueID,
|
||||
kueIso: window.kueIso,
|
||||
// kuedetail.acp Variablen
|
||||
kueValid: window.kueValid,
|
||||
kueAlarm1: window.kueAlarm1,
|
||||
kueAlarm2: window.kueAlarm2,
|
||||
kueRes: window.kueRes,
|
||||
kueCableBreak: window.kueCableBreak,
|
||||
kueGroundFault: window.kueGroundFault,
|
||||
kueLimit1: window.kueLimit1,
|
||||
kueLimit2Low: window.kueLimit2Low,
|
||||
kueLimit2High: window.kueLimit2High,
|
||||
kueDelay1: window.kueDelay1,
|
||||
kueLoopInterval: window.kueLoopInterval,
|
||||
kueVersion: window.kueVersion,
|
||||
tdrAtten: window.tdrAtten,
|
||||
tdrPulse: window.tdrPulse,
|
||||
tdrSpeed: window.tdrSpeed,
|
||||
tdrAmp: window.tdrAmp,
|
||||
tdrTrigger: window.tdrTrigger,
|
||||
tdrLocation: window.tdrLocation,
|
||||
tdrActive: window.tdrActive,
|
||||
kueOverflow: window.kueOverflow,
|
||||
kue100V: window.kue100V,
|
||||
kueResidence: window.kueResidence,
|
||||
tdrLastMeasurement: window.tdrLastMeasurement,
|
||||
kueBooting: window.kueBooting,
|
||||
});
|
||||
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error("Konnte last20Messages nicht finden."));
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user