import { useEffect, useState } from "react"; import { SERVER_URL } from "../config/urls"; import { useDispatch, useSelector } from "react-redux"; 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; const fetchData = async () => { try { 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 = {}; 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, }; } 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; } 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]; const messageDisplay = values.messages.map((msg) => `${msg.message}
`).join(""); const prioNameDisplay = statis.PrioName && statis.PrioName !== "?" ? `(${statis.PrioName})` : ""; 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}

${values.messwert ? `Messwert: ${values.messwert}
` : ""} ${values.schleifenwert ? `Schleifenwert: ${values.schleifenwert}` : ""}
`; } }); setLineColors(colorsByModule); setTooltipContents(newTooltipContents); setLineStatusData(data1.Statis); } } catch (error) { console.error("Fehler beim Abrufen der Daten:", error); try { window.location.reload(); } catch (reloadError) { console.error("Fehler beim Neuladen der Seite:", reloadError); } } }; const scheduleNextFetch = () => { if (!isCancelled) { fetchData(); setTimeout(scheduleNextFetch, 30000); } }; fetchData(); scheduleNextFetch(); return () => { isCancelled = true; }; }, [webserviceGisLinesStatusUrl, setLineStatusData]); return { lineColors, tooltipContents }; }; export default useLineData;