Skip to content

Commit

Permalink
Fix unsaved commands in the edge module #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 6227ce0 commit 7febdd8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/AzureIoTHub.Portal/Server/Entities/EdgeModuleCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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.Entities
{
public class EdgeModuleCommand : EntityBase
{
/// <summary>
/// The property name
/// </summary>
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public interface ITableClientFactory
const string EdgeDeviceTemplateTableName = "EdgeDeviceTemplates";
const string DeviceTagSettingTableName = "DeviceTagSettings";
const string DeviceTemplatePropertiesTableName = "DeviceTemplateProperties";
const string EdgeModuleCommandsTableName = "EdgeModuleCommands";

TableClient GetDeviceCommands();

Expand All @@ -24,5 +25,7 @@ public interface ITableClientFactory
TableClient GetDeviceTagSettings();

public TableClient GetTemplatesHealthCheck();

TableClient GetEdgeModuleCommands();
}
}
5 changes: 5 additions & 0 deletions src/AzureIoTHub.Portal/Server/Factories/TableClientFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,10 @@ public TableClient GetTemplatesHealthCheck()
{
return CreateClient("tableHealthCheck");
}

public TableClient GetEdgeModuleCommands()
{
return CreateClient(ITableClientFactory.EdgeModuleCommandsTableName);
}
}
}
49 changes: 49 additions & 0 deletions src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace AzureIoTHub.Portal.Server.Services
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
using System;
using AzureIoTHub.Portal.Server.Entities;

public class EdgeModelService : IEdgeModelService
{
Expand Down Expand Up @@ -91,6 +92,20 @@ public async Task<IoTEdgeModel> GetEdgeModel(string 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);
}
catch (RequestFailedException e)
Expand Down Expand Up @@ -295,6 +310,8 @@ private async Task SaveEntity(TableEntity entity, IoTEdgeModel deviceModelObject
{
this.edgeDeviceModelMapper.UpdateTableEntity(entity, deviceModelObject);

await SaveModuleCommands(deviceModelObject);

try
{
_ = await this.tableClientFactory
Expand All @@ -308,5 +325,37 @@ private async Task SaveEntity(TableEntity entity, IoTEdgeModel deviceModelObject

await this.configService.RollOutEdgeModelConfiguration(deviceModelObject);
}

/// <summary>
/// Saves the module commands for a specific model object.
/// </summary>
/// <param name="deviceModelObject">The device model object.</param>
/// <returns></returns>
/// <exception cref="InternalServerErrorException"></exception>
private async Task SaveModuleCommands(IoTEdgeModel deviceModelObject)
{
IEnumerable<EdgeModuleCommand> moduleCommands = deviceModelObject.EdgeModules
.SelectMany(x => x.Commands.Select(cmd => new EdgeModuleCommand
{
PartitionKey = deviceModelObject.ModelId,
RowKey = x.ModuleName + "-" + cmd.Name,
Timestamp = DateTime.Now,
Name = cmd.Name,
})).ToArray();

try
{
foreach (var moduleCommand in moduleCommands)
{
_ = this.tableClientFactory
.GetEdgeModuleCommands()
.UpsertEntity(moduleCommand);
}
}
catch (RequestFailedException e)
{
throw new InternalServerErrorException("Unable to save device module commands", e);
}
}
}
}

0 comments on commit 7febdd8

Please sign in to comment.