DriveMotionAnalyticsClient
DriveMotion provides some APIs to support statistical analysis of historical travel data, and each API supports asynchronous calls. In order to use these APIs, DriveMotion provides TNDriveMotionAnalyticsClient.
Get TNDriveMotionAnalyticsClient
Use TNDriveMotionAnalyticsClient
TNDriveMotionAnalyticsClient supports aggregate statistics on trips and scores within a certain time range, queries on trip information within a certain time range, and queries for detailed information about a certain historical trip or current active trip.
For each API, TNDriveMotionAnalyticsClient provides the corresponding Request entity class and its builder, and agrees on the basic behavior of callback. Therefore, for developers, they only need to construct the request object through the builder, create a call object and then use the execute
method of the request object to complete the asynchronous call of the request.
Note
asyncCall
method is deprecated from 1.2.1 and will be removed in next few releases. Please use execute
method to process call in asynchronous
Get aggregated drive score by time range (since version 1.0.0)
Get Request Builder
Build Request
TNAggregatedScoreRequest needs to set some parameters. The start date and the end date are required to define the time range for query. TNIntervalType is optional, and currently supports three modes: calendar month, calendar week, and daily. It can further provide unit aggregation capabilities within the time range.
Note
It is necessary to ensure that the called startDate
and endDate
represent complete days, meaning from 00:00:00 on the start day to midnight on the end day.
Create a Call and execute it
Callback is a closure accepting response and error parameters.
Info
Method ScoreFactorType.getType(name: String) can be used to parse the type field in ScoreFactor.
Get trips by time range (since version 1.0.0)
Get Request Builder
Build Request
TNGetTripsRequest needs to set some parameters. Where the start date and the end date are required, but limit and offset are optional, they can further provide paging capabilities within the time range.
Parameters
startDate
: Represents the starting date of the query range.
endDate
: Represents the ending date of the query range.
limit
: Represents the maximum number of trips to query. The default value is 20, with a range of 0 to 100. If there are more trips than the limit, the app needs to perform pagination queries.
offset
: Enables pagination in conjunction with the limit
parameter. The offset
value must be greater than or equal to 0, and is incremented by the number of results returned in the previous query.
sortType
: Enables custom query sorting. It represents the desired sorting method and defaults to TripSortType.tripStartTimeDESC
. Setting it to TripSortType.tripStartTimeASC
allows querying from the start time.
transportationModes
: Enables custom query filtering. It represents the desired trip types to query, with a default value of null to query all valid trip types.
excludeTripEvents
: Indicates whether to include TripEvents details in the return value. The default value is true, indicating that it is not required.
tripSelectionStatus
: when operating in cloud mode. This parameter allows filtering trips based on their selection status.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get trip detail by trip id (since version 1.0.0)
Get Request Builder
Build Request
TNGetTripDetailRequest requires trip id, assetId
and assetContext. This API only supports the query of the trip that has finished.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get live trip detail (since version 1.0.0)
Get Request Builder
Build Request
TNGetLiveTripDetailRequest does not require any parameters. Because there can only be at most one live trip at any time.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get config descriptions by requested config type (since version 1.1.0)
Get Request Builder
Build Request
TNGetConfigDescriptionsRequest require set type id to query the corresponding type description data.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get the latest trip detail (since version 1.1.0)
Get Request Builder
Build Request
TNGetLatestTripDetailRequest require assetId
and assetContext parameters. But this API only fetch the latest finished trip.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get cumulative drive score by fixed time window (since version 1.1.0)
Get Request Builder
Build Request
TNGetCumulativeScoreRequest requires optional lastNumOfDays
for historic score and sub scores and the valid days number is greater than 0 and less than or equal to 30 days, as well as parameters assetId and assetContext.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Update trip transportation mode (since version 1.2.1)
Get Request Builder
Build Request
TNUpdateTripTransportationModeRequest needs to set required tripId
and transportationMode
.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get Streaks (since version 1.2.1)
Get Request Builder
Build Request
TNGetStreaksRequest requires assetId and assetContext, API will response with current streak and best streak for each score factors.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get aggregated safety score by time range (since version 2.0.0)
Get Request Builder
Build Request
TNAggregatedSafetyScoreRequest needs to set some parameters. The start date and the end date are required to define the time range for query. TNSafetyIntervalType is optional, and currently supports three modes: calendar week, and daily. It can further provide unit aggregation capabilities within the time range.TNSafetyIntervalFilter is optional, and currently supports tree modes: all, eventOnly, scoreOnly.
Note
It is necessary to ensure that the called startDate
and endDate
represent complete days, meaning from 00:00:00 on the start day to midnight on the end day.
Parameters
startDate
: Represents the starting date of the query range (SDK will use 0:00:00 of the day).
endDate
: Represents the ending date of the query range (SDK will use 23:59:59 of the day).
intervalType
: Represents the interval type of safety score (per DAY or Week)
scoreCreatedTime
: Represents the score created time in UTC.
assetId
: Represents the vehicle's ID (value can be "VIN" or "Channel client id" based on type of assetContext). (If null, the API retrieves the aggregated safety score for the current user.)
assetContext
: Represents the context for assetId, if is "CAR", assetId value should be VIN, otherwise assetId should be Channel client id.
timeZoneId
: Represents the time zone id, Example: America/Denver
scoreVersion
: Represents the score version for calculating safety score.
Create a Call and execute it
Callback is a closure accepting response and error parameters.
Sync trips from cloud
Get Request Builder
Build Request
TNSyncTripsRequest has no parameters, API will sync trips from cloud.
Create a call and execute it
Callback is a closure accepting response and error parameters.
Get simulated safety score (since version 2.0.0)
Get Request Builder
Build Request
TNGetSimulatedSafetyScoreRequest needs to set required tripsInfo
and tripsEventStatistics
. Other parameters state
and scoreVersion
is optional, default scoreVersion is least safety socre algorithms.
Create a Call and execute it
Callback is a closure accepting response and error parameters.
Get event percentile table (since version 2.4.0)
Get Request Builder
Build Request
TNGetEventPercentileTableRequest needs to set required eventType
and state
. scoreVersion
is optional, default scoreVersion is least safety socre algorithms(Deprecated - since version 2.4.0).
TNGetEventPercentileTableRequest needs to set required state
. scoreVersion
is optional, default scoreVersion is least safety socre algorithms(since version 2.7.0).
Create a Call and execute it
Callback is a closure accepting response and error parameters.
Get Cumulative Safety Score (since 2.18.x)
TNGetCumulativeSafetyScoreRequestis to retrieve the cumulative safety score for a specified vehicle or user within a given date range.
Example Usage
| let request = try? TNDriveMotionService.getDriveMotionAnalyticsClient()
.withConnectionMode(mode: TNConnectionMode.default)
.getCumulativeSafetyScoreRequest()
.startDate(xxx)
.endDate(xxx)
.scoreBaseTime(#scoreBaseTime#)
.asset(#assetId#, #assetContext#)
.build()
TNGetCumulativeSafetyScoreCall().execute(request: request!) { response, error in
}
|
Parameters
- startDate (
Date
): The start date of the period for which the safety score is calculated.
- endDate (
Date
): The end date of the period for which the safety score is calculated.
- assetId: Represents the vehicle's ID (value can be "VIN" or "Channel client id" based on type of assetContext). (If null, the API retrieves the aggregated safety score for the current user.)
- scoreBaseTime (
Date
, Optional) the calculation of the CumulativeSafetyScore
based on the start period of trip.
- assetContext: Represents the context for assetId, if is "CAR", assetId value should be VIN, otherwise assetId should be Channel client id.
Response
The API returns the cumulative safety score based on the provided date range.
Error Handling
If the request fails, an error object is provided in the callback.
Get Contextual Coaching Statistics (since 2.23.0)
TNGetContextualCoachingStatisticsRequest is to retrieve the contextual coaching statistics and standouts for a specified vehicle or user within given date ranges.
Example Usage
Parameters
- currentPeriod (
DateRange
): The current period date range for which the statistics are calculated.
- previousPeriod (
DateRange
): The previous period date range for comparison (optional).
- groupBy (
Set<ContextualCoachingGroupFactor>
): Group factors for the statistics, nil for calculate all the factors.
- rankBy (
Set<ContextualCoachingRankMetrics>
): Rank metrics for the statistics, nil for calculate all the metrics.
- assetId: Represents the vehicle's ID (value can be "VIN" or "Channel client id" based on type of assetContext). (If nil, the API retrieves the statistics for the current user.)
- assetContext: Represents the context for assetId, if is "CAR", assetId value should be VIN, otherwise assetId should be Channel client id.
- timezone: Represents the time zone id, Example: America/Denver
- scoreVersion: The version of the score algorithm, if not set, the latest version will be used.
Create a Call and execute it
Callback is a closure accepting response and error parameters.
Response
The API returns the contextual coaching statistics based on the provided parameters, including:
- Current period statistics
- Previous period statistics (if provided)
- Standouts for each group factor and rank metric
Error Handling
If the request fails, an error object is provided in the callback.