Get Geofences by devices using API

macphisto7 years ago

According to the scheme when obtaining the devices an arrangement with the IDs of the geofences should be obtained too but not working. Can you tell me if i need extra step?
Actually i do:

  • Login with user and pass
  • Create Device
  • Create Geofence
  • Add Geofence to device

Please your kindly help.

Regards

Anton Tananaev7 years ago

What scheme and API are you talking about? Please be more specific.

macphisto7 years ago

Thanks for anwswer

To relate geofence and device I use the path /devices/geofences using body with deviceId and geofenceId

I checked the database and the data is correctly related.

The path to GET all devices is /devices using GET without any param

Thanks again

Anton Tananaev7 years ago

What exactly is not working?

macphisto7 years ago

By example i have 1 device with 2 geofences. When i get devices using the API i have this answer:

[
  {
    "id": 3,
    "attributes": {
      "speedLimit": "2"
    },
    "name": "Device 1",
    "uniqueId": "867273028860984",
    "status": "offline",
    "lastUpdate": "2017-01-24T02:44:30.000+0000",
    "positionId": 5679,
    "groupId": 0,
    "geofenceIds": [],
    "phone": "",
    "model": "",
    "contact": "",
    "category": "arrow"
  }
]

If you can see the array for geonfenceIds is empty.

Anton Tananaev7 years ago

I think you are mistaking two things. The field that you are talking about contains ids of the geofences that the device is currently in. It's not the same as the list of all geofences linked to the device.

macphisto7 years ago

OH i understand!
Do you have more detailed info about the api?
Second question using the API Is there a way to get geofences associated with device ?

Anton Tananaev7 years ago

All the API documentation is here:

https://www.traccar.org/traccar-api/

For more details you should check the source code.

To get geofences for a device, you should use "/geofences" resource with "deviceId" parameter.

macphisto7 years ago

Thanks Anton!

Great Help.

Best Regards

cosmic2 years ago

hello,
first of all sorry for writing on a old thread. I think my question is related to this topic.
My question is, can I read relational table data using API? or how can I do that?
For an example, there is the relational table tc_device_geofence. I want to read data from this table.

thank in advance for the help.
Best regards

Anton Tananaev2 years ago

Not directly, but you can do something like this:

GET /api/geofences?deviceId=1

It will return data based on tc_device_geofence.

cosmic2 years ago

Thanks much Anton for your reply.
Can I filter devices by geofence without adding goefenceId as a foreign key to the tc_devices table??

Anton Tananaev2 years ago

That doesn't seem to be related to the topic.

cosmic2 years ago

Hello
Is there any way to read devices by geofenceId from tc_device_geofence table? seems like GET /api/devices?geofenceId=1 is not working for the relation table.

please help me to figure out. Thanks.

Anton Tananaev2 years ago

Not possible in the current version of Traccar.