Skip to content

Commit

Permalink
Fix the problem of the command deletion #1139
Browse files Browse the repository at this point in the history
  • Loading branch information
Jérémy TREMBLAY committed Sep 1, 2022
1 parent 7febdd8 commit e6c8663
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
21 changes: 18 additions & 3 deletions src/AzureIoTHub.Portal/Server/Mappers/EdgeModelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ namespace AzureIoTHub.Portal.Server.Mappers
{
using System;
using System.Collections.Generic;
using System.Linq;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Server.Entities;
using AzureIoTHub.Portal.Server.Managers;
using AzureIoTHub.Portal.Shared.Models.v10;

public class EdgeModelMapper : IEdgeDeviceModelMapper
{
Expand Down Expand Up @@ -36,18 +39,30 @@ public IoTEdgeModelListItem CreateEdgeDeviceModelListItem(TableEntity entity)
};
}

public IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List<IoTEdgeModule> ioTEdgeModules)
public IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List<IoTEdgeModule> ioTEdgeModules, IEnumerable<EdgeModuleCommand> commands)
{
ArgumentNullException.ThrowIfNull(entity, nameof(entity));

return new IoTEdgeModel
var result = new IoTEdgeModel
{
ModelId = entity.RowKey,
ImageUrl = this.deviceModelImageManager.ComputeImageUri(entity.RowKey),
Name = entity[nameof(IoTEdgeModelListItem.Name)]?.ToString(),
Description = entity[nameof(IoTEdgeModelListItem.Description)]?.ToString(),
EdgeModules = ioTEdgeModules
};
foreach (var command in commands)
{
var module = result.EdgeModules.SingleOrDefault(x => (x.ModuleName + "-" + command.Name).Equals(command.RowKey, StringComparison.Ordinal));
if (module == null)
{
continue;
}
module.Commands.Add(new IoTEdgeModuleCommand
{
Name = command.Name,
});
}
return result;
}

public void UpdateTableEntity(TableEntity entity, IoTEdgeModel model)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ namespace AzureIoTHub.Portal.Server.Mappers
using System.Collections.Generic;
using Azure.Data.Tables;
using AzureIoTHub.Portal.Models.v10;
using AzureIoTHub.Portal.Server.Entities;

public interface IEdgeDeviceModelMapper
{
IoTEdgeModelListItem CreateEdgeDeviceModelListItem(TableEntity entity);

IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List<IoTEdgeModule> ioTEdgeModules);
IoTEdgeModel CreateEdgeDeviceModel(TableEntity entity, List<IoTEdgeModule> ioTEdgeModules, IEnumerable<EdgeModuleCommand> commands);

void UpdateTableEntity(TableEntity entity, IoTEdgeModel model);
}
Expand Down
28 changes: 10 additions & 18 deletions src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,18 @@ public async Task<IoTEdgeModel> GetEdgeModel(string modelId)
var query = await this.tableClientFactory
.GetEdgeDeviceTemplates()
.GetEntityAsync<TableEntity>(DefaultPartitionKey, modelId);

var modules = await this.configService.GetConfigModuleList(modelId);

var commands = this.tableClientFactory.GetEdgeModuleCommands()
.Query<EdgeModuleCommand>($"PartitionKey eq '{modelId}'").ToArray();

foreach (var command in commands)
{
foreach (var module in modules)
{
if ((module.ModuleName + "-" + command.Name).Equals(command.RowKey, StringComparison.Ordinal))
{
module.Commands.Add(new Shared.Models.v10.IoTEdgeModuleCommand { Name = command.Name });
}
}
}

return this.edgeDeviceModelMapper.CreateEdgeDeviceModel(query.Value, modules);
.Query<EdgeModuleCommand>(c => c.PartitionKey == modelId)
.ToArray();
return this.edgeDeviceModelMapper.CreateEdgeDeviceModel(query.Value, modules, commands);
}
catch (RequestFailedException e)
{
if (e.Status == StatusCodes.Status404NotFound)
{
throw new ResourceNotFoundException($"The edge model with id {modelId} doesn't exist");
}

throw new InternalServerErrorException($"Unable to get the edge model with id {modelId}", e);
}
}
Expand Down Expand Up @@ -342,9 +328,15 @@ private async Task SaveModuleCommands(IoTEdgeModel deviceModelObject)
Timestamp = DateTime.Now,
Name = cmd.Name,
})).ToArray();

try
{
var existingCommands = this.tableClientFactory.GetEdgeModuleCommands()
.Query<EdgeModuleCommand>(c => c.PartitionKey == deviceModelObject.ModelId)
.ToArray();
foreach (var command in existingCommands.Where(c => !moduleCommands.Any(x => x.RowKey == c.RowKey)))
{
_ = await this.tableClientFactory.GetEdgeModuleCommands().DeleteEntityAsync(command.PartitionKey, command.RowKey);
}
foreach (var moduleCommand in moduleCommands)
{
_ = this.tableClientFactory
Expand Down

0 comments on commit e6c8663

Please sign in to comment.