Add data_visualization/js/api.js
This commit is contained in:
parent
bf1d47135d
commit
6589c63f74
1 changed files with 47 additions and 0 deletions
47
data_visualization/js/api.js
Normal file
47
data_visualization/js/api.js
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
/**
|
||||||
|
* Modul: API-Kommunikation für das Langzeitbelichtungs-Nachtfotografie-Projekt
|
||||||
|
* Verantwortlich für den Abruf und das Parsen der Temperatur- und Bilddaten vom Server.
|
||||||
|
*
|
||||||
|
* @module api
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Führt einen GET-Request an den /data-Endpunkt aus und liefert strukturierte Daten zurück.
|
||||||
|
*
|
||||||
|
* @async
|
||||||
|
* @function fetchData
|
||||||
|
* @returns {Promise<{ temperatures: Array<{ timestamp: string, value: number }>, images: Array<{ filename: string, timestamp: string, metadata: object }> }>} Parsed JSON-Antwort
|
||||||
|
*/
|
||||||
|
export async function fetchData() {
|
||||||
|
try {
|
||||||
|
const response = await fetch('/data', {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
'Accept': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Fehler beim Abrufen der Daten: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
if (!data || typeof data !== 'object') {
|
||||||
|
throw new Error('Ungültige Datenstruktur vom Server erhalten.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const temperatures = Array.isArray(data.temperatures)
|
||||||
|
? data.temperatures.filter(item => item.timestamp && typeof item.value === 'number')
|
||||||
|
: [];
|
||||||
|
|
||||||
|
const images = Array.isArray(data.images)
|
||||||
|
? data.images.filter(item => item.filename && item.timestamp)
|
||||||
|
: [];
|
||||||
|
|
||||||
|
return { temperatures, images };
|
||||||
|
} catch (error) {
|
||||||
|
console.error('API-Fehler in fetchData:', error);
|
||||||
|
return { temperatures: [], images: [] };
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue