diff --git a/TODO.md b/TODO.md index f666106..8469fed 100644 --- a/TODO.md +++ b/TODO.md @@ -63,7 +63,9 @@ These are the features actively planned for Predator and are likely to be added - [X] Add configuration value for the number of guesses the ALPR engine should make in pre-recorded mode. - [X] Organize configuration. - [X] Add support for multiple license plate formats. -- [X] Test updated license plate validation in real-time mode. + - [X] Test updated license plate validation in pre-recorded mode. + - [X] Test updated license plate validation in real-time mode. - [X] Add Phantom alert handling to updated ALPR stream. - [ ] Complete OpenCV dashcam recording. -- [ ] Improve the efficiency of GPS location requests when many requests are made in quick succession. +- [X] Improve the efficiency of GPS location requests when many requests are made in quick succession. + - [ ] Test that improved GPS location querying behaves as expected. diff --git a/utils.py b/utils.py index 81f609f..bdb7bbf 100644 --- a/utils.py +++ b/utils.py @@ -507,15 +507,25 @@ def display_shape(shape): # Define the function that will be used to get the current GPS coordinates. +last_gps_request = {} # This is a placeholder that will store information regarding the last GPS request. +last_gps_request["time"] = 0 # This is a placeholder that will hold the time that the last GPS request was made. +last_gps_request["data"] = [] # This is a placeholder that will hold the data from the last GPS request. def get_gps_location(): # Placeholder that should be updated at a later date. + global last_gps_request debug_message("Fetching current GPS location") if (config["realtime"]["gps"]["enabled"] == True): # Check to see if GPS is enabled. - try: # Don't terminate the entire script if the GPS location fails to be aquired. - gpsd.connect() # Connect to the GPS daemon. - gps_data_packet = gpsd.get_current() # Get the current information. - return gps_data_packet.position()[0], gps_data_packet.position()[1], gps_data_packet.speed(), gps_data_packet.altitude(), gps_data_packet.movement()["track"], gps_data_packet.sats # Return GPS information. - except: # If the current location can't be established, then return placeholder location data. - return 0.0000, -0.0000, 0.0, 0.0, 0.0, 0 # Return a default placeholder location. + if (time.time()-last_gps_request["time"] > 1): # Check to see if a sufficient amount of time has passed since the last time the GPS was queried before making a new request. + try: # Don't terminate the entire script if the GPS location fails to be aquired. + gpsd.connect() # Connect to the GPS daemon. + gps_data_packet = gpsd.get_current() # Query the GPS for the most recent information. + last_gps_request["time"] = time.time() # Record the current time as the last time the GPS was queried. + last_gps_request["data"] = [gps_data_packet.position()[0], gps_data_packet.position()[1], gps_data_packet.speed(), gps_data_packet.altitude(), gps_data_packet.movement()["track"], gps_data_packet.sats] # Record the current information as the last GPS query response. + + return gps_data_packet.position()[0], gps_data_packet.position()[1], gps_data_packet.speed(), gps_data_packet.altitude(), gps_data_packet.movement()["track"], gps_data_packet.sats # Return GPS information. + except: # If the current location can't be established, then return placeholder location data. + return 0.0000, -0.0000, 0.0, 0.0, 0.0, 0 # Return a default placeholder location. + else: # Otherwise, the last GPS request was made too recently, so simply return the last GPS query response instead of submitting a new one to save time. + return last_gps_request["data"][0], last_gps_request["data"][1], last_gps_request["data"][2], last_gps_request["data"][3], last_gps_request["data"][4], last_gps_request["data"][5] else: # If GPS is disabled, then this function should never be called, but return a placeholder position regardless. return 0.0000, 0.0000, 0.0, 0.0, 0.0, 0 # Return a default placeholder location.