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

Issue #72 device command failed #104

Merged
merged 7 commits into from
Jan 26, 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
4 changes: 2 additions & 2 deletions src/AzureIoTHub.Portal.Server.Tests/Helpers/HelpersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void RetrieveProperyValue_RealTwin_ReturnsExpectedValue()
twin.Properties.Desired[PROPERTY_KEY] = PROPERTY_VALUE;

// Act
var result = DeviceHelper.RetrievePropertyValue(twin, PROPERTY_KEY);
var result = DeviceHelper.RetrieveDesiredPropertyValue(twin, PROPERTY_KEY);

// Assert
result.Contains($"{PROPERTY_VALUE}");
Expand All @@ -68,7 +68,7 @@ public void RetrievePropertyValue_EmptyTwin_ReturnsNull()
const string PROPERTY_KEY = "tag_key";

// Act
var result = DeviceHelper.RetrievePropertyValue(twin, PROPERTY_KEY);
var result = DeviceHelper.RetrieveDesiredPropertyValue(twin, PROPERTY_KEY);

// Assert
Assert.IsNull(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
@foreach (var command in Device.Commands)
{
<MudItem xs="12" Class="custom-form command_button" Style="padding:3px;">
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(async () => await ExecuteMethod(command))"> @command.CommandId</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Primary" Disabled="@(!Device.AlreadyLoggedInOnce)" OnClick="@(async () => await ExecuteMethod(command))"> @command.CommandId</MudButton>
</MudItem>
}
</MudItem>
Expand Down Expand Up @@ -238,12 +238,12 @@

if (result.IsSuccessStatusCode)
{
Snackbar.Add($"{method.Frame} has been successfully execute!",
Snackbar.Add($"{method.CommandId} has been successfully execute!",
Severity.Success);
}
else
{
Snackbar.Add(await result.Content.ReadAsStringAsync(), Severity.Error, (option)=> { option.VisibleStateDuration = 30000; });
Snackbar.Add(await result.Content.ReadAsStringAsync(), Severity.Error, (option)=> { option.VisibleStateDuration = 1000; });
}
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/AzureIoTHub.Portal/Server/Controllers/DevicesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public async Task<IActionResult> CreateDeviceAsync(DeviceDetails device)
{
try
{
if (!Eui.TryParse(device.DeviceID, out ulong deviceIdConvert))
if (!Eui.TryParse(device.DeviceID, out ulong deviceIdConvert) && device.DeviceType == "LoRa Device")
{
throw new InvalidOperationException("the device id is in the wrong format.");
}
Expand Down Expand Up @@ -187,15 +187,25 @@ public async Task<IActionResult> ExecuteCommand(string deviceId, string commandI

var result = await this.loraDeviceMethodManager.ExecuteLoRaDeviceMessage(deviceId, deviceModelCommand);

if (result.StatusCode == System.Net.HttpStatusCode.InternalServerError)
{
this.logger.LogError($"{deviceId} - Execute command on device failed \n {result}");
throw new FormatException("Incorrect port or invalid DevEui Format.");
}

this.logger.LogInformation($"{deviceId} - Execute command: {result}");

return this.Ok(await result.Content.ReadFromJsonAsync<dynamic>());
}
catch (Exception e)
catch (FormatException e)
{
this.logger.LogError($"{deviceId} - Execute command on device failed", e);
this.logger.LogError($"{deviceId} - Execute command on device failed \n {e.Message}");

return this.BadRequest();
return this.BadRequest("Something went wrong when executing the command.");
}
catch (Exception e)
{
return this.BadRequest(e.Message);
}
}
}
Expand Down
10 changes: 9 additions & 1 deletion src/AzureIoTHub.Portal/Server/Helpers/DeviceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,22 @@ public static void SetTagValue(Twin item, string tagName, string value)
/// <param name="item">Device twin.</param>
/// <param name="propertyName">Property to retrieve.</param>
/// <returns>Corresponding property value, or null if it doesn't exist.</returns>
public static string RetrievePropertyValue(Twin item, string propertyName)
public static string RetrieveDesiredPropertyValue(Twin item, string propertyName)
{
if (item.Properties.Desired.Contains(propertyName))
return item.Properties.Desired[propertyName];
else
return null;
}

public static string RetrieveReportedPropertyValue(Twin twin, string propertyName)
{
if (twin.Properties.Reported.Contains(propertyName))
return twin.Properties.Reported[propertyName];
else
return null;
}

/// <summary>
/// this function retreive and return the number of connected
/// devices.
Expand Down
8 changes: 4 additions & 4 deletions src/AzureIoTHub.Portal/Server/Mappers/DeviceTwinMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public DeviceDetails CreateDeviceDetails(Twin twin)
{
DeviceID = twin.DeviceId,
ModelId = modelId,
// ModelName = modelName,
AlreadyLoggedInOnce = Helpers.DeviceHelper.RetrieveReportedPropertyValue(twin, "DevAddr") != null,
ImageUrl = this.deviceModelImageManager.ComputeImageUri(modelId),
IsConnected = twin.ConnectionState == DeviceConnectionState.Connected,
IsEnabled = twin.Status == DeviceStatus.Enabled,
StatusUpdatedTime = twin.StatusUpdatedTime.GetValueOrDefault(DateTime.MinValue),
AppEUI = Helpers.DeviceHelper.RetrievePropertyValue(twin, nameof(DeviceDetails.AppEUI)),
AppKey = Helpers.DeviceHelper.RetrievePropertyValue(twin, nameof(DeviceDetails.AppKey)),
AppEUI = Helpers.DeviceHelper.RetrieveDesiredPropertyValue(twin, nameof(DeviceDetails.AppEUI)),
AppKey = Helpers.DeviceHelper.RetrieveDesiredPropertyValue(twin, nameof(DeviceDetails.AppKey)),
LocationCode = Helpers.DeviceHelper.RetrieveTagValue(twin, nameof(DeviceDetails.LocationCode)),
AssetID = Helpers.DeviceHelper.RetrieveTagValue(twin, nameof(DeviceDetails.AssetID)),
SensorDecoder = Helpers.DeviceHelper.RetrievePropertyValue(twin, nameof(DeviceDetails.SensorDecoder)),
SensorDecoder = Helpers.DeviceHelper.RetrieveDesiredPropertyValue(twin, nameof(DeviceDetails.SensorDecoder)),
DeviceType = Helpers.DeviceHelper.RetrieveTagValue(twin, nameof(DeviceDetails.DeviceType)),
Commands = this.deviceModelCommandsManager.RetrieveCommands(modelId)
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class DeviceDetails

public string SensorDecoder { get; set; }

public bool AlreadyLoggedInOnce { get; set; }

public List<Command> Commands { get; set; } = new List<Command>();
}
}