refactor: Lade alle window-basierten .js-Dateien dynamisch und umgebungsabhängig

- Alle Services (ae.js, de.js, da.js, kueData.js, Start.js, System.js, opcua.js) laden ihre Scripte abhängig von der Umgebung
- Vermeidet unnötige globale Script-Ladung über loadWindowVariables.ts
- Reduziert Netzwerklast und verbessert Modularität und Performance
This commit is contained in:
ISA
2025-03-26 14:13:18 +01:00
parent db67ba0709
commit 9e282c9ae5
12 changed files with 244 additions and 101 deletions

View File

@@ -1,49 +1,41 @@
/**
* Bestimmt die richtige API-URL für digitale Eingänge basierend auf Umgebung.
*/
const getApiUrl = () => {
if (typeof window === "undefined") {
console.error("❌ `window` ist nicht verfügbar (Server-Side Rendering)");
return null;
}
// ✅ Service: /services/fetchDigitaleEingaenge.ts
return process.env.NODE_ENV === "development"
? `${window.location.origin}/CPLmockData/SERVICE/de.js`
: `${window.location.origin}/CPL?/CPL/SERVICE/de.js`;
};
/**
* Holt die digitalen Eingänge und formatiert die Daten für Redux.
*/
export const fetchDigitaleEingaenge = async () => {
try {
const apiUrl = getApiUrl();
if (!apiUrl) return null;
if (typeof window === "undefined") return null;
// console.log(`📡 API-Request an: ${apiUrl}`);
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`❌ Fehler: ${response.status} ${response.statusText}`);
// ✅ de.js nur bei Bedarf nachladen (Pfad abhängig von Umgebung)
const scriptSrc =
process.env.NODE_ENV === "production"
? "/CPL?/CPL/SERVICE/de.js"
: "/CPLmockData/SERVICE/de.js";
await new Promise<void>((resolve, reject) => {
const script = document.createElement("script");
script.src = scriptSrc;
script.async = true;
script.onload = () => resolve();
script.onerror = () => reject("❌ Fehler beim Laden von de.js");
document.body.appendChild(script);
});
const win = window as any;
if (!Array.isArray(win.win_de_state)) {
console.warn("⚠️ win_de_state ist nicht vorhanden oder kein Array");
return [];
}
const rawData = await response.text();
// console.log("✅ Rohdaten erfolgreich geladen:", rawData);
const formattedData = win.win_de_state.map(
(status: number, index: number) => ({
id: index + 1,
label: win.win_de_label?.[index] || `DE${index + 1}`,
status: status === 1,
counter: win.win_counter?.[index] || 0,
flutter: win.win_flutter?.[index] || 0,
})
);
// **JavaScript-Variablen als Skript einfügen**
const script = document.createElement("script");
script.innerHTML = rawData;
document.body.appendChild(script);
// **Daten ins Redux-Format umwandeln**
const formattedData = win_de_state.map((status, index) => ({
id: index + 1,
label: win_de_label[index] || `DE${index + 1}`,
status: status === 1,
counter: win_counter[index] || 0,
flutter: win_flutter[index] || 0,
}));
// console.log("✅ Formatierte Daten:", formattedData);
return formattedData;
} catch (error) {
console.error("❌ Fehler beim Laden der digitalen Eingänge:", error);