What events are you looking at?
Event alarme SOS button, i need the positionid of the event
Alarms should have a position id. If they don't, there must be some issue with saving positions.
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>
The issue is that your query doesn't return generated position id, so that's the root cause.
What does generated position id mean and how to generate it in my query?
Have you heard about AUTO INCREMENT fields?
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);
No.
so how to do it. I couldn't do it. Help me please in my query to return generated position id
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>
You should probably add it as a last statement.
if i add it as last statement, it will select the latest insert evendata id. I need the generated position id
Actually I'm not 100% sure simple select is going to work.
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?