Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change GatewayID field to list in device creation/edition pages #1376

Merged
merged 15 commits into from
Oct 21, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.Devices
{
using System;
using AzureIoTHub.Portal.Client.Pages.Devices.LoRaWAN;
using AzureIoTHub.Portal.Client.Services;
using AzureIoTHub.Portal.Client.Validators;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using Bunit;
using Microsoft.Extensions.DependencyInjection;
using Moq;
using MudBlazor.Services;
using NUnit.Framework;
using UnitTests.Bases;
Expand All @@ -17,10 +20,14 @@ namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.Devices
[TestFixture]
public class CreateLoraDeviceTests : BlazorUnitTest
{
private Mock<ILoRaWanDeviceClientService> mockLoRaWanDeviceClientService;

public override void Setup()
{
base.Setup();
this.mockLoRaWanDeviceClientService = MockRepository.Create<ILoRaWanDeviceClientService>();

_ = Services.AddSingleton(this.mockLoRaWanDeviceClientService.Object);

Services.Add(new ServiceDescriptor(typeof(IResizeObserver), new MockResizeObserver()));
}
Expand All @@ -44,6 +51,9 @@ public void WhenUseOTAAShouldDisplayOTAATextboxes()

var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act

var cut = RenderComponent<CreateLoraDevice>(
Expand Down Expand Up @@ -76,6 +86,9 @@ public void WhenNotUseOTAAShouldDisplayABPTextboxes()
};
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<CreateLoraDevice>(
ComponentParameter.CreateParameter(nameof(CreateLoraDevice.LoRaDevice), deviceDetails),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Client.Pages.Devices
using UnitTests.Mocks;
using System.Linq;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Shared.Models.v1._0;

[TestFixture]
public class EditLoraDeviceTests : BlazorUnitTest
Expand Down Expand Up @@ -58,6 +59,9 @@ public void WhenUseOTAAShouldDisplayOTAATextboxes()

var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
ComponentParameter.CreateParameter(nameof(EditLoraDevice.LoRaDevice), deviceDetails),
Expand Down Expand Up @@ -89,6 +93,9 @@ public void WhenNotUseOTAAShouldDisplayABPTextboxes()
};
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
ComponentParameter.CreateParameter(nameof(EditLoraDevice.LoRaDevice), deviceDetails),
Expand Down Expand Up @@ -122,6 +129,9 @@ public void EditLoRaDevicePageShouldBeRenderedProperly()
};
var validator = new LoRaDeviceDetailsValidator();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
ComponentParameter.CreateParameter(nameof(EditLoraDevice.LoRaDevice), deviceDetails),
Expand Down Expand Up @@ -153,6 +163,9 @@ public void WhenDeviceNeverConnectedCommandsShouldBeDisabled()
ModelId = model.ModelId
};

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
ComponentParameter.CreateParameter(nameof(EditLoraDevice.LoRaDevice), device),
Expand Down Expand Up @@ -187,6 +200,9 @@ public void WhenDeviceAlteadyConnectedCommandsShouldBeEnabled()
AlreadyLoggedInOnce = true
};

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
ComponentParameter.CreateParameter(nameof(EditLoraDevice.LoRaDevice), device),
Expand Down Expand Up @@ -226,6 +242,9 @@ public void WhenClickToSendCommandShouldExecuteCommandToService()
_ = this.mockSnackbarService.Setup(c => c.Add(It.IsAny<string>(), Severity.Success, It.IsAny<Action<SnackbarOptions>>()))
.Returns((Snackbar)null);

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
ComponentParameter.CreateParameter(nameof(EditLoraDevice.LoRaDevice), device),
Expand Down Expand Up @@ -261,6 +280,8 @@ public void EditLoRaDeviceDetailPageShouldBeRenderedProperly()
var validator = new LoRaDeviceDetailsValidator();
var LoraDevice = new LoRaDeviceDetails();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
Expand Down Expand Up @@ -295,6 +316,8 @@ public void EditLoRaDeviceDetailPageWithReportedProperties()
var validator = new LoRaDeviceDetailsValidator();
var LoraDevice = new LoRaDeviceDetails();

_ = this.mockLoRaWanDeviceClientService.Setup(c => c.GetGatewayIdList())
.ReturnsAsync(new LoRaGatewayIDList());

// Act
var cut = RenderComponent<EditLoraDevice>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ public void SetUp()

private LoRaWANDevicesController CreateLoRaWANDevicesController()
{
var loRaGatewayIDList = new LoRaGatewayIDList
{
GatewayIds = new List<string>(){ Guid.NewGuid().ToString(), Guid.NewGuid().ToString() }
};

return new LoRaWANDevicesController(
this.mockLogger.Object,
this.mockLoRaWANCommandService.Object,
this.mockDeviceService.Object)
this.mockDeviceService.Object,
loRaGatewayIDList)
{
Url = this.mockUrlHelper.Object
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
@using AzureIoTHub.Portal.Client.Validators
@using AzureIoTHub.Portal.Models.v10.LoRaWAN
@using AzureIoTHub.Portal.Models.v10
@inject ILoRaWanDeviceClientService LoRaWanDeviceClientService

<MudExpansionPanels MultiExpansion="true">
<MudGrid>
Expand Down Expand Up @@ -40,6 +41,22 @@
<MudTextField id="@nameof(LoRaDevice.DevAddr)" @bind-Value="@LoRaDevice.DevAddr" Label="DevAddr" For="@(() => LoRaDevice.DevAddr)" Variant="Variant.Outlined" Required="true"></MudTextField>
</MudItem>
}
<MudItem xs="12" md="6">
<MudAutocomplete T="string"
id="@nameof(LoRaDevice.GatewayID)"
@bind-Value="LoRaDevice.GatewayID"
SearchFunc="@SearchGatewayID"
Label="GatewayID"
Dense=true
For="@(() => LoRaDevice.GatewayID)"
Variant="Variant.Outlined"
ResetValueOnEmptyText=true
Immediate=true
Clearable=true
CoerceText=true
CoerceValue=false>
</MudAutocomplete>
</MudItem>
</MudGrid>
</ChildContent>
</MudExpansionPanel>
Expand All @@ -48,6 +65,9 @@
</MudExpansionPanels>

@code {
[CascadingParameter]
public Error Error { get; set; }

[Parameter]
public LoRaDeviceDetails LoRaDevice { get; set; }

Expand All @@ -57,4 +77,27 @@
[Parameter]
public LoRaDeviceModelDto LoraModelDto { get; set; }

private List<string> GatewayIdList = new();

protected override async Task OnInitializedAsync()
{
try
{
GatewayIdList = (await LoRaWanDeviceClientService.GetGatewayIdList()).GatewayIds;
}
catch (ProblemDetailsException exception)
{
Error?.ProcessProblemDetails(exception);
}
}

private async Task<IEnumerable<string>> SearchGatewayID(string value)
{
// if text is null or empty, show complete list
if (string.IsNullOrEmpty(value))
return GatewayIdList;

return GatewayIdList
.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</MudItem>
<MudItem md="3" xs="6">
<MudSelect id="@nameof(LoRaDevice.Deduplication)"
Disabled="true"
Disabled="true"
@bind-Value="@LoRaDevice.Deduplication"
For="@(() => LoRaDevice.Deduplication)"
AnchorOrigin="Origin.BottomCenter"
Expand All @@ -66,8 +66,21 @@
}
</MudSelect>
</MudItem>
<MudItem md="6" xs="12">
<MudTextField id="@nameof(LoRaDevice.GatewayID)" @bind-Value="@LoRaDevice.GatewayID" Label="Gateway Id" For="@(() => LoRaDevice.GatewayID)" Variant="Variant.Outlined"></MudTextField>
<MudItem xs="12" md="6">
<MudAutocomplete T="string"
id="@nameof(LoRaDevice.GatewayID)"
@bind-Value="LoRaDevice.GatewayID"
SearchFunc="@SearchGatewayID"
Label="GatewayID"
Dense=true
For="@(() => LoRaDevice.GatewayID)"
Variant="Variant.Outlined"
ResetValueOnEmptyText=true
Immediate=true
Clearable=true
CoerceText=true
CoerceValue=false>
</MudAutocomplete>
</MudItem>
</MudGrid>
</MudItem>
Expand Down Expand Up @@ -157,8 +170,8 @@

@code {
[CascadingParameter]
public Error Error {get; set;}
public Error Error { get; set; }

[Parameter]
public LoRaDeviceDetails LoRaDevice { get; set; }

Expand All @@ -173,6 +186,30 @@

private bool isProcessing;

private List<string> GatewayIdList = new();

protected override async Task OnInitializedAsync()
{
try
{
GatewayIdList = (await LoRaWanDeviceClientService.GetGatewayIdList()).GatewayIds;
}
catch (ProblemDetailsException exception)
{
Error?.ProcessProblemDetails(exception);
}
}

private async Task<IEnumerable<string>> SearchGatewayID(string value)
{
// if text is null or empty, show complete list
if (string.IsNullOrEmpty(value))
return GatewayIdList;

return GatewayIdList
.Where(x => x.Contains(value, StringComparison.InvariantCultureIgnoreCase));
}

private async Task ExecuteMethod(DeviceModelCommandDto method)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace AzureIoTHub.Portal.Client.Services
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Models.v10.LoRaWAN;
using AzureIoTHub.Portal.Shared.Models.v1._0;

public interface ILoRaWanDeviceClientService
{
Expand All @@ -15,5 +16,7 @@ public interface ILoRaWanDeviceClientService
Task UpdateDevice(LoRaDeviceDetails device);

Task ExecuteCommand(string deviceId, string commandId);

Task<LoRaGatewayIDList> GetGatewayIdList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@

namespace AzureIoTHub.Portal.Client.Services
{
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using Portal.Models.v10.LoRaWAN;

public class LoRaWanDeviceClientService : ILoRaWanDeviceClientService
Expand Down Expand Up @@ -36,5 +38,10 @@ public Task ExecuteCommand(string deviceId, string commandId)
{
return this.http.PostAsJsonAsync($"api/lorawan/devices/{deviceId}/_command/{commandId}", string.Empty);
}

public Task<LoRaGatewayIDList> GetGatewayIdList()
{
return this.http.GetFromJsonAsync<LoRaGatewayIDList>($"api/lorawan/devices/gateways");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace AzureIoTHub.Portal.Server.Controllers.V10
{
using System.Threading.Tasks;
using AzureIoTHub.Portal.Shared.Models.v1._0;
using Filters;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -21,14 +22,17 @@ namespace AzureIoTHub.Portal.Server.Controllers.V10
public class LoRaWANDevicesController : DevicesControllerBase<LoRaDeviceDetails>
{
private readonly ILoRaWANCommandService loRaWanCommandService;
private readonly LoRaGatewayIDList gatewayIdList;

public LoRaWANDevicesController(
ILogger<LoRaWANDevicesController> logger,
ILoRaWANCommandService loRaWanCommandService,
IDeviceService<LoRaDeviceDetails> deviceService)
IDeviceService<LoRaDeviceDetails> deviceService,
LoRaGatewayIDList gatewayIdList)
: base(logger, deviceService)
{
this.loRaWanCommandService = loRaWanCommandService;
this.gatewayIdList = gatewayIdList;
}

/// <summary>
Expand Down Expand Up @@ -112,5 +116,11 @@ public override Task<ActionResult<EnrollmentCredentials>> GetCredentials(string
{
return Task.FromResult<ActionResult<EnrollmentCredentials>>(NotFound());
}

[HttpGet("gateways", Name = "Get Gateways")]
public ActionResult<LoRaGatewayIDList> GetGateways()
{
return this.gatewayIdList;
}
}
}
Loading