Traccar GPS and Python - Unable to retrieve device position latitude and longitude

764 Views Asked by At

I am using the following code to access data for a specific device on Traccar GPS using Taccar API.

import pytraccar.api as api

test_url = 'http://localhost:8082'
username, password = 'admin', 'admin'
url_positions = 'http://localhost:8082/api/positions'

user = api.TraccarAPI(base_url=test_url)
result = user.login_with_credentials(username, password)
device = user.get_devices(query='uniqueId', params=['12345'])

assert type(result) == dict
assert type(device) == list

print(device[0]["positionId"])

By using this code I am able to print the last device (12345) position. The result is an integer.

How can I get the latitude and longitude for this position?

EDIT: I am using this traccar api: https://github.com/ludeeus/pytraccar

PM

1

There are 1 best solutions below

0
On

I solved my problem using a sample code provided by the developer.

The sample code:

"""Example usage of pytraccar."""
import asyncio
import aiohttp
from pytraccar.api import API

HOST = "192.168.2.105"
PORT = 8072
USERNAME = "admin"
PASSWORD = "admin"


async def test():
    """Example usage of pytraccar."""
    async with aiohttp.ClientSession() as session:
        data = API(LOOP, session, USERNAME, PASSWORD, HOST, PORT)
        await data.get_device_info()
        await data.get_events([2])

        print()
        print("device_info:", data.device_info)
        print()
        print("positions:", data.positions)
        print()
        print("devices:", data.devices)
        print()
        print("geofences:", data.geofences)
        print()
        print("events:", data.events)


LOOP = asyncio.get_event_loop()
LOOP.run_until_complete(test())

This returns several dictionaries and one of them is "data.positions". Using this specific dictionary I am able to manage it and get the results I need.