Add API integration for sensors and rooms management
Integrate sensorsApi and roomsApi services into energy store. Add API state, loading/error handling, and async functions for fetching sensor and room data. Update room loading logic to fetch from API. Expose new API functions for analytics and health endpoints. Update SensorManagementView to use localhost WebSocket for real-time updates.
This commit is contained in:
30
src/services/roomsApi.ts
Normal file
30
src/services/roomsApi.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Rooms API Service
|
||||
* Handles room-related API calls
|
||||
*/
|
||||
import { apiClient, type RoomInfo, type RoomData } from './api'
|
||||
|
||||
export const roomsApi = {
|
||||
/**
|
||||
* Get list of all rooms with sensor counts and latest metrics
|
||||
*/
|
||||
async getRooms(): Promise<RoomInfo[]> {
|
||||
return apiClient.get<RoomInfo[]>('/api/v1/rooms')
|
||||
},
|
||||
|
||||
/**
|
||||
* Get historical data for a specific room
|
||||
*/
|
||||
async getRoomData(
|
||||
roomName: string,
|
||||
params?: {
|
||||
start_time?: number
|
||||
end_time?: number
|
||||
limit?: number
|
||||
},
|
||||
): Promise<RoomData> {
|
||||
return apiClient.get<RoomData>(`/api/v1/rooms/${encodeURIComponent(roomName)}/data`, params)
|
||||
},
|
||||
}
|
||||
|
||||
export default roomsApi
|
||||
98
src/services/sensorsApi.ts
Normal file
98
src/services/sensorsApi.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Sensors API Service
|
||||
* Handles sensor-related API calls
|
||||
*/
|
||||
import {
|
||||
apiClient,
|
||||
type SensorInfo,
|
||||
type SensorReading,
|
||||
type DataQuery,
|
||||
type DataResponse,
|
||||
type SensorType,
|
||||
type SensorStatus,
|
||||
} from './api'
|
||||
|
||||
export const sensorsApi = {
|
||||
/**
|
||||
* Get all sensors with optional filtering
|
||||
*/
|
||||
async getSensors(params?: {
|
||||
room?: string
|
||||
sensor_type?: SensorType
|
||||
status?: SensorStatus
|
||||
}): Promise<SensorInfo[]> {
|
||||
return apiClient.get<SensorInfo[]>('/api/v1/sensors', params)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get detailed information about a specific sensor
|
||||
*/
|
||||
async getSensor(sensorId: string): Promise<SensorInfo> {
|
||||
return apiClient.get<SensorInfo>(`/api/v1/sensors/${sensorId}`)
|
||||
},
|
||||
|
||||
/**
|
||||
* Get historical data for a specific sensor
|
||||
*/
|
||||
async getSensorData(
|
||||
sensorId: string,
|
||||
params?: {
|
||||
start_time?: number
|
||||
end_time?: number
|
||||
limit?: number
|
||||
offset?: number
|
||||
},
|
||||
): Promise<DataResponse> {
|
||||
return apiClient.get<DataResponse>(`/api/v1/sensors/${sensorId}/data`, params)
|
||||
},
|
||||
|
||||
/**
|
||||
* Advanced data query with multiple filters
|
||||
*/
|
||||
async queryData(query: DataQuery): Promise<DataResponse> {
|
||||
return apiClient.post<DataResponse>('/api/v1/data/query', query)
|
||||
},
|
||||
|
||||
/**
|
||||
* Update sensor metadata
|
||||
*/
|
||||
async updateSensorMetadata(
|
||||
sensorId: string,
|
||||
metadata: Record<string, any>,
|
||||
): Promise<{ message: string }> {
|
||||
return apiClient.put<{ message: string }>(`/api/v1/sensors/${sensorId}/metadata`, metadata)
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete sensor and all its data
|
||||
*/
|
||||
async deleteSensor(sensorId: string): Promise<{
|
||||
message: string
|
||||
readings_deleted: number
|
||||
metadata_deleted?: boolean
|
||||
}> {
|
||||
return apiClient.delete(`/api/v1/sensors/${sensorId}`)
|
||||
},
|
||||
|
||||
/**
|
||||
* Export sensor data for specified time range
|
||||
*/
|
||||
async exportData(params: {
|
||||
start_time: number
|
||||
end_time: number
|
||||
sensor_ids?: string
|
||||
format?: 'json' | 'csv'
|
||||
}): Promise<{
|
||||
data: SensorReading[]
|
||||
count: number
|
||||
export_params: any
|
||||
}> {
|
||||
return apiClient.get<{
|
||||
data: SensorReading[]
|
||||
count: number
|
||||
export_params: any
|
||||
}>('/api/v1/export', params)
|
||||
},
|
||||
}
|
||||
|
||||
export default sensorsApi
|
||||
Reference in New Issue
Block a user