import { DevicesResponse, DeviceDto, InitiateRegistrationRequest, InitiateRegistrationResponse, ClaimDeviceRequest, ClaimDeviceResponse, DeviceRegistrationSession, DeviceConfiguration, DeviceHeartbeat, DeviceStatus } from '../types/device' const BASE_URL = "http://localhost:3001/api/v1" // Helper function to get auth headers const getAuthHeaders = (): HeadersInit => { const token = localStorage.getItem("accessToken") if (!token) { throw new Error("No access token found") } return { "Authorization": `Bearer ${token}`, "Content-Type": "application/json", } } // Helper function to handle API responses const handleResponse = async (response: Response): Promise => { if (!response.ok) { if (response.status === 401) { throw new Error("Unauthorized - please login again") } let errorMessage = `Request failed: ${response.statusText}` try { const errorData = await response.json() errorMessage = errorData.message || errorMessage } catch { // Ignore JSON parse error, use default message } throw new Error(errorMessage) } return response.json() } export const devicesApi = { // Device Management async getDevices(): Promise { const response = await fetch(`${BASE_URL}/devices`, { method: "GET", headers: getAuthHeaders(), }) return handleResponse(response) }, async getDevice(deviceId: string): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}`, { method: "GET", headers: getAuthHeaders(), }) return handleResponse(response) }, async updateDevice(deviceId: string, updates: Partial): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}`, { method: "PATCH", headers: getAuthHeaders(), body: JSON.stringify(updates), }) return handleResponse(response) }, async deleteDevice(deviceId: string): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}`, { method: "DELETE", headers: getAuthHeaders(), }) if (!response.ok) { throw new Error(`Failed to delete device: ${response.statusText}`) } }, async updateDeviceStatus(deviceId: string, status: DeviceStatus): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}/status`, { method: "PATCH", headers: getAuthHeaders(), body: JSON.stringify({ status }), }) return handleResponse(response) }, // Device Registration async initiateRegistration(request: InitiateRegistrationRequest): Promise { const response = await fetch(`${BASE_URL}/device-registration/test-initiate`, { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify(request), }) return handleResponse(response) }, async getRegistrationSession(sessionId: string): Promise { const response = await fetch(`${BASE_URL}/device-registration/session/${sessionId}`, { method: "GET", headers: getAuthHeaders(), }) return handleResponse(response) }, async claimDevice(request: ClaimDeviceRequest): Promise { const response = await fetch(`${BASE_URL}/device-registration/claim`, { method: "POST", headers: getAuthHeaders(), body: JSON.stringify(request), }) return handleResponse(response) }, async cancelRegistration(sessionId: string): Promise { const response = await fetch(`${BASE_URL}/device-registration/${sessionId}`, { method: "DELETE", headers: getAuthHeaders(), }) if (!response.ok && response.status !== 204) { throw new Error(`Failed to cancel registration: ${response.statusText}`) } }, // Device Configuration async getDeviceConfiguration(deviceId: string): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}/configuration`, { method: "GET", headers: getAuthHeaders(), }) return handleResponse(response) }, async updateDeviceConfiguration( deviceId: string, configType: string, configData: Record ): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}/configuration`, { method: "POST", headers: getAuthHeaders(), body: JSON.stringify({ configType, configData }), }) return handleResponse(response) }, // Device Heartbeat async sendHeartbeat(heartbeat: DeviceHeartbeat): Promise { const response = await fetch(`${BASE_URL}/devices/heartbeat`, { method: "POST", headers: getAuthHeaders(), body: JSON.stringify(heartbeat), }) if (!response.ok) { throw new Error(`Failed to send heartbeat: ${response.statusText}`) } }, async getDeviceHeartbeats(deviceId: string, limit: number = 50): Promise { const response = await fetch(`${BASE_URL}/devices/${deviceId}/heartbeats?limit=${limit}`, { method: "GET", headers: getAuthHeaders(), }) return handleResponse(response) }, }