Speed violation alerts

Product Used: Live Tracking API


NextBillion.ai’s Live Tracking API allows users to monitor their fleet vehicles in real-time and receive alerts when the desired vehicles violate the desired speeds. Users can set up custom speed limits or use the limits specified by the local authorities. When the vehicle breaks the specified speed limit, the Live Tracking service will generate an event with detailed information about the violation and send it as an alert on any desired system(s).

Setup

Let’s go through the following steps to set-up a real-time speed alert system.

Step 1: Create Asset

First we will create an asset (vehicle) whose activity we want to track. To create an asset, we will:

  • Give a meaningful “name” to the asset.
  • Add the “attributes” of the asset that will be used to associate the asset to a monitor.

Following is a configured Create Asset JSON:

1
2
3
4
5
6
7
8
9
10
11
12
curl --location 'https://api.nextbillion.io/skynet/asset?key=<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{
            "name": "Staff Transportation",
            "description": "Live Tracking Speeding Event Example",
            "attributes": {
                "driver_contact_no": "321-902-838",
                "driver_name": "James Smith",
                "license": "4 ES 7167",
                "vehicle_type": "Delivery"
            }
}'

As soon as the asset is created, we get the asset ID in the acknowledgement response. Please record this ID. For our example, the asset ID is:

1
2
3
4
5
6
{
  "status": "Ok",
  "data": {
    "id": "683a3672-8eb3-4ec2-a1b9-1bdc2743d491"
  }
}

Step 2: Bind a device to the Asset

Next step is to bind a GPS device to the asset which can upload the location and other information for the “asset” as it moves. We provide a “device_id” for the asset_id received in the previous step.

1
2
3
4
5
curl --location 'https://api.nextbillion.io/skynet/asset/683a3672-8eb3-4ec2-a1b9-1bdc2743d491/bind?key=<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{
    "device_id":"GAW-A1-FB-JT0312"
}'

As soon as the binding process is successful, we get an acknowledgement response from the API. Next we will define the speeding configurations which will be used for triggering alerts.

Step 3: Define Speed Configurations

Live Tracking API gives users the flexibility to set their own criteria of determining speeding states. Speeding is a state that an asset exhibits when it maintains a speed higher than a specified threshold for a duration higher than a given value. The API uses the GPS information received to calculate the speed and duration values.

Live Tracking uses following three attributes to capture speeding constraints:

  1. time_tolerance: The amount of time for which an asset needs to over-speed to trigger a speed alert.
  2. customer_speed_limit: The desired speed limit that should not be breached.
  3. use_admin_speed_limit: This flag is used to indicate if administrative speed limits or user provided speed limits should be used for monitoring.

Following is the speeding_config object that we use:

1
2
3
4
5
"speeding_config":{
    "time_tolerance": 35000,
    "customer_speed_limit":18,
    "use_admin_speed_limit": false
}

The above configuration will create speed alerts when the asset has been traveling at more than 18 m/s for more than 35 seconds. Next we will create a monitor and attach the asset to it.

Step 4: Create a Monitor

Now that we have an asset and the speeding configurations, let’s set up a monitoring criteria using a monitor. To create a monitor, we need to:

  • Specify the “type” of activity that we want to track. We set this value to “speeding”.
  • Specify the “speeding_config” attributes to model the configurations decided in the Step 3 above.
  • Add the same “attributes” for the monitor as that of the asset to ensure that the asset is successfully linked with the monitor.

Below is the JSON we built for creating the monitor:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
curl --location 'https://api.nextbillion.io/skynet/monitor?key=<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{
  "type": "speeding",
  "speeding_config": {
    "time_tolerance": 35000,
    "customer_speed_limit": 18,
    "use_admin_speed_limit": false
  },
  "name": "Delivery Monitor The Ritz Hotel, Los Angeles",
  "description": "Track speeding activity of the delivery vehicle",
  "match_filter": {
    "include_all_of_attributes": {
      "driver_contact_no": "321-902-838",
      "driver_name": "James Smith",
      "license": "4 ES 7167",
      "vehicle_type": "Delivery"
    }
  }
}'

Please record the monitor ID from the API response:

1
2
3
4
5
6
{
  "status": "Ok",
  "data": {
    "id": "36c171d5-bdf1-47c2-9531-4c02820ab70e"
  }
}

Now that our asset and monitoring set up is done, we move to the next part that deals with capturing the events once they are created. Live Tracking API uses webhooks to share the event information. Let’s see how to configure a webhook.

Step 5: Configure a Webhook

Users need to obtain a webhook URL from their application where they want to receive the information of events created. We, for this example, use a test webhook URL - "https://my-company/api/test_webhook" - to guide users through the process. It is recommended to use an actual webhook URL.

Once we obtain a valid webhook URL, we register it with our Live Tracking API using the (Add or Update Webhook Configuration)[https://docs.nextbillion.ai/docs/tracking/api/live-tracking-api#add-or-update-webhook-configuration] method.

1
2
3
curl --location --request PUT 'https://api.nextbillion.io/skynet/config?key=<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{ "webhook":[""https://my-company/api/test_webhook""] }'

The webhook registration is acknowledged with an “Ok” response from the API.

Create a Speeding Event

Finally, we will upload tracked GPS information for the asset (received from the device GPS in real-life implementation) and ensure that the asset breaches the speeding configurations that we configured in Step 3. Following is the JSON we use at this step to upload track information:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
curl --location 'https://api.nextbillion.io/skynet/asset/683a3672-8eb3-4ec2-a1b9-1bdc2743d491/track?key=<your_api_key>' \
--header 'Content-Type: application/json' \
--data '{
    "locations":[
        {
        "location":{
            "lat": 34.04209996,
            "lon": -118.25563815
        },
        "timestamp":1694930360000,
        "accuracy":2,
        "speed":12,
        "bearing":25,
        "altitude":110.5,
        "meta_data":{
            "test": "location 1"
        }
        },
        {
        "location":{
            "lat": 34.04034565,
            "lon": -118.25688621
        },
        "timestamp":1694930376000,
        "accuracy":1,
        "speed":21,
        "bearing":25,
        "altitude":110.5,
        "meta_data":{
            "test": "location 2"
        }
        },
        {
        "location":{
            "lat": 34.03943185,
            "lon": -118.25870229
        },
        "timestamp":1694930389000,
        "accuracy":7,
        "speed":16,
        "bearing":25,
        "altitude":100.5,
        "meta_data":{
            "test": "location 3"
        }
        },
        {
        "location":{
            "lat": 34.03852984,
            "lon": -118.26134074
        },
        "timestamp":1694930401000,
        "accuracy":7,
        "speed":19,
        "bearing":25,
        "altitude":100.5,
        "meta_data":{
            "test": "location 4"
        }
        },
        {
        "location":{
            "lat": 34.03930722,
            "lon": -118.26526147
        },
        "timestamp":1694930424000,
        "accuracy":7,
        "speed":32,
        "bearing":25,
        "altitude":100.5,
        "meta_data":{
            "test": "location 5"
        }
        },
        {
        "location":{
            "lat": 34.03776894,
            "lon": -118.26990692
        },
        "timestamp":1694930455000,
        "accuracy":7,
        "speed":35,
        "bearing":25,
        "altitude":100.5,
        "meta_data":{
            "test": "location 6"
        }
        },
        {
        "location":{
            "lat": 34.03404769,
            "lon": -118.27223711
        },
        "timestamp":1694930471000,
        "accuracy":7,
        "speed":25,
        "bearing":25,
        "altitude":100.5,
        "meta_data":{
            "test": "location 7"
        }
        },
        {
        "location":{
            "lat": 34.03233502,
            "lon": -118.27334749
        },
        "timestamp":1694930491000,
        "accuracy":7,
        "speed":11,
        "bearing":25,
        "altitude":100.5,
        "meta_data":{
            "test": "location 8"
        }
        }
    ],
    "device_id":"GAW-A1-FB-JT0312"
}'

As soon as the above locations are uploaded, the speeding criteria of the monitor is satisfied for a couple of locations (location 6 and location 7) and the respective event payloads are received on the configured webhook.

Visit Live Tracking API documentation to know more about the available features