Fix jwt-token
This commit is contained in:
@@ -10,6 +10,8 @@ export const SocketProvider = ({ children }) => {
|
||||
const [connected, setConnected] = useState(false);
|
||||
const [recentDetections, setRecentDetections] = useState([]);
|
||||
const [deviceStatus, setDeviceStatus] = useState({});
|
||||
const [movementAlerts, setMovementAlerts] = useState([]);
|
||||
const [droneTracking, setDroneTracking] = useState(new Map());
|
||||
const { isAuthenticated } = useAuth();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -49,9 +51,25 @@ export const SocketProvider = ({ children }) => {
|
||||
|
||||
setRecentDetections(prev => [detection, ...prev.slice(0, 49)]); // Keep last 50
|
||||
|
||||
// Update drone tracking
|
||||
if (detection.movement_analysis) {
|
||||
const trackingKey = `${detection.drone_id}_${detection.device_id}`;
|
||||
setDroneTracking(prev => {
|
||||
const newTracking = new Map(prev);
|
||||
newTracking.set(trackingKey, {
|
||||
droneId: detection.drone_id,
|
||||
deviceId: detection.device_id,
|
||||
lastDetection: detection,
|
||||
analysis: detection.movement_analysis,
|
||||
timestamp: Date.now()
|
||||
});
|
||||
return newTracking;
|
||||
});
|
||||
}
|
||||
|
||||
// Show toast notification
|
||||
toast.error(
|
||||
`Drone detected by ${detection.device.name || `Device ${detection.device_id}`}`,
|
||||
`Drone ${detection.drone_id} detected by ${detection.device?.name || `Device ${detection.device_id}`}`,
|
||||
{
|
||||
duration: 5000,
|
||||
icon: '🚨',
|
||||
@@ -59,6 +77,32 @@ export const SocketProvider = ({ children }) => {
|
||||
);
|
||||
});
|
||||
|
||||
// Listen for drone movement alerts
|
||||
newSocket.on('drone_movement_alert', (alertData) => {
|
||||
console.log('Drone movement alert:', alertData);
|
||||
|
||||
setMovementAlerts(prev => [alertData, ...prev.slice(0, 19)]); // Keep last 20 alerts
|
||||
|
||||
// Show priority-based notifications
|
||||
const alertIcon = alertData.analysis.alertLevel >= 3 ? '🚨' :
|
||||
alertData.analysis.alertLevel >= 2 ? '⚠️' : '📍';
|
||||
|
||||
const alertColor = alertData.analysis.alertLevel >= 3 ? 'error' :
|
||||
alertData.analysis.alertLevel >= 2 ? 'warning' : 'info';
|
||||
|
||||
toast[alertColor === 'error' ? 'error' : alertColor === 'warning' ? 'error' : 'info'](
|
||||
alertData.analysis.description,
|
||||
{
|
||||
duration: alertData.analysis.alertLevel >= 2 ? 10000 : 6000,
|
||||
icon: alertIcon,
|
||||
style: {
|
||||
background: alertData.analysis.alertLevel >= 3 ? '#fee2e2' :
|
||||
alertData.analysis.alertLevel >= 2 ? '#fef3c7' : '#e0f2fe'
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
// Listen for device heartbeats
|
||||
newSocket.on('device_heartbeat', (heartbeat) => {
|
||||
console.log('Device heartbeat:', heartbeat);
|
||||
@@ -109,14 +153,27 @@ export const SocketProvider = ({ children }) => {
|
||||
setRecentDetections([]);
|
||||
};
|
||||
|
||||
const clearMovementAlerts = () => {
|
||||
setMovementAlerts([]);
|
||||
};
|
||||
|
||||
const getDroneTracking = (droneId, deviceId) => {
|
||||
const trackingKey = `${droneId}_${deviceId}`;
|
||||
return droneTracking.get(trackingKey);
|
||||
};
|
||||
|
||||
const value = {
|
||||
socket,
|
||||
connected,
|
||||
recentDetections,
|
||||
deviceStatus,
|
||||
movementAlerts,
|
||||
droneTracking,
|
||||
joinDeviceRoom,
|
||||
leaveDeviceRoom,
|
||||
clearRecentDetections
|
||||
clearRecentDetections,
|
||||
clearMovementAlerts,
|
||||
getDroneTracking
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user