Fix jwt-token
This commit is contained in:
@@ -7,6 +7,7 @@ class AlertService {
|
||||
this.twilioClient = null;
|
||||
this.twilioPhone = null;
|
||||
this.twilioEnabled = false;
|
||||
this.activeAlerts = new Map(); // Track active alerts for clear notifications
|
||||
this.initializeTwilio();
|
||||
}
|
||||
|
||||
@@ -156,6 +157,15 @@ class AlertService {
|
||||
for (const rule of alertRules) {
|
||||
if (await this.shouldTriggerAlert(rule, detection, threatAssessment)) {
|
||||
await this.triggerAlert(rule, detection, threatAssessment);
|
||||
|
||||
// Track active alert for potential clear notification
|
||||
const alertKey = `${rule.id}-${detection.device_id}`;
|
||||
this.activeAlerts.set(alertKey, {
|
||||
rule,
|
||||
detection,
|
||||
threatAssessment,
|
||||
alertTime: new Date()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,7 +323,7 @@ class AlertService {
|
||||
// SECURITY ENHANCEMENT: For critical threats, send to all available channels
|
||||
const channels = threatAssessment.level === 'critical'
|
||||
? ['sms', 'email', 'webhook'] // Force all channels for critical threats
|
||||
: rule.actions.channels || ['sms'];
|
||||
: rule.alert_channels || ['sms'];
|
||||
|
||||
// Send alerts through configured channels
|
||||
for (const channel of channels) {
|
||||
@@ -322,8 +332,8 @@ class AlertService {
|
||||
try {
|
||||
switch (channel) {
|
||||
case 'sms':
|
||||
if (rule.actions.sms && rule.actions.phone_number) {
|
||||
alertLog = await this.sendSMSAlert(rule.actions.phone_number, message, rule, detection, threatAssessment);
|
||||
if (rule.alert_channels.includes('sms') && rule.sms_phone_number) {
|
||||
alertLog = await this.sendSMSAlert(rule.sms_phone_number, message, rule, detection, threatAssessment);
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -375,7 +385,8 @@ class AlertService {
|
||||
// Check if Twilio is enabled
|
||||
if (!this.twilioEnabled || !this.twilioClient) {
|
||||
console.log('📱 SMS alert skipped - Twilio not configured');
|
||||
console.log(`📱 Would have sent to ${phoneNumber}: ${message}`);
|
||||
console.log(`📱 Would have sent to ${phoneNumber}:`);
|
||||
console.log(`📱 Message: ${message}`);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
@@ -392,7 +403,8 @@ class AlertService {
|
||||
}
|
||||
|
||||
try {
|
||||
console.log(`📱 Sending SMS alert to ${phoneNumber}`);
|
||||
console.log(`📱 Sending SMS alert to ${phoneNumber} for rule: ${rule.name}`);
|
||||
console.log(`📱 Message: ${message}`);
|
||||
|
||||
const twilioMessage = await this.twilioClient.messages.create({
|
||||
body: message,
|
||||
@@ -400,7 +412,7 @@ class AlertService {
|
||||
to: phoneNumber
|
||||
});
|
||||
|
||||
console.log(`✅ SMS sent successfully: ${twilioMessage.sid}`);
|
||||
console.log(`✅ SMS sent successfully to ${phoneNumber}: ${twilioMessage.sid}`);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
@@ -414,7 +426,7 @@ class AlertService {
|
||||
priority: rule.priority
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to send SMS:', error.message);
|
||||
console.error(`❌ Failed to send SMS to ${phoneNumber}:`, error.message);
|
||||
|
||||
return await AlertLog.create({
|
||||
alert_rule_id: rule.id,
|
||||
@@ -553,6 +565,59 @@ class AlertService {
|
||||
|
||||
return hours * 60 + minutes;
|
||||
}
|
||||
|
||||
// Check for cleared alerts (call this periodically)
|
||||
async checkClearedAlerts() {
|
||||
try {
|
||||
const now = new Date();
|
||||
const clearThreshold = 5 * 60 * 1000; // 5 minutes without detection = cleared
|
||||
|
||||
for (const [alertKey, alertData] of this.activeAlerts.entries()) {
|
||||
const timeSinceAlert = now - alertData.alertTime;
|
||||
|
||||
if (timeSinceAlert > clearThreshold) {
|
||||
// Check if there are any recent detections from this device
|
||||
const recentDetections = await DroneDetection.count({
|
||||
where: {
|
||||
device_id: alertData.detection.device_id,
|
||||
device_timestamp: {
|
||||
[Op.gte]: new Date(now - clearThreshold)
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (recentDetections === 0) {
|
||||
await this.sendClearAlert(alertData);
|
||||
this.activeAlerts.delete(alertKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error checking cleared alerts:', error);
|
||||
}
|
||||
}
|
||||
|
||||
async sendClearAlert(alertData) {
|
||||
const { rule, detection } = alertData;
|
||||
|
||||
if (rule.alert_channels.includes('sms') && rule.sms_phone_number) {
|
||||
const device = await Device.findByPk(detection.device_id);
|
||||
const clearMessage = this.generateClearMessage(device, rule);
|
||||
|
||||
console.log(`🟢 ALERT CLEARED - Sending clear notification for ${device.name} to ${rule.sms_phone_number}`);
|
||||
|
||||
await this.sendSMSAlert(rule.sms_phone_number, clearMessage, rule, detection, null);
|
||||
}
|
||||
}
|
||||
|
||||
generateClearMessage(device, rule) {
|
||||
return `🟢 ALL CLEAR 🟢\n\n` +
|
||||
`📍 LOCATION: ${device.location_description || device.name}\n` +
|
||||
`🔧 DEVICE: ${device.name}\n` +
|
||||
`⏰ TIME: ${new Date().toLocaleString('sv-SE')}\n\n` +
|
||||
`✅ No drone activity detected for 5+ minutes.\n` +
|
||||
`🛡️ Area is secure.`;
|
||||
}
|
||||
}
|
||||
|
||||
// Export the class directly (not a singleton instance)
|
||||
|
||||
Reference in New Issue
Block a user