Skip to content

Commit 3868a9b

Browse files
committed
Drivers: Added fw upload to server api and WASM app
1 parent cf56be2 commit 3868a9b

File tree

7 files changed

+188
-5
lines changed

7 files changed

+188
-5
lines changed

hio-dotnet.Demos.BlazorComponents.RadzenLib.WASM/Pages/Home.razor

+28
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@
5353
}
5454
<RadzenButton Click="OpenAutomatedCommandsWindow" Style="margin-top: 5px; width: 100%;">Automated Commands</RadzenButton>
5555

56+
<RadzenButton Click="LoadFirmware" Style="margin-top: 5px; width: 100%;">Load Firmware</RadzenButton>
57+
5658
<RadzenRow Style="margin-top: 5px; width: 100%;">
5759
<RadzenColumn>
5860
<RadzenStack Orientation="Orientation.Horizontal">
@@ -450,4 +452,30 @@
450452
, new DialogOptions() { Resizable = false, Style = "width:40%;" });
451453

452454
}
455+
456+
private async Task LoadFirmwareExec(string hash, string filename, DialogService ds)
457+
{
458+
ds.Close(true);
459+
await Task.Delay(1);
460+
await ConsoleService.LoadFirmware(hash, filename);
461+
}
462+
463+
private async Task LoadFirmware()
464+
{
465+
var result = await DialogService.OpenAsync("Automated Commands", ds =>
466+
@<RadzenStack Gap="1.5rem">
467+
<RadzenRow>
468+
<RadzenColumn>
469+
<hio_dotnet.UI.BlazorComponents.RadzenLib.CHESTER.Firmware.FirmwareLoader OnRemoteFirmwareLoadedRequested="@(async (h) => await LoadFirmwareExec(h,"", ds))" OnLocalFirmwareLoadedRequested="@(async (f) => await LoadFirmwareExec("",f, ds))" />
470+
</RadzenColumn>
471+
</RadzenRow>
472+
<RadzenStack Orientation="Orientation.Horizontal" Gap="0.5rem" AlignItems="AlignItems.Center" JustifyContent="JustifyContent.SpaceBetween">
473+
<RadzenStack Orientation="Orientation.Horizontal">
474+
<RadzenButton Text="Close" Click="() => ds.Close(false)" ButtonStyle="ButtonStyle.Light" />
475+
</RadzenStack>
476+
</RadzenStack>
477+
</RadzenStack>
478+
, new DialogOptions() { Resizable = false, Style = "width:40%;" });
479+
480+
}
453481
}

hio-dotnet.Demos.BlazorComponents.RadzenLib.WASM/Services/RemoteConsoleService.cs

+26
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,32 @@ public async Task LoadCommandsFromDevice(string parent = "")
656656
}
657657
}
658658

659+
public async Task LoadFirmware(string hash = "", string filename = "")
660+
{
661+
try
662+
{
663+
var res = await driversWebSocketClient.JLink_UploadFirmware(hash, filename);
664+
if (!string.IsNullOrEmpty(res))
665+
{
666+
if (res.Replace("\"", "").ToLower() == "ok")
667+
{
668+
IsConsoleListening = true;
669+
OnIsBusy?.Invoke(this, false);
670+
await Task.Delay(10);
671+
OnIsJLinkConnected?.Invoke(this, true);
672+
ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "Firmware Loaded", Detail = "Firmware has been loaded to the device.", Duration = 3000 });
673+
}
674+
else
675+
{
676+
ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "Firmware Load Error", Detail = res, Duration = 3000 });
677+
}
678+
}
679+
}
680+
catch (Exception ex)
681+
{
682+
Console.WriteLine($"Error loading firmware: {ex.Message}");
683+
}
684+
}
659685

660686
public async Task Dispose()
661687
{

hio-dotnet.Demos.HardwarioMonitor/Services/ConsoleService.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -690,10 +690,19 @@ public async Task LoadFirmware(string hash = "", string filename = "")
690690

691691
if (!string.IsNullOrEmpty(hash) && string.IsNullOrEmpty(filename))
692692
{
693-
//var url = $"https://firmware.hardwario.com/chester/{hash}/hex";
694-
filename = $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\{hash}.hex";
695-
await HioFirmwareDownloader.DownloadFirmwareByHashAsync(hash, filename);
696-
ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "Firmware Downloaded", Detail = "Firmware has been downloaded.", Duration = 3000 });
693+
try
694+
{
695+
//var url = $"https://firmware.hardwario.com/chester/{hash}/hex";
696+
filename = $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\{hash}.hex";
697+
await HioFirmwareDownloader.DownloadFirmwareByHashAsync(hash, filename);
698+
ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Success, Summary = "Firmware Downloaded", Detail = "Firmware has been downloaded.", Duration = 3000 });
699+
}
700+
catch (Exception ex)
701+
{
702+
ShowNotification(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "Error", Detail = $"Error while downloading firmware: {ex.Message}", Duration = 3000 });
703+
OnIsBusy?.Invoke(this, false);
704+
return;
705+
}
697706
}
698707
if (MCUConsole != null && IsConsoleListening)
699708
{

hio-dotnet.HWDrivers/Server/DriversApiControler.cs

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ public async Task<string> JLink_LoadAllCommandsFromHelp(int channel, string pare
110110
return await DriversServerMainDataContext.DriversCommonController.JLink_LoadAllCommandsFromHelp(channel, parent);
111111
}
112112

113+
// GET: /api/jlink/uploadfirmwarebyhash/{hash}
114+
[Route(HttpVerbs.Get, "/jlink/uploadfirmwarebyhash/{hash}")]
115+
public async Task<string> JLink_UploadFirmwareByHash(string hash)
116+
{
117+
return await DriversServerMainDataContext.DriversCommonController.JLink_LoadFirmware(hash, "");
118+
}
119+
120+
// GET: /api/jlink/uploadfirmwarebyfilename/{filename}
121+
[Route(HttpVerbs.Get, "/jlink/uploadfirmwarebyfilename/{filename}")]
122+
public async Task<string> JLink_UploadFirmwareByFilename(string filename)
123+
{
124+
filename = System.Net.WebUtility.UrlDecode(filename);
125+
return await DriversServerMainDataContext.DriversCommonController.JLink_LoadFirmware("",filename);
126+
}
127+
113128
// POST: /api/upload
114129
[Route(HttpVerbs.Post, "/upload")]
115130
public async Task<string> UploadFile()

hio-dotnet.HWDrivers/Server/DriversCommonController.cs

+76
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Linq;
1111
using System.Text;
1212
using System.Threading.Tasks;
13+
using hio_dotnet.APIs.HioCloud;
1314

1415
namespace hio_dotnet.HWDrivers.Server
1516
{
@@ -290,5 +291,80 @@ public async Task<string> JLink_LoadAllCommandsFromHelp(int channel = 0, string
290291
return "OK";
291292
}
292293

294+
public async Task<string> JLink_LoadFirmware(string hash = "", string filename = "")
295+
{
296+
if (string.IsNullOrEmpty(hash) && string.IsNullOrEmpty(filename))
297+
{
298+
return "Firmware hash and filename are empty. Fill at least one";
299+
}
300+
301+
if (!string.IsNullOrEmpty(hash) && string.IsNullOrEmpty(filename))
302+
{
303+
try
304+
{
305+
//var url = $"https://firmware.hardwario.com/chester/{hash}/hex";
306+
filename = $"{Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}\\{hash}.hex";
307+
await HioFirmwareDownloader.DownloadFirmwareByHashAsync(hash, filename);
308+
}
309+
catch (Exception ex)
310+
{
311+
return $"Error while downloading firmware: {ex.Message}";
312+
}
313+
}
314+
if (DriversServerMainDataContext.MCUMultiRTTConsole != null && DriversServerMainDataContext.MCUMultiRTTConsole.IsListening)
315+
{
316+
DriversServerMainDataContext.cts.Cancel();
317+
await Task.Delay(100);
318+
DriversServerMainDataContext.MCUMultiRTTConsole.ReconnectJLink();
319+
//MCUConsole.CloseAll();
320+
//IsConsoleListening = false;
321+
//OnIsJLinkConnected?.Invoke(this, false);
322+
await Task.Delay(150);
323+
324+
try
325+
{
326+
var res = DriversServerMainDataContext.MCUMultiRTTConsole.LoadFirmware("ConfigConsole", filename);
327+
if (res)
328+
{
329+
Console.WriteLine("Waiting 10 seconds after reboot of MCU");
330+
await Task.Delay(10000);
331+
332+
DriversServerMainDataContext.MCUMultiRTTConsole.ReconnectJLink();
333+
DriversServerMainDataContext.cts = new CancellationTokenSource();
334+
335+
try
336+
{
337+
DriversServerMainDataContext.cts = new CancellationTokenSource();
338+
Task listeningTask = DriversServerMainDataContext.MCUMultiRTTConsole.StartListening(DriversServerMainDataContext.cts.Token);
339+
340+
DriversServerMainDataContext.IsConsoleListening = true;
341+
342+
//await Task.WhenAny(new Task[] { listeningTask });
343+
DriversServerMainDataContext.JLinkTaskQueue.Enqueue(listeningTask);
344+
await Task.Delay(3500);
345+
}
346+
catch (Exception ex)
347+
{
348+
Console.WriteLine($"Error while starting listening task: {ex.Message}");
349+
return "Error while starting listening task.";
350+
}
351+
return "OK";
352+
}
353+
else
354+
{
355+
return "Error while loading firmware.";
356+
}
357+
}
358+
catch (Exception ex)
359+
{
360+
return "Error while loading firmware.";
361+
}
362+
}
363+
else
364+
{
365+
return "Console is not listening. Cannot load firmware.";
366+
}
367+
}
368+
293369
}
294370
}

hio-dotnet.HWDrivers/Server/DriversWebSocketClient.cs

+29-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,35 @@ public async Task<string> JLink_LoadAllCommandsFromHelp(int channel, string pare
322322
{
323323
timeout = 80000;
324324
}
325-
var response = await SendApiRequest(request);
325+
var response = await SendApiRequest(request, timeout);
326+
return response;
327+
}
328+
329+
330+
public async Task<string> JLink_UploadFirmware(string hash, string filename)
331+
{
332+
var request = new DriversWebSocketRequest();
333+
var timeout = 60000;
334+
if (filename == "")
335+
{
336+
timeout = 120000;
337+
}
338+
339+
if (string.IsNullOrEmpty(hash) && !string.IsNullOrEmpty(filename))
340+
{
341+
342+
var u = $"/api/jlink/uploadfirmwarebyfilename/{filename}";
343+
request.Message = u;
344+
request.Id = Guid.NewGuid();
345+
}
346+
else if (!string.IsNullOrEmpty(hash) && string.IsNullOrEmpty(filename))
347+
{
348+
349+
var u = $"/api/jlink/uploadfirmwarebyhash/{hash}";
350+
request.Message = u;
351+
request.Id = Guid.NewGuid();
352+
}
353+
var response = await SendApiRequest(request, timeout);
326354
return response;
327355
}
328356
}

hio-dotnet.HWDrivers/hio-dotnet.HWDrivers.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
</ItemGroup>
7676

7777
<ItemGroup>
78+
<ProjectReference Include="..\hio-dotnet.APIs.HioCloudv2\hio-dotnet.APIs.HioCloud.csproj" />
7879
<ProjectReference Include="..\hio-dotnet.Common\hio-dotnet.Common.csproj" />
7980
</ItemGroup>
8081

0 commit comments

Comments
 (0)