This commit is contained in:
rafaeldpsilva
2025-12-20 00:17:21 +00:00
parent 4b4338fb91
commit c3364cc422
31 changed files with 818 additions and 425 deletions

View File

@@ -21,7 +21,7 @@ import {
type HealthCheck,
type SystemStatus,
type DataQuery,
type DataResponse
type DataResponse,
} from '@/services'
interface ApiState {
@@ -33,16 +33,16 @@ export function useApi() {
// Global API state
const globalState = reactive<ApiState>({
loading: false,
error: null
error: null,
})
// Helper to handle API calls with state management
async function handleApiCall<T>(
apiCall: () => Promise<T>,
localState?: { loading: boolean; error: string | null }
localState?: { loading: boolean; error: string | null },
): Promise<T | null> {
const state = localState || globalState
state.loading = true
state.error = null
@@ -61,7 +61,7 @@ export function useApi() {
return {
globalState,
handleApiCall
handleApiCall,
}
}
@@ -69,7 +69,7 @@ export function useApi() {
export function useSensorsApi() {
const state = reactive<ApiState>({
loading: false,
error: null
error: null,
})
const sensors = ref<SensorDevice[]>([])
@@ -83,10 +83,7 @@ export function useSensorsApi() {
sensor_type?: SensorType
status?: SensorStatus
}) => {
const result = await handleApiCall(
() => sensorsApi.getSensors(params),
state
)
const result = await handleApiCall(() => sensorsApi.getSensors(params), state)
if (result && result.sensors) {
sensors.value = result.sensors
}
@@ -94,10 +91,7 @@ export function useSensorsApi() {
}
const fetchSensor = async (sensorId: string) => {
const result = await handleApiCall(
() => sensorsApi.getSensor(sensorId),
state
)
const result = await handleApiCall(() => sensorsApi.getSensor(sensorId), state)
if (result) {
currentSensor.value = result
}
@@ -111,12 +105,9 @@ export function useSensorsApi() {
end_time?: number
limit?: number
offset?: number
}
},
) => {
const result = await handleApiCall(
() => sensorsApi.getSensorData(sensorId, params),
state
)
const result = await handleApiCall(() => sensorsApi.getSensorData(sensorId, params), state)
if (result) {
sensorData.value = result
}
@@ -124,27 +115,15 @@ export function useSensorsApi() {
}
const queryData = async (query: DataQuery) => {
return handleApiCall(
() => sensorsApi.queryData(query),
state
)
return handleApiCall(() => sensorsApi.queryData(query), state)
}
const updateSensorMetadata = async (
sensorId: string,
metadata: SensorMetadata
) => {
return handleApiCall(
() => sensorsApi.updateSensorMetadata(sensorId, metadata),
state
)
const updateSensorMetadata = async (sensorId: string, metadata: SensorMetadata) => {
return handleApiCall(() => sensorsApi.updateSensorMetadata(sensorId, metadata), state)
}
const deleteSensor = async (sensorId: string) => {
return handleApiCall(
() => sensorsApi.deleteSensor(sensorId),
state
)
return handleApiCall(() => sensorsApi.deleteSensor(sensorId), state)
}
const exportData = async (params: {
@@ -153,10 +132,7 @@ export function useSensorsApi() {
sensor_ids?: string
format?: 'json' | 'csv'
}) => {
return handleApiCall(
() => sensorsApi.exportData(params),
state
)
return handleApiCall(() => sensorsApi.exportData(params), state)
}
return {
@@ -170,7 +146,7 @@ export function useSensorsApi() {
queryData,
updateSensorMetadata,
deleteSensor,
exportData
exportData,
}
}
@@ -178,7 +154,7 @@ export function useSensorsApi() {
export function useRoomsApi() {
const state = reactive<ApiState>({
loading: false,
error: null
error: null,
})
const rooms = ref<RoomInfo[]>([])
@@ -187,10 +163,7 @@ export function useRoomsApi() {
const { handleApiCall } = useApi()
const fetchRooms = async () => {
const result = await handleApiCall(
() => roomsApi.getRooms(),
state
)
const result = await handleApiCall(() => roomsApi.getRooms(), state)
if (result) {
rooms.value = result
}
@@ -203,12 +176,9 @@ export function useRoomsApi() {
start_time?: number
end_time?: number
limit?: number
}
},
) => {
const result = await handleApiCall(
() => roomsApi.getRoomData(roomName, params),
state
)
const result = await handleApiCall(() => roomsApi.getRoomData(roomName, params), state)
if (result) {
currentRoomData.value = result
}
@@ -220,7 +190,7 @@ export function useRoomsApi() {
rooms,
currentRoomData,
fetchRooms,
fetchRoomData
fetchRoomData,
}
}
@@ -228,7 +198,7 @@ export function useRoomsApi() {
export function useAnalyticsApi() {
const state = reactive<ApiState>({
loading: false,
error: null
error: null,
})
const summary = ref<AnalyticsSummary | null>(null)
@@ -239,10 +209,7 @@ export function useAnalyticsApi() {
const { handleApiCall } = useApi()
const fetchAnalyticsSummary = async (hours: number = 24) => {
const result = await handleApiCall(
() => analyticsApi.getAnalyticsSummary(hours),
state
)
const result = await handleApiCall(() => analyticsApi.getAnalyticsSummary(hours), state)
if (result) {
summary.value = result
}
@@ -250,10 +217,7 @@ export function useAnalyticsApi() {
}
const fetchEnergyTrends = async (hours: number = 168) => {
const result = await handleApiCall(
() => analyticsApi.getEnergyTrends(hours),
state
)
const result = await handleApiCall(() => analyticsApi.getEnergyTrends(hours), state)
if (result) {
trends.value = result
}
@@ -261,10 +225,7 @@ export function useAnalyticsApi() {
}
const fetchRoomComparison = async (hours: number = 24) => {
const result = await handleApiCall(
() => analyticsApi.getRoomComparison(hours),
state
)
const result = await handleApiCall(() => analyticsApi.getRoomComparison(hours), state)
if (result) {
roomComparison.value = result
}
@@ -277,10 +238,7 @@ export function useAnalyticsApi() {
hours?: number
limit?: number
}) => {
const result = await handleApiCall(
() => analyticsApi.getEvents(params),
state
)
const result = await handleApiCall(() => analyticsApi.getEvents(params), state)
if (result) {
events.value = result.events
}
@@ -296,7 +254,7 @@ export function useAnalyticsApi() {
fetchAnalyticsSummary,
fetchEnergyTrends,
fetchRoomComparison,
fetchEvents
fetchEvents,
}
}
@@ -304,7 +262,7 @@ export function useAnalyticsApi() {
export function useHealthApi() {
const state = reactive<ApiState>({
loading: false,
error: null
error: null,
})
const health = ref<HealthCheck | null>(null)
@@ -313,10 +271,7 @@ export function useHealthApi() {
const { handleApiCall } = useApi()
const fetchHealth = async () => {
const result = await handleApiCall(
() => healthApi.getHealth(),
state
)
const result = await handleApiCall(() => healthApi.getHealth(), state)
if (result) {
health.value = result
}
@@ -324,10 +279,7 @@ export function useHealthApi() {
}
const fetchStatus = async () => {
const result = await handleApiCall(
() => healthApi.getStatus(),
state
)
const result = await handleApiCall(() => healthApi.getStatus(), state)
if (result) {
status.value = result
}
@@ -339,6 +291,6 @@ export function useHealthApi() {
health,
status,
fetchHealth,
fetchStatus
fetchStatus,
}
}
}