Files
nodeMap/hooks/useLineData.js

153 lines
6.0 KiB
JavaScript

// /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);
const colorsByModule = {};
const newTooltipContents = {};
const valueMap = {};
// Hier führen wir die Gruppierung durch und loggen sie
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.includes("_Messwert") && statis.Value !== "True" && valueMap[key].messwert === undefined) {
valueMap[key].messwert = statis.Value;
}
if (statis.DpName.includes("_Schleifenwert") && valueMap[key].schleifenwert === undefined) {
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}`;
const values = valueMap[key];
const messageDisplay = values.messages.map((msg) => (msg ? `<span class="inline-block text-gray-800">${msg}</span><br>` : "")).join("");
const prioNameDisplay = statis.PrioName && statis.PrioName !== "?" ? `(${statis.PrioName})` : "";
colorsByModule[matchingLine.idModul] = prioColor;
newTooltipContents[matchingLine.idModul] = `
<div class="bg-white rounded-lg m-0 p-2 w-[210px]">
<span class="text-lg font-semibold text-gray-900">${statis.ModulName || "Unknown"}</span>
<br>
<span class="text-md font-bold text-gray-800">${statis.ModulTyp || "N/A"}</span>
<br>
<span class="text-md font-bold text-gray-800">Slot: ${statis.Modul || "N/A"}</span>
<br>
<div style="max-width: 100%; overflow-wrap: break-word; word-break: break-word; white-space: normal;">
<span class="inline-block w-2 h-2 rounded-full mr-2" style="background-color: ${prioColor || "#000000"};"></span>
${messageDisplay}
</div>
<span class="text-gray-800" style="color: ${prioColor || "#000000"};">${prioNameDisplay}</span>
<br>
${values.messwert ? `<span class="inline-block text-gray-800">Messwert: ${values.messwert}</span><br>` : ""}
${values.schleifenwert ? `<span class="inline-block text-gray-800">Schleifenwert: ${values.schleifenwert}</span>` : ""}
</div>
`;
}
});
setLineColors(colorsByModule);
setTooltipContents(newTooltipContents);
setLineStatusData(data1.Statis);
} catch (error) {
console.error("Fehler beim Abrufen der Daten:", error);
}
};
fetchData();
}, [webserviceGisLinesStatusUrl, setLineStatusData]);
return { lineColors, tooltipContents };
};
//----------------------------------------------------------
// Funktion zum Loggen der gruppierten und aggregierten Daten
function logGroupedData(statisList) {
const grouped = statisList.reduce((acc, item) => {
const { IdLD, Modul, Level, PrioColor, PrioName, ModulName, ModulTyp, Message } = item;
if (!acc[IdLD]) {
acc[IdLD] = {};
}
if (!acc[IdLD][Modul]) {
acc[IdLD][Modul] = {
ModulName: ModulName || "Unknown",
ModulTyp: ModulTyp || "N/A",
TotalLevel: Level, // Use the original Level directly
PrioColors: new Set(),
PrioNames: new Set(),
Messages: [],
};
}
// Speichere die Prioritätsinformationen und Nachrichten
acc[IdLD][Modul].PrioColors.add(PrioColor);
acc[IdLD][Modul].PrioNames.add(PrioName);
if (Message && Message !== "?") {
acc[IdLD][Modul].Messages.push(Message);
}
return acc;
}, {});
// Formatierte Ausgabe der gruppierten Daten, Entfernen von Modulen ohne Namen und Stationen mit leeren Arrays
const formattedData = {};
Object.entries(grouped).forEach(([stationId, modules]) => {
const filteredModules = Object.entries(modules)
.filter(([modulId, data]) => data.ModulName !== "?") // Filter out modules without names
.map(([modulId, data]) => ({
Modul: modulId,
ModulName: data.ModulName,
ModulTyp: data.ModulTyp,
Level: data.TotalLevel, // Use the original Level
PrioColors: Array.from(data.PrioColors).join(", "),
PrioNames: Array.from(data.PrioNames).join(", "),
Messages: data.Messages.join(" | "),
}));
if (filteredModules.length > 0) {
formattedData[stationId] = filteredModules;
}
});
console.log("Aggregierte und gruppierte Daten (gefiltert):", formattedData);
}
// Beispielaufruf der Funktion
// const statisList = data1.Statis; // Verwende die Liste aus deinem API-Aufruf
// logGroupedData(statisList);
//----------------------------------------------------------
export default useLineData;