feat: Connect Pulse alerts to Edge failsafe
This commit is contained in:
parent
6ae2b35f8d
commit
e4c506d074
2 changed files with 44 additions and 3 deletions
|
|
@ -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 }
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue