# Fleet Tracking & Monitoring

## Objective

Set up real-time tracking for your fleet — monitor vehicle positions, sensor data, and trip progress.

## Step 1: Register Vehicles

Create vehicle records for your fleet:


```bash
curl -X POST https://api.otms.transportial.com/api/vehicle \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Truck NL-AB-12",
    "licensePlate": "NL-AB-12",
    "type": "truck"
  }'
```

## Step 2: Add Sensors

Attach sensors for temperature, fuel level, or other IoT data:


```bash
curl -X POST https://api.otms.transportial.com/api/sensor \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Temperature - Cargo bay",
    "type": "temperature"
  }'
```

## Step 3: Create Location Update Events

As GPS data comes in, create location update events:


```bash
curl -X POST https://api.otms.transportial.com/api/event \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "type": "LocationUpdateEvent",
    "dateTime": "2026-03-22T14:30:00Z",
    "lifeCycle": "actual",
    "geoReference": {
      "lat": 52.3676,
      "lng": 4.9041
    }
  }'
```

## Step 4: Record Arrival and Departure Events

Track when vehicles arrive at and depart from locations:


```bash
# Arrival
curl -X POST https://api.otms.transportial.com/api/event \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "type": "ArrivalEvent",
    "dateTime": "2026-03-22T14:35:00Z",
    "lifeCycle": "actual"
  }'

# Departure
curl -X POST https://api.otms.transportial.com/api/event \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "type": "DepartureEvent",
    "dateTime": "2026-03-22T15:10:00Z",
    "lifeCycle": "actual"
  }'
```

## Step 5: Monitor Sensor Data

Read sensor values at a specific point in time:


```bash
curl -X GET "https://api.otms.transportial.com/api/sensor/{sensorId}/value/2026-03-22T14:30:00Z" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

Get sensor updates over a time range:


```bash
curl -X GET "https://api.otms.transportial.com/api/sensors/{sensorId}/updates/2026-03-22T00:00:00Z/2026-03-22T23:59:59Z" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

## Step 6: Get Vehicle Trip History

View what trips a vehicle has been on:


```bash
# All trips for a vehicle
curl -X GET "https://api.otms.transportial.com/api/vehicle/{vehicleId}/trips/0/20" \
  -H "Authorization: Bearer YOUR_TOKEN"

# Trips within a date range
curl -X GET "https://api.otms.transportial.com/api/vehicle/{vehicleId}/trips:byDate/2026-03-01/2026-03-31" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

## Step 7: Fleet-Wide Analytics

Get insights across your entire fleet:


```bash
# Fleet trip report
curl -X GET "https://api.otms.transportial.com/api/insights/fleet/{fleetId}/trips/2026-03-01/2026-03-31" \
  -H "Authorization: Bearer YOUR_TOKEN"

# Move actions report
curl -X GET "https://api.otms.transportial.com/api/insights/fleet/{fleetId}/moveActions/2026-03-01/2026-03-31" \
  -H "Authorization: Bearer YOUR_TOKEN"

# CO2 emissions
curl -X POST "https://api.otms.transportial.com/api/insights/vehicles/CO2:byDateRange" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "vehicleIds": ["vehicle-uuid-1", "vehicle-uuid-2"],
    "startDate": "2026-03-01",
    "endDate": "2026-03-31"
  }'

# Sensor values across vehicles
curl -X POST "https://api.otms.transportial.com/api/insights/vehicles/sensorValueAtDate" \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "vehicleIds": ["vehicle-uuid-1", "vehicle-uuid-2"],
    "date": "2026-03-22T14:00:00Z"
  }'
```

## Step 8: Set Up Alerts

Create alerts for when conditions are met (e.g., temperature out of range):


```bash
curl -X POST https://api.otms.transportial.com/api/alert \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "name": "Cold chain temperature alert",
    "type": "sensor_threshold"
  }'
```

## Event Types for Tracking

| Event | Use Case |
|  --- | --- |
| `ArrivalEvent` | Vehicle arrives at a geofenced location |
| `DepartureEvent` | Vehicle leaves a location |
| `GateInEvent` | Vehicle enters a facility |
| `GateOutEvent` | Vehicle exits a facility |
| `StartMovingEvent` | Vehicle starts driving |
| `StopMovingEvent` | Vehicle stops |
| `StartEngineEvent` | Engine on |
| `StopEngineEvent` | Engine off |
| `SensorUpdateEvent` | Temperature, fuel, weight, etc. |


## Key Takeaways

- Events create the real-time tracking data layer
- Sensors provide IoT data (temperature, fuel, humidity)
- Fleet insights give aggregated analytics across vehicles
- Date-range queries let you analyze historical patterns