Skip to content

Commit

Permalink
Fix Edgedevice load and update
Browse files Browse the repository at this point in the history
  • Loading branch information
kbeaugrand authored and hocinehacherouf committed Jun 2, 2023
1 parent eea16fd commit 9457394
Show file tree
Hide file tree
Showing 14 changed files with 1,059 additions and 1,077 deletions.
8 changes: 4 additions & 4 deletions src/AzureIoTHub.Portal.Application/Services/IConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace AzureIoTHub.Portal.Application.Services
{
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Shared.Models.v10;
using Microsoft.Azure.Devices;
Expand All @@ -15,13 +15,13 @@ public interface IConfigService

Task<IEnumerable<Configuration>> GetDevicesConfigurations();

Task RollOutDeviceModelConfiguration(string modelId, Dictionary<string, object> desiredProperties);
Task<string> RollOutDeviceModelConfiguration(string modelId, Dictionary<string, object> desiredProperties);

Task DeleteDeviceModelConfigurationByConfigurationNamePrefix(string configurationNamePrefix);

Task RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel);
Task<string> RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel);

Task RollOutDeviceConfiguration(string modelId, Dictionary<string, object> desiredProperties, string configurationId, Dictionary<string, string> targetTags, int priority = 0);
Task<string> RollOutDeviceConfiguration(string modelId, Dictionary<string, object> desiredProperties, string configurationId, Dictionary<string, string> targetTags, int priority = 0);

Task<Configuration> GetConfigItem(string id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@
Label="Device name"
Variant="Variant.Outlined"
For="@(()=> edgeDevice.DeviceName)"
Required="true" />
Required="true"
ReadOnly=@(Portal.CloudProvider == CloudProviders.AWS)/>
</MudItem>
</MudGrid>
</ChildContent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ public async Task<IoTEdgeDevice> UpdateEdgeDevice(IoTEdgeDevice edgeDevice)
{
ArgumentNullException.ThrowIfNull(edgeDevice, nameof(edgeDevice));

_ = await this.awsExternalDevicesService.GetDevice(edgeDevice.DeviceId);
_ = await this.awsExternalDevicesService.GetDevice(edgeDevice.DeviceName);

// TODO
var result = await UpdateEdgeDeviceInDatabase(edgeDevice);

await this.unitOfWork.SaveAsync();
Expand Down Expand Up @@ -131,8 +130,11 @@ public async Task<IoTEdgeDevice> GetEdgeDevice(string edgeDeviceId)
var deviceDto = await base.GetEdgeDevice(edgeDeviceId);

deviceDto.LastDeployment = await this.externalDeviceService.RetrieveLastConfiguration(deviceDto);
deviceDto.RuntimeResponse = deviceDto.LastDeployment.Status;
deviceDto.Modules = await this.configService.GetConfigModuleList(deviceDto.ModelId);
deviceDto.RuntimeResponse = deviceDto.LastDeployment?.Status;

var model = await this.deviceModelRepository.GetByIdAsync(deviceDto.ModelId);

deviceDto.Modules = await this.configService.GetConfigModuleList(model.ExternalIdentifier!);
deviceDto.NbDevices = await this.awsExternalDevicesService.GetEdgeDeviceNbDevices(deviceDto);
deviceDto.NbModules = deviceDto.Modules.Count;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public AwsConfigService(
this.config = config;
}

public async Task RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel)
public async Task<string> RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel)
{

var createDeploymentRequest = new CreateDeploymentRequest
Expand All @@ -61,27 +61,9 @@ public async Task RollOutEdgeModelConfiguration(IoTEdgeModel edgeModel)
if (createDeploymentResponse.HttpStatusCode != HttpStatusCode.Created)
{
throw new InternalServerErrorException("The deployment creation failed due to an error in the Amazon IoT API.");

}
else
{
var edgeModelEntity = await this.edgeModelRepository.GetByIdAsync(edgeModel?.ModelId!);
if (edgeModelEntity == null)
{
throw new Domain.Exceptions.ResourceNotFoundException($"The edge model with id {edgeModel?.ModelId} not found");

}
else
{
edgeModel!.ExternalIdentifier = createDeploymentResponse.DeploymentId;

_ = this.mapper.Map(edgeModel, edgeModelEntity);

this.edgeModelRepository.Update(edgeModelEntity);
await this.unitOfWork.SaveAsync();
}

}
return createDeploymentResponse.DeploymentId;
}

private async Task<string> GetThingGroupArn(IoTEdgeModel edgeModel)
Expand Down Expand Up @@ -188,7 +170,7 @@ public Task<IEnumerable<Configuration>> GetDevicesConfigurations()
throw new NotImplementedException();
}

public Task RollOutDeviceModelConfiguration(string modelId, Dictionary<string, object> desiredProperties)
public Task<string> RollOutDeviceModelConfiguration(string modelId, Dictionary<string, object> desiredProperties)
{
throw new NotImplementedException();
}
Expand All @@ -198,7 +180,7 @@ public Task DeleteDeviceModelConfigurationByConfigurationNamePrefix(string confi
throw new NotImplementedException();
}

public Task RollOutDeviceConfiguration(string modelId, Dictionary<string, object> desiredProperties, string configurationId, Dictionary<string, string> targetTags, int priority = 0)
public Task<string> RollOutDeviceConfiguration(string modelId, Dictionary<string, object> desiredProperties, string configurationId, Dictionary<string, string> targetTags, int priority = 0)
{
throw new NotImplementedException();
}
Expand All @@ -211,7 +193,8 @@ public Task<Configuration> GetConfigItem(string id)
public async Task DeleteConfiguration(string modelId)
{
var modules = await GetConfigModuleList(modelId);
foreach (var module in modules)

foreach (var module in modules.Where(c => string.IsNullOrEmpty(c.Id)))
{
var deletedComponentResponse = await this.greengrass.DeleteComponentAsync(new DeleteComponentRequest
{
Expand All @@ -221,14 +204,14 @@ public async Task DeleteConfiguration(string modelId)
if (deletedComponentResponse.HttpStatusCode != HttpStatusCode.NoContent)
{
throw new InternalServerErrorException("The deletion of the component failed due to an error in the Amazon IoT API.");

}
}

var cancelDeploymentResponse = await this.greengrass.CancelDeploymentAsync(new CancelDeploymentRequest
{
DeploymentId = modelId
});

if (cancelDeploymentResponse.HttpStatusCode != HttpStatusCode.OK)
{
throw new InternalServerErrorException("The cancellation of the deployment failed due to an error in the Amazon IoT API.");
Expand All @@ -246,7 +229,6 @@ public async Task DeleteConfiguration(string modelId)
throw new InternalServerErrorException("The deletion of the deployment failed due to an error in the Amazon IoT API.");
}
}

}

public Task<int> GetFailedDeploymentsCount()
Expand All @@ -256,7 +238,6 @@ public Task<int> GetFailedDeploymentsCount()

public async Task<List<IoTEdgeModule>> GetConfigModuleList(string modelId)
{

var moduleList = new List<IoTEdgeModule>();

var getDeployement = new GetDeploymentRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,24 @@ public async Task<DeviceCredentials> GetDeviceCredentials(string deviceName)

public async Task<ConfigItem> RetrieveLastConfiguration(IoTEdgeDevice ioTEdgeDevice)
{
var coreDevice = await this.greengrass.GetCoreDeviceAsync(new GetCoreDeviceRequest
try
{
CoreDeviceThingName = ioTEdgeDevice.DeviceName
});
var coreDevice = await this.greengrass.GetCoreDeviceAsync(new GetCoreDeviceRequest
{
CoreDeviceThingName = ioTEdgeDevice.DeviceName
});

return new ConfigItem
return new ConfigItem
{
Name = coreDevice.CoreDeviceThingName,
DateCreation = coreDevice.LastStatusUpdateTimestamp,
Status = coreDevice.Status
};
}
catch (Amazon.GreengrassV2.Model.ResourceNotFoundException)
{
Name = coreDevice.CoreDeviceThingName,
DateCreation = coreDevice.LastStatusUpdateTimestamp,
Status = coreDevice.Status
};
return null!;
}
}

public Task<Device> UpdateDevice(Device device)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public async Task CreateEdgeModel(IoTEdgeModel edgeModel)
await SaveModuleCommands(edgeModel);
}

await this.configService.RollOutEdgeModelConfiguration(edgeModel);
edgeModelEntity.ExternalIdentifier = await this.configService.RollOutEdgeModelConfiguration(edgeModel);

await this.unitOfWork.SaveAsync();
}
Expand Down Expand Up @@ -266,7 +266,7 @@ public async Task UpdateEdgeModel(IoTEdgeModel edgeModel)
await SaveModuleCommands(edgeModel);
}

await this.configService.RollOutEdgeModelConfiguration(edgeModel);
edgeModel.ExternalIdentifier = await this.configService.RollOutEdgeModelConfiguration(edgeModel);

await this.unitOfWork.SaveAsync();
}
Expand Down
Loading

0 comments on commit 9457394

Please sign in to comment.