-
Notifications
You must be signed in to change notification settings - Fork 12
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
#2075 Sync AWS IoT Edge Device #2148
Merged
kbeaugrand
merged 5 commits into
main-vnext
from
2075-task-sync-aws-iot-edge-devices-with-iot-hub-portal
Jun 5, 2023
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
199 changes: 199 additions & 0 deletions
199
src/AzureIoTHub.Portal.Infrastructure/Jobs/AWS/SyncGreenGrassDevicesJob.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// 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.Infrastructure.Jobs.AWS | ||
{ | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
using Amazon.GreengrassV2; | ||
using Amazon.GreengrassV2.Model; | ||
using Amazon.IoT; | ||
using Amazon.IoT.Model; | ||
using Amazon.SecretsManager.Model; | ||
using AutoMapper; | ||
using AzureIoTHub.Portal.Application.Services; | ||
using AzureIoTHub.Portal.Application.Services.AWS; | ||
using AzureIoTHub.Portal.Domain; | ||
using AzureIoTHub.Portal.Domain.Entities; | ||
using AzureIoTHub.Portal.Domain.Repositories; | ||
using AzureIoTHub.Portal.Models.v10; | ||
using Microsoft.Extensions.Logging; | ||
using Quartz; | ||
using Quartz.Util; | ||
|
||
[DisallowConcurrentExecution] | ||
public class SyncGreenGrassDevicesJob : IJob | ||
{ | ||
|
||
private readonly ILogger<SyncGreenGrassDevicesJob> logger; | ||
private readonly IMapper mapper; | ||
private readonly IUnitOfWork unitOfWork; | ||
private readonly IEdgeDeviceRepository edgeDeviceRepository; | ||
private readonly IEdgeDeviceModelRepository edgeDeviceModelRepository; | ||
private readonly IDeviceTagValueRepository deviceTagValueRepository; | ||
private readonly IAmazonIoT amazonIoTClient; | ||
private readonly IAmazonGreengrassV2 amazonGreenGrass; | ||
private readonly IConfigService configService; | ||
private readonly IAWSExternalDeviceService awsExternalDevicesService; | ||
|
||
public SyncGreenGrassDevicesJob( | ||
ILogger<SyncGreenGrassDevicesJob> logger, | ||
IMapper mapper, | ||
IUnitOfWork unitOfWork, | ||
IEdgeDeviceRepository edgeDeviceRepository, | ||
IEdgeDeviceModelRepository edgeDeviceModelRepository, | ||
IDeviceTagValueRepository deviceTagValueRepository, | ||
IAmazonIoT amazonIoTClient, | ||
IAmazonGreengrassV2 amazonGreenGrass, | ||
IConfigService configService, | ||
IAWSExternalDeviceService awsExternalDevicesService) | ||
{ | ||
this.mapper = mapper; | ||
this.unitOfWork = unitOfWork; | ||
this.edgeDeviceRepository = edgeDeviceRepository; | ||
this.edgeDeviceModelRepository = edgeDeviceModelRepository; | ||
this.deviceTagValueRepository = deviceTagValueRepository; | ||
this.amazonIoTClient = amazonIoTClient; | ||
this.amazonGreenGrass = amazonGreenGrass; | ||
this.configService = configService; | ||
this.awsExternalDevicesService = awsExternalDevicesService; | ||
this.logger = logger; | ||
} | ||
|
||
|
||
public async Task Execute(IJobExecutionContext context) | ||
{ | ||
try | ||
{ | ||
this.logger.LogInformation("Start of sync GreenGrass Devices job"); | ||
|
||
await SyncGreenGrassDevicesAsEdgeDevices(); | ||
|
||
this.logger.LogInformation("End of sync GreenGrass Devices job"); | ||
} | ||
catch (Exception e) | ||
{ | ||
this.logger.LogError(e, "Sync GreenGrass Devices job has failed"); | ||
} | ||
} | ||
|
||
private async Task SyncGreenGrassDevicesAsEdgeDevices() | ||
{ | ||
var things = await GetAllGreenGrassDevices(); | ||
|
||
foreach (var thing in things) | ||
{ | ||
//Thing error | ||
if (thing.HttpStatusCode != HttpStatusCode.OK) | ||
{ | ||
this.logger.LogWarning($"Cannot import device '{thing.ThingName}' due to an error in the Amazon IoT API : {thing.HttpStatusCode}"); | ||
continue; | ||
} | ||
|
||
//ThingType not specified | ||
if (thing.ThingTypeName.IsNullOrWhiteSpace()) | ||
{ | ||
this.logger.LogInformation($"Cannot import Greengrass device '{thing.ThingName}' since it doesn't have related thing type."); | ||
continue; | ||
} | ||
|
||
//EdgeDeviceModel not find in DB | ||
var edgeDeviceModel = await this.edgeDeviceModelRepository.GetByNameAsync(thing.ThingTypeName); | ||
if (edgeDeviceModel == null) | ||
{ | ||
this.logger.LogWarning($"Cannot import Greengrass device '{thing.ThingName}'. The EdgeDeviceModel '{thing.ThingTypeName}' doesn't exist"); | ||
continue; | ||
} | ||
|
||
//Map with EdgeDevice | ||
var edgeDevice = this.mapper.Map<EdgeDevice>(thing); | ||
edgeDevice.DeviceModelId = edgeDeviceModel.Id; | ||
//EdgeDevices properties that are not present in the thing | ||
try | ||
{ | ||
var modules = await this.configService.GetConfigModuleList(edgeDevice.DeviceModelId); | ||
edgeDevice.NbDevices = await this.awsExternalDevicesService.GetEdgeDeviceNbDevices(this.mapper.Map<IoTEdgeDevice>(edgeDevice)); | ||
edgeDevice.NbModules = modules.Count; | ||
var coreDevice = await amazonGreenGrass.GetCoreDeviceAsync(new GetCoreDeviceRequest() { CoreDeviceThingName = thing.ThingName }); | ||
if (coreDevice.HttpStatusCode != HttpStatusCode.OK) | ||
{ | ||
this.logger.LogWarning($"Cannot import Greengrass device '{thing.ThingName}' due to an error retrieving core device in the Amazon IoT Data API : {coreDevice.HttpStatusCode}"); | ||
continue; | ||
} | ||
edgeDevice.ConnectionState = coreDevice.Status == CoreDeviceStatus.HEALTHY ? "Connected" : "Disconnected"; | ||
} | ||
catch (Exception e) | ||
{ | ||
this.logger.LogWarning($"Cannot import Greengrass device '{thing.ThingName}' due to an error retrieving Greengrass device properties in the Amazon IoT Data API.", e); | ||
continue; | ||
} | ||
Comment on lines
+126
to
+130
Check notice Code scanning / CodeQL Generic catch clause
Generic catch clause.
|
||
|
||
//Create or update the Edge Device | ||
await CreateOrUpdateGreenGrassDevice(edgeDevice); | ||
} | ||
|
||
foreach (var item in (await this.edgeDeviceRepository.GetAllAsync( | ||
edgeDevice => !things.Select(x => x.ThingId).Contains(edgeDevice.Id), | ||
default, | ||
d => d.Tags, | ||
d => d.Labels | ||
))) | ||
{ | ||
this.edgeDeviceRepository.Delete(item.Id); | ||
} | ||
|
||
await this.unitOfWork.SaveAsync(); | ||
} | ||
|
||
private async Task<List<DescribeThingResponse>> GetAllGreenGrassDevices() | ||
{ | ||
var devices = new List<DescribeThingResponse>(); | ||
|
||
var nextToken = string.Empty; | ||
|
||
var response = await amazonGreenGrass.ListCoreDevicesAsync( | ||
new ListCoreDevicesRequest | ||
{ | ||
NextToken = nextToken | ||
}); | ||
|
||
foreach (var requestDescribeThing in response.CoreDevices.Select(device => new DescribeThingRequest { ThingName = device.CoreDeviceThingName })) | ||
kbeaugrand marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
try | ||
{ | ||
devices.Add(await this.amazonIoTClient.DescribeThingAsync(requestDescribeThing)); | ||
} | ||
catch (AmazonIoTException e) | ||
{ | ||
this.logger.LogWarning($"Cannot import Greengrass device '{requestDescribeThing.ThingName}' due to an error in the Amazon IoT API.", e); | ||
continue; | ||
} | ||
} | ||
|
||
return devices; | ||
} | ||
|
||
private async Task CreateOrUpdateGreenGrassDevice(EdgeDevice edgeDevice) | ||
{ | ||
var edgeDeviceEntity = await this.edgeDeviceRepository.GetByIdAsync(edgeDevice.Id, d => d.Tags); | ||
|
||
if (edgeDeviceEntity == null) | ||
{ | ||
await this.edgeDeviceRepository.InsertAsync(edgeDevice); | ||
} | ||
else | ||
{ | ||
if (edgeDeviceEntity.Version >= edgeDevice.Version) return; | ||
|
||
foreach (var deviceTagEntity in edgeDeviceEntity.Tags) | ||
{ | ||
this.deviceTagValueRepository.Delete(deviceTagEntity.Id); | ||
} | ||
|
||
_ = this.mapper.Map(edgeDevice, edgeDeviceEntity); | ||
this.edgeDeviceRepository.Update(edgeDeviceEntity); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Generic catch clause