Is this the right way to acquire a Geofence?

memesaregood6 days ago

I'm tinkering with the codebase and figured out that, using the Storage, I can fetch and create objects of model classes. I need to acquire a geofence during the handling of a report. Is this the right way to do it?

                Geofence geofence = storage.getObject(Geofence.class,
                        new Request(
                                new Columns.All(),
                                new Condition.Equals("id", geofenceId)
                        )
                );
Anton Tananaev6 days ago

Looks correct. Is there a reason you're asking this question? I'm sure it would have been easier to just try it out.

memesaregood6 days ago

Of course there is. I was just thinking I'm doing something wrong.

Here's a snippet of the code I'm trying to get to work. I wrote a custom ReportItem, so don't be surprised by the setGeofence() method.

        List<Long> geofences = startTrip.getGeofenceIds();
        System.out.println(geofences.size());
        if (geofences.getClass() != null) {
            for (Long geofenceid : geofences) {
                System.out.println(geofenceid);
                Geofence geofence = storage.getObject(Geofence.class,
                        new Request(
                                new Columns.All(),
                                new Condition.Equals("id", geofenceid)
                        )
                );
                System.out.println(geofence.getName());
                //Cannot invoke "org.traccar.model.Geofence.getName()" because "geofence" is null
                GeofenceGeometry geometry = geofence.getGeometry();
                if (geometry.containsPoint(config, geofence, startTrip.getLatitude(), startTrip.getLongitude())
                        || geometry.containsPoint(config, geofence, endTrip.getLatitude(), endTrip.getLongitude())
                ) {
                    trip.setGeofence(geofence.getName());
                    
                }
            }
        }

I checked: the geofenceId is not null, meaning it's actually an existant geofence, and it is a number. When it's passed through the getObject() method, the Geofence object crafted is somehow a null. How is this possible? What am I doing wrong?

Anton Tananaev6 days ago

Are you sure the id you're trying to get exists?

memesaregood6 days ago

Pardon me, it appears it got deleted.

It appears that there's no geofenceids stored in the positions table, in the DB schema. How are geofence IDs stored in the positions?
Screenshot at 2025-04-20 23-54-03.png