Skip to content

Commit

Permalink
Added optional http timeout to get list and download file http commands
Browse files Browse the repository at this point in the history
  • Loading branch information
morrisonbrett committed Oct 28, 2018
1 parent 18b99b5 commit 795bd4c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
63 changes: 37 additions & 26 deletions BlackVueDownloader.PCL/BlackVueDownloader.cs
Expand Up @@ -47,17 +47,18 @@ public BlackVueDownloader(IFileSystemHelper fileSystemHelper)
/// </summary>
/// <param name="ip"></param>
/// <param name="directory"></param>
public void Run(string ip, string directory)
/// <param name="timeout"></param>
public void Run(string ip, string directory, int timeout)
{
var body = QueryCameraForFileList(ip);
var body = QueryCameraForFileList(ip, timeout);
var list = GetListOfFilesFromResponse(body);

var tempdir = Path.Combine(directory, "_tmp");
var targetdir = Path.Combine(directory, "Record");

CreateDirectories(tempdir, targetdir);

ProcessList(ip, list, tempdir, targetdir);
ProcessList(ip, list, tempdir, targetdir, timeout);
}

public void CreateDirectories(string tempdir, string targetdir)
Expand Down Expand Up @@ -85,15 +86,16 @@ public IList<string> GetListOfFilesFromResponse(string body)
return body.ParseBody().Select(e => e.Replace("n:/Record/", "").Replace(",s:1000000", "")).ToList();
}

/// <summary>
/// For given camera ip, filename, and filetype, download the file and return a status
/// </summary>
/// <param name="ip"></param>
/// <param name="filename"></param>
/// <param name="filetype"></param>
/// <param name="tempdir"></param>
/// <param name="targetdir"></param>
public void DownloadFile(string ip, string filename, string filetype, string tempdir, string targetdir)
/// <summary>
/// For given camera ip, filename, and filetype, download the file and return a status
/// </summary>
/// <param name="ip"></param>
/// <param name="filename"></param>
/// <param name="filetype"></param>
/// <param name="tempdir"></param>
/// <param name="targetdir"></param>
/// <param name="timeout"></param>
public void DownloadFile(string ip, string filename, string filetype, string tempdir, string targetdir, int timeout)
{
string filepath;
string tempFilepath;
Expand Down Expand Up @@ -146,7 +148,10 @@ public void DownloadFile(string ip, string filename, string filetype, string tem
// it won't leave a partial file in the target directory
logger.Info($"Downloading {filetype} file: {url}");
Stopwatch st = Stopwatch.StartNew();
url.DownloadFileAsync(tempdir).Wait();
if (timeout > 0)
url.WithTimeout(timeout).DownloadFileAsync(tempdir).Wait();
else
url.DownloadFileAsync(tempdir).Wait();
st.Stop();
BlackVueDownloaderCopyStats.DownloadingTime = BlackVueDownloaderCopyStats.DownloadingTime.Add(st.Elapsed);

Expand Down Expand Up @@ -182,14 +187,15 @@ public void DownloadFile(string ip, string filename, string filetype, string tem
}
}

/// <summary>
/// For the list, loop through and process it
/// </summary>
/// <param name="ip"></param>
/// <param name="list"></param>
/// <param name="tempdir"></param>
/// <param name="targetdir"></param>
public void ProcessList(string ip, IList<string> list, string tempdir, string targetdir)
/// <summary>
/// For the list, loop through and process it
/// </summary>
/// <param name="ip"></param>
/// <param name="list"></param>
/// <param name="tempdir"></param>
/// <param name="targetdir"></param>
/// <param name="timeout"></param>
public void ProcessList(string ip, IList<string> list, string tempdir, string targetdir, int timeout)
{
var sw = new Stopwatch();
sw.Start();
Expand All @@ -200,19 +206,19 @@ public void ProcessList(string ip, IList<string> list, string tempdir, string ta
{
logger.Info($"Processing File: {s}");

DownloadFile(ip, s, "video", tempdir, targetdir);
DownloadFile(ip, s, "video", tempdir, targetdir, timeout);

// Line below because the list may include _NF and _NR named files. Only continue if it's an NF.
// Otherwise it's trying to download files that are probably already downloaded
if (!s.Contains("_NF.mp4")) continue;

// Make filenames for accompanying gps file
var gpsfile = s.Replace("_NF.mp4", "_N.gps");
DownloadFile(ip, gpsfile, "gps", tempdir, targetdir);
DownloadFile(ip, gpsfile, "gps", tempdir, targetdir, timeout);

// Make filenames for accompanying gff file
var gffile = s.Replace("_NF.mp4", "_N.3gf");
DownloadFile(ip, gffile, "3gf", tempdir, targetdir);
DownloadFile(ip, gffile, "3gf", tempdir, targetdir, timeout);
}

sw.Stop();
Expand All @@ -229,14 +235,19 @@ public void ProcessList(string ip, IList<string> list, string tempdir, string ta
/// Get a raw string response from the camera
/// </summary>
/// <param name="ip"></param>
/// <param name="timeout"></param>
/// <returns>Raw string list of files</returns>
public string QueryCameraForFileList(string ip)
public string QueryCameraForFileList(string ip, int timeout)
{
try
{
var url = $"http://{ip}/blackvue_vod.cgi";

var fileListBody = url.GetStringAsync();
System.Threading.Tasks.Task<string> fileListBody;
if (timeout > 0)
fileListBody = url.WithTimeout(timeout).GetStringAsync();
else
fileListBody = url.GetStringAsync();
fileListBody.Wait();

var content = fileListBody.Result;
Expand Down
22 changes: 11 additions & 11 deletions BlackVueDownloader.Tests/BlackVueDownloaderTests.cs
Expand Up @@ -75,7 +75,7 @@ public void QueryCameraForFileListTest(string ip)
{
httpTest.RespondWith("this is the body");

var body = blackVueDownloader.QueryCameraForFileList(ip);
var body = blackVueDownloader.QueryCameraForFileList(ip, 0);

httpTest.ShouldHaveCalled($"http://{ip}/blackvue_vod.cgi");

Expand All @@ -93,7 +93,7 @@ public void EmptyResponseTest(string ip)
{
httpTest.RespondWith("");

var body = blackVueDownloader.QueryCameraForFileList(ip);
var body = blackVueDownloader.QueryCameraForFileList(ip, 0);

httpTest.ShouldHaveCalled($"http://{ip}/blackvue_vod.cgi");

Expand All @@ -113,7 +113,7 @@ public void InvalidResponseTest(string ip)

try
{
blackVueDownloader.QueryCameraForFileList(ip);
blackVueDownloader.QueryCameraForFileList(ip, 0);
}
catch (Exception e)
{
Expand All @@ -136,7 +136,7 @@ public void CantFindCameraTest(string ip)

try
{
blackVueDownloader.QueryCameraForFileList(ip);
blackVueDownloader.QueryCameraForFileList(ip, 0);
}
catch (Exception e)
{
Expand All @@ -157,7 +157,7 @@ public void CameraRespondValidTest(string ip, int numRecords)
{
httpTest.RespondWith(GenerateRecords(numRecords));

var body = blackVueDownloader.QueryCameraForFileList(ip);
var body = blackVueDownloader.QueryCameraForFileList(ip, 0);

httpTest.ShouldHaveCalled($"http://{ip}/blackvue_vod.cgi");

Expand Down Expand Up @@ -193,7 +193,7 @@ public void GetListOfFilesAndProcessTest(string ip, int numRecords)
httpTest.RespondWith("OK");
}
blackVueDownloader.BlackVueDownloaderCopyStats.Clear();
blackVueDownloader.ProcessList(ip, list, targetdir, targetdir);
blackVueDownloader.ProcessList(ip, list, targetdir, targetdir, 0);
Assert.Equal(numRecords*4, blackVueDownloader.BlackVueDownloaderCopyStats.Copied);

// Ignored from above test
Expand All @@ -203,7 +203,7 @@ public void GetListOfFilesAndProcessTest(string ip, int numRecords)
// And if we loop through again, they should all exist, and therefore be "ignored"
// We need to do this with an unmocked version of the file system helper
blackVueDownloaderNoMock.BlackVueDownloaderCopyStats.Clear();
blackVueDownloaderNoMock.ProcessList(ip, list, targetdir, targetdir);
blackVueDownloaderNoMock.ProcessList(ip, list, targetdir, targetdir, 0);
Assert.Equal(numRecords*4, blackVueDownloaderNoMock.BlackVueDownloaderCopyStats.Ignored);

// Fail test
Expand All @@ -212,7 +212,7 @@ public void GetListOfFilesAndProcessTest(string ip, int numRecords)
httpTest.RespondWith("FAILURE", 500);
}
blackVueDownloader.BlackVueDownloaderCopyStats.Clear();
blackVueDownloader.ProcessList(ip, list, targetdir, targetdir);
blackVueDownloader.ProcessList(ip, list, targetdir, targetdir, 0);
Assert.Equal(numRecords*4, blackVueDownloader.BlackVueDownloaderCopyStats.Errored);

// Timeout Fail test
Expand All @@ -221,7 +221,7 @@ public void GetListOfFilesAndProcessTest(string ip, int numRecords)
httpTest.SimulateTimeout();
}
blackVueDownloader.BlackVueDownloaderCopyStats.Clear();
blackVueDownloader.ProcessList(ip, list, targetdir, targetdir);
blackVueDownloader.ProcessList(ip, list, targetdir, targetdir, 0);
Assert.Equal(numRecords*4, blackVueDownloader.BlackVueDownloaderCopyStats.Errored);
}

Expand All @@ -239,7 +239,7 @@ public void DownloadFileIgnoreTest(string ip)
blackVueDownloaderNoMock.CreateDirectories(targetdir, targetdir);

filesystem.Setup(x => x.Exists(Path.Combine(targetdir, "ignorefile.mp4"))).Returns(true);
blackVueDownloader.DownloadFile(ip, "ignorefile.mp4", "video", targetdir, targetdir);
blackVueDownloader.DownloadFile(ip, "ignorefile.mp4", "video", targetdir, targetdir, 0);

Assert.Equal(1, blackVueDownloader.BlackVueDownloaderCopyStats.Ignored);
}
Expand All @@ -259,7 +259,7 @@ public void DownloadFileTmpExistsTest(string ip)

filesystem.Setup(x => x.Exists(Path.Combine(tempdir, "ignorefile.mp4"))).Returns(true);
filesystem.Setup(x => x.Delete(Path.Combine(tempdir, "ignorefile.mp4")));
blackVueDownloader.DownloadFile(ip, Path.Combine(tempdir, "ignorefile.mp4"), "video", tempdir, tempdir);
blackVueDownloader.DownloadFile(ip, Path.Combine(tempdir, "ignorefile.mp4"), "video", tempdir, tempdir, 0);

Assert.Equal(1, blackVueDownloader.BlackVueDownloaderCopyStats.Ignored);
}
Expand Down
10 changes: 8 additions & 2 deletions BlackVueDownloader/Program.cs
Expand Up @@ -10,14 +10,15 @@ internal class Program
private static void Main(string[] args)
{
Logger logger = LogManager.GetCurrentClassLogger();
var timeout = 0;

var version = Assembly.GetEntryAssembly().GetName().Version.ToString();

logger.Info($"BlackVue Downloader Version {version}");

if (args.Length < 1)
{
logger.Warn("Usage: BlackVueDownloader.exe ipaddress [destinationdirectory]");
logger.Warn("Usage: BlackVueDownloader.exe ipaddress [destinationdirectory] [timeoutinminutes (default is no timeout)]");
return;
}

Expand All @@ -34,10 +35,15 @@ private static void Main(string[] args)
directory = args[1];
}

if (args.Length == 3)
{
timeout = int.Parse(args[2]) * 60;
}

try
{
var blackVueDownloader = new PCL.BlackVueDownloader();
blackVueDownloader.Run(ip, directory);
blackVueDownloader.Run(ip, directory, timeout);
}
catch(Exception e)
{
Expand Down

0 comments on commit 795bd4c

Please sign in to comment.