dynamic graph
This commit is contained in:
parent
a606796d9e
commit
5e029ff99c
17 changed files with 1707 additions and 569 deletions
69
hooks/useBedPressureData.ts
Normal file
69
hooks/useBedPressureData.ts
Normal file
|
@ -0,0 +1,69 @@
|
|||
import { useEffect } from 'react'
|
||||
import { useBedPressureStore, SensorData } from '@/stores/bedPressureStore'
|
||||
|
||||
// Mock data generator
|
||||
const generateTimeSeriesData = (hours = 24) => {
|
||||
const data = []
|
||||
const now = new Date()
|
||||
|
||||
for (let i = hours * 60; i >= 0; i -= 5) {
|
||||
const time = new Date(now.getTime() - i * 60 * 1000)
|
||||
data.push({
|
||||
time: time.toLocaleTimeString("en-US", { hour12: false }),
|
||||
timestamp: time.getTime(),
|
||||
value: Math.floor(Math.random() * 4096 + Math.sin(i / 60) * 500 + 2000), // 0-4095 range
|
||||
})
|
||||
}
|
||||
return data
|
||||
}
|
||||
|
||||
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: Math.floor(Math.random() * 1000 + 1000), // Start with baseline analog value (1000-2000)
|
||||
data: generateTimeSeriesData(),
|
||||
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
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue