Insert to another table when received new positions

brenthmiras8 years ago

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?

Anton Tananaev8 years ago

You can add extra query to the config file.

brenthmiras8 years ago

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?

Anton Tananaev8 years ago

First option.

brenthmiras8 years ago

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?

Anton Tananaev8 years ago

It can happen if the position is not the "latest" by time stamp.

brenthmiras8 years ago

How can I be sure that the queries were executed? Can I find logs of queries that traccar executes?

Anton Tananaev8 years ago

It would only log something if there is an error.

brenthmiras8 years ago

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?

Anton Tananaev8 years ago

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.

brenthmiras8 years ago

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.

Anton Tananaev8 years ago

Not sure why you are checking server time. Check fix time.

brenthmiras8 years ago
<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 case
SET 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.

brenthmiras8 years ago

The fixtime is 2036-10-10 16:30:39.

Anton Tananaev8 years ago

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.