Utilize the Driving Insights API
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)")
}
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).
}
}
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)")
}
Last updated