-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Map 100% width and height Added script add_measures to generate random measures python manage.py add_measures Zaragoza I need to add more cities
- Loading branch information
Showing
5 changed files
with
145 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import os | ||
import requests | ||
import random | ||
import time | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
# Define constants for movement simulation | ||
METERS_IN_LATITUDE = 1 / 111111 # Approximate conversion: 1 meter = 1 / 111111 degree latitude | ||
METERS_IN_LONGITUDE = 1 / (111111 * 0.89) # Adjust for longitude at Zaragoza's latitude | ||
|
||
class Command(BaseCommand): | ||
help = 'Add measurement values via the API in a random walk' | ||
|
||
def add_arguments(self, parser): | ||
# Add argument for location name (optional) or latitude/longitude directly | ||
parser.add_argument('location', type=str, nargs='?', help='Location name, e.g., Zaragoza') | ||
parser.add_argument('--latitude', type=float, help='Starting latitude') | ||
parser.add_argument('--longitude', type=float, help='Starting longitude') | ||
|
||
def handle(self, *args, **options): | ||
# URL of your API endpoint | ||
api_url = 'http://localhost:8000/api/measurements/' | ||
|
||
# Handle location argument or latitude/longitude directly | ||
location = options.get('location', None) | ||
latitude = options.get('latitude', None) | ||
longitude = options.get('longitude', None) | ||
|
||
# Hardcoded locations (Zaragoza example) | ||
location_coordinates = { | ||
'Zaragoza': (41.6488, -0.8891), # Latitude, Longitude of Zaragoza | ||
# Add more locations if needed | ||
} | ||
|
||
# Determine starting coordinates based on location or provided lat/long | ||
if location and location in location_coordinates: | ||
current_latitude, current_longitude = location_coordinates[location] | ||
elif latitude and longitude: | ||
current_latitude, current_longitude = latitude, longitude | ||
else: | ||
self.stdout.write(self.style.ERROR('You must provide a valid location or latitude and longitude.')) | ||
return | ||
|
||
# Number of iterations (optional): Stop after 10 measurements | ||
num_iterations = 1000 | ||
|
||
for _ in range(num_iterations): | ||
# Simulate a random walk (change latitude and longitude slightly) | ||
current_latitude += random.uniform(-1, 1) * METERS_IN_LATITUDE * 50 # Adjust for 50 meters | ||
current_longitude += random.uniform(-1, 1) * METERS_IN_LONGITUDE * 50 # Adjust for 50 meters | ||
|
||
|
||
# Example data to be sent | ||
data = { | ||
"device": 1, # Device ID | ||
"user": 1, # User ID | ||
"latitude": current_latitude, | ||
"longitude": current_longitude, | ||
"altitude": 200, | ||
"values": {"radiation": random.uniform(50, 100)}, # Random radiation value | ||
"dateTime": timezone.now().isoformat(), # Current time in ISO format | ||
"accuracy": 1.0, | ||
"unit": "Sieverts", | ||
"notes": "Random walk measurement added via API", | ||
"weather": {"temperature": 22, "humidity": 50} | ||
} | ||
|
||
# Optional: Authentication if needed (Bearer token example) | ||
headers = { | ||
'X-CSRFToken': 'rVFqCl8tria1mSouQnZiRlxx8URnw5ePfq1f2wFuLJEPbKwDY9O7gYEUZqVFqkH2', | ||
'Content-Type': 'application/json', | ||
} | ||
|
||
# Make the POST request to the API | ||
response = requests.post(api_url, json=data, headers=headers) | ||
|
||
if response.status_code == 201: | ||
self.stdout.write(self.style.SUCCESS(f'Successfully added measurement at ({current_latitude}, {current_longitude})')) | ||
else: | ||
self.stdout.write(self.style.ERROR(f'Failed to add measurement. Status code: {response.status_code}')) | ||
self.stdout.write(self.style.ERROR(f'Response: {response.text}')) | ||
|
||
# Wait for 10 seconds before adding the next measurement | ||
time.sleep(10) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,7 @@ | |
# Other apps | ||
'rest_framework', | ||
'drf_yasg', | ||
'django_bootstrap5', | ||
|
||
# Custom apps | ||
'devices', | ||
|