import random import time from django.core.management.base import BaseCommand from core.models import Unit class Command(BaseCommand): help = 'Starts the data simulation for emergency units.' def handle(self, *args, **options): self.stdout.write(self.style.SUCCESS('Starting simulation...')) statuses = ['Available', 'Enroute', 'On Scene', 'Returning'] while True: try: units = Unit.objects.all() for unit in units: # Simulate location change unit.location_lat += random.uniform(-0.01, 0.01) unit.location_lon += random.uniform(-0.01, 0.01) # Simulate status change if random.random() < 0.1: # 10% chance to change status unit.status = random.choice(statuses) unit.save() self.stdout.write(self.style.SUCCESS(f'Updated {len(units)} units.')) time.sleep(5) except KeyboardInterrupt: self.stdout.write(self.style.WARNING('Simulation stopped by user.')) break except Exception as e: self.stderr.write(self.style.ERROR(f'An error occurred: {e}')) time.sleep(10) # Wait a bit before retrying