diff --git a/test_drone_data.py b/test_drone_data.py index 2f0f2c7..73ceb55 100644 --- a/test_drone_data.py +++ b/test_drone_data.py @@ -257,6 +257,53 @@ class DroneSimulator: return detection +def send_heartbeat(device_id): + """Send heartbeat to keep device online""" + global DEBUG_MODE + url = f"{API_BASE_URL}/heartbeat" + headers = { + "Content-Type": "application/json" + } + + heartbeat_data = { + "type": "heartbeat", + "key": f"device_{device_id}", + "device_id": device_id, + "signal_strength": random.randint(-70, -30), # Simulate signal strength + "battery_level": random.randint(70, 100), # Simulate battery level + "temperature": random.randint(20, 45), # Simulate temperature + "uptime": random.randint(3600, 86400), # Simulate uptime + "memory_usage": random.randint(30, 80), # Simulate memory usage + "firmware_version": "1.0.0" + } + + # Debug output for heartbeat + if DEBUG_MODE: + print("\n" + "-"*40) + print("💓 DEBUG: Sending heartbeat to backend") + print("-"*40) + print(f"URL: {url}") + print("Heartbeat payload:") + print(json.dumps(heartbeat_data, indent=2, sort_keys=True)) + print("-"*40 + "\n") + + try: + response = requests.post(url, json=heartbeat_data, headers=headers, timeout=10) + + if response.status_code == 201: + if DEBUG_MODE: + print(f"💓 Device {device_id}: Heartbeat sent successfully") + return True + else: + if DEBUG_MODE: + print(f"❌ Heartbeat failed: {response.status_code} - {response.text}") + return False + + except requests.exceptions.RequestException as e: + if DEBUG_MODE: + print(f"❌ Heartbeat network error: {e}") + return False + def send_detection(detection_data): """Send detection data to the API""" global DEBUG_MODE @@ -375,18 +422,32 @@ def simulate_orlan_detection_test(): print("\n" + "=" * 70) print("STARTING ORLAN APPROACH SIMULATION") + print("💓 Sending initial heartbeat to keep device online...") print("=" * 70) + # Send initial heartbeat to target device + send_heartbeat(target_device['id']) + # Simulation parameters total_steps = 50 final_distance = 0.0 # Directly overhead drone_id = 3000 # Unique ID for Orlan test + last_heartbeat = time.time() + heartbeat_interval = 60 # Send heartbeat every 60 seconds detection_started = False critical_alerts_started = False try: for step in range(1, total_steps + 1): + current_time = time.time() + + # Send periodic heartbeat to keep device online + if current_time - last_heartbeat >= heartbeat_interval: + print("💓 Sending heartbeat to maintain device status...") + send_heartbeat(target_device['id']) + last_heartbeat = current_time + # Calculate current distance (exponential approach) progress = step / total_steps distance_km = start_distance * (1 - progress) ** 1.8 + final_distance * progress ** 1.8 @@ -476,11 +537,19 @@ def simulate_realistic_scenario(duration_minutes=10): """Simulate realistic drone scenario with persistent tracking""" print(f"🚁 Starting realistic drone simulation for {duration_minutes} minutes...") print("📊 Scenario: Multiple drones with persistent tracking, RSSI changes, movement patterns") + print("💓 Heartbeat system: Keeping devices online during test") print("=" * 80) simulator = DroneSimulator() start_time = time.time() end_time = start_time + (duration_minutes * 60) + last_heartbeat = start_time + heartbeat_interval = 60 # Send heartbeat every 60 seconds (well under 300s limit) + + # Send initial heartbeats to all devices + print("💓 Sending initial heartbeats to all devices...") + for device in DEVICES: + send_heartbeat(device['id']) # Force create initial drone for testing if len(DEVICES) > 0: @@ -491,6 +560,7 @@ def simulate_realistic_scenario(duration_minutes=10): detection_count = 0 last_update = start_time + heartbeat_count = 0 try: while time.time() < end_time: @@ -498,6 +568,14 @@ def simulate_realistic_scenario(duration_minutes=10): time_delta = current_time - last_update last_update = current_time + # Send periodic heartbeats to keep devices online + if current_time - last_heartbeat >= heartbeat_interval: + print(f"💓 Sending heartbeats to {len(DEVICES)} devices...") + for device in DEVICES: + if send_heartbeat(device['id']): + heartbeat_count += 1 + last_heartbeat = current_time + # Randomly spawn new drones (1-15% chance per cycle) if random.random() < 0.15 and len(simulator.active_drones) < 8: device = random.choice(DEVICES) @@ -531,7 +609,6 @@ def simulate_realistic_scenario(duration_minutes=10): if should_detect: detection = simulator.generate_detection(drone, device, distance) - detection["_distance"] = f"{distance:.1f}" # For logging only if send_detection(detection): detection_count += 1 @@ -547,7 +624,7 @@ def simulate_realistic_scenario(duration_minutes=10): if int(elapsed_minutes) % 2 == 0 and elapsed_minutes > 0: active_count = len(simulator.active_drones) if active_count > 0: - print(f"📍 Status: {active_count} active drones, {detection_count} total detections") + print(f"📍 Status: {active_count} active drones, {detection_count} total detections, {heartbeat_count} heartbeats sent") time.sleep(3) # 3 second cycle time