From 1102df8194b7b68fc8106987698cecb40ed9056c Mon Sep 17 00:00:00 2001
From: Kevin BEAUGRAND <9513635+kbeaugrand@users.noreply.github.com>
Date: Mon, 28 Feb 2022 19:26:06 +0100
Subject: [PATCH] Add DPS connection on device detail page if not lora (#357)
---
.../Devices/ConnectionStringDialog.razor | 80 +++++++++++++++++++
.../Pages/Devices/DeviceDetailPage.razor | 22 ++++-
.../Edge_Devices/EdgeDeviceDetailPage.razor | 10 +--
.../DeviceProvisioningServiceManager.cs | 5 ++
4 files changed, 110 insertions(+), 7 deletions(-)
create mode 100644 src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor
diff --git a/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor b/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor
new file mode 100644
index 000000000..6eef4422e
--- /dev/null
+++ b/src/AzureIoTHub.Portal/Client/Pages/Devices/ConnectionStringDialog.razor
@@ -0,0 +1,80 @@
+@using Microsoft.AspNetCore.Authorization
+@using AzureIoTHub.Portal.Shared.Models.V10
+
+@inject HttpClient Http
+@inject ISnackbar Snackbar
+
+
+
+
+ @if (loading)
+ {
+
+
+
+ }
+ else
+ {
+
+
+
+
+ Service Endpoint
+
+
+
+ Registation Scope ID
+
+
+
+ Device Id
+
+
+
+ Symmetric Key
+
+
+
+
+
+ }
+
+
+ Cancel
+
+
+
+
+@code {
+ [CascadingParameter] MudDialogInstance MudDialog { get; set; }
+ [Parameter] public string deviceId { get; set; }
+ private EnrollmentCredentials Credentials;
+ private bool loading = true;
+
+ protected override async Task OnInitializedAsync()
+ {
+ try
+ {
+ await base.OnInitializedAsync();
+
+ Credentials = await this.Http.GetFromJsonAsync($"api/devices/{deviceId}/credentials");
+ loading = false;
+
+ }
+ catch (HttpRequestException e)
+ {
+ if (e.StatusCode == System.Net.HttpStatusCode.NotFound)
+ {
+ Snackbar.Add("Cannot obtain the connection string
because the enrollment group does not exist.", Severity.Error);
+ }
+ else
+ {
+ Snackbar.Add($"Something went wrong.", Severity.Error);
+ }
+
+ MudDialog.Close();
+ }
+ }
+
+ void Cancel() => MudDialog.Cancel();
+}
diff --git a/src/AzureIoTHub.Portal/Client/Pages/Devices/DeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/Devices/DeviceDetailPage.razor
index 3caa6bae6..ecc02dd18 100644
--- a/src/AzureIoTHub.Portal/Client/Pages/Devices/DeviceDetailPage.razor
+++ b/src/AzureIoTHub.Portal/Client/Pages/Devices/DeviceDetailPage.razor
@@ -12,6 +12,7 @@
@inject HttpClient Http
@inject ISnackbar Snackbar
@inject NavigationManager NavManager
+@inject IDialogService DialogService
Device Details
@@ -22,8 +23,14 @@
- @Device.DeviceID
+ @Device.DeviceID
+
+ @if (isLoaded && (!IsLoRa || !(Device is LoRaDeviceDetails)))
+ {
+ Connect
+ }
+
@@ -117,6 +124,8 @@
private DeviceDetails Device { get; set; } = new DeviceDetails();
+ private bool isLoaded = false;
+
private IEnumerable
DeviceModelList { get; set; } = new List();
private IEnumerable Commands { get; set; }
@@ -132,6 +141,8 @@
{
Device = await Http.GetFromJsonAsync($"{ApiUrlBase}/{DeviceID}");
}
+
+ isLoaded = true;
}
///
@@ -184,4 +195,13 @@
return DeviceModelList
.Where(x => x.Name.StartsWith(value, StringComparison.InvariantCultureIgnoreCase));
}
+
+ public void ShowConnectionString()
+ {
+ var parameters = new DialogParameters();
+ parameters.Add(nameof(ConnectionStringDialog.deviceId), this.DeviceID);
+
+ DialogService.Show("Device Credentials", parameters);
+ }
+
}
\ No newline at end of file
diff --git a/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/EdgeDeviceDetailPage.razor b/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/EdgeDeviceDetailPage.razor
index a826462ad..c3403fef5 100644
--- a/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/EdgeDeviceDetailPage.razor
+++ b/src/AzureIoTHub.Portal/Client/Pages/Edge_Devices/EdgeDeviceDetailPage.razor
@@ -28,14 +28,12 @@
- @Gateway.DeviceId
+ @Gateway.DeviceId
+
+ Connect
+
-
-
- Connect
-
-
diff --git a/src/AzureIoTHub.Portal/Server/Managers/DeviceProvisioningServiceManager.cs b/src/AzureIoTHub.Portal/Server/Managers/DeviceProvisioningServiceManager.cs
index 49c7491b1..0f2bb39bf 100644
--- a/src/AzureIoTHub.Portal/Server/Managers/DeviceProvisioningServiceManager.cs
+++ b/src/AzureIoTHub.Portal/Server/Managers/DeviceProvisioningServiceManager.cs
@@ -145,6 +145,11 @@ private static string GenerateKey()
private static string ComputeEnrollmentGroupName(string deviceType)
{
+ if (string.IsNullOrEmpty(deviceType))
+ {
+ return "default";
+ }
+
return deviceType.Trim()
.ToLowerInvariant()
.Replace(" ", "-");