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

Refactor source code + Fix unit tests #1907

Merged
merged 10 commits into from
Mar 21, 2023
10 changes: 5 additions & 5 deletions src/AzureIoTHub.Portal.Application/Helpers/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public static IoTEdgeModule CreateGatewayModule(Configuration config, JProperty
var edgeModule = new IoTEdgeModule
{
ModuleName = module.Name,
ImageURI = module.Value["settings"]["image"]?.Value<string>(),
ContainerCreateOptions = module.Value["settings"]["createOptions"]?.Value<string>(),
ImageURI = module.Value["settings"]?["image"]?.Value<string>(),
ContainerCreateOptions = module.Value["settings"]?["createOptions"]?.Value<string>(),
Status = module.Value["status"]?.Value<string>(),
};

Expand Down Expand Up @@ -211,7 +211,7 @@ private static Dictionary<string, string> GetEnvironmentVariables(JProperty modu
{
foreach (var val in environmentVariables.Cast<JProperty>())
{
envVariables.Add(val.Name, val.Value["value"].Value<string>());
envVariables.Add(val.Name, val.Value["value"]!.Value<string>()!);
}
}

Expand Down Expand Up @@ -251,12 +251,12 @@ public static Dictionary<string, IDictionary<string, object>> GenerateModulesCon

foreach (var item in edgeModel.SystemModules.Single(x => x.Name == "edgeAgent").EnvironmentVariables)
{
edgeAgentPropertiesDesired.SystemModules.EdgeAgent.EnvironmentVariables.Add(item.Name, new EnvironmentVariable() { EnvValue = item.Value });
edgeAgentPropertiesDesired.SystemModules.EdgeAgent.EnvironmentVariables?.Add(item.Name, new EnvironmentVariable() { EnvValue = item.Value });
}

foreach (var item in edgeModel.SystemModules.Single(x => x.Name == "edgeHub").EnvironmentVariables)
{
edgeAgentPropertiesDesired.SystemModules.EdgeHub.EnvironmentVariables.Add(item.Name, new EnvironmentVariable() { EnvValue = item.Value });
edgeAgentPropertiesDesired.SystemModules.EdgeHub.EnvironmentVariables?.Add(item.Name, new EnvironmentVariable() { EnvValue = item.Value });
}

var modulesContent = new Dictionary<string, IDictionary<string, object>>();
Expand Down
28 changes: 14 additions & 14 deletions src/AzureIoTHub.Portal.Application/Helpers/DeviceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static string RetrieveSymmetricKey(string deviceId, SymmetricKeyAttestati
/// <param name="item">the device twin.</param>
/// <param name="tagName">the tag property.</param>
/// <returns>The corresponding tag value, or null if it doesn't exist.</returns>
public static string RetrieveTagValue(Twin item, string tagName)
public static string? RetrieveTagValue(Twin item, string tagName)
{
ArgumentNullException.ThrowIfNull(item, nameof(item));

Expand Down Expand Up @@ -77,7 +77,7 @@ public static void SetTagValue(Twin item, string tagName, string value)
/// <param name="twin">Device twin.</param>
/// <param name="propertyName">Property to retrieve.</param>
/// <returns>The corresponding property value, or null if it doesn't exist.</returns>
public static string RetrieveDesiredPropertyValue(Twin twin, string propertyName)
public static string? RetrieveDesiredPropertyValue(Twin twin, string propertyName)
{
ArgumentNullException.ThrowIfNull(twin, nameof(twin));

Expand All @@ -91,7 +91,7 @@ public static string RetrieveDesiredPropertyValue(Twin twin, string propertyName
/// <param name="twin">Device twin.</param>
/// <param name="propertyName">Property to set.</param>
/// <param name="value">Property value.</param>
public static void SetDesiredProperty(Twin twin, string propertyName, object value)
public static void SetDesiredProperty(Twin twin, string propertyName, object? value)
{
ArgumentNullException.ThrowIfNull(twin, nameof(twin));

Expand All @@ -105,7 +105,7 @@ public static void SetDesiredProperty(Twin twin, string propertyName, object val
/// <param name="twin">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 RetrieveReportedPropertyValue(Twin twin, string propertyName)
public static string? RetrieveReportedPropertyValue(Twin twin, string propertyName)
{
ArgumentNullException.ThrowIfNull(twin, nameof(twin));

Expand Down Expand Up @@ -149,15 +149,15 @@ public static int RetrieveNbModuleCount(Twin twin, string deviceId)
/// </summary>
/// <param name="twin">the twin of the device we want.</param>
/// <returns>string.</returns>
public static string RetrieveRuntimeResponse(Twin twin)
public static string? RetrieveRuntimeResponse(Twin twin)
{
ArgumentNullException.ThrowIfNull(twin, nameof(twin));

var reportedProperties = JObject.Parse(twin.Properties.Reported.ToJson());

if (reportedProperties.TryGetValue("systemModules", out var systemModules)
&& systemModules.Value<JObject>().TryGetValue("edgeAgent", out var edgeAgentModule)
&& edgeAgentModule.Value<JObject>().TryGetValue("runtimeStatus", out var runtimeStatus))
&& systemModules.Value<JObject>()!.TryGetValue("edgeAgent", out var edgeAgentModule)
&& edgeAgentModule.Value<JObject>()!.TryGetValue("runtimeStatus", out var runtimeStatus))
{
return runtimeStatus.Value<string>();
}
Expand All @@ -182,19 +182,19 @@ public static IReadOnlyCollection<IoTEdgeModule> RetrieveModuleList(Twin twin)
return list;
}

foreach (var property in modules.Value<JObject>())
foreach (var property in modules.Value<JObject>()!)
{
var propertyObject = property.Value.Value<JObject>();
var propertyObject = property.Value?.Value<JObject>();

var module = new IoTEdgeModule()
{
ModuleName = property.Key
};

if (propertyObject.TryGetValue("settings", out var moduleSettings))
if (propertyObject!.TryGetValue("settings", out var moduleSettings))
{
var setting = moduleSettings.ToObject<Dictionary<string, string>>();
module.ImageURI = setting["image"];
module.ImageURI = setting?["image"];
}

if (propertyObject.TryGetValue("status", out var status))
Expand Down Expand Up @@ -247,7 +247,7 @@ public static TwinCollection PropertiesWithDotNotationToTwinCollection(Dictionar
return new TwinCollection(JsonConvert.SerializeObject(root));
}

public static string RetrieveClientThumbprintValue(Twin twin)
public static string? RetrieveClientThumbprintValue(Twin twin)
{
var serializedClientThumbprint = RetrieveDesiredPropertyValue(twin, nameof(Concentrator.ClientThumbprint).ToCamelCase());

Expand All @@ -261,13 +261,13 @@ public static string RetrieveClientThumbprintValue(Twin twin)
{
var clientThumbprintArray=JsonConvert.DeserializeObject<string[]>(serializedClientThumbprint);

if (clientThumbprintArray.Length == 0)
if (clientThumbprintArray?.Length == 0)
{
// clientThumbprint array is empty in the device twin
return null;
}

return clientThumbprintArray[0];
return clientThumbprintArray?[0];
}
catch (JsonReaderException)
{
Expand Down
10 changes: 5 additions & 5 deletions src/AzureIoTHub.Portal.Application/Services/IDeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public interface IDeviceService<TDto>
where TDto : IDeviceDetails
{
Task<PaginatedResult<DeviceListItem>> GetDevices(
string searchText = null,
string? searchText = null,
bool? searchStatus = null,
bool? searchState = null,
int pageSize = 10,
int pageNumber = 0,
string[] orderBy = null,
Dictionary<string, string> tags = default,
string modelId = null,
List<string> labels = default);
string[]? orderBy = null,
Dictionary<string, string>? tags = default,
string? modelId = null,
List<string>? labels = default);

Task<TDto> GetDevice(string deviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ namespace AzureIoTHub.Portal.Application.Services
public interface IEdgeDevicesService
{
Task<PaginatedResult<IoTEdgeListItem>> GetEdgeDevicesPage(
string searchText = null,
string? searchText = null,
bool? searchStatus = null,
int pageSize = 10,
int pageNumber = 0,
string[] orderBy = null,
string modelId = null,
List<string> labels = default);
string[]? orderBy = null,
string? modelId = null,
List<string>? labels = default);

Task<IoTEdgeDevice> GetEdgeDevice(string edgeDeviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,20 @@ public interface IExternalDeviceService
Task DeleteDevice(string deviceId);

Task<PaginationResult<Twin>> GetAllDevice(
string continuationToken = null,
string filterDeviceType = null,
string excludeDeviceType = null,
string searchText = null,
string? continuationToken = null,
string? filterDeviceType = null,
string? excludeDeviceType = null,
string? searchText = null,
bool? searchStatus = null,
bool? searchState = null,
Dictionary<string, string> searchTags = null,
Dictionary<string, string>? searchTags = null,
int pageSize = 10);

Task<PaginationResult<Twin>> GetAllEdgeDevice(
string continuationToken = null,
string searchText = null,
string? continuationToken = null,
string? searchText = null,
bool? searchStatus = null,
string searchType = null,
string? searchType = null,
int pageSize = 10);

Task<IEnumerable<IoTEdgeDeviceLog>> GetEdgeDeviceLogs(string deviceId, IoTEdgeModule edgeModule);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public interface ILoRaWanManagementService
{
Task<HttpResponseMessage> CheckAzureFunctionReturn(CancellationToken cancellationToken);
Task<HttpResponseMessage> ExecuteLoRaDeviceMessage(string deviceId, DeviceModelCommandDto commandDto);
Task<RouterConfig> GetRouterConfig(string loRaRegion);
Task<RouterConfig?> GetRouterConfig(string loRaRegion);
}
}
2 changes: 1 addition & 1 deletion src/AzureIoTHub.Portal.Client/App.razor
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@if (!context.User.Identity.IsAuthenticated)
@if (!(context.User.Identity?.IsAuthenticated ?? false))
{
<RedirectToLogin />
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@

@code {

private IEnumerable<LabelDto> displayedLabels;
private IEnumerable<LabelDto> displayedLabels = Array.Empty<LabelDto>();

[Parameter]
public int MaxDisplayedLabels { get; set; } = 3;

[Parameter]
public IEnumerable<LabelDto> SearchedLabels { get; set; }
public IEnumerable<LabelDto> SearchedLabels { get; set; } = Array.Empty<LabelDto>();

[Parameter]
public IEnumerable<LabelDto> Items { get; set; }
public IEnumerable<LabelDto> Items { get; set; } = Array.Empty<LabelDto>();

protected override void OnInitialized()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
[Parameter]
public List<LabelDto> Labels { get; set; } = new();

private string labelNameValue;
private string labelColorValue;
private string labelNameValue = default!;
private string labelColorValue = default!;

public void AddLabel()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

@code {
[Parameter]
public string Title { get; set; }
public string Title { get; set; } = default!;

[Parameter]
public int Counter { get; set; }

[Parameter]
public string Icon { get; set; }
public string Icon { get; set; } = default!;

[Parameter]
public Color Color { get; set; } = Color.Inherit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@

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

private PortalMetric portalMetric = new();

protected override async Task OnInitializedAsync()
{
DashboardLayoutService.RefreshDashboardOccurred += DashboardLayoutServiceOnRefreshDashboardOccurred;
DashboardLayoutService.RefreshDashboardOccurred += DashboardLayoutServiceOnRefreshDashboardOccurred!;
await LoadMetrics();
}

public void Dispose()
{
DashboardLayoutService.RefreshDashboardOccurred -= DashboardLayoutServiceOnRefreshDashboardOccurred;
DashboardLayoutService.RefreshDashboardOccurred -= DashboardLayoutServiceOnRefreshDashboardOccurred!;
}

private async Task LoadMetrics()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@

@code {
[Parameter]
public LoRaDeviceModelDto LoRaDeviceModel { get; set; }
public LoRaDeviceModelDto LoRaDeviceModel { get; set; } = default!;

[Parameter]
public IList<DeviceModelCommandDto> Commands { get; set; }
public IList<DeviceModelCommandDto> Commands { get; set; } = new List<DeviceModelCommandDto>();

private bool loraProperty { get; set; }

Expand All @@ -189,7 +189,7 @@
{
var last = Commands.LastOrDefault();

if (Commands.Count == 0 || (last.Name is not null && last.Frame is not null))
if (Commands.Count == 0 || (last?.Name is not null && last?.Frame is not null))
{
Commands.Add(new DeviceModelCommandDto());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@

@code {
[Parameter]
public LoRaDeviceModelDto LoRaDeviceModel { get; set; }
public LoRaDeviceModelDto LoRaDeviceModel { get; set; } = default!;

[Parameter]
public IList<DeviceModelCommandDto> Commands { get; set; }
public IList<DeviceModelCommandDto> Commands { get; set; } = default!;

private DeviceModelCommandDto DeviceModelCommandDto { get; set; } = new DeviceModelCommandDto();

Expand All @@ -195,7 +195,7 @@
{
var last = Commands.LastOrDefault();

if (Commands.Count == 0 || (last.Name is not null && last.Frame is not null))
if (Commands.Count == 0 || (last?.Name is not null && last?.Frame is not null))
{
Commands.Add(new DeviceModelCommandDto());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

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

private async Task<IEnumerable<DeviceListItem>> SearchDevicesToDuplicate(string query)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

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

[Parameter]
public string DeviceId { get; set; }
public string DeviceId { get; set; } = default!;

private IEnumerable<LoRaDeviceTelemetryDto> Telemetries = new List<LoRaDeviceTelemetryDto>();
private LineConfig chartConfig;
private LineConfig chartConfig = default!;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@

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

[Parameter]
public LoRaDeviceDetails LoRaDevice { get; set; }
public LoRaDeviceDetails LoRaDevice { get; set; } = default!;

[Parameter]
public LoRaDeviceDetailsValidator LoraValidator { get; set; }
public LoRaDeviceDetailsValidator LoraValidator { get; set; } = default!;

[Parameter]
public LoRaDeviceModelDto LoraModelDto { get; set; }
public LoRaDeviceModelDto LoraModelDto { get; set; } = default!;

public List<string> GatewayIdList = new();

Expand All @@ -93,6 +93,8 @@

private async Task<IEnumerable<string>> SearchGatewayID(string value)
{
await Task.Delay(50);

// if text is null or empty, show complete list
if (string.IsNullOrEmpty(value))
return GatewayIdList;
Expand Down
Loading