Traccar alarm event: positionid always NULL in events table

Abou5 years ago

I'm using traccar 4.6 version. I need the positionid of the event to determine where the device started send alarme or moving.... But the positionid is always NULL. Why is that?

Anton Tananaev5 years ago

What events are you looking at?

Abou5 years ago

Event alarme SOS button, i need the positionid of the event

Anton Tananaev5 years ago

Alarms should have a position id. If they don't, there must be some issue with saving positions.

Abou5 years ago

They don't have a position id, the positionid column in events table allways null.
This is my entry in traccar.xml, to insert a new position :

 <entry key='database.insertPosition'>
        START TRANSACTION;

        INSERT INTO tc_positions (protocol, deviceid, servertime, devicetime, fixtime, valid, latitude, longitude, altitude, speed, course, address, attributes, accuracy, network)
        VALUES (:protocol, :deviceid, :servertime, :devicetime, :fixtime, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :attributes, :accuracy, :network);

        SELECT @accountID := accountID, @objectID := isAttached FROM tc_devices WHERE id = :deviceid;

        SELECT @distance:= json_extract(attributes, '$.distance') FROM tc_positions;
        SELECT @totalDistance:= json_extract(attributes, '$.totalDistance') FROM tc_positions;

        INSERT INTO eventdata (accountID, objectID, deviceID, timestamp, statusCode, latitude, longitude, speedKPH, heading, altitude, address, rawData, distanceKM, odometerKM, creationTime)

        VALUES (@accountID, @objectID, :deviceid, UNIX_TIMESTAMP(:devicetime), 0, :latitude, :longitude, :speed*1.852, :course, :altitude, :address, :attributes, @totalDistance, @distance, UNIX_TIMESTAMP(NOW()));

        UPDATE object SET lastDataID = LAST_INSERT_ID(), lastValidLatitude = :latitude, lastValidLongitude = :longitude, lastValidspeedKPH = :speed*1.852, lastValidHeading = :course, lastGPSTimestamp = UNIX_TIMESTAMP(:devicetime), lastAddress = :address, lastUpdateTime = UNIX_TIMESTAMP(NOW()) WHERE objectID = @objectID;
        COMMIT;
    </entry>
Anton Tananaev5 years ago

The issue is that your query doesn't return generated position id, so that's the root cause.

Abou5 years ago

What does generated position id mean and how to generate it in my query?

Anton Tananaev5 years ago

Have you heard about AUTO INCREMENT fields?

Abou5 years ago

Yes of cours. So it means i have to insert the position id in my query as bellow:

INSERT INTO tc_positions (id, protocol, deviceid, servertime, devicetime, fixtime, valid, latitude, longitude, altitude, speed, course, address, attributes, accuracy, network)
VALUES (:id, :protocol, :deviceid, :servertime, :devicetime, :fixtime, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :attributes, :accuracy, :network);
Anton Tananaev5 years ago

No.

Abou5 years ago

so how to do it. I couldn't do it. Help me please in my query to return generated position id

Abou5 years ago

Anton Tananaev, this query is correct to return genereted position id?
I added SELECT LAST_INSERT_ID()

   <entry key='database.insertPosition'>
        START TRANSACTION;

        INSERT INTO tc_positions (protocol, deviceid, servertime, devicetime, fixtime, valid, latitude, longitude, altitude, speed, course, address, attributes, accuracy, network)
        VALUES (:protocol, :deviceid, :servertime, :devicetime, :fixtime, :valid, :latitude, :longitude, :altitude, :speed, :course, :address, :attributes, :accuracy, :network);

        SELECT LAST_INSERT_ID();
        SELECT @accountID := accountID, @objectID := isAttached FROM tc_devices WHERE id = :deviceid;

        SELECT @distance:= json_extract(attributes, '$.distance') FROM tc_positions;
        SELECT @totalDistance:= json_extract(attributes, '$.totalDistance') FROM tc_positions;

        INSERT INTO eventdata (accountID, objectID, deviceID, timestamp, statusCode, latitude, longitude, speedKPH, heading, altitude, address, rawData, distanceKM, odometerKM, creationTime)

        VALUES (@accountID, @objectID, :deviceid, UNIX_TIMESTAMP(:devicetime), 0, :latitude, :longitude, :speed*1.852, :course, :altitude, :address, :attributes, @totalDistance, @distance, UNIX_TIMESTAMP(NOW()));

        UPDATE object SET lastDataID = LAST_INSERT_ID(), lastValidLatitude = :latitude, lastValidLongitude = :longitude, lastValidspeedKPH = :speed*1.852, lastValidHeading = :course, lastGPSTimestamp = UNIX_TIMESTAMP(:devicetime), lastAddress = :address, lastUpdateTime = UNIX_TIMESTAMP(NOW()) WHERE objectID = @objectID;
        COMMIT;

    </entry>
Anton Tananaev5 years ago

You should probably add it as a last statement.

Abou5 years ago

if i add it as last statement, it will select the latest insert evendata id. I need the generated position id

Anton Tananaev5 years ago

Actually I'm not 100% sure simple select is going to work.