diff --git a/server/services/alertService.js b/server/services/alertService.js index aeb4879..1a94806 100644 --- a/server/services/alertService.js +++ b/server/services/alertService.js @@ -273,7 +273,7 @@ class AlertService { console.log(`✅ Rule "${rule.name}": No device filter (applies to all devices)`); } - // PRIORITY 2: Cooldown period check (applies even to critical threats for the same device) + // PRIORITY 2: Cooldown period check (applies even to critical threats for the same device+drone combination) if (rule.cooldown_period > 0) { const cooldownStart = new Date(Date.now() - rule.cooldown_period * 1000); @@ -289,29 +289,32 @@ class AlertService { include: [{ model: DroneDetection, as: 'detection', - attributes: ['device_id'], + attributes: ['device_id', 'drone_id'], required: false // Allow alerts without detection_id (like device offline alerts) }] }); - // Check if any recent alert was for the same device - const deviceAlert = recentAlerts.find(alert => { - // For alerts with detection, check device_id match - if (alert.detection && alert.detection.device_id === detection.device_id) { + // Check if any recent alert was for the same device AND same drone + const deviceDroneAlert = recentAlerts.find(alert => { + // For alerts with detection, check both device_id AND drone_id match + if (alert.detection && + alert.detection.device_id === detection.device_id && + alert.detection.drone_id === detection.drone_id) { return true; } // For alerts without detection (like device offline), check device_id field directly + // These don't have drone_id so they follow device-level cooldown if (!alert.detection && alert.device_id === detection.device_id) { return true; } return false; }); - if (deviceAlert) { - console.log(`❌ Rule "${rule.name}": Still in cooldown period for device ${detection.device_id} (last alert: ${deviceAlert.sent_at})`); + if (deviceDroneAlert) { + console.log(`❌ Rule "${rule.name}": Still in cooldown period for device ${detection.device_id} + drone ${detection.drone_id} (last alert: ${deviceDroneAlert.sent_at})`); return false; } else { - console.log(`✅ Rule "${rule.name}": Cooldown period expired or no recent alerts for device ${detection.device_id}`); + console.log(`✅ Rule "${rule.name}": Cooldown period expired or no recent alerts for device ${detection.device_id} + drone ${detection.drone_id}`); } }