From 29f7b25954a6a409bf741e01d75b96755a99a530 Mon Sep 17 00:00:00 2001 From: Alexander Borg Date: Tue, 9 Sep 2025 06:54:13 +0200 Subject: [PATCH] Fix jwt-token --- .env | 6 +-- server/routes/detections.js | 89 +++++++++++++++++++++++++++++++++++++ server/routes/detectors.js | 74 ++++++++++++++---------------- 3 files changed, 126 insertions(+), 43 deletions(-) diff --git a/.env b/.env index 9d873df..7be1d1b 100644 --- a/.env +++ b/.env @@ -30,9 +30,9 @@ TWILIO_PHONE_NUMBER=your_twilio_phone_number_here CORS_ORIGIN=http://localhost:3000 # Debug Configuration -STORE_HEARTBEATS=false -STORE_DRONE_TYPE0=false -LOG_ALL_DETECTIONS=false +STORE_HEARTBEATS=true +STORE_DRONE_TYPE0=true +LOG_ALL_DETECTIONS=true API_DEBUG=true # Health Probe Simulator Configuration diff --git a/server/routes/detections.js b/server/routes/detections.js index 2104a7d..0d535b3 100644 --- a/server/routes/detections.js +++ b/server/routes/detections.js @@ -271,4 +271,93 @@ router.delete('/:id', authenticateToken, async (req, res) => { } }); +/** + * GET /api/detections/debug + * Get all drone detections including drone_type 0 for debugging + */ +router.get('/debug', authenticateToken, async (req, res) => { + try { + const { + device_id, + start_date, + end_date, + page = 1, + limit = 100, + sort = 'server_timestamp', + order = 'desc' + } = req.query; + + // Build where clause for filtering (includes drone_type 0) + const whereClause = {}; + + if (device_id) { + whereClause.device_id = device_id; + } + + if (start_date) { + whereClause.server_timestamp = { ...whereClause.server_timestamp, [Op.gte]: new Date(start_date) }; + } + + if (end_date) { + whereClause.server_timestamp = { ...whereClause.server_timestamp, [Op.lte]: new Date(end_date) }; + } + + // Calculate offset for pagination + const offset = (parseInt(page) - 1) * parseInt(limit); + + // Query ALL detections including drone_type 0 + const { count, rows: detections } = await DroneDetection.findAndCountAll({ + where: whereClause, + include: [{ + model: Device, + attributes: ['id', 'name', 'geo_lat', 'geo_lon', 'location_description'] + }], + order: [[sort, order.toUpperCase()]], + limit: parseInt(limit), + offset: offset + }); + + const totalPages = Math.ceil(count / parseInt(limit)); + const hasNextPage = parseInt(page) < totalPages; + const hasPrevPage = parseInt(page) > 1; + + // Enhance detections with drone type information and debug flags + const enhancedDetections = detections.map(detection => { + const droneTypeInfo = getDroneTypeInfo(detection.drone_type); + const isDebugDetection = detection.drone_type === 0; + + return { + ...detection.toJSON(), + drone_type_info: droneTypeInfo, + is_debug_detection: isDebugDetection, + debug_note: isDebugDetection ? 'Debug detection (drone_type 0) - no alerts triggered' : null + }; + }); + + res.json({ + detections: enhancedDetections, + pagination: { + currentPage: parseInt(page), + totalPages, + totalCount: count, + limit: parseInt(limit), + hasNextPage, + hasPrevPage + }, + debug_info: { + includes_drone_type_0: true, + total_detections: count, + debug_detections: enhancedDetections.filter(d => d.is_debug_detection).length + } + }); + + } catch (error) { + console.error('Error fetching debug detections:', error); + res.status(500).json({ + error: 'Failed to fetch debug detections', + details: error.message + }); + } +}); + module.exports = router; diff --git a/server/routes/detectors.js b/server/routes/detectors.js index a2ea743..2c3b87d 100644 --- a/server/routes/detectors.js +++ b/server/routes/detectors.js @@ -280,24 +280,13 @@ async function handleDetection(req, res) { }); } - // Handle drone type 0 (None) - should not trigger alarms or be stored as detection + // Handle drone type 0 (None) - store for debugging but don't trigger alarms + let isDebugDetection = false; if (detectionData.drone_type === 0) { if (DEBUG_CONFIG.logAllDetections) { - console.log(`🔍 Debug: Drone type 0 (None) received from device ${detectionData.device_id}`); + console.log(`🔍 Debug: Drone type 0 (None) received from device ${detectionData.device_id} - storing for debug purposes`); } - - if (!DEBUG_CONFIG.storeNoneDetections) { - // Don't store in database, just acknowledge receipt - return res.status(200).json({ - success: true, - message: 'Heartbeat received (no detection)', - stored: false, - debug: DEBUG_CONFIG.logAllDetections - }); - } - - // If debugging enabled, store but mark as debug data - console.log(`🐛 Debug mode: Storing drone type 0 detection for debugging`); + isDebugDetection = true; } // Create detection record @@ -313,33 +302,38 @@ async function handleDetection(req, res) { }); // Emit real-time update via Socket.IO with movement analysis (from original) - req.io.emit('drone_detection', { - id: detection.id, - device_id: detection.device_id, - drone_id: detection.drone_id, - drone_type: detection.drone_type, - rssi: detection.rssi, - freq: detection.freq, - geo_lat: detection.geo_lat, - geo_lon: detection.geo_lon, - server_timestamp: detection.server_timestamp, - confidence_level: detection.confidence_level, - signal_duration: detection.signal_duration, - movement_analysis: movementAnalysis, - device: { - id: device.id, - name: device.name, - geo_lat: device.geo_lat, - geo_lon: device.geo_lon - } - }); + // Skip real-time updates for debug detections (drone_type 0) + if (!isDebugDetection) { + req.io.emit('drone_detection', { + id: detection.id, + device_id: detection.device_id, + drone_id: detection.drone_id, + drone_type: detection.drone_type, + rssi: detection.rssi, + freq: detection.freq, + geo_lat: detection.geo_lat, + geo_lon: detection.geo_lon, + server_timestamp: detection.server_timestamp, + confidence_level: detection.confidence_level, + signal_duration: detection.signal_duration, + movement_analysis: movementAnalysis, + device: { + id: device.id, + name: device.name, + geo_lat: device.geo_lat, + geo_lon: device.geo_lon + } + }); - // Process alerts asynchronously (from original) - alertService.processAlert(detection, req.io).catch(error => { - console.error('Alert processing error:', error); - }); + // Process alerts asynchronously (from original) + alertService.processAlert(detection, req.io).catch(error => { + console.error('Alert processing error:', error); + }); - console.log(`✅ Detection recorded and alert processing initiated for detection ${detection.id}`); + console.log(`✅ Detection recorded and alert processing initiated for detection ${detection.id}`); + } else { + console.log(`🐛 Debug detection stored for device ${detection.device_id} (drone_type 0) - no alerts or real-time updates`); + } return res.status(201).json({ success: true,