Session management
Before being able to use any of the openOBD session's functionality, a session would first have to be created. This page lists some examples, showing which functionality is available for managing sessions using Python.
Starting and closing a session
Below is an example demonstrating how to start and close an openOBD session. In this example, a session is started on a ticket, and the session will be closed successfully if the procedure has been carried out. If any openOBD-related exceptions occur during the procedure, they will be caught and the session will be closed with a failure result.
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)
# Start the SessionTokenHandler to ensure the openOBD session remains authenticated
SessionTokenHandler(openobd_session)
try:
# The session has been started, so the procedure can be carried out here
# Close the session with a successful result
result = ServiceResult(result=[Result.RESULT_SUCCESS])
openobd_session.finish(result)
print("openOBD session finished successfully.")
# Catch any exceptions that were raised because of openOBD
except OpenOBDException as e:
print(f"Encountered exception: {e}")
# Close the session with a RESULT_FAILURE_RUNTIME_ERROR, indicating something went wrong while running the code
result = ServiceResult(result=[Result.RESULT_FAILURE_RUNTIME_ERROR])
openobd_session.finish(result)
Retrieving and interrupting sessions
In addition to starting a session, it is also possible to manage openOBD sessions by retrieving or interrupting them. It is not necessary to start an openOBD session to use this functionality. Below is a simple example program which retrieves current sessions and allows you to select a session to be interrupted.
from openobd import *
# Retrieve all current openOBD sessions
openobd = OpenOBD()
session_list_object = openobd.get_session_list()
# Check if any sessions were returned
if len(session_list_object.sessions) == 0:
print("No sessions currently active.")
else:
# Print some info on each SessionInfo object that was returned
print("Current sessions:")
for number, session_info in enumerate(session_list_object.sessions, 1):
session_info_string = f"State: {session_info.state}, created at: {session_info.created_at}."
print(f"{number}. {session_info_string}")
while True:
try:
# Continue asking which session should be interrupted until a valid input has been given
choice = int(input("Which session do you want to interrupt? Enter 0 to cancel.\n"))
if choice == 0:
print("No sessions were interrupted.")
else:
# A session has been selected, so interrupt it
selected_session = session_list_object.sessions[choice - 1]
openobd.interrupt_session(session_id=SessionId(value=selected_session.id))
print(f"Session with ID {selected_session.id} has been interrupted.")
break
except (ValueError, IndexError):
print("Invalid input. Please enter a valid number")
except OpenOBDException as e:
print(e)