Skip to content

Commit

Permalink
Refactoring Sync Things Job (#2172)
Browse files Browse the repository at this point in the history
* #2168 Refactoring Device & EdgeDevice sync job for AWS

- 1 job Sync things job => update device & edgedevice based on thingtype (iotEdge or not)
- Remove Sync GreenGrass Devices
- Update the job thingtype for isEdge method

* #2168 Add TU for isEdge & ConnectionState

* #2168 Fix TU
  • Loading branch information
delager authored and kbeaugrand committed Jun 16, 2023
1 parent 07edc2a commit 1d604d3
Show file tree
Hide file tree
Showing 11 changed files with 979 additions and 920 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public interface IAWSExternalDeviceService
Task<UpdateThingShadowResponse> UpdateDeviceShadow(UpdateThingShadowRequest shadow);
Task<int> GetEdgeDeviceNbDevices(IoTEdgeDevice device);

Task<bool?> IsEdgeThingType(DescribeThingTypeResponse thingType);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,42 @@ private async Task<List<IoTEdgeModel>> GetAllGreenGrassDeployments()

var nextToken = string.Empty;

var getAllAwsGreenGrassDeployments = await this.amazonGreenGrass.ListDeploymentsAsync(
new ListDeploymentsRequest
do
{
var request = new ListDeploymentsRequest
{
NextToken = nextToken,
HistoryFilter = DeploymentHistoryFilter.LATEST_ONLY
});
};

foreach (var deployment in getAllAwsGreenGrassDeployments.Deployments)
{
var awsThingGroupRegex = new Regex(@"/([^/]+)$");
var matches = awsThingGroupRegex.Match(deployment.TargetArn);
var response = await this.amazonGreenGrass.ListDeploymentsAsync(request);

if (matches.Success && matches.Groups.Count > 1)
foreach (var deployment in response.Deployments)
{
var thinggroupName = matches.Groups[1].Value;
var s = await this.amazonIoTClient.DescribeThingGroupAsync(new Amazon.IoT.Model.DescribeThingGroupRequest { ThingGroupName = thinggroupName });
if (s.QueryString != null)
var awsThingGroupRegex = new Regex(@"/([^/]+)$");
var matches = awsThingGroupRegex.Match(deployment.TargetArn);

if (matches.Success && matches.Groups.Count > 1)
{
var iotEdgeModel = new IoTEdgeModel
var thinggroupName = matches.Groups[1].Value;
var s = await this.amazonIoTClient.DescribeThingGroupAsync(new Amazon.IoT.Model.DescribeThingGroupRequest { ThingGroupName = thinggroupName });
if (s.QueryString != null)
{
ModelId = deployment.DeploymentId, //Instead of giving a random Id here, we can give the deploymentID
Name = deployment.DeploymentName,
ExternalIdentifier = deployment.DeploymentId
};
deployments.Add(iotEdgeModel);
var iotEdgeModel = new IoTEdgeModel
{
ModelId = deployment.DeploymentId, //Instead of giving a random Id here, we can give the deploymentID
Name = deployment.DeploymentName,
ExternalIdentifier = deployment.DeploymentId
};
deployments.Add(iotEdgeModel);
}
}
}

nextToken = response.NextToken;
}
while (!string.IsNullOrEmpty(nextToken));

return deployments;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace AzureIoTHub.Portal.Infrastructure.Jobs.AWS
using Amazon.IoT.Model;
using AutoMapper;
using AzureIoTHub.Portal.Application.Managers;
using AzureIoTHub.Portal.Application.Services.AWS;
using AzureIoTHub.Portal.Domain;
using AzureIoTHub.Portal.Domain.Entities;
using AzureIoTHub.Portal.Domain.Repositories;
Expand All @@ -24,20 +25,23 @@ public class SyncThingTypesJob : IJob
private readonly IDeviceModelRepository deviceModelRepository;
private readonly IAmazonIoT amazonIoTClient;
private readonly IDeviceModelImageManager deviceModelImageManager;
private readonly IAWSExternalDeviceService awsExternalDeviceService;

public SyncThingTypesJob(
ILogger<SyncThingTypesJob> logger,
IMapper mapper,
IUnitOfWork unitOfWork,
IDeviceModelRepository deviceModelRepository,
IAmazonIoT amazonIoTClient,
IDeviceModelImageManager awsImageManager)
IDeviceModelImageManager awsImageManager,
IAWSExternalDeviceService awsExternalDeviceService)
{
this.deviceModelImageManager = awsImageManager;
this.mapper = mapper;
this.unitOfWork = unitOfWork;
this.deviceModelRepository = deviceModelRepository;
this.amazonIoTClient = amazonIoTClient;
this.awsExternalDeviceService = awsExternalDeviceService;
this.logger = logger;
}

Expand All @@ -64,19 +68,15 @@ private async Task SyncThingTypesAsDeviceModels()

foreach (var thingType in thingTypes)
{
var isEdge = await IsEdgeThingType(thingType);
var isEdge = await awsExternalDeviceService.IsEdgeThingType(thingType);

// Cannot know if the thing type was created for an iotEdge or not, so skipping...
if (!isEdge.HasValue)
{
continue;
}

if (isEdge == true)
{
// TODO: Implement CreateOrUpdateEdgeModel here.
}
else
if (isEdge == false)
{
await CreateOrUpdateDeviceModel(thingType);
}
Expand All @@ -86,38 +86,6 @@ private async Task SyncThingTypesAsDeviceModels()
await DeleteThingTypes(thingTypes);
}

private async Task<bool?> IsEdgeThingType(DescribeThingTypeResponse thingType)
{
var response = await this.amazonIoTClient.ListTagsForResourceAsync(new ListTagsForResourceRequest
{
ResourceArn = thingType.ThingTypeArn
});

do
{
if (response == null || !response.Tags.Any())
{
return null;
}

var iotEdgeTag = response.Tags.Where(c => c.Key.Equals("iotEdge", StringComparison.OrdinalIgnoreCase));

if (!iotEdgeTag.Any())
{
response = await this.amazonIoTClient.ListTagsForResourceAsync(new ListTagsForResourceRequest
{
ResourceArn = thingType.ThingTypeArn,
NextToken = response.NextToken
});

continue;
}

return bool.TryParse(iotEdgeTag.Single().Value, out var result) ? result : null;

} while (true);
}

private async Task<List<DescribeThingTypeResponse>> GetAllThingTypes()
{
var thingTypes = new List<DescribeThingTypeResponse>();
Expand Down
Loading

0 comments on commit 1d604d3

Please sign in to comment.