Vehicle
The Vehicle module offers a VehicleProvider interface that is responsible for the control, access, and listening to changes in the vehicle state.
The integrator can choose to provide its implementation of the VehicleProvider or use the TomTom default thread-safe implementation.
The TomTom implementation can be obtained via the VehicleProviderFactory .
VehicleProviderFactory allows the user to pass a custom vehicle object for initialization of the VehicleProvider . If no vehicle object is provided, a non-commercial car with a combustion engine is used by default.
When an instance of VehicleProvider is created, it can be injected into other TomTom modules that depend on it, e.g., Navigation.
Setting the initial vehicle profile, then switching to a different vehicle
The following code snippet shows how to set the initial profile as an electric engine car and later switch to a bicycle, another vehicle type.
let chargeLevel = try ElectricEngine.ChargeLevel( currentCharge: Measurement.tt.kilowattHours(30.0), maxCharge: Measurement.tt.kilowattHours(60.0))let electricEngine = ElectricEngine(chargeLevel: chargeLevel)let electricCar = Car(electricEngine: electricEngine)var vehicleProvider = VehicleProviderFactory.create(vehicle: electricCar)
// Switch to a bicyclelet bicycle = Bicycle()vehicleProvider.vehicle = bicycleUpdating the vehicle profile
This updates the listed VehicleProperty s in the current vehicle profile. If a property, which is not PropertyID.maxSpeed or PropertyID.isCommercial , was not previously set, it throws a VehicleError . PropertyID.maxSpeed can be updated for every vehicle type. PropertyID.isCommercial can be updated for a motorized vehicle type.
let updatedCurrentChargeProperty = Measurement.tt.kilowattHours(25.0)let updatedElectricEngineProperties: [ElectricEngineProperty] = [.currentCharge(updatedCurrentChargeProperty)]let updatedVehicleProperties = VehicleProperty.electricEngine(updatedElectricEngineProperties)
try vehicleProvider.updateVehicleProperties([updatedVehicleProperties])Snapshot of the current vehicle profile
The following code snippet shows how to get the snapshot of the current vehicle’s state:
let currentVehicleSnapshot = vehicleProvider.vehicleListening to vehicle updates
The integrator can listen for the specific changes of the VehicleProperty s or every vehicle state change. If the integrator attempts to add the same observer twice, the VehicleError is thrown, regardless of how it was added.
Vehicle state changes
The following code snippet shows how to add an observer whose method will be called once the vehicle’s state changes:
class MyVehicleUpdateObserver: VehicleUpdateObserver { func didUpdateVehicle(_ vehicle: any Vehicle, updatedPropertyIDs: [PropertyID]) { // YOUR CODE GOES HERE }}let myVehicleUpdateObserver = MyVehicleUpdateObserver()try vehicleProvider.addVehicleUpdateObserver(myVehicleUpdateObserver)Next steps
Since you have learned how to setup a vehicle provider, here are recommendations for the next step: