Fix jwt-token

This commit is contained in:
2025-09-09 06:54:13 +02:00
parent a879cd7487
commit 29f7b25954
3 changed files with 126 additions and 43 deletions

6
.env
View File

@@ -30,9 +30,9 @@ TWILIO_PHONE_NUMBER=your_twilio_phone_number_here
CORS_ORIGIN=http://localhost:3000 CORS_ORIGIN=http://localhost:3000
# Debug Configuration # Debug Configuration
STORE_HEARTBEATS=false STORE_HEARTBEATS=true
STORE_DRONE_TYPE0=false STORE_DRONE_TYPE0=true
LOG_ALL_DETECTIONS=false LOG_ALL_DETECTIONS=true
API_DEBUG=true API_DEBUG=true
# Health Probe Simulator Configuration # Health Probe Simulator Configuration

View File

@@ -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; module.exports = router;

View File

@@ -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 (detectionData.drone_type === 0) {
if (DEBUG_CONFIG.logAllDetections) { 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`);
} }
isDebugDetection = true;
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`);
} }
// Create detection record // Create detection record
@@ -313,33 +302,38 @@ async function handleDetection(req, res) {
}); });
// Emit real-time update via Socket.IO with movement analysis (from original) // Emit real-time update via Socket.IO with movement analysis (from original)
req.io.emit('drone_detection', { // Skip real-time updates for debug detections (drone_type 0)
id: detection.id, if (!isDebugDetection) {
device_id: detection.device_id, req.io.emit('drone_detection', {
drone_id: detection.drone_id, id: detection.id,
drone_type: detection.drone_type, device_id: detection.device_id,
rssi: detection.rssi, drone_id: detection.drone_id,
freq: detection.freq, drone_type: detection.drone_type,
geo_lat: detection.geo_lat, rssi: detection.rssi,
geo_lon: detection.geo_lon, freq: detection.freq,
server_timestamp: detection.server_timestamp, geo_lat: detection.geo_lat,
confidence_level: detection.confidence_level, geo_lon: detection.geo_lon,
signal_duration: detection.signal_duration, server_timestamp: detection.server_timestamp,
movement_analysis: movementAnalysis, confidence_level: detection.confidence_level,
device: { signal_duration: detection.signal_duration,
id: device.id, movement_analysis: movementAnalysis,
name: device.name, device: {
geo_lat: device.geo_lat, id: device.id,
geo_lon: device.geo_lon name: device.name,
} geo_lat: device.geo_lat,
}); geo_lon: device.geo_lon
}
});
// Process alerts asynchronously (from original) // Process alerts asynchronously (from original)
alertService.processAlert(detection, req.io).catch(error => { alertService.processAlert(detection, req.io).catch(error => {
console.error('Alert processing error:', 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({ return res.status(201).json({
success: true, success: true,