// /hooks/useLineData.js import { useEffect, useState } from "react"; import { SERVER_URL } from "../config/urls"; import { useDispatch, useSelector } from "react-redux"; import { connectWebSocket, disconnectWebSocket } from "../redux/actions"; const useLineData = (webserviceGisLinesStatusUrl, setLineStatusData) => { const dispatch = useDispatch(); const messages = useSelector((state) => state.messages); const [lineColors, setLineColors] = useState({}); const [tooltipContents, setTooltipContents] = useState({}); useEffect(() => { let isCancelled = false; // Flag to cancel ongoing operations if component unmounts const fetchData = async () => { try { console.log("Fetching data..."); const response1 = await fetch(webserviceGisLinesStatusUrl); const data1 = await response1.json(); const response2 = await fetch(`${SERVER_URL}:3000/api/talas_v5_DB/gisLines/readGisLines`); const data2 = await response2.json(); const response3 = await fetch(`${SERVER_URL}:3000/api/talas_v5_DB/device/getAllStationsNames`); const namesData = await response3.json(); if (!isCancelled) { const colorsByModule = {}; const newTooltipContents = {}; const valueMap = {}; // Sortiere Statis nach Level const sortedStatis = [...data1.Statis].sort((a, b) => a.Level - b.Level); sortedStatis.forEach((statis) => { const key = `${statis.IdLD}-${statis.Modul}`; if (!valueMap[key]) { valueMap[key] = { messages: [], messwert: undefined, schleifenwert: undefined, }; } // Sammle Messwert und Schleifenwert if (statis.DpName.endsWith("_Messwert") && statis.Value !== "True" && !valueMap[key].messwert) { valueMap[key].messwert = statis.Value; } if (statis.DpName.endsWith("_Schleifenwert") && !valueMap[key].schleifenwert) { valueMap[key].schleifenwert = statis.Value; } // Füge die Meldung zusammen mit der entsprechenden PrioColor hinzu if (statis.Message && statis.Message !== "?") { valueMap[key].messages.push({ message: statis.Message, prioColor: statis.PrioColor && statis.PrioColor !== "#ffffff" ? statis.PrioColor : "green", }); } }); sortedStatis.forEach((statis) => { const key = `${statis.IdLD}-${statis.Modul}`; const matchingLine = data2.find((item) => item.idLD === statis.IdLD && item.idModul === statis.Modul); if (matchingLine) { const values = valueMap[key]; if (!values) { console.error(`Keine Werte gefunden für Key: ${key}`); return; } // Generiere das HTML für jede Meldung mit der jeweiligen PrioColor const messageDisplay = values.messages.length > 0 ? values.messages.map((msg) => `${msg.message}
`).join("") : ""; const prioNameDisplay = statis.PrioName && statis.PrioName !== "?" ? `(${statis.PrioName})` : ""; // Setze die Hauptfarbe für das Modul basierend auf der PrioColor der ersten Meldung colorsByModule[key] = values.messages.length > 0 ? values.messages[0].prioColor : "green"; newTooltipContents[key] = `
${statis.ModulName || "Unknown"}
${statis.ModulTyp || "N/A"}
Slot: ${statis.Modul || "N/A"}
Station: ${namesData[matchingLine.idLD] || "N/A"}
${messageDisplay}
${prioNameDisplay}
${values.messwert ? `Messwert: ${values.messwert}
` : ""} ${values.schleifenwert ? `Schleifenwert: ${values.schleifenwert}` : ""}
`; } }); setLineColors(colorsByModule); setTooltipContents(newTooltipContents); setLineStatusData(data1.Statis); } } catch (error) { if (!isCancelled) { console.error("Fehler beim Abrufen der Daten:", error); } } }; // Funktion für rekursiven Aufruf mit Timeout const scheduleNextFetch = () => { if (!isCancelled) { setTimeout(async () => { await fetchData(); scheduleNextFetch(); }, 20000); } }; // Starte den ersten Aufruf fetchData(); scheduleNextFetch(); // Cleanup-Funktion, um sicherzustellen, dass keine weiteren Daten nach dem Unmount gesetzt werden return () => { isCancelled = true; }; }, [webserviceGisLinesStatusUrl, setLineStatusData]); return { lineColors, tooltipContents }; }; // Funktion zur Gruppierung der Daten function logGroupedData(statisList) { const grouped = statisList.reduce((acc, item) => { const { IdLD, Modul, Level, PrioColor, PrioName, ModulName, ModulTyp, Message, DpName, Value } = item; if (!acc[IdLD]) { acc[IdLD] = {}; } if (!acc[IdLD][Modul]) { acc[IdLD][Modul] = { ModulName: ModulName || "Unknown", ModulTyp: ModulTyp || "N/A", TotalLevel: Level, PrioColors: new Set(), PrioNames: new Set(), Messages: [], Messwert: undefined, Schleifenwert: undefined, }; } acc[IdLD][Modul].PrioColors.add(PrioColor); acc[IdLD][Modul].PrioNames.add(PrioName); if (Message && Message !== "?") { acc[IdLD][Modul].Messages.push(Message); } if (DpName.endsWith("_Messwert") && !acc[IdLD][Modul].Messwert) { acc[IdLD][Modul].Messwert = Value; } if (DpName.endsWith("_Schleifenwert") && !acc[IdLD][Modul].Schleifenwert) { acc[IdLD][Modul].Schleifenwert = Value; } return acc; }, {}); const formattedData = {}; Object.entries(grouped).forEach(([stationId, modules]) => { const filteredModules = Object.entries(modules) .filter(([modulId, data]) => data.ModulName !== "?") .map(([modulId, data]) => ({ Modul: modulId, ModulName: data.ModulName, ModulTyp: data.ModulTyp, TotalLevel: data.TotalLevel, PrioColors: Array.from(data.PrioColors).join(", "), PrioNames: Array.from(data.PrioNames).join(", "), Messages: data.Messages.join(" | "), Messwert: data.Messwert, Schleifenwert: data.Schleifenwert, })); if (filteredModules.length > 0) { formattedData[stationId] = filteredModules; } }); console.log("Aggregierte und gruppierte Daten (gefiltert):", formattedData); } export default useLineData;