Skip to content

Commit

Permalink
Get AWS iot Edge Device (#2136)
Browse files Browse the repository at this point in the history
* Get AWS iot Edge Device

* Get AWS iot Edge Device

* Get AWS Edge Device Done

* use the act varaiable
  • Loading branch information
ssgueye2 authored Jun 1, 2023
1 parent 380be2f commit afd9a86
Show file tree
Hide file tree
Showing 16 changed files with 184 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace AzureIoTHub.Portal.Application.Services.AWS
using System.Threading.Tasks;
using Amazon.IoT.Model;
using Amazon.IotData.Model;
using AzureIoTHub.Portal.Models.v10;

public interface IAWSExternalDeviceService
{
Expand All @@ -20,6 +21,7 @@ public interface IAWSExternalDeviceService
Task<GetThingShadowResponse> GetDeviceShadow(string deviceName);

Task<UpdateThingShadowResponse> UpdateDeviceShadow(UpdateThingShadowRequest shadow);
Task<int> GetEdgeDeviceNbDevices(IoTEdgeDevice device);


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Task<PaginationResult<Twin>> GetAllEdgeDevice(

Task<DeviceCredentials> GetEdgeDeviceCredentials(IoTEdgeDevice device);

Task<ConfigItem> RetrieveLastConfiguration(Twin twin);
Task<ConfigItem> RetrieveLastConfiguration(IoTEdgeDevice ioTEdgeDevice);

Task<Twin> CreateNewTwinFromDeviceId(string deviceId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
<MudText>
<b>Runtime response</b>
</MudText>
@if (edgeDevice.RuntimeResponse == "running")
@if (edgeDevice.RuntimeResponse == "running" || edgeDevice.RuntimeResponse == "HEALTHY")
{
<MudTooltip Text="Runtime is running well">
<MudIcon Icon="@Icons.Material.Filled.CheckCircle" Color="Color.Success" />
Expand Down Expand Up @@ -148,35 +148,62 @@
</MudGrid>
<MudGrid>
<MudItem xs="12">
<MudExpansionPanel Text="Modules" IsInitiallyExpanded="true">
<TitleContent><MudText Typo="Typo.h6">Modules</MudText></TitleContent>
<ChildContent>
<MudTable Items="@edgeDevice.Modules" Dense=true Hover=true Bordered=true Striped=true Elevation=0>
<ColGroup>
<col style="width: 40%;" />
<col style="width: 10%;" />
<col style="width: 20%;" />
</ColGroup>
<HeaderContent>
<MudTh Style="text-align: center">Module Name</MudTh>
<MudTh Style="text-align: center">Status</MudTh>
<MudTh Style="text-align: center"></MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Module Name" Style="word-break: break-all;">@context.ModuleName</MudTd>
<MudTd DataLabel="Status" Style="text-align: center">@context.Status</MudTd>
<MudTd DataLabel="Device" Style="text-align: center;display:flex;justify-content:space-evenly;">
<MudButton Variant="Variant.Filled" Class="btn showLogs" Color="Color.Dark" Disabled="btn_disable" OnClick="@(async () => await ShowEdgeDeviceLogs(context) )">logs</MudButton>
<MudButton Variant="Variant.Filled" Class="rebootModule" Color="Color.Primary" Disabled="btn_disable" OnClick="@(async () => await OnMethod(context.ModuleName,"RestartModule") )">reboot</MudButton>
@foreach (var command in context.Commands)
{
<MudButton Variant="Variant.Filled" id="@(command.Name)" Disabled="btn_disable" Color="Color.Success" OnClick="@(async () => await OnMethod(context.ModuleName, command.Name) )">@command.Name</MudButton>
}
</MudTd>
</RowTemplate>
</MudTable>
</ChildContent>
</MudExpansionPanel>
@if (Portal.CloudProvider.Equals(CloudProviders.Azure))
{
<MudExpansionPanel Text="Modules" IsInitiallyExpanded="true">
<TitleContent><MudText Typo="Typo.h6">Modules</MudText></TitleContent>
<ChildContent>
<MudTable Items="@edgeDevice.Modules" Dense=true Hover=true Bordered=true Striped=true Elevation=0>
<ColGroup>
<col style="width: 40%;" />
<col style="width: 10%;" />
<col style="width: 20%;" />
</ColGroup>

<HeaderContent>
<MudTh Style="text-align: center">Module Name</MudTh>
<MudTh Style="text-align: center">Status</MudTh>
<MudTh Style="text-align: center"></MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Module Name" Style="word-break: break-all;">@context.ModuleName</MudTd>
<MudTd DataLabel="Status" Style="text-align: center">@context.Status</MudTd>
<MudTd DataLabel="Device" Style="text-align: center;display:flex;justify-content:space-evenly;">
<MudButton Variant="Variant.Filled" Class="btn showLogs" Color="Color.Dark" Disabled="btn_disable" OnClick="@(async () => await ShowEdgeDeviceLogs(context) )">logs</MudButton>
<MudButton Variant="Variant.Filled" Class="rebootModule" Color="Color.Primary" Disabled="btn_disable" OnClick="@(async () => await OnMethod(context.ModuleName,"RestartModule") )">reboot</MudButton>
@foreach (var command in context.Commands)
{
<MudButton Variant="Variant.Filled" id="@(command.Name)" Disabled="btn_disable" Color="Color.Success" OnClick="@(async () => await OnMethod(context.ModuleName, command.Name) )">@command.Name</MudButton>
}
</MudTd>
</RowTemplate>
</MudTable>
</ChildContent>
</MudExpansionPanel>
}
else
{
<MudExpansionPanel Text="Modules" IsInitiallyExpanded="true">
<TitleContent><MudText Typo="Typo.h6">Modules</MudText></TitleContent>
<ChildContent>
<MudTable Items="@edgeDevice.Modules" Dense=true Hover=true Bordered=true Striped=true Elevation=0>
<ColGroup>
<col style="width: 50%;" />
<col style="width: 20%;" />
</ColGroup>

<HeaderContent>
<MudTh Style="text-align: center">Module Name</MudTh>
<MudTh Style="text-align: center">Version</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Module Name" Style="word-break: break-all;">@context.ModuleName</MudTd>
<MudTd DataLabel="Status" Style="text-align: center">@context.Version</MudTd>
</RowTemplate>
</MudTable>
</ChildContent>
</MudExpansionPanel>
}
</MudItem>
</MudGrid>
<MudGrid>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// 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.Services
namespace AzureIoTHub.Portal.Infrastructure.Services
{
using System;
using System.Threading.Tasks;
Expand All @@ -22,12 +22,11 @@ public class AWSEdgeDevicesService : EdgeDevicesServiceBase, IEdgeDevicesService
/// </summary>
private readonly IAWSExternalDeviceService awsExternalDevicesService;
private readonly IExternalDeviceService externalDeviceService;

private readonly IUnitOfWork unitOfWork;
private readonly IEdgeDeviceRepository edgeDeviceRepository;
private readonly IEdgeEnrollementHelper edgeEnrollementHelper;
private readonly IEdgeDeviceModelRepository deviceModelRepository;

private readonly IConfigService configService;
private readonly ConfigHandler configHandler;

public AWSEdgeDevicesService(
Expand All @@ -36,6 +35,7 @@ public AWSEdgeDevicesService(
IExternalDeviceService externalDeviceService,
IAWSExternalDeviceService awsExternalDevicesService,
IDeviceTagService deviceTagService,
IConfigService configService,
IMapper mapper,
IUnitOfWork unitOfWork,
IEdgeDeviceRepository edgeDeviceRepository,
Expand All @@ -49,6 +49,7 @@ public AWSEdgeDevicesService(
this.edgeEnrollementHelper = edgeEnrollementHelper;
this.awsExternalDevicesService = awsExternalDevicesService;
this.externalDeviceService = externalDeviceService;
this.configService = configService;
this.deviceModelRepository = deviceModelRepository;

this.unitOfWork = unitOfWork;
Expand Down Expand Up @@ -123,11 +124,17 @@ public async Task DeleteEdgeDeviceAsync(string deviceId)
/// </summary>
/// <param name="edgeDeviceId">device id.</param>
/// <returns>IoTEdgeDevice object.</returns>
#pragma warning disable CS0108 // Un membre masque un membre hérité ; le mot clé new est manquant
public async Task<IoTEdgeDevice> GetEdgeDevice(string edgeDeviceId)
#pragma warning restore CS0108 // Un membre masque un membre hérité ; le mot clé new est manquant
{
var deviceDto = await base.GetEdgeDevice(edgeDeviceId);

// TODO
deviceDto.LastDeployment = await this.externalDeviceService.RetrieveLastConfiguration(deviceDto);
deviceDto.RuntimeResponse = deviceDto.LastDeployment.Status;
deviceDto.Modules = await this.configService.GetConfigModuleList(deviceDto.ModelId);
deviceDto.NbDevices = await this.awsExternalDevicesService.GetEdgeDeviceNbDevices(deviceDto);
deviceDto.NbModules = deviceDto.Modules.Count;

return deviceDto;
}
Expand Down Expand Up @@ -164,5 +171,6 @@ public async Task<string> GetEdgeDeviceEnrollementScript(string deviceId, string

return await this.externalDeviceService.CreateEnrollementScript(template, device);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@
namespace AzureIoTHub.Portal.Infrastructure.Services.AWS
{
using System.Threading.Tasks;
using Amazon.GreengrassV2;
using Amazon.IoT;
using Amazon.IoT.Model;
using Amazon.IotData;
using Amazon.IotData.Model;
using AzureIoTHub.Portal.Application.Services.AWS;
using AzureIoTHub.Portal.Domain.Exceptions;
using AzureIoTHub.Portal.Models.v10;

public class AWSExternalDeviceService : IAWSExternalDeviceService
{
private readonly IAmazonIoT amazonIotClient;
private readonly IAmazonIotData amazonIotDataClient;
private readonly IAmazonGreengrassV2 amazonGreenGrasss;

public AWSExternalDeviceService(IAmazonIoT amazonIoTClient, IAmazonIotData amazonIotDataClient)
public AWSExternalDeviceService(
IAmazonIoT amazonIoTClient,
IAmazonIotData amazonIotDataClient,
IAmazonGreengrassV2 amazonGreenGrasss)
{
this.amazonIotClient = amazonIoTClient;
this.amazonIotDataClient = amazonIotDataClient;
this.amazonGreenGrasss = amazonGreenGrasss;
}

public async Task<DescribeThingResponse> GetDevice(string deviceName)
Expand Down Expand Up @@ -97,5 +104,17 @@ public async Task<UpdateThingShadowResponse> UpdateDeviceShadow(UpdateThingShado

return shadowResponse;
}

public async Task<int> GetEdgeDeviceNbDevices(IoTEdgeDevice device)
{
var listClientDevices = await this.amazonGreenGrasss.ListClientDevicesAssociatedWithCoreDeviceAsync(
new Amazon.GreengrassV2.Model.ListClientDevicesAssociatedWithCoreDeviceRequest
{
CoreDeviceThingName = device.DeviceName
});
return listClientDevices.HttpStatusCode != System.Net.HttpStatusCode.OK
? throw new InternalServerErrorException($"Can not list Client Devices Associated to {device.DeviceName} Core Device due to an error in the Amazon IoT API.")
: listClientDevices.AssociatedClientDevices.Count;
}
}
}
Loading

0 comments on commit afd9a86

Please sign in to comment.