Skip to content

Violation Warning

Violation warning

This tutorial shows how to handle violation warnings.

Get started

Violation warning requires alert component to be enabled at the creation of a navigation service. A navigation service created with alert component enabled will have violation warning turned off by default. Violation warning is detected along the most possible path (MPP) in free mode, in navigation mode it follows the route.

1
2
3
4
5
6
// enable alert component
tn::drive::NavigationServiceOptions options;
options.enable_alert = true;

const auto navigationService = tn::drive::api::NavigationServiceFactory::createNavigationService(
    options, system, settings, mapContent, directionService);

Turn it on if violation warning is needed. Once turned on, you will receive violation warning for over speed.

// turn on violation warning
navigationService->enableViolationWarningDetection(true);

Violation warning listener

Make a customized alert listener to handle violation warnings.

class AlertObserver : public tn::drive::api::AlertEventListener
{
public:
    void onViolationWarningUpdated(const tn::drive::models::v1::ViolationWarning& violationWarning) override
    {
        const auto& warning = violationWarning.over_speed_warning;
        if (warning.has_value())
        {
            const auto speedLimit = warning->speed_limit.value;
            if ((speedLimit != tn::foundation::INVALID_SPEED_LIMIT) &&
                (speedLimit != tn::foundation::MAX_SPEED_UNLIMITED) &&
                // vehicle speed unit in violation warning is the same as speed limit, can be compared directly
                (warning->vehicle_speed > speedLimit))
            {
                tn::drive::models::v1::AudioRequest request;
                request.request_type = warning->violation_type;

                tn::drive::models::v1::AudioInstruction instruction;
                navigationService->requestAudio(request, instruction);
            }
        }
    }

    // omit other methods
};

Register the listener after creation of navigation service.

const auto listener = tn::make_shared<AlertObserver>();
navigationService->addAlertEventListener(listener);

Unregister it when not needed.

navigationService->removeAlertEventListener(listener);

Customize over speed violation warning trigger condition

Over speed violation warning will not be given immediately when vehicle speed is over the limit. There's a tolerance range between speed limit and the value actually triggers the warning.

const std::string violationWarningSettings = R"json(
{
    // Following configuration shows the default values for over speed violation warning trigger condition
    "ViolationWarning": 
    {
        //  key should be ISO-3166 alpha-3 country code in upper case or "default"
        //  both the values should be integer and can't be negative
        "OverSpeed": 
        {
            //  We have two dimension tolerance on the over speed condition, absolute and percentage
            //  absolute means if the current speed is N higher than the speed limit (unit is the speed limit unit)
            //  percentage means if the current speed is N% higher than the speed limit
            //  when both of the two conditions are met, we consider it's over speed.
            "default": 
            {
                "absolute": 0,
                "percentage": 0
            },
            "USA": 
            {
                "absolute": 5,
                "percentage": 0
            },
            "GBR": {
                "absolute": 0,
                "percentage": 10
            },
            // omit settings for other countries, refer to resources/global/alert/base_config.json for details
        }
    }
}
)json";

const auto settings = tn::foundation::Settings::Builder()
    .setString(tn::drive::api::SettingConstants::SETTING_ALERT_JSON_CONTENT, violationWarningSettings)
    .build();

// enable alert component at the creation of a navigation service to make customized violation warning settings effective
tn::drive::NavigationServiceOptions options;
options.enable_alert = true;

const auto navigationService = tn::drive::api::NavigationServiceFactory::createNavigationService(
    options, system, settings, mapContent, directionService);