Simulated devices

Mohamed3 months ago

Greetings
I created a simulated device using osmand protocol, the server receives the data, I can find it in the logs, but I get bad response 400, I tried to change traccar.xml config file, still the same response

Anton Tananaev3 months ago

What do you see in the logs?

Mohamed3 months ago
2024-09-06 21:07:35  INFO: [T121c8eef] connected
2024-09-06 21:07:35  INFO: [T121c8eef: osmand < 127.0.0.1] POST / HTTP/1.1\r\nHost: localhost:5055\r\nUser-Agent: python-requests/2.31.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 188\r\nContent-Type: application/json\r\n\r\n{"deviceid": "107351610804199", "lat": 40.482513009619886, "lon": -73.86123261951467, "valid": "true", "timestamp": 1725646055406, "speed": 45, "altitude": 100, "heading": 343, "batt": 86}
2024-09-06 21:07:35  INFO: [T121c8eef: osmand > 127.0.0.1] HTTP/1.1 400 Bad Request\r\ncontent-length: 0\r\n\r\n
2024-09-06 21:07:35  INFO: [T121c8eef] disconnected
Anton Tananaev3 months ago

The format is invalid, so you get an error.

Mohamed3 months ago

Provide me with the right format, I tried the format in osmand document and didn't work right too, I tried different approaches

Anton Tananaev3 months ago
Mohamed3 months ago
import random
import time
import requests
import threading

# Function to generate a random IMEI number
def generate_imei():
    imei_base = random.randint(100000000000000, 999999999999999)  # Generate a 15-digit random number
    return str(imei_base)

# Function to simulate GPS data for a single vehicle
def simulate_gps_data(vehicle_id, imei, start_lat, start_lon):
    lat, lon = start_lat, start_lon
    while True:
        lat += random.uniform(-0.001, 0.001)  # Random movement
        lon += random.uniform(-0.001, 0.001)
        speed = random.randint(10, 100)  # Speed in km/hr
        timestamp = int(time.time() * 1000)  # Current timestamp in milliseconds
        
        # Data to be sent conforming to OsmAnd protocol
        data = {
            "deviceid": imei,  # Unique identifier for the device
            #"valid": "true",  # Indicates if the location is valid
            "lat": lat,  # Latitude of the position
            "lon": lon,  # Longitude of the position
            "timestamp": timestamp,  # Timestamp of the position in milliseconds
            "speed": speed,  # Speed of the device
            "altitude": 100,  # Altitude of the device (static example)
            "heading": random.randint(0, 359),  # Direction in degrees
            "batt": random.randint(0, 100),  # Battery level of the device
        }

        send_data_to_website(data)
        time.sleep(random.uniform(1, 3))  # Adjust the delay as needed

# Function to send data to the server
def send_data_to_website(data):
    url = "http://localhost:5055"  # Adjust the URL to point to your Traccar server
    try:
        # Use query parameters to match OsmAnd protocol
        response = requests.get(url, params=data)
        print(f"Sent data for vehicle {data['deviceid']}: {data}, Response: {response.status_code}")
    except requests.exceptions.RequestException as e:
        print(f"Error sending data for vehicle {data['deviceid']}: {e}")
        print(f"Details: {e.response.text if e.response else 'No response received'}")

# Function to start simulation for multiple vehicles
def start_simulation(vehicle_count):
    threads = []
    for i in range(vehicle_count):
        vehicle_id = f"vehicle_{i+1}"
        imei = generate_imei()
        start_lat = random.uniform(40.0, 41.0)
        start_lon = random.uniform(-74.0, -73.0)
        thread = threading.Thread(target=simulate_gps_data, args=(vehicle_id, imei, start_lat, start_lon))
        threads.append(thread)
        thread.start()
    
    for thread in threads:
        thread.join()

start_simulation(1)
Anton Tananaev3 months ago

Where are the logs with the correct format?