43 lines
1.3 KiB
JavaScript
43 lines
1.3 KiB
JavaScript
/**
|
|
* @module api
|
|
* Daten-Schicht: Kommunikation mit Backend-Endpunkt /data
|
|
* Verantwortlich für das Laden und Bereitstellen der Loggerdaten für Visualisierung
|
|
*/
|
|
|
|
/**
|
|
* Sendet GET-Request an /data und empfängt Loggerdaten als JSON.
|
|
* @async
|
|
* @param {string} [log_file_path] - Optionaler Pfad zu einer spezifischen Logdatei oder Zeitspanne.
|
|
* @returns {Promise<Array<Object>>} - Array von Messobjekten mit Zeitstempel, Intensität, Wellenlänge, Sensor-ID.
|
|
*/
|
|
export async function fetchData(log_file_path) {
|
|
const baseUrl = '/data';
|
|
const url = log_file_path ? `${baseUrl}?log_file_path=${encodeURIComponent(log_file_path)}` : baseUrl;
|
|
|
|
try {
|
|
const response = await fetch(url, {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
}
|
|
});
|
|
|
|
if (!response.ok) {
|
|
console.error(`Serverantwort war nicht ok (Status: ${response.status})`);
|
|
return [];
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
// Defensive: Sicherstellen, dass Daten ein Array sind
|
|
if (!Array.isArray(data)) {
|
|
console.warn('Erwartetes Datenformat ist ein Array, erhalten:', typeof data);
|
|
return [];
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.error('Fehler beim Abrufen der Daten:', error);
|
|
return [];
|
|
}
|
|
}
|