Skip to content

Commit

Permalink
Allow Update Progress to pass back the percentage too.
Browse files Browse the repository at this point in the history
  • Loading branch information
CartBlanche committed Jul 2, 2024
1 parent 4e056e6 commit f45c848
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class FirmwareUpdater<T> where T : BaseDeviceCommand<T>

private BaseDeviceCommand<T> command;

public event EventHandler<string> UpdateProgress = default!;
public event EventHandler<(string message, double percentage)> UpdateProgress = default!;

public FirmwareUpdater(BaseDeviceCommand<T> command, ISettingsManager settings, FileManager fileManager, MeadowConnectionManager connectionManager, string? individualFile, FirmwareType[]? firmwareFileTypes, bool useDfu, string? osVersion, string? serialNumber, ILogger? logger, CancellationToken cancellationToken)
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public async Task<bool> UpdateFirmware()

if (firmwareFileTypes.Contains(FirmwareType.OS))
{
UpdateProgress?.Invoke(this, "Flashing OS");
UpdateProgress?.Invoke(this, ("Flashing OS", 20));
await WriteOSFiles(connection, deviceInfo, package, useDfu);
}

Expand All @@ -108,7 +108,7 @@ public async Task<bool> UpdateFirmware()

if (firmwareFileTypes.Contains(FirmwareType.Runtime) || Path.GetFileName(individualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.RuntimeFile)
{
UpdateProgress?.Invoke(this, "Writing Runtime");
UpdateProgress?.Invoke(this, ("Writing Runtime", 40));
await WriteRuntimeFiles(connection, deviceInfo, package, individualFile);
}

Expand All @@ -117,7 +117,7 @@ public async Task<bool> UpdateFirmware()
|| Path.GetFileName(individualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocApplicationFile
|| Path.GetFileName(individualFile) == F7FirmwarePackageCollection.F7FirmwareFiles.CoprocBootloaderFile)
{
UpdateProgress?.Invoke(this, "Writing ESP");
UpdateProgress?.Invoke(this, ("Writing ESP", 60));
await WriteEspFiles(connection, deviceInfo, package);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Concurrent;
using System.Threading.Tasks;
using CliFx.Attributes;
using Meadow.CLI.Commands.DeviceManagement;
using Meadow.Cloud.Client;
Expand Down Expand Up @@ -235,16 +236,16 @@ await AnsiConsole.Progress()
{
foreach (var deviceSerialNumber in selectedDeviceList)
{
var formatedDevice = $"[green]{deviceSerialNumber}[/]";
var task = ctx.AddTask(formatedDevice, maxValue: 100);

try
{
var formatedDevice = $"[green]{deviceSerialNumber}[/]";
var task = ctx.AddTask(formatedDevice, maxValue: 100);

var firmareUpdater = new FirmwareUpdater<ProvisionCommand>(this, settingsManager, fileManager, this.connectionManager, null, null, true, OsVersion, deviceSerialNumber, null, CancellationToken);
firmareUpdater.UpdateProgress += (o, e) =>
{
task.Increment(20.00);
task.Description = $"{formatedDevice}: {e}";
task.Value = e.percentage;
task.Description = $"{formatedDevice}: {e.message}";
};

if (!await firmareUpdater.UpdateFirmware())
Expand Down Expand Up @@ -280,7 +281,11 @@ await AnsiConsole.Progress()
}
catch (Exception ex)
{
task.Description = $"{formatedDevice}: [red]{ex.Message}[/]";
task.StopTask();
#if DEBUG
AnsiConsole.MarkupLine($"[red]{ex.Message}[/]{Environment.NewLine}{ex.StackTrace}");
#endif
}
}
});
Expand Down

0 comments on commit f45c848

Please sign in to comment.