Commit of Current Status
This commit is contained in:
104
lib/api.ts
Normal file
104
lib/api.ts
Normal 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');
|
||||
}
|
||||
62
lib/utils.test.ts
Normal file
62
lib/utils.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { describe, it, expect, setSystemTime, beforeAll, afterAll } from "bun:test";
|
||||
import { getVehicleDisplayStatus } from "./utils";
|
||||
import { Vehicle } from "./api";
|
||||
|
||||
describe("getVehicleDisplayStatus", () => {
|
||||
beforeAll(() => {
|
||||
setSystemTime(new Date("2024-01-01T12:00:10Z")); // Mock Current Time: 12:00:10 UTC
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
setSystemTime(); // Reset
|
||||
});
|
||||
|
||||
it("should return Online if heartbeat is recent (<5s)", () => {
|
||||
const vehicle: Vehicle = {
|
||||
vin: "TEST",
|
||||
status: "Online",
|
||||
currentVersion: "1.0",
|
||||
lastHeartbeat: "2024-01-01T12:00:08", // 2 seconds ago (assuming UTC fix)
|
||||
groupId: null
|
||||
};
|
||||
// The function appends Z -> 12:00:08Z. Diff is 2000ms.
|
||||
expect(getVehicleDisplayStatus(vehicle)).toBe("Online");
|
||||
});
|
||||
|
||||
it("should return Offline if heartbeat is old (>5s)", () => {
|
||||
const vehicle: Vehicle = {
|
||||
vin: "TEST",
|
||||
status: "Online",
|
||||
currentVersion: "1.0",
|
||||
lastHeartbeat: "2024-01-01T12:00:04", // 6 seconds ago
|
||||
groupId: null
|
||||
};
|
||||
// The function appends Z -> 12:00:04Z. Diff is 6000ms.
|
||||
expect(getVehicleDisplayStatus(vehicle)).toBe("Offline");
|
||||
});
|
||||
|
||||
it("should handle already UTC string with Z", () => {
|
||||
const vehicle: Vehicle = {
|
||||
vin: "TEST",
|
||||
status: "Online",
|
||||
currentVersion: "1.0",
|
||||
lastHeartbeat: "2024-01-01T12:00:09Z", // 1s ago
|
||||
groupId: null
|
||||
};
|
||||
expect(getVehicleDisplayStatus(vehicle)).toBe("Online");
|
||||
});
|
||||
|
||||
it("should preserve Updating status even if old heartbeat", () => {
|
||||
// Usually updating vehicles might not heartbeat? Or they do.
|
||||
// Logic says: if (displayStatus === 'Online' && diffMs > 5000) -> Offline.
|
||||
// If status is 'Updating', it stays 'Updating'.
|
||||
const vehicle: Vehicle = {
|
||||
vin: "TEST",
|
||||
status: "Updating",
|
||||
currentVersion: "1.0",
|
||||
lastHeartbeat: "2024-01-01T12:00:00", // 10s ago
|
||||
groupId: null
|
||||
};
|
||||
expect(getVehicleDisplayStatus(vehicle)).toBe("Updating");
|
||||
});
|
||||
});
|
||||
18
lib/utils.ts
Normal file
18
lib/utils.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { Vehicle } from "./api";
|
||||
|
||||
export function getVehicleDisplayStatus(vehicle: Vehicle): 'Online' | 'Offline' | 'Updating' | string {
|
||||
// Calculate status: Offline if no heartbeat for 5s
|
||||
let lastHeartbeatStr = vehicle.lastHeartbeat;
|
||||
// Fix timezone if missing Z
|
||||
if (!lastHeartbeatStr.endsWith('Z')) {
|
||||
lastHeartbeatStr += 'Z';
|
||||
}
|
||||
const lastHeartbeatDate = new Date(lastHeartbeatStr);
|
||||
const diffMs = Date.now() - lastHeartbeatDate.getTime();
|
||||
|
||||
let displayStatus = vehicle.status;
|
||||
if (displayStatus === 'Online' && diffMs > 5000) {
|
||||
displayStatus = 'Offline';
|
||||
}
|
||||
return displayStatus;
|
||||
}
|
||||
Reference in New Issue
Block a user