Files
CPLv4.0/services/fetchSystemChartDataService.ts

82 lines
2.2 KiB
TypeScript

// /services/fetchSystemChartDataService.ts
/**
* hier ist nur noch ein Test, wenn nicht gebraucht, kann die Datei gelöscht werden.
*/
const getApiUrl = (
mode: "DIA0" | "DIA1" | "DIA2",
type: number,
slotNumber: number,
vonDatum: string,
bisDatum: string
) => {
if (!slotNumber) {
console.error("⚠️ Slot-Nummer nicht gesetzt!");
return "";
}
// type: 3 → Isolationswiderstand
// type: 4 → Schleifenwiderstand
const typeFolder =
type === 3
? "isolationswiderstand"
: type === 4
? "schleifenwiderstand"
: "unbekannterTyp";
return process.env.NEXT_PUBLIC_NODE_ENV === "development"
? `/api/cpl/slotDataAPIHandler?slot=${slotNumber}&messart=${typeFolder}&dia=${mode}&vonDatum=${vonDatum}&bisDatum=${bisDatum}`
: `${window.location.origin}/CPL?seite.ACP&${mode}=${formatDate(
vonDatum
)};${formatDate(bisDatum)};${slotNumber};${type};`;
};
/**
* Wandelt ein Datum von "YYYY-MM-DD" zu "YYYY;MM;DD" um (für die API-URL).
*/
const formatDate = (dateString: string) => {
const dateParts = dateString.split("-");
return `${dateParts[0]};${dateParts[1]};${dateParts[2]}`;
};
/**
* Holt die Messwerte vom Embedded-System oder einer JSON-Datei.
*/
export const fetchSystemChartData = async (
mode: "DIA0" | "DIA1" | "DIA2",
type: number,
slotNumber: number,
vonDatum: string,
bisDatum: string
) => {
try {
const apiUrl = getApiUrl(mode, type, slotNumber, vonDatum, bisDatum);
if (!apiUrl) {
throw new Error(
"❌ Keine gültige API-URL! in /services/fetchSystemChartData.ts"
);
}
console.log(
`📡 Fetching data from in /services/fetchSystemChartData.ts: ${apiUrl}`
);
const response = await fetch(apiUrl);
if (!response.ok) {
throw new Error(`❌ Fehler: ${response.status} ${response.statusText}`);
}
const data = await response.json();
console.log(
"✅ Daten erfolgreich geladen in /services/fetchSystemChartData.ts:",
data
);
return data;
} catch (error) {
console.error(
"❌ Fehler beim Laden der Schleifenmesskurvendaten in /services/fetchSystemChartData.ts:",
error
);
return null;
}
};