feat: Redux-Thunk für digitale Eingänge integriert & UI mit zwei Tabellen umgesetzt
- `fetchDigitaleEingaengeThunk.ts` erstellt, um digitale Eingänge in Redux zu speichern.
- `fetchDigitaleEingaenge.ts` erstellt, um API-Daten aus `de.js` zu laden.
- `digitalInputsSlice.ts` hinzugefügt, um digitale Eingänge in Redux zu verwalten.
- `DigitalInputs.tsx` überarbeitet: Zwei Tabellen für digitale Eingänge hinzugefügt.
- Sicherstellung, dass Redux-Thunk nur im Client (`useEffect`) ausgeführt wird.
- API-Calls werden nun alle 10 Sekunden aktualisiert.
✅ Jetzt läuft Redux-Thunk stabil & effizient für digitale Eingänge!
This commit is contained in:
@@ -1,15 +1,21 @@
|
|||||||
"use client"; // components/main/einausgaenge/DigitalInputs.tsx
|
"use client"; // components/main/einausgaenge/DigitalInputs.tsx
|
||||||
|
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import { RootState } from "../../../redux/store";
|
||||||
import { Icon } from "@iconify/react";
|
import { Icon } from "@iconify/react";
|
||||||
|
|
||||||
export default function DigitalInputs({
|
export default function DigitalInputs({ openInputModal }) {
|
||||||
inputsGroup1,
|
const digitalInputs = useSelector(
|
||||||
inputsGroup2,
|
(state: RootState) => state.digitalInputs.inputs
|
||||||
openInputModal,
|
);
|
||||||
}) {
|
|
||||||
|
// **Gruppiere Eingänge in zwei Tabellen**
|
||||||
|
const midIndex = Math.ceil(digitalInputs.length / 2);
|
||||||
|
const inputsGroup1 = digitalInputs.slice(0, midIndex);
|
||||||
|
const inputsGroup2 = digitalInputs.slice(midIndex);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="bg-white shadow-md rounded-lg border border-gray-200 p-4 w-3/5 flex-grow flex flex-col">
|
<div className="bg-white shadow-md rounded-lg border border-gray-200 p-4 w-full flex-grow flex flex-col">
|
||||||
<h2 className="text-md font-bold mb-4 flex items-center">
|
<h2 className="text-md font-bold mb-4 flex items-center">
|
||||||
<Icon icon="mdi:input" className="text-blue-500 mr-2 text-2xl" />
|
<Icon icon="mdi:input" className="text-blue-500 mr-2 text-2xl" />
|
||||||
Digitale Eingänge
|
Digitale Eingänge
|
||||||
@@ -39,15 +45,13 @@ export default function DigitalInputs({
|
|||||||
<td className="p-2 xl:py-0">
|
<td className="p-2 xl:py-0">
|
||||||
<span
|
<span
|
||||||
className={
|
className={
|
||||||
input.status === "active"
|
input.status ? "text-green-500" : "text-red-500"
|
||||||
? "text-green-500"
|
|
||||||
: "text-red-500"
|
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
{input.status === "active" ? "●" : "⨉"}
|
{input.status ? "●" : "⨉"}
|
||||||
</span>
|
</span>
|
||||||
</td>
|
</td>
|
||||||
<td className="p-2 xl:py-0">{input.description}</td>
|
<td className="p-2 xl:py-0">{input.label}</td>
|
||||||
<td className="p-2 xl:py-0">
|
<td className="p-2 xl:py-0">
|
||||||
<Icon
|
<Icon
|
||||||
icon="mdi:settings"
|
icon="mdi:settings"
|
||||||
|
|||||||
@@ -6,5 +6,5 @@
|
|||||||
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
2: Patch oder Hotfix (Bugfixes oder kleine Änderungen).
|
||||||
|
|
||||||
*/
|
*/
|
||||||
const webVersion = "1.6.136";
|
const webVersion = "1.6.137";
|
||||||
export default webVersion;
|
export default webVersion;
|
||||||
|
|||||||
@@ -4,7 +4,6 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Provider } from "react-redux";
|
import { Provider } from "react-redux";
|
||||||
import store, { useAppDispatch } from "../redux/store";
|
import store, { useAppDispatch } from "../redux/store";
|
||||||
import { fetchAnalogeEingaengeThunk } from "../redux/thunks/fetchAnalogeEingaengeThunk";
|
|
||||||
import { loadWindowVariables } from "../utils/loadWindowVariables";
|
import { loadWindowVariables } from "../utils/loadWindowVariables";
|
||||||
import Header from "../components/header/Header";
|
import Header from "../components/header/Header";
|
||||||
import Navigation from "../components/navigation/Navigation";
|
import Navigation from "../components/navigation/Navigation";
|
||||||
@@ -19,6 +18,8 @@ import {
|
|||||||
setOpcUaActiveClientCount,
|
setOpcUaActiveClientCount,
|
||||||
setOpcUaNodesetName,
|
setOpcUaNodesetName,
|
||||||
} from "../redux/slices/opcuaSettingsSlice";
|
} from "../redux/slices/opcuaSettingsSlice";
|
||||||
|
import { fetchAnalogeEingaengeThunk } from "../redux/thunks/fetchAnalogeEingaengeThunk";
|
||||||
|
import { fetchDigitaleEingaengeThunk } from "../redux/thunks/fetchDigitaleEingaengeThunk";
|
||||||
|
|
||||||
function MyApp({ Component, pageProps }: AppProps) {
|
function MyApp({ Component, pageProps }: AppProps) {
|
||||||
return (
|
return (
|
||||||
@@ -95,7 +96,7 @@ function AppContent({ Component, pageProps }: AppProps) {
|
|||||||
return () => clearInterval(intervalId);
|
return () => clearInterval(intervalId);
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
//---------------------------------------------------------
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
dispatch(fetchAnalogeEingaengeThunk());
|
dispatch(fetchAnalogeEingaengeThunk());
|
||||||
@@ -105,7 +106,17 @@ function AppContent({ Component, pageProps }: AppProps) {
|
|||||||
return () => clearInterval(interval);
|
return () => clearInterval(interval);
|
||||||
}
|
}
|
||||||
}, [dispatch]);
|
}, [dispatch]);
|
||||||
|
//---------------------------------------------------------
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window !== "undefined") {
|
||||||
|
dispatch(fetchDigitaleEingaengeThunk());
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
dispatch(fetchDigitaleEingaengeThunk());
|
||||||
|
}, 10000);
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}
|
||||||
|
}, [dispatch]);
|
||||||
|
//---------------------------------------------------------
|
||||||
return (
|
return (
|
||||||
<div className="flex flex-col h-screen overflow-hidden">
|
<div className="flex flex-col h-screen overflow-hidden">
|
||||||
<WindowVariablesInitializer />
|
<WindowVariablesInitializer />
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ interface DigitalInput {
|
|||||||
id: number;
|
id: number;
|
||||||
label: string;
|
label: string;
|
||||||
status: boolean;
|
status: boolean;
|
||||||
|
counter: number;
|
||||||
|
flutter: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface DigitalInputsState {
|
interface DigitalInputsState {
|
||||||
@@ -12,7 +14,7 @@ interface DigitalInputsState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const initialState: DigitalInputsState = {
|
const initialState: DigitalInputsState = {
|
||||||
inputs: [], // Initial leerer Zustand
|
inputs: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
const digitalInputsSlice = createSlice({
|
const digitalInputsSlice = createSlice({
|
||||||
@@ -32,19 +34,8 @@ const digitalInputsSlice = createSlice({
|
|||||||
input.status = status;
|
input.status = status;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
updateInputLabel: (
|
|
||||||
state,
|
|
||||||
action: PayloadAction<{ id: number; label: string }>
|
|
||||||
) => {
|
|
||||||
const { id, label } = action.payload;
|
|
||||||
const input = state.inputs.find((input) => input.id === id);
|
|
||||||
if (input) {
|
|
||||||
input.label = label;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export const { setInputs, updateInputStatus, updateInputLabel } =
|
export const { setInputs, updateInputStatus } = digitalInputsSlice.actions;
|
||||||
digitalInputsSlice.actions;
|
|
||||||
export default digitalInputsSlice.reducer;
|
export default digitalInputsSlice.reducer;
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import authReducer from "./slices/authSlice";
|
|||||||
import variablesReducer from "./slices/variablesSlice";
|
import variablesReducer from "./slices/variablesSlice";
|
||||||
import kueChartModeReducer from "./slices/kueChartModeSlice";
|
import kueChartModeReducer from "./slices/kueChartModeSlice";
|
||||||
import webVersionReducer from "./slices/webVersionSlice";
|
import webVersionReducer from "./slices/webVersionSlice";
|
||||||
import digitalInputsReducer from "./slices/digitalInputsSlice";
|
|
||||||
import kabelueberwachungChartReducer from "./slices/kabelueberwachungChartSlice";
|
import kabelueberwachungChartReducer from "./slices/kabelueberwachungChartSlice";
|
||||||
import dashboardReducer from "./slices/dashboardSlice";
|
import dashboardReducer from "./slices/dashboardSlice";
|
||||||
import systemSettingsReducer from "./slices/systemSettingsSlice";
|
import systemSettingsReducer from "./slices/systemSettingsSlice";
|
||||||
@@ -14,6 +13,7 @@ import digitalOutputsReducer from "./slices/digitalOutputsSlice";
|
|||||||
import brushReducer from "./slices/brushSlice";
|
import brushReducer from "./slices/brushSlice";
|
||||||
import tdrChartReducer from "./slices/tdrChartSlice";
|
import tdrChartReducer from "./slices/tdrChartSlice";
|
||||||
import analogeEingaengeReducer from "./slices/analogeEingaengeSlice";
|
import analogeEingaengeReducer from "./slices/analogeEingaengeSlice";
|
||||||
|
import digitalInputsReducer from "./slices/digitalInputsSlice";
|
||||||
|
|
||||||
const store = configureStore({
|
const store = configureStore({
|
||||||
reducer: {
|
reducer: {
|
||||||
|
|||||||
22
redux/thunks/fetchDigitaleEingaengeThunk.ts
Normal file
22
redux/thunks/fetchDigitaleEingaengeThunk.ts
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import { createAsyncThunk } from "@reduxjs/toolkit";
|
||||||
|
import { fetchDigitaleEingaenge } from "../../services/fetchDigitaleEingaenge";
|
||||||
|
import { setInputs } from "../slices/digitalInputsSlice";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holt digitale Eingänge von der API und speichert sie in Redux.
|
||||||
|
*/
|
||||||
|
export const fetchDigitaleEingaengeThunk = createAsyncThunk(
|
||||||
|
"digitalInputs/fetchDigitaleEingaenge",
|
||||||
|
async (_, { dispatch }) => {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const data = await fetchDigitaleEingaenge();
|
||||||
|
if (data) {
|
||||||
|
dispatch(setInputs(data)); // ✅ Redux mit API-Daten füllen
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error("❌ Fehler beim Laden der digitalen Eingänge:", error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
@@ -1,56 +0,0 @@
|
|||||||
// /redux/thunks/fetchLoopChartDataThunk.ts
|
|
||||||
import { createAsyncThunk } from "@reduxjs/toolkit";
|
|
||||||
import { fetchLoopChartData } from "../../services/fetchLoopChartData";
|
|
||||||
import { setLoopMeasurementCurveChartData } from "../slices/kabelueberwachungChartSlice";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Holt die neuesten Daten von der API und speichert sie in Redux.
|
|
||||||
*/
|
|
||||||
export const fetchLoopChartDataThunk = createAsyncThunk(
|
|
||||||
"kabelueberwachungChart/fetchLoopChartData",
|
|
||||||
async (
|
|
||||||
{
|
|
||||||
mode,
|
|
||||||
type,
|
|
||||||
slotNumber,
|
|
||||||
vonDatum,
|
|
||||||
bisDatum,
|
|
||||||
}: {
|
|
||||||
mode: "DIA0" | "DIA1" | "DIA2";
|
|
||||||
type: number;
|
|
||||||
slotNumber: number;
|
|
||||||
vonDatum: string;
|
|
||||||
bisDatum: string;
|
|
||||||
},
|
|
||||||
{ dispatch }
|
|
||||||
) => {
|
|
||||||
const data = await fetchLoopChartData(
|
|
||||||
mode,
|
|
||||||
type,
|
|
||||||
slotNumber,
|
|
||||||
vonDatum,
|
|
||||||
bisDatum
|
|
||||||
);
|
|
||||||
if (data) {
|
|
||||||
dispatch(setLoopMeasurementCurveChartData(data));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Starte automatisches Polling (alle 10 Sekunden).
|
|
||||||
*/
|
|
||||||
export const startLoopChartDataPolling = () => (dispatch: any) => {
|
|
||||||
setInterval(() => {
|
|
||||||
console.log("🔄 Daten werden aktualisiert...");
|
|
||||||
dispatch(
|
|
||||||
fetchLoopChartDataThunk({
|
|
||||||
mode: "DIA0",
|
|
||||||
type: 4,
|
|
||||||
slotNumber: 6,
|
|
||||||
vonDatum: "2024-02-01",
|
|
||||||
bisDatum: "2024-02-10",
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}, 10000);
|
|
||||||
};
|
|
||||||
52
services/fetchDigitaleEingaenge.ts
Normal file
52
services/fetchDigitaleEingaenge.ts
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
return process.env.NODE_ENV === "development"
|
||||||
|
? `${window.location.origin}/CPLmockData/SERVICE/de.js`
|
||||||
|
: `${window.location.origin}/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;
|
||||||
|
|
||||||
|
console.log(`📡 API-Request an: ${apiUrl}`);
|
||||||
|
const response = await fetch(apiUrl);
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`❌ Fehler: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const rawData = await response.text();
|
||||||
|
console.log("✅ Rohdaten erfolgreich geladen:", rawData);
|
||||||
|
|
||||||
|
// **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);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user