Files
CPLv4.0/services/fetchAnalogInputsHistoryService.ts
ISA 40e2b54836 feat: fetchAnalogInputsHistoryService mit dynamischem Fallback für Mock- und Live-Daten
- In der Entwicklungsumgebung werden lokale Mock-Dateien verwendet
- In Produktion werden Live-Messdaten der CPL über DIA0 geladen
- Automatische Datumsgenerierung von gestern bis heute integriert
2025-04-29 10:30:21 +02:00

55 lines
1.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// services/fetchAnalogInputsHistoryService.ts
export async function fetchAnalogInputsHistoryService(): Promise<
Record<number, any[]>
> {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
const formatDate = (date: Date) =>
`${date.getFullYear()};${String(date.getMonth() + 1).padStart(
2,
"0"
)};${String(date.getDate()).padStart(2, "0")}`;
const fromDate = formatDate(yesterday);
const toDate = formatDate(today);
const result: Record<number, any[]> = {};
const isDev = process.env.NODE_ENV === "development";
for (let i = 0; i < 8; i++) {
const inputNumber = i + 1; // 18
const sourceId = 99 + inputNumber; // 100107 für AE1AE8
try {
let data: any[];
if (isDev) {
// ⬇️ Lädt lokale Mock-Dateien (z.B. /api/cpl/fetchAnalogInputsHistory)
const response = await fetch(
`/apiMockData/analogInputsHistoryData/analogInput${inputNumber}.json`
);
if (!response.ok) throw new Error("Mock-Daten nicht verfügbar");
data = await response.json();
} else {
// ⬇️ Holt Live-Daten über CPL-HTTP-Endpunkt
const url = `${window.location.origin}/CPL?Service/empty.acp&DIA0=${fromDate};${toDate};${sourceId};1`;
const response = await fetch(url);
if (!response.ok)
throw new Error(`Fehler bei AE${inputNumber}: ${response.status}`);
data = await response.json();
}
result[sourceId] = data;
} catch (error) {
console.error(`❌ Fehler beim Laden von AE${inputNumber}:`, error);
result[sourceId] = [];
}
}
return result;
}