refactor: digitale Ausgänge über eigenen Service und Redux Thunk laden
- neue Datei fetchDigitalOutputs.ts liest win_da_state und win_da_bezeichnung aus window - fetchDigitalOutputsThunk.ts verwendet den Service und befüllt Redux Slice - entfernt alte Logik aus loadWindowVariables.ts - verbessert Performance und Struktur, lädt Ausgänge nur bei Bedarf
This commit is contained in:
@@ -6,5 +6,5 @@
|
||||
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
||||
|
||||
*/
|
||||
const webVersion = "1.6.166";
|
||||
const webVersion = "1.6.167";
|
||||
export default webVersion;
|
||||
|
||||
@@ -12,6 +12,7 @@ import { useDispatch } from "react-redux";
|
||||
import { AppDispatch } from "../redux/store";
|
||||
import { setDigitalOutputs } from "../redux/slices/digitalOutputsSlice";
|
||||
import { fetchDigitaleEingaengeThunk } from "../redux/thunks/fetchDigitaleEingaengeThunk";
|
||||
import { fetchDigitalOutputsThunk } from "../redux/thunks/fetchDigitalOutputsThunk";
|
||||
const EinAusgaenge: React.FC = () => {
|
||||
const dispatch = useDispatch<AppDispatch>();
|
||||
const { digitalOutputs, isLoading: isLoadingOutputs } = useDigitalOutputs();
|
||||
@@ -74,6 +75,15 @@ const EinAusgaenge: React.FC = () => {
|
||||
return () => clearInterval(interval);
|
||||
}
|
||||
}, [dispatch]);
|
||||
//---------------------------------------------------------
|
||||
useEffect(() => {
|
||||
dispatch(fetchDigitalOutputsThunk());
|
||||
const interval = setInterval(() => {
|
||||
dispatch(fetchDigitalOutputsThunk());
|
||||
}, 10000);
|
||||
return () => clearInterval(interval);
|
||||
}, [dispatch]);
|
||||
|
||||
//---------------------------------------------------------
|
||||
|
||||
return (
|
||||
|
||||
13
redux/thunks/fetchDigitalOutputsThunk.ts
Normal file
13
redux/thunks/fetchDigitalOutputsThunk.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
// ✅ Thunk: /redux/thunks/fetchDigitalOutputsThunk.ts
|
||||
|
||||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||
import { fetchDigitalOutputs } from "../../services/fetchDigitalOutputs";
|
||||
import { setDigitalOutputs } from "../slices/digitalOutputsSlice";
|
||||
|
||||
export const fetchDigitalOutputsThunk = createAsyncThunk(
|
||||
"digitalOutputs/fetch",
|
||||
async (_, { dispatch }) => {
|
||||
const outputs = await fetchDigitalOutputs();
|
||||
dispatch(setDigitalOutputs(outputs));
|
||||
}
|
||||
);
|
||||
26
services/fetchDigitalOutputs.ts
Normal file
26
services/fetchDigitalOutputs.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
// ✅ Service: /services/fetchDigitalOutputs.ts
|
||||
|
||||
export const fetchDigitalOutputs = async () => {
|
||||
const win = window as any;
|
||||
|
||||
const state = win.win_da_state;
|
||||
const labels = win.win_da_bezeichnung;
|
||||
|
||||
if (
|
||||
Array.isArray(state) &&
|
||||
Array.isArray(labels) &&
|
||||
state.length === labels.length
|
||||
) {
|
||||
return state.map((status: number, index: number) => ({
|
||||
id: index + 1,
|
||||
label: labels[index] || `Ausgang ${index + 1}`,
|
||||
status: status === 1,
|
||||
}));
|
||||
} else {
|
||||
console.warn("⚠️ Digitale Ausgänge unvollständig oder inkonsistent:", {
|
||||
state,
|
||||
labels,
|
||||
});
|
||||
return [];
|
||||
}
|
||||
};
|
||||
@@ -1,52 +1,14 @@
|
||||
// /utils/loadWindowVariables.ts
|
||||
import store from "../redux/store";
|
||||
import { setSystemSettings } from "../redux/slices/systemSettingsSlice";
|
||||
import {
|
||||
toggleOpcUaServer,
|
||||
setOpcUaEncryption,
|
||||
setOpcUaZustand,
|
||||
setOpcUaActiveClientCount,
|
||||
setOpcUaNodesetName,
|
||||
addOpcUaUser,
|
||||
removeOpcUaUser,
|
||||
} from "../redux/slices/opcuaSettingsSlice";
|
||||
import { setDigitalOutputs } from "../redux/slices/digitalOutputsSlice";
|
||||
|
||||
// ✅ Interface für `window`-Objekt zur TypeScript-Sicherheit
|
||||
interface CustomWindow extends Window {
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// ✅ Interface für System-Einstellungen im Redux-Store
|
||||
interface SystemSettings {
|
||||
deviceName: string;
|
||||
mac1: string;
|
||||
ip: string;
|
||||
subnet: string;
|
||||
gateway: string;
|
||||
cplInternalTimestamp: string;
|
||||
ntp1: string;
|
||||
ntp2: string;
|
||||
ntp3: string;
|
||||
ntpTimezone: string;
|
||||
ntpActive: boolean;
|
||||
}
|
||||
|
||||
// ✅ Interface für OPC-UA Einstellungen im Redux-Store
|
||||
interface OpcUaSettings {
|
||||
isEnabled: boolean;
|
||||
encryption: string;
|
||||
opcUaZustand: string;
|
||||
opcUaActiveClientCount: number;
|
||||
opcUaNodesetName: string;
|
||||
users: { id: number; username: string; password: string }[];
|
||||
}
|
||||
|
||||
// ✅ Hauptfunktion zum Laden von `window`-Variablen
|
||||
export async function loadWindowVariables(): Promise<Record<string, any>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const requiredVars: string[] = [
|
||||
"win_de_state",
|
||||
"win_counter",
|
||||
"win_flutter",
|
||||
"win_kueOnline",
|
||||
@@ -73,8 +35,6 @@ export async function loadWindowVariables(): Promise<Record<string, any>> {
|
||||
"win_kueOverflow",
|
||||
"win_tdrLast",
|
||||
"win_appVersion",
|
||||
"win_da_state",
|
||||
"win_da_bezeichnung",
|
||||
];
|
||||
|
||||
const scripts: string[] = [
|
||||
@@ -132,32 +92,3 @@ export async function loadWindowVariables(): Promise<Record<string, any>> {
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ✅ Funktion zum Speichern der digitalen Ausgänge in Redux
|
||||
const loadAndStoreDigitalOutputs = (win: CustomWindow) => {
|
||||
console.log("Prüfe digitale Ausgänge in window:");
|
||||
console.log("win_da_state:", win.win_da_state);
|
||||
console.log("win_da_bezeichnung:", win.win_da_bezeichnung);
|
||||
|
||||
if (
|
||||
Array.isArray(win.win_da_state) &&
|
||||
Array.isArray(win.win_da_bezeichnung) &&
|
||||
win.win_da_state.length === win.win_da_bezeichnung.length
|
||||
) {
|
||||
const digitalOutputs = win.win_da_state.map(
|
||||
(status: number, index: number) => ({
|
||||
id: index + 1,
|
||||
label: win.win_da_bezeichnung[index] || `Ausgang ${index + 1}`,
|
||||
status: status === 1, // Status in Boolean umwandeln
|
||||
})
|
||||
);
|
||||
|
||||
console.log("Dispatching digitalOutputs:", digitalOutputs);
|
||||
store.dispatch(setDigitalOutputs(digitalOutputs));
|
||||
} else {
|
||||
console.warn("Digitale Ausgänge konnten nicht geladen werden:", {
|
||||
da_state: win.win_da_state,
|
||||
da_bezeichnung: win.win_da_bezeichnung,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user