diff --git a/backend/src/routes/environment.routes.ts b/backend/src/routes/environment.routes.ts index b7c8e8e..28a5519 100644 --- a/backend/src/routes/environment.routes.ts +++ b/backend/src/routes/environment.routes.ts @@ -521,9 +521,27 @@ 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); + const criticalAlert = await prisma.environmentAlert.findFirst({ + where: { + type: 'TEMPERATURE_HIGH', + resolvedAt: null, + acknowledgedAt: null, + createdAt: { lt: fifteenMinsAgo } + } + }); + + const commands: any[] = []; + if (criticalAlert) { + fastify.log.warn(`🚨 Failsafe triggered for alert ${criticalAlert.id}! Sending Kasa ON command to edge.`); + commands.push({ type: 'toggle_plug', state: true }); + } + return { ack: true, - serverTime: new Date().toISOString() + serverTime: new Date().toISOString(), + commands }; } catch (error) { fastify.log.error(error);