warning system

This commit is contained in:
Siwat Sirichai 2025-06-21 13:29:01 +07:00
parent 5e029ff99c
commit 0c5c7bcb5f
10 changed files with 1074 additions and 54 deletions

View file

@ -1,9 +1,10 @@
import { NextRequest, NextResponse } from 'next/server';
import { BedHardware, PinState, PinChange } from '@/services/BedHardware';
import { SensorDataStorage, SensorDataPoint } from '@/services/SensorDataStorage';
import { SensorConfig } from '@/types/sensor';
// Complete sensor configuration with positions, pin mappings, and thresholds
const SENSOR_CONFIG = [
const SENSOR_CONFIG: SensorConfig[] = [
// Head area
{ id: "head-1", x: 45, y: 15, zone: "head", label: "Head Left", pin: 2, baseNoise: 200, warningThreshold: 3000, alarmThreshold: 3500, warningDelayMs: 30000 },
{ id: "head-2", x: 55, y: 15, zone: "head", label: "Head Right", pin: 3, baseNoise: 150, warningThreshold: 3000, alarmThreshold: 3500, warningDelayMs: 30000 },
@ -235,6 +236,25 @@ function updateMockSensorData() {
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,
@ -248,8 +268,8 @@ function updateMockSensorData() {
value: newValue,
}
],
status: newValue >= sensor.alarmThreshold ? 'alarm' :
newValue >= sensor.warningThreshold ? 'warning' : 'normal'
status,
warningStartTime
};
}
});