You can add extra query to the config file.
How do I add an extra query to the config file?
Do I add it below the insertPosition query like this?
<entry key='database.insertPosition'>
INSERT INTO positions (deviceId, protocol, serverTime, deviceTime, fixTime, valid, latitude, longitude, altitude, speed, course, address, attributes)
VALUES (:deviceId, :protocol, :now, :deviceTime, :fixTime, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :attributes);
[My insert query here]
</entry>
Or should I create an entry for my database.[query name] like this?
<entry key='database.[my query name]'>
INSERT INTO something...
</entry>
If the latter is true, then how will this query be called when a new position is inserted?
First option.
It seems to be not working.
By the way, updatelatestposition was what I really needed so here is my code:
<entry key='database.updateLatestPosition'>
UPDATE devices SET positionId = :id WHERE id = :deviceId;
UPDATE containers SET positionId = :id WHERE deviceId = :deviceId;
</entry>
Notice how I added the additional query below the original one as how I understood it.
I turned on my device, saw some position logs on tracker.log,
And then I checked the second query by inspecting the associated container and the positionId was not set.
So I checked also the first query by inspecting the positionId of the device. It was also not updated.
The database.insertPosition (which was untouched) seems to have worked because I can see new positions in position table.
So what could have I missed?
It can happen if the position is not the "latest" by time stamp.
How can I be sure that the queries were executed? Can I find logs of queries that traccar executes?
It would only log something if there is an error.
The device I was using was not added using the api.
But I wonder why new positions are being stored
but the current position (devices.positionId) is not updated?
I would recommend to restart the service, if you haven't done it yet. Then if it doesn't help, check time on current position vs time on the latest one.
I restarted the service using:sudo /opt/traccar/bin/traccar restart
then checked the latest position of device with id=25
The latest position id is 13594.
The servertime is "2017-03-24 16:26:25" which is 3 days behind.
Then I checked the positionid of device where id=25
The positionid was still left behind 12928.
But the time is lastupdate="2017-03-27 11:45:46" which is the current time on our country.
The lastupdate column seems to be updated but the positionid is not.
Not sure why you are checking server time. Check fix time.
<entry key='database.updateLatestPosition'>
UPDATE devices SET positionId = :id WHERE id = :deviceId;
UPDATE containers SET positionId = :id WHERE deviceId = :deviceId;
</entry>
I notice that in the first query, positionId is camel caseSET positionId
But in the database it is lowercase: positionid
This is the default config generated by traccar.
But if this is wrong, then devices.positionid would be null.
But I got some devices.positionid set.
The fixtime is 2036-10-10 16:30:39.
So, everything is correct. That's the latest position by time.
Your problem is not with executing the query. Your problem is that device reporting invalid data. You need to find where it happened and check if it's a device or a server problem.
Is it possible to configure traccar to insert some data to another custom table aside from positions table when it receives position from device? If yes how can we accomplish this?