Skip to content

Running a function

openOBD functions can be created to encapsulate an action or procedure that can then be called from an openOBD session. This can be used to automate actions that are common, or use the functions as building blocks by calling multiple functions in a single session. To be able to run an openOBD function, an openOBD session will have to be created, and you will need to know the UUID of a function that you are allowed to run (such as functions hosted by yourself). More information on function calls and contexts can be found on this page.

Example

Below is an example demonstrating how an openOBD function could be executed. In this example, a session is started (but not authenticated) and a function context is created on the session. This function context is then transferred to the function launcher, who will execute the requested function in the context. The function launcher will only have access to the context it has been given, and therefore does not control the entire session. When they are done executing the requested function, the context will be closed.

from openobd import *

# Start an openOBD session on a ticket
openobd = OpenOBD()
openobd_session = openobd.start_session_on_ticket(TICKET_ID)  # The ticket ID needs to be filled in (as a string)

# Create a new function context on the session (the session must not be authenticated to do this)
function_context = FunctionContextHandler(openobd_session)

try:
    # Run an openOBD function in the newly created context and wait until it has finished
    # An exception is raised if the function could not be run or was unsuccessful
    function_context.run_function(FUNCTION_ID)  # The function ID needs to be filled in (as a string)
    session_result = ServiceResult(result=[Result.RESULT_SUCCESS])
except OpenOBDFunctionException as e:
    print("Failed to run function: " + str(e))
    session_result = ServiceResult(result=[Result.RESULT_FAILURE])

# Close the session
openobd_session.finish(session_result)