Skip to content

Commit

Permalink
better progress
Browse files Browse the repository at this point in the history
  • Loading branch information
spektor56 committed May 2, 2022
1 parent f1c10ca commit ce31497
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 20 deletions.
65 changes: 54 additions & 11 deletions OpenpilotSdk/Hardware/OpenpilotDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
using Serilog;
using System.Globalization;
using System.Runtime.CompilerServices;
using OpenpilotSdk.OpenPilot.Segment;

namespace OpenpilotSdk.Hardware
{
Expand Down Expand Up @@ -131,10 +132,32 @@ public async Task DeleteDriveAsync(Drive drive)
await Task.WhenAll(deleteTasks);
}

public async Task ExportDriveAsync(string exportPath, Drive drive, Camera camera, bool combineSegments = false, IProgress<int> progress = null)
public async Task ExportDriveAsync(string exportPath, Drive drive, Camera camera, bool combineSegments = false, IProgress<OpenPilot.Camera.Progress> progress = null)
{
await ConnectAsync();

var cameraProgress = new OpenPilot.Camera.Progress(camera);
Progress<Progress> segmentProgress = null;
if (progress != null)
{
segmentProgress = new Progress<Progress>();
var segmentProgressDictionary = drive.Segments.ToDictionary(segment => segment.Index, _ => 0);
int previousProgress = 0;
segmentProgress.ProgressChanged += async (sender, segmentProgressResult) =>
{
segmentProgressDictionary[segmentProgressResult.Segment] = segmentProgressResult.Percent;
cameraProgress.Percent = (segmentProgressDictionary.Sum(segment => segment.Value) * 100) /
(segmentProgressDictionary.Count * 100);
if (cameraProgress.Percent > previousProgress)
{
previousProgress = cameraProgress.Percent;
progress.Report(cameraProgress);
}
};
}

if (!Directory.Exists(exportPath))
{
Directory.CreateDirectory(exportPath);
Expand All @@ -143,7 +166,7 @@ public async Task ExportDriveAsync(string exportPath, Drive drive, Camera camera
if (combineSegments)
{
var exportTasks =
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, false)).ToArray();
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, false, segmentProgress)).ToArray();

if (exportTasks.Length > 0)
{
Expand All @@ -166,11 +189,6 @@ await using (var inputFile = File.OpenRead(tempFilePath))
}
File.Delete(tempFilePath);
}

if (progress != null)
{
progress.Report(i);
}
}

fileWritten = outputFile.Length > 0;
Expand All @@ -191,7 +209,8 @@ await using (var inputFile = File.OpenRead(tempFilePath))
var m3uFileName = drive.ToString() + (char)camera.Type + ".m3u";

var exportTasks =
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, true, progress));
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, true, segmentProgress)).ToArray();

var exportedSegments = (await Task.WhenAll(exportTasks)).Where(file => !string.IsNullOrWhiteSpace(file)).ToArray();

if (exportedSegments.Length > 1)
Expand Down Expand Up @@ -228,8 +247,10 @@ public void ExportDrive(string path, Drive drive, IProgress<int> progress = null
}

public async Task<string> ExportSegmentAsync(string path, DriveSegment driveSegment, Camera camera, bool containerize,
IProgress<int> progress = null)
IProgress<Progress> progress = null)
{
var segmentProgress = new Progress(driveSegment.Index);

Video video = null;
switch (camera.Type)
{
Expand Down Expand Up @@ -284,7 +305,28 @@ await using (var outputFile = File.Create(outputFilePath))
await using (var stream = await sftpClient.OpenAsync(videoPath, FileMode.Open,
FileAccess.Read, CancellationToken.None))
{
await stream.CopyToAsync(outputFile);
var buffer = new byte[81920];
int bytesRead = 0;
var sourceLength = stream.Length;
int totalBytesRead = 0;
int previousProgress = 0;

while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
{
await outputFile.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);

if (progress != null)
{
totalBytesRead += bytesRead;

segmentProgress.Percent = (int)(((double)totalBytesRead / (double)sourceLength) * 100);
if (segmentProgress.Percent > previousProgress)
{
previousProgress = segmentProgress.Percent;
progress.Report(segmentProgress);
}
}
}
}
}
}
Expand All @@ -304,7 +346,8 @@ await using (var outputFile = File.Create(outputFilePath))

if (progress != null)
{
progress.Report(driveSegment.Index);
segmentProgress.Percent = 100;
progress.Report(segmentProgress);
}

return fileName;
Expand Down
17 changes: 17 additions & 0 deletions OpenpilotSdk/OpenPilot/Camera/Progress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenpilotSdk.OpenPilot.Camera
{
public class Progress
{
public Hardware.Camera Camera { get; }
public int Percent { get; set; }

public Progress(Hardware.Camera camera)
{
Camera = camera;
}
}
}
17 changes: 17 additions & 0 deletions OpenpilotSdk/OpenPilot/Segment/Progress.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenpilotSdk.OpenPilot.Segment
{
public class Progress
{
public int Segment { get; }
public int Percent { get; set; }

public Progress(int segment)
{
Segment = segment;
}
}
}
2 changes: 2 additions & 0 deletions OpenpilotSdk/OpenpilotSdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
<ItemGroup>
<Content Include="ffmpeg.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
<Content Include="ffprobe.exe">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</Content>
</ItemGroup>

Expand Down
1 change: 1 addition & 0 deletions OpenpilotToolkit/OpenpilotToolkit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<StartupObject>OpenpilotToolkit.Program</StartupObject>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<ApplicationManifest>app.manifest</ApplicationManifest>
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(PlatformTarget)' == 'x86'">
Expand Down
30 changes: 22 additions & 8 deletions OpenpilotToolkit/OpenpilotToolkitForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,21 +425,34 @@ private async void btnExport_Click(object sender, EventArgs e)
continue;
}

var ucDrive = new ucTaskProgress(drive.ToString(), drive.Segments.Count*cameras.Count)
var ucDrive = new ucTaskProgress(drive.ToString(), cameras.Count * 100)
{
Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top)
};

tlpTasks.Controls.Add(ucDrive);

var segmentsProcessed = 0;

var progress = new Progress<int>((segmentIndex) =>
var progressDictionary = cameras.ToDictionary(cam => cam, cam => 0);
var progressLock = new SemaphoreSlim(1, 1);
var progress = new Progress<OpenpilotSdk.OpenPilot.Camera.Progress>(async (cameraProgress) =>
{
if (!IsDisposed)
{
Interlocked.Increment(ref segmentsProcessed);
ucDrive.Progress = segmentsProcessed;
progressDictionary[cameraProgress.Camera] = cameraProgress.Percent;
try
{
await progressLock.WaitAsync();
var currentProgress = progressDictionary.Sum(progress => progress.Value);
if (currentProgress > ucDrive.Progress)
{
ucDrive.Progress = currentProgress;
}
}
finally
{
progressLock.Release();
}
}
});

Expand Down Expand Up @@ -1552,6 +1565,7 @@ await using (var destinationFile = await openpilotDevice.OpenWriteAsync(destinat
var sourceLength = sourceFile.Length;
int totalBytesRead = 0;
int previousProgress = 0;

while ((bytesRead = await sourceFile.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await destinationFile.WriteAsync(buffer, 0, bytesRead);
Expand Down Expand Up @@ -1929,7 +1943,7 @@ private async void btnOsmUpload_Click(object sender, EventArgs e)
{
content.Add(new ByteArrayContent(binaryFile), "file", drive.ToString());
content.Add(new StringContent(drive.ToString() + " openpilot drive"), @"description");
content.Add(new StringContent("openpilot,commai,comma2"), @"tags");
content.Add(new StringContent("openpilottoolkit,optk,openpilot,commai,comma2,comma3"), @"tags");
content.Add(new StringContent("1"), @"public");
content.Add(new StringContent("identifiable"), @"visibility");
Expand Down
5 changes: 4 additions & 1 deletion OpenpilotToolkit/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,13 @@ public static int Main(string[] args)
CefSharp.Cef.Initialize(settings);

GlobalFFOptions.Configure(options => options.BinaryFolder = "./");

var logPath = Path.Combine(AppContext.BaseDirectory, @"logs\log.txt");

Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.WriteTo.Console()
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day, shared: true)
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day, shared: true)
.CreateLogger();

Application.ApplicationExit += (_, _) =>
Expand Down

0 comments on commit ce31497

Please sign in to comment.