feat: Remove mock sensor data and update initialization for real hardware usage
This commit is contained in:
parent
7e5ed7f26b
commit
17fb57f1d4
1 changed files with 14 additions and 108 deletions
|
@ -22,18 +22,9 @@ const SENSOR_CONFIG: SensorConfig[] = [
|
||||||
{ id: "hip-1", x: 35, y: 50, zone: "hips", label: "Left Hip", pin: 9, baseNoise: 400, warningThreshold: 2200, alarmThreshold: 2800, warningDelayMs: 90000 },
|
{ id: "hip-1", x: 35, y: 50, zone: "hips", label: "Left Hip", pin: 9, baseNoise: 400, warningThreshold: 2200, alarmThreshold: 2800, warningDelayMs: 90000 },
|
||||||
{ id: "hip-2", x: 50, y: 50, zone: "hips", label: "Lower Back", pin: 10, baseNoise: 450, warningThreshold: 2200, alarmThreshold: 2800, warningDelayMs: 90000 },
|
{ id: "hip-2", x: 50, y: 50, zone: "hips", label: "Lower Back", pin: 10, baseNoise: 450, warningThreshold: 2200, alarmThreshold: 2800, warningDelayMs: 90000 },
|
||||||
{ id: "hip-3", x: 65, y: 50, zone: "hips", label: "Right Hip", pin: 11, baseNoise: 380, warningThreshold: 2200, alarmThreshold: 2800, warningDelayMs: 90000 },
|
{ id: "hip-3", x: 65, y: 50, zone: "hips", label: "Right Hip", pin: 11, baseNoise: 380, warningThreshold: 2200, alarmThreshold: 2800, warningDelayMs: 90000 },
|
||||||
|
|
||||||
// Thigh area
|
// Thigh area
|
||||||
{ id: "thigh-1", x: 40, y: 65, zone: "legs", label: "Left Thigh", pin: 12, baseNoise: 320, warningThreshold: 2000, alarmThreshold: 2500, warningDelayMs: 120000 },
|
{ id: "thigh-1", x: 40, y: 65, zone: "legs", label: "Left Thigh", pin: 12, baseNoise: 320, warningThreshold: 2000, alarmThreshold: 2500, warningDelayMs: 120000 },
|
||||||
{ id: "thigh-2", x: 60, y: 65, zone: "legs", label: "Right Thigh", pin: 13, baseNoise: 300, warningThreshold: 2000, alarmThreshold: 2500, warningDelayMs: 120000 },
|
{ id: "thigh-2", x: 60, y: 65, zone: "legs", label: "Right Thigh", pin: 13, baseNoise: 300, warningThreshold: 2000, alarmThreshold: 2500, warningDelayMs: 120000 },
|
||||||
|
|
||||||
// Calf area (mock data)
|
|
||||||
{ id: "calf-1", x: 40, y: 75, zone: "legs", label: "Left Calf", baseNoise: 200, warningThreshold: 1800, alarmThreshold: 2200, warningDelayMs: 150000 },
|
|
||||||
{ id: "calf-2", x: 60, y: 75, zone: "legs", label: "Right Calf", baseNoise: 220, warningThreshold: 1800, alarmThreshold: 2200, warningDelayMs: 150000 },
|
|
||||||
|
|
||||||
// Feet (mock data)
|
|
||||||
{ id: "feet-1", x: 45, y: 85, zone: "feet", label: "Left Foot", baseNoise: 150, warningThreshold: 1500, alarmThreshold: 1800, warningDelayMs: 180000 },
|
|
||||||
{ id: "feet-2", x: 55, y: 85, zone: "feet", label: "Right Foot", baseNoise: 160, warningThreshold: 1500, alarmThreshold: 1800, warningDelayMs: 180000 },
|
|
||||||
];
|
];
|
||||||
|
|
||||||
// Create pin mapping from sensor config
|
// Create pin mapping from sensor config
|
||||||
|
@ -68,18 +59,18 @@ let isHardwareConnected = false;
|
||||||
// Initialize all sensor data
|
// Initialize all sensor data
|
||||||
function initializeSensorData() {
|
function initializeSensorData() {
|
||||||
SENSOR_CONFIG.forEach(sensor => {
|
SENSOR_CONFIG.forEach(sensor => {
|
||||||
if (!sensorData[sensor.id]) {
|
if (!sensorData[sensor.id] && sensor.pin) { // Only initialize sensors with pins (real hardware)
|
||||||
sensorData[sensor.id] = {
|
sensorData[sensor.id] = {
|
||||||
id: sensor.id,
|
id: sensor.id,
|
||||||
x: sensor.x,
|
x: sensor.x,
|
||||||
y: sensor.y,
|
y: sensor.y,
|
||||||
label: sensor.label,
|
label: sensor.label,
|
||||||
zone: sensor.zone,
|
zone: sensor.zone,
|
||||||
value: 1000 + Math.random() * 500, // Start with baseline analog value (1000-1500)
|
value: 0, // Start with zero until real data arrives
|
||||||
pin: sensor.pin,
|
pin: sensor.pin,
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
source: sensor.pin ? 'hardware' : 'mock',
|
source: 'hardware',
|
||||||
data: generateTimeSeriesData(),
|
data: [], // Start with empty data array
|
||||||
status: 'normal',
|
status: 'normal',
|
||||||
warningThreshold: sensor.warningThreshold,
|
warningThreshold: sensor.warningThreshold,
|
||||||
alarmThreshold: sensor.alarmThreshold,
|
alarmThreshold: sensor.alarmThreshold,
|
||||||
|
@ -89,21 +80,7 @@ function initializeSensorData() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate time series data for a sensor
|
|
||||||
function generateTimeSeriesData(hours = 1) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize hardware connection
|
// Initialize hardware connection
|
||||||
async function initializeHardware() {
|
async function initializeHardware() {
|
||||||
|
@ -133,39 +110,19 @@ async function initializeHardware() {
|
||||||
isHardwareConnected = false;
|
isHardwareConnected = false;
|
||||||
});
|
});
|
||||||
|
|
||||||
await bedHardwareInstance.connect();
|
await bedHardwareInstance.connect(); } catch (error) {
|
||||||
} catch (error) {
|
console.warn('Failed to connect to hardware, system will wait for real hardware data:', error);
|
||||||
console.warn('Failed to connect to hardware, using mock data:', error);
|
|
||||||
isHardwareConnected = false;
|
isHardwareConnected = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert digital pin state to analog value with noise
|
|
||||||
function digitalToAnalogValue(pinState: number, baseNoise: number): number {
|
|
||||||
// Base value from digital state
|
|
||||||
const baseValue = pinState === 1 ? 3000 : 1000; // High when pin is HIGH, low when LOW
|
|
||||||
|
|
||||||
// Add realistic noise and variation
|
|
||||||
const timeNoise = Math.sin(Date.now() / 10000) * 200; // Slow oscillation
|
|
||||||
const randomNoise = (Math.random() - 0.5) * baseNoise;
|
|
||||||
const sensorDrift = (Math.random() - 0.5) * 50; // Small drift
|
|
||||||
|
|
||||||
const value = baseValue + timeNoise + randomNoise + sensorDrift;
|
|
||||||
|
|
||||||
// Clamp between 0 and 4095
|
|
||||||
return Math.max(0, Math.min(4095, Math.floor(value)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update sensor data from pin change
|
// Update sensor data from pin change
|
||||||
async function updateSensorFromPin(pin: number, state: number) {
|
async function updateSensorFromPin(pin: number, value: number) {
|
||||||
const mapping = PIN_SENSOR_MAP[pin];
|
const mapping = PIN_SENSOR_MAP[pin];
|
||||||
if (!mapping) return;
|
if (!mapping) return;
|
||||||
|
|
||||||
const value = digitalToAnalogValue(state, mapping.baseNoise);
|
|
||||||
const timestamp = Date.now();
|
const timestamp = Date.now();
|
||||||
const time = new Date(timestamp).toLocaleTimeString("en-US", { hour12: false });
|
const time = new Date(timestamp).toLocaleTimeString("en-US", { hour12: false });
|
||||||
|
// Save to persistent storage
|
||||||
// Save to persistent storage
|
|
||||||
const dataPoint: SensorDataPoint = {
|
const dataPoint: SensorDataPoint = {
|
||||||
sensorId: mapping.id,
|
sensorId: mapping.id,
|
||||||
value,
|
value,
|
||||||
|
@ -173,7 +130,7 @@ async function updateSensorFromPin(pin: number, state: number) {
|
||||||
time,
|
time,
|
||||||
source: 'hardware',
|
source: 'hardware',
|
||||||
pin,
|
pin,
|
||||||
digitalState: state
|
digitalState: value
|
||||||
};
|
};
|
||||||
await sensorDataStorage.addDataPoint(dataPoint);
|
await sensorDataStorage.addDataPoint(dataPoint);
|
||||||
|
|
||||||
|
@ -198,11 +155,10 @@ async function updateSensorFromPin(pin: number, state: number) {
|
||||||
} else {
|
} else {
|
||||||
warningStartTime = undefined; // Clear warning timer
|
warningStartTime = undefined; // Clear warning timer
|
||||||
}
|
}
|
||||||
|
sensorData[mapping.id] = {
|
||||||
sensorData[mapping.id] = {
|
|
||||||
...currentData,
|
...currentData,
|
||||||
value,
|
value,
|
||||||
digitalState: state,
|
digitalState: value,
|
||||||
timestamp: new Date().toISOString(),
|
timestamp: new Date().toISOString(),
|
||||||
source: 'hardware',
|
source: 'hardware',
|
||||||
data: [
|
data: [
|
||||||
|
@ -219,68 +175,18 @@ async function updateSensorFromPin(pin: number, state: number) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update mock sensor data with variation
|
|
||||||
function updateMockSensorData() {
|
|
||||||
SENSOR_CONFIG.forEach(sensor => {
|
|
||||||
if (!sensor.pin && sensorData[sensor.id]) {
|
|
||||||
// This is a mock sensor, update with variation
|
|
||||||
const currentSensor = sensorData[sensor.id];
|
|
||||||
const variation = (Math.random() - 0.5) * 200; // Larger variation for analog values
|
|
||||||
const newValue = Math.max(0, Math.min(4095, currentSensor.value + variation));
|
|
||||||
const timestamp = Date.now();
|
|
||||||
|
|
||||||
// Determine status based on thresholds
|
|
||||||
let status = 'normal';
|
|
||||||
let warningStartTime = currentSensor.warningStartTime;
|
|
||||||
|
|
||||||
if (newValue >= sensor.alarmThreshold) {
|
|
||||||
status = 'alarm';
|
|
||||||
warningStartTime = undefined; // Clear warning timer for immediate alarm
|
|
||||||
} else if (newValue >= sensor.warningThreshold) {
|
|
||||||
status = 'warning';
|
|
||||||
if (!warningStartTime) {
|
|
||||||
warningStartTime = timestamp; // Start warning timer
|
|
||||||
} else if (timestamp - warningStartTime >= sensor.warningDelayMs) {
|
|
||||||
status = 'alarm'; // Escalate to alarm after delay
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
warningStartTime = undefined; // Clear warning timer
|
|
||||||
}
|
|
||||||
|
|
||||||
sensorData[sensor.id] = {
|
|
||||||
...currentSensor,
|
|
||||||
value: newValue,
|
|
||||||
timestamp: new Date().toISOString(),
|
|
||||||
data: [
|
|
||||||
...currentSensor.data.slice(-287), // Keep last ~24 hours
|
|
||||||
{
|
|
||||||
time: new Date().toLocaleTimeString("en-US", { hour12: false }),
|
|
||||||
timestamp: Date.now(),
|
|
||||||
value: newValue,
|
|
||||||
}
|
|
||||||
],
|
|
||||||
status,
|
|
||||||
warningStartTime
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
// Initialize sensor data if not already done
|
// Initialize sensor data if not already done
|
||||||
if (Object.keys(sensorData).length === 0) {
|
if (Object.keys(sensorData).length === 0) {
|
||||||
initializeSensorData();
|
initializeSensorData();
|
||||||
}
|
} // Initialize hardware if not already done
|
||||||
|
|
||||||
// Initialize hardware if not already done
|
|
||||||
if (!isHardwareConnected) {
|
if (!isHardwareConnected) {
|
||||||
await initializeHardware();
|
await initializeHardware();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update mock sensor data
|
|
||||||
updateMockSensorData();
|
|
||||||
|
|
||||||
// If hardware is connected, get current pin states
|
// If hardware is connected, get current pin states
|
||||||
if (isHardwareConnected) {
|
if (isHardwareConnected) {
|
||||||
const pinStates = bedHardwareInstance.getAllPinStates();
|
const pinStates = bedHardwareInstance.getAllPinStates();
|
||||||
|
@ -318,7 +224,7 @@ export async function POST(request: NextRequest) {
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
connected: isHardwareConnected,
|
connected: isHardwareConnected,
|
||||||
message: isHardwareConnected ? 'Hardware connected' : 'Using mock data'
|
message: isHardwareConnected ? 'Hardware connected' : 'Hardware connection failed'
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue