Files
CPLv4.0/services/fetchSystemspannung98VminusService.ts
2025-08-01 12:10:12 +02:00

39 lines
1.4 KiB
TypeScript

/**
* Holt Messwerte für -98V aus der passenden JSON-Datei über die API
* @param type - Typ der Daten: DIA0 = alle, DIA1 = stündlich, DIA2 = täglich
*/
export const fetchSystemspannung98VminusService = async (
type: "DIA0" | "DIA1" | "DIA2"
) => {
try {
const isDev = process.env.NODE_ENV === "development";
const channel = 115; // 115 = -98V laut Spezifikation
// Dynamisch: to = heute, from = 30 Tage zurück
const getDateParts = (date: Date) => {
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, "0");
const d = String(date.getDate()).padStart(2, "0");
return { y, m, d };
};
const today = new Date();
const toParts = getDateParts(today);
const fromDateObj = new Date(today);
fromDateObj.setDate(today.getDate() - 30);
const fromParts = getDateParts(fromDateObj);
const path = isDev
? `/api/cpl/getSystemspannung98VminusHandler?typ=${type}`
: `/CPL?seite.ACP&${type}=${fromParts.y};${fromParts.m};${fromParts.d};${toParts.y};${toParts.m};${toParts.d};${channel};1`;
console.log("[Service] fetchSystemspannung98VminusService", path);
const res = await fetch(path);
if (!res.ok) throw new Error("❌ Fehler beim Abrufen der -98V-Daten");
return await res.json();
} catch (err) {
console.error("❌ Fehler in fetchSystemspannung98VminusService:", err);
return null;
}
};