Overview
The Trailers API provides endpoints for querying trailer locations and receiving real-time updates. Trailers represent cargo units that are tracked independently from tractors via GPS devices. Database Table:trailers2 (aliased as trailers in code)
Data Sources:
- Terminal API (
/tsp/v1/trailers/locations) - Real-time trailer GPS locations - Terminal API (
/tsp/v1/trailers) - Static trailer info (VIN, make, model, etc.)
Get Current Trailer Locations
GET /api/trailers/current
Get current trailer locations from the database. This is a DB-first read endpoint with staleness metadata.Maximum number of trailers to return (1-1000)
ISO 8601 timestamp - only return trailers updated after this time
- Database Query: Calls
get_latest_trailer_locations()which queries thetrailers2table - Window Function: Uses PostgreSQL
ROW_NUMBER()to get latest row perterminal_trailer_id, ordered byupdated_at DESC - Filters Applied:
- Only trailers with non-null
terminal_trailer_id,latitude, andlongitude - Optional
sincetimestamp filter
- Only trailers with non-null
- Cold-Start Handling: If no data and poll in progress, waits up to
COLD_START_WAIT_MS(250ms) before returning 202 - Staleness Calculation: Compares current time to
max(updated_at)
| Header | Value |
|---|---|
X-Data-Source | Always db |
X-Stale-Ms | Milliseconds since most recent update |
ETag | MD5 hash of payload |
Last-Modified | HTTP date of most recent update |
Retry-After | Seconds to wait (only on 202) |
Real-Time Trailer Stream
GET /api/trailers/stream
Server-Sent Events (SSE) endpoint for real-time trailer location updates. Event Types:connected
connected
Sent immediately when client connects.
trailer_update
trailer_update
Sent when new trailer location data is available (every 60 seconds during active polling).
trailer_info
trailer_info
Sent every hour with static trailer information (make, model, VIN, etc.).
heartbeat
heartbeat
Sent every 30 seconds to keep connection alive.
- Connection: Client connects via
EventSource, receives uniqueclient_id - Client Registration:
SSEConnectionManagercreates asyncio queue for this client - Polling Wake-up: Notifies
TrailerPollingServiceto switch from slow (300s) to fast (60s) polling - Initial Data: Reads from
trailers2table viaget_latest_trailer_locations()and sends firsttrailer_update - Live Updates:
TrailerPollingServicebroadcasts to all connected clients after each poll - Static Info: Every hour, fetches static trailer info and sends as
trailer_infoevent - Heartbeat: Sends
:heartbeatevery 30 seconds - Disconnect: Removes client from manager on disconnect
Data Model
Trailers Table Schema
| Column | Type | Description |
|---|---|---|
id | VARCHAR(8) | Legacy 8-char ID |
trailer_id | UUID | Full UUID for internal tracking |
company_id | VARCHAR | Always “TERM” for Terminal data |
terminal_trailer_id | VARCHAR | Terminal trailer ID (unique, trl_xxx) |
last_location_latitude | NUMERIC | GPS latitude |
last_location_longitude | NUMERIC | GPS longitude |
last_located_at | TIMESTAMPTZ | GPS timestamp from provider |
last_location_address | VARCHAR | Formatted address from provider |
last_location_heading | NUMERIC | Heading in degrees (0-360) |
last_location_speed | NUMERIC | Speed in mph |
last_location_provider | VARCHAR | ELD provider (geotab, motive, etc.) |
make | VARCHAR | Trailer manufacturer |
model | VARCHAR | Trailer model |
model_year | INT | Manufacturing year |
vin | VARCHAR | Vehicle Identification Number |
physical_trailer_id | VARCHAR | Serial number/physical ID |
license_state | VARCHAR | License plate state |
license_no | VARCHAR | License plate number |
statuscode | VARCHAR | Terminal status (active/inactive) |
is_active | BOOLEAN | Derived from statuscode |
name | VARCHAR | Trailer name/identifier |
updated_at | TIMESTAMPTZ | Last DB update |
Background Services
TrailerPollingService
Runs as a background service started during application lifespan. Location Polling:- Interval: 60 seconds (when SSE clients connected), 300 seconds (idle)
- Source: Terminal API
/tsp/v1/trailers/locations - Target:
trailers2table viabulk_upsert_trailer_locations() - Deduplication: Only keeps latest record per
terminal_trailer_id
- Interval: Every hour (3600 seconds)
- Source: Terminal API
/tsp/v1/trailers - Target:
trailers2table viabulk_upsert_trailer_static() - Data: VIN, make, model, year, license plate, status
LOCATION_POLL_ENABLED=true- Enable/disable pollingTERMINAL_POLLING_INTERVAL_SECONDS=60- Fast poll intervalCACHE_TTL_SECONDS=300- Slow poll interval (cache TTL)

