Fix jwt-token
This commit is contained in:
@@ -4,10 +4,32 @@ const Joi = require('joi');
|
||||
const { DroneDetection, Device } = require('../models');
|
||||
const { Op } = require('sequelize');
|
||||
const AlertService = require('../services/alertService');
|
||||
const DroneTrackingService = require('../services/droneTrackingService');
|
||||
const { validateRequest } = require('../middleware/validation');
|
||||
|
||||
// Initialize AlertService instance
|
||||
// Initialize services
|
||||
const alertService = new AlertService();
|
||||
const droneTracker = new DroneTrackingService();
|
||||
|
||||
// Handle movement alerts from the tracking service
|
||||
droneTracker.on('movement_alert', (alertData) => {
|
||||
const { io } = require('../index');
|
||||
if (io) {
|
||||
// Emit to dashboard with detailed movement information
|
||||
io.emitToDashboard('drone_movement_alert', {
|
||||
...alertData,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
|
||||
// Emit to specific device room
|
||||
io.emitToDevice(alertData.deviceId, 'drone_movement_alert', {
|
||||
...alertData,
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
|
||||
console.log(`🚨 Movement Alert: ${alertData.analysis.description} (Drone ${alertData.droneId})`);
|
||||
}
|
||||
});
|
||||
|
||||
// Validation schema for drone detection
|
||||
const droneDetectionSchema = Joi.object({
|
||||
@@ -45,7 +67,13 @@ router.post('/', validateRequest(droneDetectionSchema), async (req, res) => {
|
||||
server_timestamp: new Date()
|
||||
});
|
||||
|
||||
// Emit real-time update via Socket.IO
|
||||
// Process detection through tracking service for movement analysis
|
||||
const movementAnalysis = droneTracker.processDetection({
|
||||
...detectionData,
|
||||
server_timestamp: detection.server_timestamp
|
||||
});
|
||||
|
||||
// Emit real-time update via Socket.IO with movement analysis
|
||||
req.io.emit('drone_detection', {
|
||||
id: detection.id,
|
||||
device_id: detection.device_id,
|
||||
@@ -56,6 +84,9 @@ router.post('/', validateRequest(droneDetectionSchema), async (req, res) => {
|
||||
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,
|
||||
@@ -229,4 +260,55 @@ router.get('/:id', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/detections/tracking/active - Get active drone tracking information
|
||||
router.get('/tracking/active', async (req, res) => {
|
||||
try {
|
||||
const activeTracking = droneTracker.getAllActiveTracking();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: {
|
||||
active_drones: activeTracking.length,
|
||||
tracking_data: activeTracking
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching active tracking:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch active tracking data',
|
||||
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/detections/tracking/:droneId/:deviceId - Get specific drone tracking
|
||||
router.get('/tracking/:droneId/:deviceId', async (req, res) => {
|
||||
try {
|
||||
const { droneId, deviceId } = req.params;
|
||||
const trackingData = droneTracker.getDroneStatus(droneId, deviceId);
|
||||
|
||||
if (!trackingData) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
message: 'No tracking data found for this drone-device combination'
|
||||
});
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
data: trackingData
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error fetching drone tracking:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
message: 'Failed to fetch drone tracking data',
|
||||
error: process.env.NODE_ENV === 'development' ? error.message : 'Internal server error'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user