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

Bug Fix: Executing command to lorawan device should not change the path #442

Merged
merged 3 commits into from
Mar 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -119,8 +119,22 @@ public async Task ExecuteCommandStateUnderTestExpectedBehavior()
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()));

_ = this.mockDeviceService.Setup(c => c.GetDeviceTwin(It.Is<string>(x => x == deviceId)))
.ReturnsAsync(new Twin()
{
DeviceId = deviceId,
ModelId = modelId,
});

_ = this.mockDeviceTwinMapper.Setup(c => c.CreateDeviceDetails(It.IsAny<Twin>(), It.IsAny<IEnumerable<string>>()))
.Returns<Twin, IEnumerable<string>>((x, y) => new LoRaDeviceDetails
{
DeviceID = deviceId,
ModelId = modelId,
});

// Act
var result = await loRaWANDevicesController.ExecuteCommand(modelId, deviceId, commandId);
var result = await loRaWANDevicesController.ExecuteCommand(deviceId, commandId);

// Assert
Assert.IsNotNull(result);
Expand Down Expand Up @@ -176,8 +190,22 @@ public async Task ExecuteCommandFailedShouldReturnHttp400()
It.IsAny<Exception>(),
It.IsAny<Func<It.IsAnyType, Exception?, string>>()));

_ = this.mockDeviceService.Setup(c => c.GetDeviceTwin(It.Is<string>(x => x == deviceId)))
.ReturnsAsync(new Twin()
{
DeviceId = deviceId,
ModelId = modelId,
});

_ = this.mockDeviceTwinMapper.Setup(c => c.CreateDeviceDetails(It.IsAny<Twin>(), It.IsAny<IEnumerable<string>>()))
.Returns<Twin, IEnumerable<string>>((x, y) => new LoRaDeviceDetails
{
DeviceID = deviceId,
ModelId = modelId,
});

// Act
var result = await loRaWANDevicesController.ExecuteCommand(modelId, deviceId, commandId);
var result = await loRaWANDevicesController.ExecuteCommand(deviceId, commandId);

// Assert
Assert.IsNotNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@

private async Task ExecuteMethod(DeviceModelCommand method)
{
var result = await Http.PostAsJsonAsync($"api/lorawan/devices/{LoRaDevice.ModelId}/{LoRaDevice.DeviceID}/_command/{method.Name}", method);
var result = await Http.PostAsJsonAsync($"api/lorawan/devices/{LoRaDevice.DeviceID}/_command/{method.Name}", method);

if (result.IsSuccessStatusCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class LoRaWANDevicesController : DevicesControllerBase<DeviceListItem, Lo
private readonly ITableClientFactory tableClientFactory;
private readonly ILoraDeviceMethodManager loraDeviceMethodManager;
private readonly IDeviceModelCommandMapper deviceModelCommandMapper;
private readonly IDeviceService deviceService;
private readonly IDeviceTwinMapper<DeviceListItem, LoRaDeviceDetails> deviceTwinMapper;

public LoRaWANDevicesController(
ILogger<LoRaWANDevicesController> logger,
Expand All @@ -43,6 +45,8 @@ public LoRaWANDevicesController(
this.tableClientFactory = tableClientFactory;
this.loraDeviceMethodManager = loraDeviceMethodManager;
this.deviceModelCommandMapper = deviceModelCommandMapper;
this.deviceService = devicesService;
this.deviceTwinMapper = deviceTwinMapper;
}

/// <summary>
Expand Down Expand Up @@ -102,14 +106,17 @@ public override Task<IActionResult> Delete(string deviceID)
/// <summary>
/// Executes the command on the device..
/// </summary>
/// <param name="modelId">The model identifier.</param>
/// <param name="deviceId">The device identifier.</param>
/// <param name="commandId">The command identifier.</param>
/// <returns></returns>
/// <exception cref="System.FormatException">Incorrect port or invalid DevEui Format.</exception>
[HttpPost("{modelId}/{deviceId}/_command/{commandId}", Name = "POST Execute LoRaWAN command")]
public async Task<IActionResult> ExecuteCommand(string modelId, string deviceId, string commandId)
[HttpPost("{deviceId}/_command/{commandId}", Name = "POST Execute LoRaWAN command")]
public async Task<IActionResult> ExecuteCommand(string deviceId, string commandId)
{

var twin = await deviceService.GetDeviceTwin(deviceId);
var modelId = deviceTwinMapper.CreateDeviceDetails(twin, null).ModelId;

var commandEntity = this.tableClientFactory
.GetDeviceCommands()
.Query<TableEntity>(filter: $"RowKey eq '{commandId}' and PartitionKey eq '{modelId}'")
Expand Down
18 changes: 9 additions & 9 deletions src/AzureIoTHub.Portal/Server/Mappers/DeviceTwinMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) CGI France. All rights reserved.
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Server.Mappers
Expand All @@ -22,10 +22,10 @@ public DeviceTwinMapper(IDeviceModelImageManager deviceModelImageManager)
public DeviceDetails CreateDeviceDetails(Twin twin, IEnumerable<string> tags)
{
var modelId = Helpers.DeviceHelper.RetrieveTagValue(twin, nameof(DeviceDetails.ModelId));
Dictionary<string, string> customTags = new Dictionary<string, string>();
if(tags != null)
var customTags = new Dictionary<string, string>();
if (tags != null)
{
foreach(string tag in tags)
foreach (var tag in tags)
{
customTags.Add(tag, Helpers.DeviceHelper.RetrieveTagValue(twin, tag));
}
Expand All @@ -46,15 +46,15 @@ public DeviceDetails CreateDeviceDetails(Twin twin, IEnumerable<string> tags)

public DeviceListItem CreateDeviceListItem(Twin twin, IEnumerable<string> tags)
{
Dictionary<string, string> customTags = new Dictionary<string, string>();
if(tags != null)
var customTags = new Dictionary<string, string>();
if (tags != null)
{
foreach(string tag in tags)
foreach (var tag in tags)
{
customTags.Add(tag, Helpers.DeviceHelper.RetrieveTagValue(twin, tag));
}
}

return new DeviceListItem
{
DeviceID = twin.DeviceId,
Expand All @@ -76,7 +76,7 @@ public void UpdateTwin(Twin twin, DeviceDetails item)

if(item.Tags != null)
{
foreach(KeyValuePair<string,string> customTag in item.Tags)
foreach (var customTag in item.Tags)
{
Helpers.DeviceHelper.SetTagValue(twin, customTag.Key, customTag.Value);
}
Expand Down