loadWindowVariables und variablesSlice entfernt und die daten ausgelagert und chaeckSession.ts erstellt
This commit is contained in:
@@ -6,5 +6,5 @@
|
||||
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
||||
|
||||
*/
|
||||
const webVersion = "1.6.173";
|
||||
const webVersion = "1.6.174";
|
||||
export default webVersion;
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Provider } from "react-redux";
|
||||
import store, { useAppDispatch } from "../redux/store";
|
||||
import { loadWindowVariables } from "../utils/loadWindowVariables";
|
||||
import { checkSession } from "../utils/checkSession";
|
||||
import Header from "../components/header/Header";
|
||||
import Navigation from "../components/navigation/Navigation";
|
||||
import Footer from "../components/footer/Footer";
|
||||
@@ -23,28 +23,16 @@ function MyApp({ Component, pageProps }: AppProps) {
|
||||
function AppContent({ Component, pageProps }: AppProps) {
|
||||
const dispatch = useAppDispatch();
|
||||
const [sessionExpired, setSessionExpired] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const loadAndStoreVariables = async () => {
|
||||
try {
|
||||
const variables = await loadWindowVariables();
|
||||
if (!variables) throw new Error("Sitzungsfehler");
|
||||
|
||||
//console.log("✅ Window-Variablen geladen:", variables);
|
||||
|
||||
const { ...restVariables } = variables;
|
||||
|
||||
setSessionExpired(false);
|
||||
} catch (error) {
|
||||
console.error("❌ Fehler beim Laden der Sitzung:", error);
|
||||
setSessionExpired(true);
|
||||
}
|
||||
const sessionChecker = async () => {
|
||||
const ok = await checkSession();
|
||||
setSessionExpired(!ok);
|
||||
};
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
loadAndStoreVariables();
|
||||
sessionChecker();
|
||||
|
||||
const intervalId = setInterval(loadAndStoreVariables, 10000);
|
||||
const intervalId = setInterval(sessionChecker, 10000);
|
||||
return () => clearInterval(intervalId);
|
||||
}
|
||||
}, []);
|
||||
|
||||
14
utils/checkSession.ts
Normal file
14
utils/checkSession.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
// utils/checkSession.ts
|
||||
|
||||
export async function checkSession(): Promise<boolean> {
|
||||
try {
|
||||
// Beispiel: prüfe ob ein erwartetes globales Fenster-Objekt noch existiert
|
||||
const expectedGlobalVar = "win_kueID"; // oder "win_deviceName", je nach System
|
||||
|
||||
if (typeof window === "undefined") return false;
|
||||
|
||||
return window[expectedGlobalVar] !== undefined;
|
||||
} catch (error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
// /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_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",
|
||||
];
|
||||
|
||||
const scripts: string[] = ["kueData.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);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user