Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TeslaUSB Archive New Folder Trigger #60

Open
GitHubGoody opened this issue Aug 25, 2019 · 11 comments
Open

TeslaUSB Archive New Folder Trigger #60

GitHubGoody opened this issue Aug 25, 2019 · 11 comments
Labels
enhancement New feature or request

Comments

@GitHubGoody
Copy link

I use a RPi to automatically archive my TeslaCam clips to my computer (https://github.com/marcone/teslausb).

What commands do I use to automatically start processing those clips? If it's "--monitor_trigger" what do I put for the file trigger when it's just monitoring a folder for newly added subfolders?

Thank you.

@GitHubGoody GitHubGoody changed the title Trigger TeslaUSB Archive New Folder Trigger Aug 25, 2019
@user68329
Copy link

user68329 commented Aug 26, 2019 via email

@ehendrix23
Copy link
Owner

Currently it does not monitor for newly added subfolders. It monitors for existence of a file (or folder, or link) and when it exist starts processing based on source provided.
Reason for this is that I cannot start processing a folder until all files are there, and when using --merge until all folders are there. But when would I know that everything is there?
Depending on how the copy works, it can take some time. Yet I would then already see new sub-folders.

Looking at how TeslaUSB works; I would say that in file archive-clips.sh (located in folders rsync_archive, ) you can then add something to copy (clone, rsync) a temp file to the location as well. This would be done after everything is copied over.
Then monitor for that file.

@GitHubGoody
Copy link
Author

@ehendrix23 Thanks, but I wouldn't know where to start with your recommendation. You couldn't build in a time delay option to the monitor function that would delay processing files in that folder until x seconds after the last file appeared (300 seconds would probably be a good default)?

If not, no big deal, I'll just copy/paste the existing (non monitor) script each time I want to review a few days worth of clips.

Hopefully, I can eventually review the 10 seconds in each of these I care about right on the display in the car...here's to hoping.

@ehendrix23
Copy link
Owner

Delay will not always work correctly. Been further looking into it and think I have something I can do. Hopefully next version.

@ehendrix23 ehendrix23 added the enhancement New feature or request label Sep 30, 2019
@brent7320
Copy link

This is what i do. It is a Powershell script that runs on the server every 15 minutes via a windows scheduled task. It looks for folders in source that teslacam_usb writes, then waits until the last folder/file has a write time of more than 10 minutes ago, then it makes a backup of the raw files, then runs two tesla_dashcam conversion (one normal speed, one fast speed) and writes them to different locations. Plex then monitors this location and imports them, it then send me an email saying it is completed.

If the scheduled task kicks off and a file is being actively copied (the write time on the folder will be newer than ~10 minutes ago), so the main code branch will not execute. Next time around if teslausb is completed transferring the write time would be more than 10 minutes ago and therefore the assumption is the transfer is finished and the script goes about processing.

If you have slow wifi, or issues that make transfers take abnormally long you might need to tweak some of the below values.

Yes it is ugly, yes it can be improved, but it was a great temporary solution i was able to throw together and has not failed me yet.

$ErrorActionPreference = 'SilentlyContinue'
$fullpath="Z:\Tesla"
$destination = "Z:\Backup\Tesla"
$newfiles = @(Get-ChildItem $fullpath | where-object {$_.Name.Length -gt 6})
$secpasswd = ConvertTo-SecureString "supersecurepassword" -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential ("[email protected], $secpasswd)
$currentdate = get-date -format yyyy-MM-dd_HH-mm
$teslacam = "Z:\Software\tesla_dashcam\tesla_dashcam.exe"
$teslacamargs1 = "--merge --gpu --gpu_type nvidia --quality HIGH source ""Z:\Tesla"" --output ""Z:\Tesla\Videos\TeslaCam_Normal\$currentdate.mp4"""
$teslacamargs2 = "--merge --gpu --gpu_type nvidia  --quality HIGH --speed 10 --delete_source source ""Z:\Tesla"" --output ""Z:\Tesla\Videos\TeslaCam_Fast\$currentdate.mp4"""
$StopWatch = [system.diagnostics.stopwatch]::startNew()

##check to make sure the last time that something was written to the folder was more than 10 minutes ago. If it is more than 10 minutes ago, begin copying
If ((Test-Path $fullpath -OlderThan (Get-Date).Addminutes(-10)) -and ($newfiles.Length -ne 0)) {
    
Copy-Item -Path (Get-Item -Path "$fullpath\*" -Exclude ('Videos', 'TeslaCam_Fast', 'TeslaCam_Normal')).FullName -Destination $destination -Recurse -Force
    Start-Process $teslacam $teslacamargs1 -Wait -WindowStyle Hidden
    Start-Process $teslacam $teslacamargs2 -Wait -WindowStyle Hidden
    Get-ChildItem  "Z:\Tesla\Videos" -recurse | Where-Object {$_.Name.Length -gt 30} | Remove-Item -Force
    Send-MailMessage -SmtpServer smtp.gmail.com -Port 587 -Credential $credential -UseSsl -From '[email protected]' -To '[email protected]' -Subject 'TeslaCam Merge Finished' -body "TeslaCam Merge Completed and took $($StopWatch.Elapsed.Minutes) minutes to complete"

    } else {

   exit
}

@meisterblau
Copy link

Delay will not always work correctly. Been further looking into it and think I have something I can do. Hopefully next version.

I’m looking forward to playing around with this!

What’s the latest on watching for new subfolders? An alternative might be to use teslausb’s IFTTT function to run your program via triggercmd.

@magicalyak
Copy link
Contributor

I was thinking of doing something similar with a cron job and just run nightly.
I have teslausb and plex setup on centos7

  1. teslausb rsync to server in folder "tesla"
  2. nightly run tesla_dashcam (midnight?)
    2a. run tesla_dashcam on import folder excluding subfolder archive to "tesla_dashcam" folder
    2b. move that day's folder to archive subfolder in "tesla"
    2c. remove folders older than 30 days in "tesla/archive"

IFTTT should work or could you add a file in the archive-clips.sh script that tesla_dashcam could detect and then delete it after import?

@meisterblau
Copy link

could you add a file in the archive-clips.sh script that tesla_dashcam could detect and then delete it after import?

That would be much cleaner than using IFTTT. The only catch is that I'm pretty far from a programmer. If anyone can help with the code I'd need to add to archive-clips.sh, that would be awesome.

@magicalyak
Copy link
Contributor

magicalyak commented Oct 25, 2019

Here is an example I am using on a plex server, it's not perfect but maybe you can use it as a basis to start. Note that it calls my customer Dockerfile which runs on Centos. The Docker container is public on DockerHub so this should also work but this fork is really focused on running tesla_dashcam as a docker container. This script will run on linux but if you're running it on a Mac you'll need to fix it as the date program is more limited.
https://github.com/magicalyak/tesla_dashcam/blob/dev/examples/teslausbarchive.sh

@magicalyak
Copy link
Contributor

Here is a copy of the output. You likely need to modify but hopefully it gives some ideas. If people want this included I can submit a pull request and maybe add an examples or contrib folder here.

/etc/cron.daily/teslausbarchive:

Looping through /mnt2/tesla/ for teslacam video to move
/mnt2/tesla/2019-10-27_13-43-43/ is over 24 hours old
Creating /mnt2/tesla_dashcam/2019-10-27
Creating /mnt2/tesla/processing/2019-10-27
Moving  to processing director /mnt2/tesla/processing/2019-10-27
Converting Videos from processing folder with tesla_dashcam container
Discovered 2 folders containing total of 44 files in /root/Import
There are 1 folders with 11 clips to process.
   Processing 11 clips in folder /root/Import/2019-10-27_13-43-43 (1/1)
       Processing clip 1/11 from 10/27/19 13:33:26 and 58 seconds long.
       Processing clip 2/11 from 10/27/19 13:34:27 and 58 seconds long.
       Processing clip 3/11 from 10/27/19 13:35:28 and 58 seconds long.
       Processing clip 4/11 from 10/27/19 13:36:29 and 57 seconds long.
       Processing clip 5/11 from 10/27/19 13:37:29 and 58 seconds long.
       Processing clip 6/11 from 10/27/19 13:38:29 and 59 seconds long.
       Processing clip 7/11 from 10/27/19 13:39:31 and 57 seconds long.
       Processing clip 8/11 from 10/27/19 13:40:31 and 58 seconds long.
       Processing clip 9/11 from 10/27/19 13:41:31 and 58 seconds long.
       Processing clip 10/11 from 10/27/19 13:42:32 and 59 seconds long.
       Processing clip 11/11 from 10/27/19 13:43:33 and 5 seconds long.
       Creating movie /root/Videos/2019-10-27T13-33-26_2019-10-27T13-43-38.mp4, please be patient.
   Movie /root/Videos/2019-10-27T13-33-26_2019-10-27T13-43-38.mp4 for folder /root/Import/2019-10-27_13-43-43 with duration 0:07:16 is ready.
   Creating movie /root/Videos/2019-10-27.mp4, please be patient.
Movie /root/Videos/2019-10-27.mp4 with duration 0:07:16 has been created, enjoy.
Total processing time: 0:11:24

Archiving folder in /mnt2/tesla/archive/2019-10-27
Removing intermediate files in /mnt2/tesla_dashcam/2019-10-27
Deleting /mnt2/tesla_dashcam/2019-10-27/2019-10-27T13-33-26_2019-10-27T13-43-38.mp4
Removing directories in /mnt2/tesla/archive if over 3 months and .preserve isn't present

@appark
Copy link

appark commented Mar 8, 2020

has anyone written a script for mac?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

7 participants