Add data_visualization/js/api.js
This commit is contained in:
parent
cc7ce79481
commit
e77e3f1a54
1 changed files with 43 additions and 0 deletions
43
data_visualization/js/api.js
Normal file
43
data_visualization/js/api.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* @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 [];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue