Geofence Processing
This guide explains how the geofence processing system works in the tracker application.
Overview
The geofence processing system is responsible for updating the status of trackers based on their location history. When a tracker has been within a geofence (a defined geographical area) for a certain period of time, its status is updated accordingly.
Components
The geofence processing system consists of the following components:
- Location Reports: Raw location data from trackers, stored in the
location_reportstable. - Location History: Aggregated location data, stored in the
location_historymaterialized view. - Geofences: Defined geographical areas, stored in the
storage_locationsanddelivery_locationstables. - match_geofences() Function: A PostgreSQL function that processes the location history and updates tracker statuses.
- process_geofences.py Script: A Python script that calls the
match_geofences()function periodically.
How It Works
- Data Collection: Location reports are collected from trackers and stored in the
location_reportstable. - Data Aggregation: The location reports are aggregated into the
location_historymaterialized view using TimescaleDB continuous aggregates. - Geofence Processing: The
match_geofences()function is called periodically to process the location history and update tracker statuses. - Status Updates: When a tracker has been within a geofence for a certain period of time (default: 12 hours), its status is updated accordingly:
- If the tracker is within a storage location geofence, its status is updated to
IN_STORAGE. - If the tracker is within a delivery location geofence, its status is updated to
DELIVERED.
- If the tracker is within a storage location geofence, its status is updated to
- Notifications: When a tracker's status changes, notifications are sent to users who have access to the tracker.
Configuration
The geofence processing system can be configured in the following ways:
- Geofence Radius: The radius of the geofence is set to 200 meters by default. This can be changed in the
match_geofences()function. - Time Threshold: The time threshold for a status change is set to 12 hours by default. This can be changed in the
match_geofences()function. - Processing Frequency: The geofence processing script is run every hour by default. This can be changed in the cron job configuration.
Implementation Details
Location History Materialized View
The location_history materialized view is created from the location_history_hourly and location_history_daily continuous aggregates. It includes the following columns:
id: A unique identifier for the location history record.timestamp: The timestamp of the location report.location: The location of the tracker as a PostGIS geometry.nearest_city: The nearest city to the location, if available.tracker_id: The ID of the tracker.confidence: The confidence level of the location report (0-100).
match_geofences() Function
The match_geofences() function processes the location history and updates tracker statuses. It performs the following steps:
- Get all trackers with location history in the last 24 hours.
- For each tracker, get its production run ID and current status.
- Process each hour of data for the tracker.
- For each hour, find the location with the longest time in a storage location geofence.
- If a storage location is found and the time in the geofence is >= the threshold, update the tracker's status to
IN_STORAGE. - If no storage location is found, find the location with the longest time in a delivery location geofence.
- If a delivery location is found and the time in the geofence is >= the threshold, update the tracker's status to
DELIVERED. - When a tracker's status changes, add a status history entry and send notifications to users.
process_geofences.py Script
The process_geofences.py script calls the match_geofences() function periodically. It performs the following steps:
- Connect to the database.
- Call the
match_geofences()function. - Log the result of the operation.
Deployment
The geofence processing system can be deployed in the following ways:
- Systemd Service: The
systemd/geofence-processing.servicefile can be used to create a systemd service that runs the geofence processing script periodically. See Systemd Services for more details. - Cron Job: The
cron_geofence_processing.txtfile contains a cron job configuration that runs the geofence processing script every hour.
Troubleshooting
If the geofence processing system is not working as expected, check the following:
- Location Reports: Make sure that location reports are being collected and stored in the
location_reportstable. - Location History: Make sure that the
location_historymaterialized view is being updated with the latest location data. - Geofences: Make sure that the geofences are defined correctly in the
storage_locationsanddelivery_locationstables. - match_geofences() Function: Make sure that the
match_geofences()function is working correctly by running it manually. - process_geofences.py Script: Make sure that the
process_geofences.pyscript is being run periodically by checking the logs.