Skip to content

Commit ce31497

Browse files
committed
better progress
1 parent f1c10ca commit ce31497

File tree

7 files changed

+117
-20
lines changed

7 files changed

+117
-20
lines changed

OpenpilotSdk/Hardware/OpenpilotDevice.cs

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
using Serilog;
2828
using System.Globalization;
2929
using System.Runtime.CompilerServices;
30+
using OpenpilotSdk.OpenPilot.Segment;
3031

3132
namespace OpenpilotSdk.Hardware
3233
{
@@ -131,10 +132,32 @@ public async Task DeleteDriveAsync(Drive drive)
131132
await Task.WhenAll(deleteTasks);
132133
}
133134

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

139+
var cameraProgress = new OpenPilot.Camera.Progress(camera);
140+
Progress<Progress> segmentProgress = null;
141+
if (progress != null)
142+
{
143+
segmentProgress = new Progress<Progress>();
144+
var segmentProgressDictionary = drive.Segments.ToDictionary(segment => segment.Index, _ => 0);
145+
int previousProgress = 0;
146+
segmentProgress.ProgressChanged += async (sender, segmentProgressResult) =>
147+
{
148+
segmentProgressDictionary[segmentProgressResult.Segment] = segmentProgressResult.Percent;
149+
150+
cameraProgress.Percent = (segmentProgressDictionary.Sum(segment => segment.Value) * 100) /
151+
(segmentProgressDictionary.Count * 100);
152+
153+
if (cameraProgress.Percent > previousProgress)
154+
{
155+
previousProgress = cameraProgress.Percent;
156+
progress.Report(cameraProgress);
157+
}
158+
};
159+
}
160+
138161
if (!Directory.Exists(exportPath))
139162
{
140163
Directory.CreateDirectory(exportPath);
@@ -143,7 +166,7 @@ public async Task ExportDriveAsync(string exportPath, Drive drive, Camera camera
143166
if (combineSegments)
144167
{
145168
var exportTasks =
146-
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, false)).ToArray();
169+
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, false, segmentProgress)).ToArray();
147170

148171
if (exportTasks.Length > 0)
149172
{
@@ -166,11 +189,6 @@ public async Task ExportDriveAsync(string exportPath, Drive drive, Camera camera
166189
}
167190
File.Delete(tempFilePath);
168191
}
169-
170-
if (progress != null)
171-
{
172-
progress.Report(i);
173-
}
174192
}
175193

176194
fileWritten = outputFile.Length > 0;
@@ -191,7 +209,8 @@ await FFMpegArguments.FromFileInput(outputFilePath, true,
191209
var m3uFileName = drive.ToString() + (char)camera.Type + ".m3u";
192210

193211
var exportTasks =
194-
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, true, progress));
212+
drive.Segments.Select((segment) => ExportSegmentAsync(exportPath, segment, camera, true, segmentProgress)).ToArray();
213+
195214
var exportedSegments = (await Task.WhenAll(exportTasks)).Where(file => !string.IsNullOrWhiteSpace(file)).ToArray();
196215

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

230249
public async Task<string> ExportSegmentAsync(string path, DriveSegment driveSegment, Camera camera, bool containerize,
231-
IProgress<int> progress = null)
250+
IProgress<Progress> progress = null)
232251
{
252+
var segmentProgress = new Progress(driveSegment.Index);
253+
233254
Video video = null;
234255
switch (camera.Type)
235256
{
@@ -284,7 +305,28 @@ public async Task<string> ExportSegmentAsync(string path, DriveSegment driveSegm
284305
await using (var stream = await sftpClient.OpenAsync(videoPath, FileMode.Open,
285306
FileAccess.Read, CancellationToken.None))
286307
{
287-
await stream.CopyToAsync(outputFile);
308+
var buffer = new byte[81920];
309+
int bytesRead = 0;
310+
var sourceLength = stream.Length;
311+
int totalBytesRead = 0;
312+
int previousProgress = 0;
313+
314+
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false)) > 0)
315+
{
316+
await outputFile.WriteAsync(buffer, 0, bytesRead).ConfigureAwait(false);
317+
318+
if (progress != null)
319+
{
320+
totalBytesRead += bytesRead;
321+
322+
segmentProgress.Percent = (int)(((double)totalBytesRead / (double)sourceLength) * 100);
323+
if (segmentProgress.Percent > previousProgress)
324+
{
325+
previousProgress = segmentProgress.Percent;
326+
progress.Report(segmentProgress);
327+
}
328+
}
329+
}
288330
}
289331
}
290332
}
@@ -304,7 +346,8 @@ await FFMpegArguments.FromFileInput(outputFilePath, true,
304346

305347
if (progress != null)
306348
{
307-
progress.Report(driveSegment.Index);
349+
segmentProgress.Percent = 100;
350+
progress.Report(segmentProgress);
308351
}
309352

310353
return fileName;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace OpenpilotSdk.OpenPilot.Camera
6+
{
7+
public class Progress
8+
{
9+
public Hardware.Camera Camera { get; }
10+
public int Percent { get; set; }
11+
12+
public Progress(Hardware.Camera camera)
13+
{
14+
Camera = camera;
15+
}
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace OpenpilotSdk.OpenPilot.Segment
6+
{
7+
public class Progress
8+
{
9+
public int Segment { get; }
10+
public int Percent { get; set; }
11+
12+
public Progress(int segment)
13+
{
14+
Segment = segment;
15+
}
16+
}
17+
}

OpenpilotSdk/OpenpilotSdk.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525
<ItemGroup>
2626
<Content Include="ffmpeg.exe">
2727
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
28+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
2829
</Content>
2930
<Content Include="ffprobe.exe">
3031
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
32+
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
3133
</Content>
3234
</ItemGroup>
3335

OpenpilotToolkit/OpenpilotToolkit.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<StartupObject>OpenpilotToolkit.Program</StartupObject>
1212
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
1313
<ApplicationManifest>app.manifest</ApplicationManifest>
14+
<SupportedOSPlatformVersion>7.0</SupportedOSPlatformVersion>
1415
</PropertyGroup>
1516

1617
<PropertyGroup Condition="'$(PlatformTarget)' == 'x86'">

OpenpilotToolkit/OpenpilotToolkitForm.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,21 +425,34 @@ private async void btnExport_Click(object sender, EventArgs e)
425425
continue;
426426
}
427427

428-
var ucDrive = new ucTaskProgress(drive.ToString(), drive.Segments.Count*cameras.Count)
428+
var ucDrive = new ucTaskProgress(drive.ToString(), cameras.Count * 100)
429429
{
430430
Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top)
431431
};
432-
432+
433433
tlpTasks.Controls.Add(ucDrive);
434434

435-
var segmentsProcessed = 0;
436-
437-
var progress = new Progress<int>((segmentIndex) =>
435+
var progressDictionary = cameras.ToDictionary(cam => cam, cam => 0);
436+
var progressLock = new SemaphoreSlim(1, 1);
437+
var progress = new Progress<OpenpilotSdk.OpenPilot.Camera.Progress>(async (cameraProgress) =>
438438
{
439439
if (!IsDisposed)
440440
{
441-
Interlocked.Increment(ref segmentsProcessed);
442-
ucDrive.Progress = segmentsProcessed;
441+
progressDictionary[cameraProgress.Camera] = cameraProgress.Percent;
442+
try
443+
{
444+
await progressLock.WaitAsync();
445+
446+
var currentProgress = progressDictionary.Sum(progress => progress.Value);
447+
if (currentProgress > ucDrive.Progress)
448+
{
449+
ucDrive.Progress = currentProgress;
450+
}
451+
}
452+
finally
453+
{
454+
progressLock.Release();
455+
}
443456
}
444457
});
445458

@@ -1552,6 +1565,7 @@ private async Task UploadFile(OpenpilotDevice openpilotDevice, string file, stri
15521565
var sourceLength = sourceFile.Length;
15531566
int totalBytesRead = 0;
15541567
int previousProgress = 0;
1568+
15551569
while ((bytesRead = await sourceFile.ReadAsync(buffer, 0, buffer.Length)) > 0)
15561570
{
15571571
await destinationFile.WriteAsync(buffer, 0, bytesRead);
@@ -1929,7 +1943,7 @@ private async void btnOsmUpload_Click(object sender, EventArgs e)
19291943
{
19301944
content.Add(new ByteArrayContent(binaryFile), "file", drive.ToString());
19311945
content.Add(new StringContent(drive.ToString() + " openpilot drive"), @"description");
1932-
content.Add(new StringContent("openpilot,commai,comma2"), @"tags");
1946+
content.Add(new StringContent("openpilottoolkit,optk,openpilot,commai,comma2,comma3"), @"tags");
19331947
content.Add(new StringContent("1"), @"public");
19341948
content.Add(new StringContent("identifiable"), @"visibility");
19351949

OpenpilotToolkit/Program.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ public static int Main(string[] args)
5050
CefSharp.Cef.Initialize(settings);
5151

5252
GlobalFFOptions.Configure(options => options.BinaryFolder = "./");
53+
54+
var logPath = Path.Combine(AppContext.BaseDirectory, @"logs\log.txt");
55+
5356
Log.Logger = new LoggerConfiguration()
5457
.MinimumLevel.Debug()
5558
.WriteTo.Console()
56-
.WriteTo.File("logs/log.txt", rollingInterval: RollingInterval.Day, shared: true)
59+
.WriteTo.File(logPath, rollingInterval: RollingInterval.Day, shared: true)
5760
.CreateLogger();
5861

5962
Application.ApplicationExit += (_, _) =>

0 commit comments

Comments
 (0)