From e4c506d0747e009ad937bc3b313404f3a7e3f15f Mon Sep 17 00:00:00 2001 From: fullsizemalt <106900403+fullsizemalt@users.noreply.github.com> Date: Mon, 5 Jan 2026 23:51:48 -0800 Subject: [PATCH] feat: Connect Pulse alerts to Edge failsafe --- backend/src/routes/environment.routes.ts | 6 ++-- backend/src/routes/pulse.routes.ts | 41 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/backend/src/routes/environment.routes.ts b/backend/src/routes/environment.routes.ts index 28a5519..3b74edb 100644 --- a/backend/src/routes/environment.routes.ts +++ b/backend/src/routes/environment.routes.ts @@ -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 } } }); diff --git a/backend/src/routes/pulse.routes.ts b/backend/src/routes/pulse.routes.ts index 99938d3..04d9ee9 100644 --- a/backend/src/routes/pulse.routes.ts +++ b/backend/src/routes/pulse.routes.ts @@ -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()