feat: Connect Pulse alerts to Edge failsafe
Some checks are pending
Test / backend-test (push) Waiting to run
Test / frontend-test (push) Waiting to run

This commit is contained in:
fullsizemalt 2026-01-05 23:51:48 -08:00
parent 6ae2b35f8d
commit e4c506d074
2 changed files with 44 additions and 3 deletions

View file

@ -521,14 +521,14 @@ export async function environmentRoutes(fastify: FastifyInstance) {
fastify.log.warn(`Edge device ${data.edgeId} status: ${data.status}`);
}
// Check for unaddressed critical temperature alerts (> 15 mins)
const fifteenMinsAgo = new Date(Date.now() - 15 * 60 * 1000);
// Check for unaddressed critical temperature alerts (> 30 seconds for DEMO)
const thirtySecondsAgo = new Date(Date.now() - 30 * 1000);
const criticalAlert = await prisma.environmentAlert.findFirst({
where: {
type: 'TEMPERATURE_HIGH',
resolvedAt: null,
acknowledgedAt: null,
createdAt: { lt: fifteenMinsAgo }
createdAt: { lt: thirtySecondsAgo }
}
});

View file

@ -5,8 +5,11 @@
*/
import { FastifyInstance } from 'fastify';
import { PrismaClient } from '@prisma/client';
import { getPulseService, initPulseService } from '../services/pulse.service';
const prisma = new PrismaClient();
export async function pulseRoutes(fastify: FastifyInstance) {
// Auth middleware
fastify.addHook('onRequest', async (request) => {
@ -110,6 +113,44 @@ export async function pulseRoutes(fastify: FastifyInstance) {
try {
const readings = await pulse.getCurrentReadings();
// Check thresholds and persist alerts (for demo/failsafe)
const alerts: any[] = [];
for (const reading of readings) {
if (reading.temperature !== undefined && pulseThresholds.temperature.max && reading.temperature > pulseThresholds.temperature.max) {
const alertType = 'TEMPERATURE_HIGH';
// Check if active alert exists
const activeAlert = await prisma.environmentAlert.findFirst({
where: {
type: alertType,
message: { contains: reading.deviceName || reading.deviceId },
resolvedAt: null
}
});
if (!activeAlert) {
const newAlert = await prisma.environmentAlert.create({
data: {
type: alertType,
severity: 'WARNING',
message: `${reading.deviceName || 'Pulse Device'}: ${alertType.replace('_', ' ')} (${reading.temperature} vs ${pulseThresholds.temperature.max})`,
value: reading.temperature,
threshold: pulseThresholds.temperature.max,
createdAt: new Date()
}
});
alerts.push(createAlert(reading, alertType, reading.temperature, pulseThresholds.temperature.max));
fastify.log.info(`🚨 Created new Pulse alert: ${newAlert.id}`);
}
}
}
// Broadcast any new alerts
if (alerts.length > 0) {
alerts.forEach(a => broadcastAlert(a));
}
return {
readings,
timestamp: new Date().toISOString()