// /hooks/useLineData.js
import { useEffect, useState } from "react";
import { SERVER_URL } from "../config/urls";
const useLineData = (webserviceGisLinesStatusUrl, setLineStatusData) => {
const [lineColors, setLineColors] = useState({});
const [tooltipContents, setTooltipContents] = useState({});
useEffect(() => {
const fetchData = async () => {
try {
console.log("Daten werden abgerufen...");
const response1 = await fetch(webserviceGisLinesStatusUrl);
const data1 = await response1.json();
console.log("Daten vom Webservice:", data1);
const response2 = await fetch(`${SERVER_URL}:3000/api/talas_v5_DB/gisLines/readGisLines`);
const data2 = await response2.json();
console.log("GIS Linien Daten:", data2);
// Abrufen der Namen basierend auf idLD
const response3 = await fetch(`${SERVER_URL}:3000/api/talas_v5_DB/station/getAllStationsNames`);
const namesData = await response3.json();
console.log("Namen der Linien:", namesData);
const colorsByModule = {};
const newTooltipContents = {};
const valueMap = {};
logGroupedData(data1.Statis);
data1.Statis.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(statis.Message);
}
});
data1.Statis.reverse().forEach((statis) => {
const matchingLine = data2.find((item) => item.idLD === statis.IdLD && item.idModul === statis.Modul);
if (matchingLine) {
const prioColor = statis.PrioColor === "#ffffff" ? "green" : statis.PrioColor;
const key = `${matchingLine.idLD}-${matchingLine.idModul}`; // Sicherstellen, dass der Key eindeutig ist
const values = valueMap[key];
if (!values) {
console.error(`No values found for key: ${key}`); // Debug: Überprüfe, ob Werte existieren
return;
}
const messageDisplay = values.messages.map((msg) => (msg ? `${msg}
` : "")).join("");
const prioNameDisplay = statis.PrioName && statis.PrioName !== "?" ? `(${statis.PrioName})` : "";
colorsByModule[key] = prioColor; // Stelle sicher, dass der Key richtig verwendet wird
newTooltipContents[key] = `