73 lines
2.5 KiB
Python
73 lines
2.5 KiB
Python
import requests
|
|
|
|
def get_positions(server_url, token, device_ids, 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.
|
|
"""
|
|
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
|
|
"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:
|
|
positions = response.json()
|
|
print(f"Retrieved {len(positions)} positions:")
|
|
for position in positions:
|
|
print(position)
|
|
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!")
|
|
else:
|
|
print("No position data found or an error occurred.") |