Utilize the Driving Insights API
Accessing some of the API classes that are mentioned on this page requires additional Sentiance SDK dependencies. See this page for more information.
On this page, you can find examples of how to query the Sentiance SDK for driving insights, and how to register to receive real time driving insights in your app, for completed transports.
Query for Driving Insights
if let drivingInsights = Sentiance.shared.getDrivingInsights(forTransportId: transportId) {
let event = drivingInsights.transportEvent
let safetyScores = drivingInsights.safetyScores
print("Focus score: \(safetyScores.focusScore ?? -1)")
print("Legal score: \(safetyScores.legalScore ?? -1)")
print("Smooth score: \(safetyScores.smoothScore ?? -1)")
print("Call-while-moving score: \(safetyScores.callWhileMovingScore ?? -1)")
print("Overall score: \(safetyScores.overallScore ?? -1)")
print("Event ID: \(event.eventId)")
print("Started on: \(event.startDate)")
print("Ended on: \(String(describing: event.endDate))")
print("Mode: \(event.transportMode)")
if let distanceInMeters = event.distanceInMeters {
print("Distance: \(distanceInMeters)")
}
print("Waypoints: \(event.waypoints)")
}
val drivingInsights = DrivingInsightsApi.getInstance(context).getDrivingInsights(transportId)
if (drivingInsights != null) {
val event = drivingInsights.transportEvent
val safetyScores = drivingInsights.safetyScores
print("Focus score: ${safetyScores.focusScore}")
print("Legal score: ${safetyScores.legalScore}")
print("Smooth score: ${safetyScores.smoothScore}")
print("Call-while-moving score: ${safetyScores.callWhileMovingScore}")
print("Overall score: ${safetyScores.overallScore}")
print("Event ID: ${event.id}")
print("Started on: ${event.startTime}")
print("Ended on: ${event.endTime}")
print("Mode: ${event.transportMode}")
if (event.distanceInMeters != null) {
print("Distance: ${event.distanceInMeters}")
}
print("Waypoints: ${event.waypoints}")
}
import SentianceDrivingInsights from "@sentiance-react-native/driving-insights";
const drivingInsights = await SentianceDrivingInsights.getDrivingInsights(transportId);
const event = drivingInsights.transportEvent;
const safetyScores = drivingInsights.safetyScores;
if (safetyScores.focusScore) {
console.log(`Focus score: ${safetyScores.focusScore}`);
}
if (safetyScores.legalScore) {
console.log(`Legal score: ${safetyScores.legalScore}`);
}
if (safetyScores.smoothScore) {
console.log(`Smooth score: ${safetyScores.smoothScore}`);
}
if (safetyScores.callWhileMovingScore) {
console.log(`Call-while-moving score: ${safetyScores.callWhileMovingScore}`);
}
if (safetyScores.overallScore) {
console.log(`Overall score: ${safetyScores.overallScore}`);
}
console.log(`Event ID: ${event.id}`);
console.log(`Started on: ${event.startTime}`);
console.log(`Ended on: ${event.endTime}`);
console.log(`Mode: ${transport.transportMode}`);
if (event.distance) {
console.log(`Distance: ${event.distance}`);
}
console.log(`Waypoints: ${JSON.stringify(event.waypoints)}`);
Subscribe for Driving Insights Updates
public class DrivingInsightsUpdateReceiver: DrivingInsightsReadyDelegate {
func subscribe() {
Sentiance.shared.drivingInsightsReadyDelegate = self
}
public func onDrivingInsightsReady(insights: DrivingInsights) {
// Handle the insights here (see the Query for Driving Insights
// example above).
}
}
DrivingInsightsApi.getInstance(context)
.setDrivingInsightsReadyListener { insights ->
// Handle the insights here (see the Query for Driving Insights
// example above).
}
import SentianceDrivingInsights from "@sentiance-react-native/driving-insights";
const subscription = await SentianceDrivingInsights.addDrivingInsightsReadyListener(drivingInsights => {
// Handle the insights here (see the Query for Driving Insights
// example above).
});
Query For Driving Events
// Harsh driving events, used for computing the smooth driving score
Sentiance.shared.getHarshDrivingEvents(forTransportId: transportId).forEach { harshDrivingEvent in
print("Start date: \(harshDrivingEvent.startDate)")
print("End date: \(harshDrivingEvent.endDate)")
print("Magnitude: \(harshDrivingEvent.magnitude)")
}
// Phone usage events, used for computing the focused driving score
Sentiance.shared.getPhoneUsageEvents(forTransportId: transportId).forEach { phoneUsageEvent in
print("Start date: \(phoneUsageEvent.startDate)")
print("End date: \(phoneUsageEvent.endDate)")
}
// Call-while-moving events, used for computing the call-while-moving score
Sentiance.shared.getCallsWhileMovingEvents(forTransportId: transportId).forEach { callWhileMovingEvent in
print("Start date: \(callWhileMovingEvent.startDate)")
print("End date: \(callWhileMovingEvent.endDate)")
if let minSpeed = callWhileMovingEvent.minTraveledSpeedInMps?.floatValue {
print("Min traveled speed: \(minSpeed)")
}
if let maxSpeed = callWhileMovingEvent.maxTraveledSpeedInMps?.floatValue {
print("Max traveled speed: \(maxSpeed)")
}
}
// Speeding events, used for computing the legal driving score
Sentiance.shared.getSpeedingEvents(forTransportId: transportId).forEach { speedingEvent in
print("Start Date: \(speedingEvent.startDate)")
print("End Date: \(speedingEvent.endDate)")
print("Waypoints: \(speedingEvent.waypoints)")
}
// Harsh driving events, used for computing the smooth driving score
DrivingInsightsApi.getInstance(context).getHarshDrivingEvents(transportId)
.forEach { harshDrivingEvent ->
print("Start time: ${harshDrivingEvent.startTime}")
print("End time: ${harshDrivingEvent.endTime}")
print("Magnitude: ${harshDrivingEvent.magnitude}")
}
// Phone usage events, used for computing the focused driving score
DrivingInsightsApi.getInstance(context).getPhoneUsageEvents(transportId)
.forEach { phoneUsageEvent ->
print("Start time: ${phoneUsageEvent.startTime}")
print("End time: ${phoneUsageEvent.endTime}")
}
// Call-while-moving events, used for computing the call-while-moving score
DrivingInsightsApi.getInstance(context).getCallWhileMovingEvents(transportId)
.forEach { callWhileMovingEvent ->
print("Start time: ${callWhileMovingEvent.startTime}")
print("End time: ${callWhileMovingEvent.endTime}")
print("Min traveled speed: ${callWhileMovingEvent.minTraveledSpeedInMps}")
print("Max traveled speed: ${callWhileMovingEvent.maxTraveledSpeedInMps}")
}
// Speeding events, used for computing the legal driving score
DrivingInsightsApi.getInstance(context).getSpeedingEvents(transportId)
.forEach { speedingEvent ->
print("Start time: ${speedingEvent.startTime}")
print("End time: ${speedingEvent.endTime}")
print("Waypoints: ${speedingEvent.waypoints}")
}
import SentianceDrivingInsights from "@sentiance-react-native/driving-insights";
// Harsh driving events, used for computing the smooth driving score
const harshDrivingEvents = await SentianceDrivingInsights.getHarshDrivingEvents(transportId);
for (const event of harshDrivingEvents) {
console.log(`Start time: ${event.startTime}`);
console.log(`End time: ${event.endTime}`);
console.log(`Magnitude: ${event.magnitude}`);
}
// Phone usage events, used for computing the focused driving score
const phoneUsageEvents = await SentianceDrivingInsights.getPhoneUsageEvents(transportId);
for (const event of phoneUsageEvents) {
console.log(`Start time: ${event.startTime}`);
console.log(`End time: ${event.endTime}`);
}
// Call-while-moving events, used for computing the call-while-moving score
const callWhileMovingEvents = await SentianceDrivingInsights.getCallWhileMovingEvents(transportId);
for (const event of callWhileMovingEvents) {
console.log(`Start time: ${event.startTime}`);
console.log(`End time: ${event.endTime}`);
console.log(`Min traveled speed: ${event.minTravelledSpeedInMps}`);
console.log(`Max traveled speed: ${event.maxTravelledSpeedInMps}`);
}
// Speeding events, used for computing the legal driving score
const speedingEvents = await SentianceDrivingInsights.getSpeedingEvents(transportId);
for (const event of speedingEvents) {
console.log(`Start time: ${event.startTime}`);
console.log(`End time: ${event.endTime}`);
console.log(`Waypoints: ${JSON.stringify(event.waypoints)}`);
}
Last updated