diff --git a/src/AzureIoTHub.Portal/Server/Entities/EdgeModuleCommand.cs b/src/AzureIoTHub.Portal/Server/Entities/EdgeModuleCommand.cs new file mode 100644 index 000000000..c810d9c3f --- /dev/null +++ b/src/AzureIoTHub.Portal/Server/Entities/EdgeModuleCommand.cs @@ -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 + { + /// + /// The property name + /// + public string Name { get; set; } + } +} diff --git a/src/AzureIoTHub.Portal/Server/Factories/ITableClientFactory.cs b/src/AzureIoTHub.Portal/Server/Factories/ITableClientFactory.cs index ac7ad6911..9c493e148 100644 --- a/src/AzureIoTHub.Portal/Server/Factories/ITableClientFactory.cs +++ b/src/AzureIoTHub.Portal/Server/Factories/ITableClientFactory.cs @@ -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(); @@ -24,5 +25,7 @@ public interface ITableClientFactory TableClient GetDeviceTagSettings(); public TableClient GetTemplatesHealthCheck(); + + TableClient GetEdgeModuleCommands(); } } diff --git a/src/AzureIoTHub.Portal/Server/Factories/TableClientFactory.cs b/src/AzureIoTHub.Portal/Server/Factories/TableClientFactory.cs index 208941364..48a4e99da 100644 --- a/src/AzureIoTHub.Portal/Server/Factories/TableClientFactory.cs +++ b/src/AzureIoTHub.Portal/Server/Factories/TableClientFactory.cs @@ -64,5 +64,10 @@ public TableClient GetTemplatesHealthCheck() { return CreateClient("tableHealthCheck"); } + + public TableClient GetEdgeModuleCommands() + { + return CreateClient(ITableClientFactory.EdgeModuleCommandsTableName); + } } } diff --git a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs index d05e2fa99..b1996fde1 100644 --- a/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/EdgeModelService.cs @@ -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 { @@ -91,6 +92,20 @@ public async Task GetEdgeModel(string modelId) var modules = await this.configService.GetConfigModuleList(modelId); + var commands = this.tableClientFactory.GetEdgeModuleCommands() + .Query($"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) @@ -295,6 +310,8 @@ private async Task SaveEntity(TableEntity entity, IoTEdgeModel deviceModelObject { this.edgeDeviceModelMapper.UpdateTableEntity(entity, deviceModelObject); + await SaveModuleCommands(deviceModelObject); + try { _ = await this.tableClientFactory @@ -308,5 +325,37 @@ private async Task SaveEntity(TableEntity entity, IoTEdgeModel deviceModelObject await this.configService.RollOutEdgeModelConfiguration(deviceModelObject); } + + /// + /// Saves the module commands for a specific model object. + /// + /// The device model object. + /// + /// + private async Task SaveModuleCommands(IoTEdgeModel deviceModelObject) + { + IEnumerable 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); + } + } } }