What is "socket.event.devices"?
Found this in app.min.js, and I am using the same code in my client side script.
protocol='https:'===window.location.protocol?'wss:': 'ws:';
pathname=window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')+1);
protocol=new WebSocket(protocol+'//'+window.location.host+pathname+'api/socket');
protocol.onclose=function(event) {
Traccar.app.showToast(Strings.errorSocket, Strings.errorTitle);
Ext.Ajax.request( {
url:'api/devices', success:function(response) {
self.updateDevices(Ext.decode(response.responseText))
}
, failure:function(response) {
401===response.status&&window.location.reload()
}
}
);
Ext.Ajax.request( {
url:'api/positions', headers: {
Accept: 'application/json'
}
, success:function(response) {
self.updatePositions(Ext.decode(response.responseText))
}
}
);
setTimeout(function() {
self.asyncUpdate(!1)
}
, Traccar.Style.reconnectTimeout)
}
;
protocol.onmessage=function(event) {
event=Ext.decode(event.data);
event.devices&& self.updateDevices(event.devices);
event.positions&&(self.updatePositions(event.positions, first), first=!1);
event.events&&self.updateEvents(event.events)
}
}
event.devices only has data about online devices, i never got any data about offline devices.
You only receive device status updates through the WebSocket. To get initial state you need to use regular REST API.
Got it.
Can you tell, how do you guys decide whether a device is online or offline except TCP ConnectionManager ? So that I can use the same logic on my server side code, rather than getting all the devices list.
If you can add just a deviceid
to the /devices api rather than userid
will be much helpful.
Thanks again man for building Traccar and keeping it open source.
Everything is in ConnectionManager, as far as I remember.
You can already use "id" (which is "deviceid") in "/devices" API. I'm not sure why you are requesting something that's already available.
I don't find the deviceid parameter in the api reference.
I will try it for sure right now.
API documentation is a bit outdated, but you can already use "id" parameter in the latest version of Traccar.
Tested it.
Thanks Anton for the update. It will help me a lot.
Hey Anton,
I just need to know one thing, why is the 1st entry in the positions table has totalDistance attribute as 9582537, where as it should be 0, as its the first record in the positions table.
Thanks.!!
Does it have "distance" value?
Yup, the distance value is also the same.
It means that it's not the first position.
Can you help me in one thing?
When I tried to calculate the totalDistance traveled from one position(let it be x) to another position(y)=>
y.attributes.totalDistance-x.attributes.totalDistance, I get 120 Kms, where as the vehicle must have traveled max up to 9 or 10 kms.
Whats the best way to do it ?
Thanks man...
Distance attribute is in meters.
Hey Anton, thanks for building such a useful tool.
The server works flawlessly. But one thing I found with the socket api, when I access the socket.event.devices, I don't get any offline devices in result. I only get the online devices. Is that a bug, or I am doing something wrong ?