Modify database query or merge last few packets attributes

czerwony032 years ago

Hello
Is it still possible to modify database queries? I found some old topics from around 3.0 version talking about this, but couldn't make it works nowadays.

My problem is that Wetrack Lite device (GT06 protocol) sends two distinct packets, one with ignition (heart beat) and second with motorcycle battery voltage (adc1).
Based on that which packet arrives first, I can see on web gui information about ignition (on devices list) or battery voltage on device details list.
Here are my database records:

387,gt06,1,2023-03-01 14:22:24,2023-03-01 14:22:24,2023-02-28 15:37:05,false,50.02620888888889,21.99274888888889,0,0,315,,"{""status"":5,""ignition"":false,""charge"":true,""blocked"":false,""batteryLevel"":100,""rssi"":1,""raw"":""78780a130506010002004a3f840d0a"",""distance"":0.0,""totalDistance"":5948726.75,""motion"":false,""hours"":1677598599000}",0,null
386,gt06,1,2023-03-01 14:22:23,2023-03-01 14:22:23,2023-02-28 15:37:05,false,50.02620888888889,21.99274888888889,0,0,315,,"{""adc1"":12.47,""raw"":""79790008940004df004962c00d0a"",""distance"":0.0,""totalDistance"":5948726.75,""motion"":false,""hours"":1677598599000,""battery"":12.47}",0,null
385,gt06,1,2023-03-01 14:17:24,2023-03-01 14:17:24,2023-02-28 15:37:05,false,50.02620888888889,21.99274888888889,0,0,315,,"{""status"":5,""ignition"":false,""charge"":true,""blocked"":false,""batteryLevel"":100,""rssi"":1,""raw"":""78780a13050601000200481c960d0a"",""distance"":0.0,""totalDistance"":5948726.75,""motion"":false,""hours"":1677598599000}",0,null
384,gt06,1,2023-03-01 14:12:24,2023-03-01 14:12:24,2023-02-28 15:37:05,false,50.02620888888889,21.99274888888889,0,0,315,,"{""status"":5,""ignition"":false,""charge"":true,""blocked"":false,""batteryLevel"":100,""rssi"":2,""raw"":""78780a1305060200020047f9ad0d0a"",""distance"":0.0,""totalDistance"":5948726.75,""motion"":false,""hours"":1677598599000}",0,null
383,gt06,1,2023-03-01 14:12:23,2023-03-01 14:12:23,2023-02-28 15:37:05,false,50.02620888888889,21.99274888888889,0,0,315,,"{""adc1"":12.48,""raw"":""79790008940004e00046565e0d0a"",""distance"":0.0,""totalDistance"":5948726.75,""motion"":false,""hours"":1677598599000,""battery"":12.48}",0,null

I thought about modifying query and merging last 3-4 packets attributes when searching for last position:

SELECT tc_positions.*,
       JSON_MERGE_PATCH((SELECT attributes FROM tc_positions WHERE id = (tc_devices.positionid - 2)),
                        (SELECT attributes FROM tc_positions WHERE id = (tc_devices.positionid - 1)),
                        (SELECT attributes FROM tc_positions WHERE id = tc_devices.positionid)) AS attributes
FROM tc_positions
         INNER JOIN tc_devices ON tc_positions.id = tc_devices.positionid;

I have added that into config:

<entry key='database.selectLatestPositions'>
     SELECT tc_positions.*, JSON_MERGE_PATCH((SELECT attributes FROM tc_posi>.......
</entry>

But without success...
Any ideas how to work it out?

Best regards

code_af2 years ago

As much I know, the database queries are no longer supported through config in newer versions of traccar.
If you want to get the copy of attributes if they are not present, then please use copyAttributes feature, check Config for more information.
If you need to make some decisions based on attributes, you can use computed attributes, please check them here.

czerwony032 years ago

Thanks, working like a charm!