Skip to content

Commit

Permalink
Fixed dash-cam segment locking
Browse files Browse the repository at this point in the history
  • Loading branch information
connervieira committed Dec 20, 2024
1 parent 51cbe38 commit d0d9db7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,4 @@ November 20th, 2024
- "N" indicates normal recording.
- "P" indicates parked recording.
- `ext` is replaced by the file extension.
- Added `--help` commmand line option.
28 changes: 18 additions & 10 deletions dashcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,19 +426,20 @@ def lock_dashcam_segment(file):
if (time.time()-last_played_dashcam_saved_sound > 5):
utils.play_sound("dashcam_saved")
last_played_dashcam_saved_sound = time.time()
utils.display_message("Locked dash-cam segment file")

if (os.path.isdir(os.path.join(config["general"]["working_directory"], config["dashcam"]["saving"]["directory"])) == False): # Check to see if the saved dashcam video folder needs to be created.
os.system("mkdir -p '" + os.path.join(config["general"]["working_directory"], config["dashcam"]["saving"]["directory"] + "'")) # Create the saved dashcam video directory.

if (os.path.isdir(os.path.join(config["general"]["working_directory"], config["dashcam"]["saving"]["directory"]))): # Check to see if the dashcam saving directory exists.
os.system("cp '" + file1 + "' '" + config["general"]["working_directory"] + "/" + config["dashcam"]["saving"]["directory"] + "'") # Copy the current dashcam video segment to the saved folder.
os.system("cp \"" + file + "\" \"" + os.path.join(config["general"]["working_directory"], config["dashcam"]["saving"]["directory"]) + "\"") # Copy the current dashcam video segment to the saved folder.
anything_saved = True # Indicate that at least one file was saved.
else:
display_message("The dashcam saving directory does not exist, and could not be created. The dashcam video could not be locked.", 3)

time.sleep(0.5) # Wait for a short period of time so that other dashcam recording threads have time to detect the trigger file.
os.system("rm -rf '" + os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"]) + "'") # Remove the dashcam lock trigger file.
if (os.path.exists(config["general"]["interface_directory"] + "/" + config["dashcam"]["saving"]["trigger"])): # Check to see if the trigger file exists even after it should have been removed.
os.system("rm -f '" + os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"]) + "'") # Remove the dashcam lock trigger file.
if (os.path.exists(os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"]))): # Check to see if the trigger file exists even after it should have been removed.
display_message("Unable to remove dashcam lock trigger file.", 3)

process_timing("end", "Dashcam/File Maintenance")
Expand Down Expand Up @@ -973,13 +974,20 @@ def dashcam_normal(device):

# Handle segment locking:
if (os.path.exists(os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"])) == True): # Check to see if the trigger file exists.
# == Save the most recent segment: ==
if (config["dashcam"]["capture"]["audio"]["merge"] == True and config["dashcam"]["capture"]["audio"]["enabled"] == True):
threading.Thread(target=lock_dashcam_segment, args=[segment_names[-1]], name="DashcamSegmentSave") # Create the thread to save this dash-cam segment.
if (len(segment_names) >= 2): # Check to see if there is a segment before the current.
with open(os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"])) as file:
last_trigger_timestamp = utils.to_int(file.read())
if (last_trigger_timestamp - (first_segment_start_time + (segment_number * config["dashcam"]["saving"]["segment_length"])) <= 15): # Check to see if the save was initiated within the first 15 seconds of the segment.
threading.Thread(target=lock_dashcam_segment, args=[segment_names[-1]], name="DashcamSegmentSave") # Create the thread to save the previous segment.
threading.Thread(target=lock_dashcam_segment, args=[os.path.join(directory, segment_names[-1] + ".mkv")], name="DashcamSegmentSave").start() # Create the thread to save this dash-cam segment.
else:
threading.Thread(target=lock_dashcam_segment, args=[os.path.join(directory, segment_names[-1] + ".*")], name="DashcamSegmentSave").start() # Create the thread to save this dash-cam segment.
# == Save the segment before the most recent (if applicable): ==
if (len(segment_names) >= 2): # Check to see if there is a segment before the current.
with open(os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"])) as file:
last_trigger_timestamp = utils.to_int(file.read())
if (last_trigger_timestamp - (first_segment_start_time + (segment_number * config["dashcam"]["saving"]["segment_length"])) <= 15): # Check to see if the save was initiated within the first 15 seconds of the segment.
if (config["dashcam"]["capture"]["audio"]["merge"] == True and config["dashcam"]["capture"]["audio"]["enabled"] == True):
threading.Thread(target=lock_dashcam_segment, args=[os.path.join(directory, segment_names[-2] + ".mkv")], name="DashcamSegmentSave").start() # Create the thread to save this dash-cam segment.
else:
threading.Thread(target=lock_dashcam_segment, args=[os.path.join(directory, segment_names[-2] + ".*")], name="DashcamSegmentSave").start() # Create the thread to save this dash-cam segment.

# Calculate the frame-rate of the last segment:
calculated_framerate[device] = frames_since_last_segment/(time.time()-segment_started_time) # Calculate the frame-rate of the previous segment.
Expand Down Expand Up @@ -1154,7 +1162,7 @@ def dashcam():


update_status_lighting("normal") # Initialize the status lighting to normal.

os.system("rm -f '" + os.path.join(config["general"]["interface_directory"], config["dashcam"]["saving"]["trigger"]) + "'") # Remove the dashcam lock trigger file, in case it exists at start-up.

# =================================
# Initialize the physical controls:
Expand Down
17 changes: 16 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
import time # Required to add delays and handle dates/times.
import sys # Required to read command line arguments.

if ("--help" in sys.argv):
print("\npython3 main.py [MODE] [DIRECTORY] [OPTIONS]")
print(" MODE is the number representing operating mode Predator will use.")
print(" 0\t\tManagement Mode")
print(" 1\t\tPre-recorded Mode")
print(" 2\t\tReal-time Mode")
print(" 3\t\tDash-cam Mode")
print(" DIRECTORY specifies the working directory Predator will use.")
print(" OPTIONS is any number of command-line options from the list below.")
print(" --headless\tenables headless mode, where all user prompts are skipped")
print(" --help\t\tdisplays this help message, then exits")

global_variables.predator_running = False
exit()

import utils # Import the utils.py scripts.
style = utils.style # Load the style from the utils script.
debug_message = utils.debug_message # Load the debug message function from the utils script.
Expand Down Expand Up @@ -261,7 +276,7 @@
config["general"]["modes"]["auto_start"] = sys.argv[1] # Set the automatic start mode to the mode specified by the command line argument.

if (len(sys.argv) > 2): # Check to see if there are at least 2 command line arguments.
if (sys.argv[2] != "--headless"):
if (sys.argv[2] not in ["--headless", "--help"]):
config["general"]["working_directory"] = str(sys.argv[2]) # Set the working directory to the path specified by the command line argument.


Expand Down

0 comments on commit d0d9db7

Please sign in to comment.