Commit of Current Status

This commit is contained in:
Denis Urs Rudolph
2025-12-11 20:58:10 +01:00
parent d492352d50
commit 6de23e0e77
10 changed files with 827 additions and 58 deletions

104
lib/api.ts Normal file
View File

@@ -0,0 +1,104 @@
const API_URL = 'http://localhost:5000/api';
export interface Vehicle {
vin: string;
status: string;
currentVersion: string;
lastHeartbeat: string;
groupId?: number;
group?: VehicleGroup;
}
export interface VehicleGroup {
id: number;
name: string;
description?: string;
vehicles?: Vehicle[];
}
export interface FirmwareUpdate {
id: number;
version: string;
description?: string;
uploadedAt: string;
}
export interface Deployment {
id: number;
updateId: number;
targetVin?: string;
targetGroupId?: number;
status: string;
createdAt: string;
update: FirmwareUpdate;
}
export async function fetchVehicles(): Promise<Vehicle[]> {
const res = await fetch(`${API_URL}/admin/vehicles`);
if (!res.ok) throw new Error('Failed to fetch vehicles');
return res.json();
}
export async function fetchGroups(): Promise<VehicleGroup[]> {
const res = await fetch(`${API_URL}/admin/groups`);
if (!res.ok) throw new Error('Failed to fetch groups');
return res.json();
}
export async function getUpdates(): Promise<FirmwareUpdate[]> {
const res = await fetch(`${API_URL}/admin/updates`);
if (!res.ok) throw new Error('Failed to fetch updates');
return res.json();
}
export async function createGroup(name: string, description?: string): Promise<VehicleGroup> {
const res = await fetch(`${API_URL}/admin/groups`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, description }),
});
if (!res.ok) throw new Error('Failed to create group');
return res.json();
}
export async function updateGroup(id: number, name: string, description?: string): Promise<VehicleGroup> {
const res = await fetch(`${API_URL}/admin/groups/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, description }),
});
if (!res.ok) throw new Error('Failed to update group');
return res.json();
}
export async function assignVehicleToGroup(groupId: number, vin: string): Promise<void> {
const res = await fetch(`${API_URL}/admin/groups/${groupId}/vehicles`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ vin }),
});
if (!res.ok) throw new Error('Failed to assign vehicle to group');
}
export async function fetchDeployments(): Promise<Deployment[]> {
const res = await fetch(`${API_URL}/admin/deployments`);
if (!res.ok) throw new Error('Failed to fetch deployments');
return res.json();
}
export async function uploadUpdate(formData: FormData): Promise<void> {
const res = await fetch(`${API_URL}/admin/updates`, {
method: 'POST',
body: formData,
});
if (!res.ok) throw new Error('Failed to upload update');
}
export async function createDeployment(updateId: number, targetVin?: string, targetGroupId?: number): Promise<void> {
const res = await fetch(`${API_URL}/admin/deployments`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ updateId, targetVin, targetGroupId }),
});
if (!res.ok) throw new Error('Failed to create deployment');
}