import requests def get_device_route(server_url, token, device_id, from_time, to_time): """ Fetch all positions for a device in a time frame from Traccar server. """ url = f"{server_url}/reports/route" headers = { "Authorization": f"Bearer {token}", "Accept": "application/json" } params = { "deviceId": device_id, "from": from_time, "to": to_time } response = requests.get(url, headers=headers, params=params) if response.status_code == 200: try: positions = response.json() print(f"Retrieved {len(positions)} positions.") return positions except Exception as e: print(f"Error parsing JSON: {e}") print(response.text) return [] else: print(f"Failed to fetch positions: {response.status_code} - {response.text}") return [] # Example usage: if __name__ == "__main__": # Use your actual Traccar API endpoint (not /reports/route) server_url = "https://gps.moto-adv.com/api" token = "SDBGAiEA4sNXvVhL8w_Jd-5oCiXAuS5La5yelCQemfNysZYItaMCIQDOkzaoWKHbNnbZJw9ruGlsvbp35d90x3EGOZLXW_Gls3sidSI6MSwiZSI6IjIwMjUtMDYtMDlUMjE6MDA6MDAuMDAwKzAwOjAwIn0" device_id = 1 # Replace with your device ID from_time = "2024-06-02T21:00:00Z" to_time = "2025-06-03T20:59:00Z" positions = get_device_route(server_url, token, device_id, from_time, to_time) for pos in positions: print(f"{pos['deviceTime']}: {pos['latitude']}, {pos['longitude']}")