From 4880943b95f6664ba0f7ee71f9ea21b6c9d013e6 Mon Sep 17 00:00:00 2001 From: ropiteauxi Date: Thu, 1 Jun 2023 17:43:15 +0200 Subject: [PATCH] Add on edge device screen and allow file download --- .../Devices/ConnectionStringDialog.razor | 30 ++++- .../EdgeDevices/ConnectionStringDialog.razor | 105 +++++++++++++----- .../Services/EdgeDeviceClientService.cs | 4 +- .../Services/IEdgeDeviceClientService.cs | 2 +- 4 files changed, 105 insertions(+), 36 deletions(-) diff --git a/src/AzureIoTHub.Portal.Client/Pages/Devices/ConnectionStringDialog.razor b/src/AzureIoTHub.Portal.Client/Pages/Devices/ConnectionStringDialog.razor index 8b7e45f04..6c08a1b12 100644 --- a/src/AzureIoTHub.Portal.Client/Pages/Devices/ConnectionStringDialog.razor +++ b/src/AzureIoTHub.Portal.Client/Pages/Devices/ConnectionStringDialog.razor @@ -1,8 +1,10 @@ @using AzureIoTHub.Portal.Models.v10 @using AzureIoTHub.Portal.Shared.Models.v10 +@using System.Text @inject ClipboardService ClipboardService @inject IDeviceClientService DeviceClientService +@inject IJSRuntime JS
@@ -38,15 +40,15 @@ { Certificate Pem - + Public Key - + Private Key - + } } @@ -83,5 +85,27 @@ } } + private async Task DownloadPemFile() + { + var stream = new DotNetStreamReference(stream: new MemoryStream( + Encoding.UTF8.GetBytes(credentials.CertificateCredentials.CertificatePem))); + await JS.InvokeVoidAsync("downloadFileFromStream", "certificate.pem", stream); + } + + private async Task DownloadPublicKeyFile() + { + var stream = new DotNetStreamReference(stream: new MemoryStream( + Encoding.UTF8.GetBytes(credentials.CertificateCredentials.PublicKey))); + await JS.InvokeVoidAsync("downloadFileFromStream", "key.pub", stream); + } + + private async Task DownloadPrivateKeyFile() + { + var stream = new DotNetStreamReference(stream: new MemoryStream( + Encoding.UTF8.GetBytes(credentials.CertificateCredentials.PrivateKey))); + await JS.InvokeVoidAsync("downloadFileFromStream", "key", stream); + } + + void Cancel() => MudDialog.Cancel(); } diff --git a/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/ConnectionStringDialog.razor b/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/ConnectionStringDialog.razor index dc3e2b363..668e82a70 100644 --- a/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/ConnectionStringDialog.razor +++ b/src/AzureIoTHub.Portal.Client/Pages/EdgeDevices/ConnectionStringDialog.razor @@ -1,6 +1,7 @@ -@using AzureIoTHub.Portal.Models.v10 -@using AzureIoTHub.Portal.Shared.Constants; - +@using AzureIoTHub.Portal.Shared.Models.v10 +@using AzureIoTHub.Portal.Models.v10 +@using System.Text +@inject IJSRuntime JS @inject ClipboardService ClipboardService @inject IEdgeDeviceClientService EdgeDeviceClientService @inject PortalSettings Portal @@ -10,52 +11,75 @@ - @if (Portal.CloudProvider.Equals(CloudProviders.Azure)) + @if (Credentials.AuthenticationMode != null && AuthenticationMode.SymmetricKey.Equals(Credentials.AuthenticationMode)) { - - Service Endpoint - - - - Registration Id - - - - Scope Id - - - - Symmetric Key - - + @if (Credentials.SymmetricCredentials != null) + { + + Service Endpoint + + + + Registration Id + + + + Scope Id + + + + Symmetric Key + + + } } - else if (Portal.CloudProvider.Equals(CloudProviders.AWS)) + else { + @if (Credentials.CertificateCredentials != null) + { + + Certificate Pem + + + + Public Key + + + + Private Key + + + } + +
+ OR +
+ @if (string.IsNullOrEmpty(EnrollementScriptCommand)) { Quick connect -
+
Quiclky connect your Edge device on the platform by executing one command. -
+
Get the magic command } else { -
+
Operating system - Debian 11 (Bulleseye) + Debian 11 (Bulleseye) -
+
- Copy this command line above and paste it into the device prompt.
+ Copy this command line above and paste it into the device prompt.
Note that you should have administrative rights on the device to execute the command.
-
- +
+ }
} @@ -76,7 +100,7 @@ [Parameter] public string deviceId { get; set; } = default!; - private SymmetricCredentials Credentials = new SymmetricCredentials(); + private DeviceCredentials Credentials = new DeviceCredentials(); public string EnrollementScriptCommand { get; set; } = default!; @@ -103,5 +127,26 @@ EnrollementScriptCommand = $"curl -s {url} | bash"; } + private async Task DownloadPemFile() + { + var stream = new DotNetStreamReference(stream: new MemoryStream( + Encoding.UTF8.GetBytes(Credentials.CertificateCredentials.CertificatePem))); + await JS.InvokeVoidAsync("downloadFileFromStream", "certificate.pem", stream); + } + + private async Task DownloadPublicKeyFile() + { + var stream = new DotNetStreamReference(stream: new MemoryStream( + Encoding.UTF8.GetBytes(Credentials.CertificateCredentials.PublicKey))); + await JS.InvokeVoidAsync("downloadFileFromStream", "key.pub", stream); + } + + private async Task DownloadPrivateKeyFile() + { + var stream = new DotNetStreamReference(stream: new MemoryStream( + Encoding.UTF8.GetBytes(Credentials.CertificateCredentials.PrivateKey))); + await JS.InvokeVoidAsync("downloadFileFromStream", "key", stream); + } + void Close() => MudDialog.Cancel(); } diff --git a/src/AzureIoTHub.Portal.Client/Services/EdgeDeviceClientService.cs b/src/AzureIoTHub.Portal.Client/Services/EdgeDeviceClientService.cs index 8fd6146f7..18adca77a 100644 --- a/src/AzureIoTHub.Portal.Client/Services/EdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal.Client/Services/EdgeDeviceClientService.cs @@ -44,9 +44,9 @@ public Task DeleteDevice(string deviceId) return this.http.DeleteAsync($"api/edge/devices/{deviceId}"); } - public Task GetEnrollmentCredentials(string deviceId) + public Task GetEnrollmentCredentials(string deviceId) { - return this.http.GetFromJsonAsync($"api/edge/devices/{deviceId}/credentials")!; + return this.http.GetFromJsonAsync($"api/edge/devices/{deviceId}/credentials")!; } public Task GetEnrollmentScriptUrl(string deviceId, string templateName) diff --git a/src/AzureIoTHub.Portal.Client/Services/IEdgeDeviceClientService.cs b/src/AzureIoTHub.Portal.Client/Services/IEdgeDeviceClientService.cs index 8167a37dd..c2788a763 100644 --- a/src/AzureIoTHub.Portal.Client/Services/IEdgeDeviceClientService.cs +++ b/src/AzureIoTHub.Portal.Client/Services/IEdgeDeviceClientService.cs @@ -20,7 +20,7 @@ public interface IEdgeDeviceClientService Task DeleteDevice(string deviceId); - Task GetEnrollmentCredentials(string deviceId); + Task GetEnrollmentCredentials(string deviceId); Task GetEnrollmentScriptUrl(string deviceId, string templateName);