Traccar django

muhamedoufi2 years ago

I have developed a Django application based on the Traccar server database, and now I want to listen for changes in the positions table. The Traccar server automatically populates the positions table with coordinates sent by the device. Although there is an API socket offered by Traccar that can fetch positions, I am looking for something better than the API because it only gives the last position sent by devices and does not distinguish which device sent the position. Do you have any ideas about this?

I have too a problem with the api because I cannot fetch from api without connecting on traccar web interface

Anton Tananaev2 years ago

What do you mean by "does not distinguish which device sent the position"? Each position has the device id, so you know what device sent it.

muhamedoufi2 years ago

Ok I mean that I don't have an api can fetch last position for specific device
Example 127.0.0.1:8082/api/socket/{device_id}

Anton Tananaev2 years ago

You can always filter the WebSocket data by the id. There's no other alternatives currently.

muhamedoufi2 years ago

That mean this api exist ?

127.0.0.1:8082/api/socket/{device_id}
Anton Tananaev2 years ago

No, it doesn't. I'm talking about filtering on your end.

muhamedoufi2 years ago

ok thank you.

muhamedoufi2 years ago

I mentioned earlier that I have a problem with the socket api because the api cannot fetch data without i connect on traccar web interface

Is there a way to solve this؟

Anton Tananaev2 years ago

If you can't, you're doing something wrong. It's an API, so you should be able to use it directly.

muhamedoufi2 years ago

track_devices:590

   WebSocket connection to 'ws://127.0.0.1:8082/api/socket' failed: 

there is my implementation

let socket = new WebSocket("ws://127.0.0.1:8082/api/socket");
        var map;
         map = L.map('map',{editable: true}).setView([18.0928, -15.9611], 13);
        var gmaps=L.tileLayer('http://{s}.google.com/vt/lyrs=m&x={x}&y={y}&z={z}', {
                maxZoom: 20,
                subdomains: ['mt0', 'mt1', 'mt2', 'mt3']
                });
            gmaps.addTo(map);


        // console.log(socket.url)
let feature = null;

socket.onmessage = function(msg) {
    console.log(msg.data);
    let data = JSON.parse(msg.data);
    if (data.hasOwnProperty('positions')) {
        let position = data.positions[0];

        if (feature === null) {
            feature = L.marker([position.longitude, position.latitude]);
            feature.addTo(map);


        } else {
 
            var newLatLng = new L.LatLng(position.longitude, position.latitude);
            feature.setLatLng(newLatLng); 
        }

   

       
    }


};


muhamedoufi2 years ago

The exact problem that I mentioned is that I am unable to fetch from the socket API unless I am also connected to the Traccar interface at the same time. If I connect, the error disappears.

muhamedoufi2 years ago

How to configure Traccar to send events to my Django application using Traccar webhooks or any other suitable option?