updated trips
This commit is contained in:
@@ -1,73 +1,41 @@
|
||||
import requests
|
||||
|
||||
def get_positions(server_url, token, device_ids, from_time, to_time):
|
||||
def get_device_route(server_url, token, device_id, from_time, to_time):
|
||||
"""
|
||||
Fetch position information from the Traccar server.
|
||||
|
||||
Args:
|
||||
server_url (str): The URL of the Traccar server.
|
||||
token (str): The authentication token.
|
||||
device_ids (list of int): List of device IDs.
|
||||
from_time (str): The start time in ISO 8601 format.
|
||||
to_time (str): The end time in ISO 8601 format.
|
||||
|
||||
Returns:
|
||||
list: The position information.
|
||||
Fetch all positions for a device in a time frame from Traccar server.
|
||||
"""
|
||||
if not device_ids or not from_time or not to_time:
|
||||
print("deviceId, from, and to are required parameters.")
|
||||
return None
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
url = f"{server_url}/positions"
|
||||
payload = {
|
||||
"deviceId": device_ids, # Send as list
|
||||
url = f"{server_url}/reports/route"
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Accept": "application/json"
|
||||
}
|
||||
params = {
|
||||
"deviceId": device_id,
|
||||
"from": from_time,
|
||||
"to": to_time
|
||||
}
|
||||
|
||||
try:
|
||||
print(f"Request Payload: {payload}")
|
||||
response = requests.get(url, params=payload, headers=headers)
|
||||
print(f"Response Status Code: {response.status_code}")
|
||||
print(f"Response Content: {response.text}")
|
||||
|
||||
if response.status_code == 200:
|
||||
response = requests.get(url, headers=headers, params=params)
|
||||
if response.status_code == 200:
|
||||
try:
|
||||
positions = response.json()
|
||||
print(f"Retrieved {len(positions)} positions:")
|
||||
for position in positions:
|
||||
print(position)
|
||||
print(f"Retrieved {len(positions)} positions.")
|
||||
return positions
|
||||
elif response.status_code == 400:
|
||||
print("Bad Request: Please check the request payload and token.")
|
||||
return None
|
||||
else:
|
||||
print(f"Failed to fetch positions: {response.status_code} - {response.reason}")
|
||||
return None
|
||||
except requests.exceptions.RequestException as e:
|
||||
print(f"Error fetching positions: {str(e)}")
|
||||
return None
|
||||
|
||||
|
||||
# Test the function
|
||||
if __name__ == "__main__":
|
||||
# Manually enter the server URL and token
|
||||
server_url = "https://gps.moto-adv.com/api"
|
||||
token = "RjBEAiB69jflCQJ9HQwGyFdAHF8J5JPMwIIclglbWiKMWeX6PwIgdBWc-YllE_-RwoQi7zX25WxQJqQX_OTIKOHk9a3mmBN7InUiOjEsImUiOiIyMDI1LTA2LTA4VDIxOjAwOjAwLjAwMCswMDowMCJ9"
|
||||
|
||||
device_id = input("Enter the device ID (required): ").strip()
|
||||
if not device_id:
|
||||
print("Device ID is required.")
|
||||
exit(1)
|
||||
device_ids = [int(device_id)]
|
||||
|
||||
# Set fixed time interval
|
||||
from_time = "2024-06-01T12:00:00Z"
|
||||
to_time = "2024-06-01T23:00:00Z"
|
||||
print(f"Using time interval: {from_time} to {to_time}")
|
||||
|
||||
positions = get_positions(server_url, token, device_ids, from_time, to_time)
|
||||
if positions:
|
||||
print("Position data retrieved successfully!")
|
||||
except Exception as e:
|
||||
print(f"Error parsing JSON: {e}")
|
||||
print(response.text)
|
||||
return []
|
||||
else:
|
||||
print("No position data found or an error occurred.")
|
||||
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']}")
|
||||
Reference in New Issue
Block a user