76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
/**
|
|
* API layer for Calendink Provider ESP32-S3 dashboard.
|
|
*
|
|
* VITE_API_BASE controls the backend URL:
|
|
* - Development (PC): "http://<ESP32_IP>" (set in .env.development)
|
|
* - Production (ESP32): "" (empty = relative URLs, same origin)
|
|
*/
|
|
|
|
const API_BASE = import.meta.env.VITE_API_BASE || '';
|
|
|
|
/**
|
|
* Fetch system information from the ESP32.
|
|
* @returns {Promise<{chip: string, freeHeap: number, uptime: number, firmware: string, connection: string}>}
|
|
*/
|
|
export async function getSystemInfo() {
|
|
const res = await fetch(`${API_BASE}/api/system/info`);
|
|
if (!res.ok) {
|
|
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
}
|
|
const data = await res.json();
|
|
return {
|
|
...data,
|
|
freeHeap: data.free_heap
|
|
};
|
|
}
|
|
|
|
/**
|
|
* Send a reboot command to the ESP32.
|
|
* @returns {Promise<{message: string}>}
|
|
*/
|
|
export async function reboot() {
|
|
const res = await fetch(`${API_BASE}/api/system/reboot`, {
|
|
method: 'POST',
|
|
});
|
|
if (!res.ok) {
|
|
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
/**
|
|
* Fetch OTA status from the ESP32.
|
|
* @returns {Promise<{active_slot: number, active_partition: string, target_partition: string}>}
|
|
*/
|
|
export async function getOTAStatus() {
|
|
const res = await fetch(`${API_BASE}/api/ota/status`);
|
|
if (!res.ok) {
|
|
throw new Error(`HTTP ${res.status}: ${res.statusText}`);
|
|
}
|
|
return res.json();
|
|
}
|
|
|
|
/**
|
|
* Upload a new frontend binary image.
|
|
* @param {File} file The binary file to upload.
|
|
* @returns {Promise<{status: string, message: string}>}
|
|
*/
|
|
export async function uploadOTAFrontend(file) {
|
|
const res = await fetch(`${API_BASE}/api/ota/frontend`, {
|
|
method: 'POST',
|
|
body: file, // Send the raw file Blob/Buffer
|
|
headers: {
|
|
// Let the browser set Content-Type for the binary payload,
|
|
// or we could force application/octet-stream.
|
|
'Content-Type': 'application/octet-stream'
|
|
}
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const errorText = await res.text();
|
|
throw new Error(`Upload failed (${res.status}): ${errorText || res.statusText}`);
|
|
}
|
|
|
|
return res.json();
|
|
}
|