Fix jwt-token
This commit is contained in:
@@ -98,15 +98,56 @@ async function handleHeartbeat(req, res) {
|
||||
deviceId = keyMatch ? parseInt(keyMatch[1]) : key.hashCode();
|
||||
}
|
||||
|
||||
// Ensure device exists or create it
|
||||
const [device] = await Device.findOrCreate({
|
||||
where: { id: deviceId },
|
||||
defaults: {
|
||||
// Check if device exists and is approved
|
||||
let device = await Device.findOne({ where: { id: deviceId } });
|
||||
|
||||
if (!device) {
|
||||
// Create new device as unapproved
|
||||
device = await Device.create({
|
||||
id: deviceId,
|
||||
name: `Device ${deviceId}`,
|
||||
last_heartbeat: new Date()
|
||||
}
|
||||
});
|
||||
last_heartbeat: new Date(),
|
||||
is_approved: false
|
||||
});
|
||||
|
||||
// Emit notification for new device requiring approval
|
||||
req.io.emit('new_device_pending', {
|
||||
device_id: deviceId,
|
||||
device_key: key,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: `New device ${deviceId} (${key}) requires approval`
|
||||
});
|
||||
|
||||
console.log(`⚠️ New unapproved device ${deviceId} created, awaiting approval`);
|
||||
|
||||
return res.status(202).json({
|
||||
success: false,
|
||||
error: 'Device not approved',
|
||||
message: 'Device has been registered but requires approval before it can send data',
|
||||
device_id: deviceId,
|
||||
approval_required: true
|
||||
});
|
||||
}
|
||||
|
||||
if (!device.is_approved) {
|
||||
console.log(`🚫 Heartbeat rejected from unapproved device ${deviceId}`);
|
||||
|
||||
// Emit reminder notification
|
||||
req.io.emit('device_approval_reminder', {
|
||||
device_id: deviceId,
|
||||
device_key: key,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: `Device ${deviceId} (${key}) still awaiting approval`
|
||||
});
|
||||
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
error: 'Device not approved',
|
||||
message: 'Device requires approval before it can send data',
|
||||
device_id: deviceId,
|
||||
approval_required: true
|
||||
});
|
||||
}
|
||||
|
||||
// Update device's last heartbeat
|
||||
await device.update({ last_heartbeat: new Date() });
|
||||
@@ -143,16 +184,56 @@ async function handleDetection(req, res) {
|
||||
|
||||
console.log(`🚁 Drone detection received from device ${detectionData.device_id}: drone_id=${detectionData.drone_id}, type=${detectionData.drone_type}, rssi=${detectionData.rssi}`);
|
||||
|
||||
// Ensure device exists or create it (from original detection route)
|
||||
const [device] = await Device.findOrCreate({
|
||||
where: { id: detectionData.device_id },
|
||||
defaults: {
|
||||
// Check if device exists and is approved
|
||||
let device = await Device.findOne({ where: { id: detectionData.device_id } });
|
||||
|
||||
if (!device) {
|
||||
// Create new device as unapproved
|
||||
device = await Device.create({
|
||||
id: detectionData.device_id,
|
||||
name: `Device ${detectionData.device_id}`,
|
||||
geo_lat: detectionData.geo_lat || 0,
|
||||
geo_lon: detectionData.geo_lon || 0,
|
||||
last_heartbeat: new Date()
|
||||
}
|
||||
});
|
||||
last_heartbeat: new Date(),
|
||||
is_approved: false
|
||||
});
|
||||
|
||||
// Emit notification for new device requiring approval
|
||||
req.io.emit('new_device_pending', {
|
||||
device_id: detectionData.device_id,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: `New device ${detectionData.device_id} requires approval`
|
||||
});
|
||||
|
||||
console.log(`⚠️ New unapproved device ${detectionData.device_id} created, awaiting approval`);
|
||||
|
||||
return res.status(202).json({
|
||||
success: false,
|
||||
error: 'Device not approved',
|
||||
message: 'Device has been registered but requires approval before it can send data',
|
||||
device_id: detectionData.device_id,
|
||||
approval_required: true
|
||||
});
|
||||
}
|
||||
|
||||
if (!device.is_approved) {
|
||||
console.log(`🚫 Detection rejected from unapproved device ${detectionData.device_id}`);
|
||||
|
||||
// Emit reminder notification
|
||||
req.io.emit('device_approval_reminder', {
|
||||
device_id: detectionData.device_id,
|
||||
timestamp: new Date().toISOString(),
|
||||
message: `Device ${detectionData.device_id} still awaiting approval`
|
||||
});
|
||||
|
||||
return res.status(403).json({
|
||||
success: false,
|
||||
error: 'Device not approved',
|
||||
message: 'Device requires approval before it can send data',
|
||||
device_id: detectionData.device_id,
|
||||
approval_required: true
|
||||
});
|
||||
}
|
||||
|
||||
// Create detection record
|
||||
const detection = await DroneDetection.create({
|
||||
|
||||
Reference in New Issue
Block a user