Fix jwt-token

This commit is contained in:
2025-08-28 11:40:04 +02:00
parent f54a763ebb
commit 5919794408
6 changed files with 421 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ const express = require('express');
const DroneDetection = require('../models/DroneDetection');
const Device = require('../models/Device');
const { authenticateToken } = require('../middleware/auth');
const { getDroneTypeInfo } = require('../utils/droneTypes');
const router = express.Router();
/**
@@ -66,8 +67,17 @@ router.get('/', authenticateToken, async (req, res) => {
const hasNextPage = parseInt(page) < totalPages;
const hasPrevPage = parseInt(page) > 1;
// Enhance detections with drone type information
const enhancedDetections = detections.map(detection => {
const droneTypeInfo = getDroneTypeInfo(detection.drone_type);
return {
...detection.toJSON(),
drone_type_info: droneTypeInfo
};
});
res.json({
detections,
detections: enhancedDetections,
pagination: {
currentPage: parseInt(page),
totalPages,
@@ -107,7 +117,14 @@ router.get('/:id', authenticateToken, async (req, res) => {
return res.status(404).json({ error: 'Detection not found' });
}
res.json(detection);
// Enhance detection with drone type information
const droneTypeInfo = getDroneTypeInfo(detection.drone_type);
const enhancedDetection = {
...detection.toJSON(),
drone_type_info: droneTypeInfo
};
res.json(enhancedDetection);
} catch (error) {
console.error('Error fetching detection:', error);

View File

@@ -5,6 +5,7 @@ const { validateRequest } = require('../middleware/validation');
const { Heartbeat, Device, DroneDetection } = require('../models');
const AlertService = require('../services/alertService');
const DroneTrackingService = require('../services/droneTrackingService');
const { getDroneTypeInfo, getDroneTypeName } = require('../utils/droneTypes');
// Initialize services
const alertService = new AlertService();
@@ -191,7 +192,11 @@ async function handleHeartbeat(req, res) {
async function handleDetection(req, res) {
const detectionData = req.body;
console.log(`🚁 Drone detection received from device ${detectionData.device_id}: drone_id=${detectionData.drone_id}, type=${detectionData.drone_type}, rssi=${detectionData.rssi}`);
// Get drone type information
const droneTypeInfo = getDroneTypeInfo(detectionData.drone_type);
console.log(`🚁 Drone detection received from device ${detectionData.device_id}: drone_id=${detectionData.drone_id}, type=${detectionData.drone_type} (${droneTypeInfo.name}), rssi=${detectionData.rssi}`);
console.log(`🎯 Drone Type Details: ${droneTypeInfo.category} | Threat: ${droneTypeInfo.threat_level} | ${droneTypeInfo.description}`);
console.log('🔍 Complete detection data:', JSON.stringify(detectionData, null, 2));
// Check if device exists and is approved

160
server/routes/droneTypes.js Normal file
View File

@@ -0,0 +1,160 @@
const express = require('express');
const router = express.Router();
const {
getAllDroneTypes,
getDroneTypesByCategory,
getDroneTypesByThreatLevel,
getDroneTypeInfo
} = require('../utils/droneTypes');
/**
* GET /api/drone-types
* Get all available drone types
*/
router.get('/', (req, res) => {
try {
const droneTypes = getAllDroneTypes();
// Convert to array format with IDs
const droneTypesArray = Object.entries(droneTypes).map(([id, info]) => ({
id: parseInt(id),
...info
}));
res.json({
success: true,
data: droneTypesArray,
total: droneTypesArray.length
});
} catch (error) {
console.error('Error fetching drone types:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch drone types',
details: error.message
});
}
});
/**
* GET /api/drone-types/:id
* Get specific drone type by ID
*/
router.get('/:id', (req, res) => {
try {
const { id } = req.params;
const droneTypeInfo = getDroneTypeInfo(parseInt(id));
res.json({
success: true,
data: droneTypeInfo
});
} catch (error) {
console.error('Error fetching drone type:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch drone type',
details: error.message
});
}
});
/**
* GET /api/drone-types/category/:category
* Get drone types by category
*/
router.get('/category/:category', (req, res) => {
try {
const { category } = req.params;
const droneTypes = getDroneTypesByCategory(category);
res.json({
success: true,
data: droneTypes,
category: category,
total: droneTypes.length
});
} catch (error) {
console.error('Error fetching drone types by category:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch drone types by category',
details: error.message
});
}
});
/**
* GET /api/drone-types/threat/:threatLevel
* Get drone types by threat level
*/
router.get('/threat/:threatLevel', (req, res) => {
try {
const { threatLevel } = req.params;
const droneTypes = getDroneTypesByThreatLevel(threatLevel);
res.json({
success: true,
data: droneTypes,
threat_level: threatLevel,
total: droneTypes.length
});
} catch (error) {
console.error('Error fetching drone types by threat level:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch drone types by threat level',
details: error.message
});
}
});
/**
* GET /api/drone-types/categories
* Get all available categories
*/
router.get('/meta/categories', (req, res) => {
try {
const droneTypes = getAllDroneTypes();
const categories = [...new Set(Object.values(droneTypes).map(type => type.category))];
res.json({
success: true,
data: categories,
total: categories.length
});
} catch (error) {
console.error('Error fetching drone type categories:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch drone type categories',
details: error.message
});
}
});
/**
* GET /api/drone-types/threat-levels
* Get all available threat levels
*/
router.get('/meta/threat-levels', (req, res) => {
try {
const droneTypes = getAllDroneTypes();
const threatLevels = [...new Set(Object.values(droneTypes).map(type => type.threat_level))];
res.json({
success: true,
data: threatLevels,
total: threatLevels.length
});
} catch (error) {
console.error('Error fetching drone type threat levels:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch drone type threat levels',
details: error.message
});
}
});
module.exports = router;

View File

@@ -10,6 +10,7 @@ const healthRoutes = require('./health');
const debugRoutes = require('./debug');
const detectorsRoutes = require('./detectors');
const detectionsRoutes = require('./detections');
const droneTypesRoutes = require('./droneTypes');
// API versioning
router.use('/v1/devices', deviceRoutes);
@@ -19,6 +20,7 @@ router.use('/v1/dashboard', dashboardRoutes);
router.use('/v1/health', healthRoutes);
router.use('/v1/detectors', detectorsRoutes);
router.use('/v1/detections', detectionsRoutes);
router.use('/v1/drone-types', droneTypesRoutes);
// Default routes (no version prefix for backward compatibility)
router.use('/devices', deviceRoutes);
@@ -29,6 +31,7 @@ router.use('/health', healthRoutes);
router.use('/debug', debugRoutes);
router.use('/detectors', detectorsRoutes);
router.use('/detections', detectionsRoutes);
router.use('/drone-types', droneTypesRoutes);
// API documentation endpoint
router.get('/', (req, res) => {