54 lines
No EOL
1.1 KiB
TypeScript
54 lines
No EOL
1.1 KiB
TypeScript
import { useEffect } from 'react'
|
|
import { useBedPressureStore, SensorData } from '@/stores/bedPressureStore'
|
|
|
|
|
|
export function useBedPressureData() {
|
|
const {
|
|
sensorConfig,
|
|
sensorData,
|
|
isMonitoring,
|
|
fetchSensorConfig,
|
|
fetchSensorData,
|
|
setSensorData
|
|
} = useBedPressureStore()
|
|
|
|
// Initialize sensor configuration
|
|
useEffect(() => {
|
|
fetchSensorConfig()
|
|
}, [fetchSensorConfig])
|
|
|
|
// Initialize sensor data
|
|
useEffect(() => {
|
|
if (sensorConfig.length === 0) return
|
|
|
|
const initialData: Record<string, SensorData> = {}
|
|
sensorConfig.forEach((sensor) => {
|
|
initialData[sensor.id] = {
|
|
...sensor,
|
|
currentValue: 0,
|
|
data: [],
|
|
status: "normal",
|
|
}
|
|
})
|
|
setSensorData(initialData)
|
|
}, [sensorConfig, setSensorData])
|
|
|
|
// Fetch sensor data from API
|
|
useEffect(() => {
|
|
if (!isMonitoring) return
|
|
|
|
// Initial fetch
|
|
fetchSensorData()
|
|
|
|
// Set up polling
|
|
const interval = setInterval(fetchSensorData, 2000)
|
|
|
|
return () => clearInterval(interval)
|
|
}, [isMonitoring, fetchSensorData])
|
|
|
|
return {
|
|
sensorData,
|
|
sensorConfig,
|
|
isMonitoring
|
|
}
|
|
} |