- DigitalOutputs.tsx auf Redux umgestellt, um Werte direkt aus dem Store zu lesen - toggleSwitch-Funktion angepasst, um den Status von digitalen Ausgängen in Redux zu aktualisieren - useDigitalOutputsData.ts nutzt nun Redux zum Speichern der `win_da_state` und `win_da_bezeichnung` Werte - Entfernen von Prop `digitalOutputs` in `DigitalOutputs.tsx`, da Redux nun als Datenquelle dient - Weboberfläche zeigt nun digitale Ausgänge korrekt an und Status kann geändert werden
68 lines
2.1 KiB
TypeScript
68 lines
2.1 KiB
TypeScript
// hooks/windowvariables/useLoadWindowVariables.ts
|
|
import { useEffect } from "react";
|
|
import { useDispatch } from "react-redux";
|
|
import { setDigitalOutputs } from "../../redux/slices/digitalOutputsSlice";
|
|
|
|
const requiredVars: string[] = ["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);
|
|
});
|
|
};
|
|
|
|
export const useLoadWindowVariables = () => {
|
|
const dispatch = useDispatch();
|
|
|
|
useEffect(() => {
|
|
const loadVariables = async () => {
|
|
try {
|
|
await loadScript("da.js"); // Lade `da.js` zuerst
|
|
for (const script of scripts) {
|
|
if (script !== "da.js") await loadScript(script);
|
|
}
|
|
|
|
console.log("Laden abgeschlossen. Jetzt werden Variablen geprüft.");
|
|
console.log("win_da_state:", window.win_da_state);
|
|
console.log("win_da_bezeichnung:", window.win_da_bezeichnung);
|
|
|
|
if (window.win_da_state && window.win_da_bezeichnung) {
|
|
const digitalOutputs = window.win_da_state.map(
|
|
(status: number, index: number) => ({
|
|
id: index + 1,
|
|
label: window.win_da_bezeichnung[index] || `Ausgang ${index + 1}`,
|
|
status: status === 1,
|
|
})
|
|
);
|
|
|
|
console.log("Dispatching digitalOutputs:", digitalOutputs);
|
|
dispatch(setDigitalOutputs(digitalOutputs));
|
|
}
|
|
} catch (error) {
|
|
console.error("Fehler beim Laden der Skripte:", error);
|
|
}
|
|
};
|
|
|
|
loadVariables();
|
|
}, [dispatch]);
|
|
};
|