diff --git a/CoreAgent.API/Program.cs b/CoreAgent.API/Program.cs index 179b8ec..e4938e8 100644 --- a/CoreAgent.API/Program.cs +++ b/CoreAgent.API/Program.cs @@ -1,4 +1,5 @@ using CoreAgent.API; +using CoreAgent.Domain.Models.System; using CoreAgent.Infrastructure.Logging; using Serilog; using System; @@ -11,6 +12,7 @@ try // 第一步:添加所有配置 builder.AddConfigurations(); + // 第二步:创建Startup实例 var startup = builder.CreateStartup(); diff --git a/CoreAgent.API/Startup.cs b/CoreAgent.API/Startup.cs index 87e7a25..37d11f4 100644 --- a/CoreAgent.API/Startup.cs +++ b/CoreAgent.API/Startup.cs @@ -90,6 +90,9 @@ namespace CoreAgent.API var networkCommandConfig = builder.Configuration.GetSection("networkCommand").Get(); CellularNetworkContext.Instance.SetNetworkCommandConfig(networkCommandConfig); + // 添加配置 + builder.Services.Configure(builder.Configuration); + return builder; } diff --git a/CoreAgent.API/appsettings.json b/CoreAgent.API/appsettings.json index 365d9c5..dc3b1a4 100644 --- a/CoreAgent.API/appsettings.json +++ b/CoreAgent.API/appsettings.json @@ -13,5 +13,8 @@ "Url": "http://0.0.0.0:11003" } } + }, + "AppSettings": { + "TempDirectory": "/tmp/" } } diff --git a/CoreAgent.Domain/Models/System/AppSettings.cs b/CoreAgent.Domain/Models/System/AppSettings.cs new file mode 100644 index 0000000..e866d4c --- /dev/null +++ b/CoreAgent.Domain/Models/System/AppSettings.cs @@ -0,0 +1,12 @@ +namespace CoreAgent.Domain.Models.System; + +/// +/// 应用程序配置 +/// +public class AppSettings +{ + /// + /// 临时目录路径 + /// + public string TempDirectory { get; set; } = "/tmp/"; +} \ No newline at end of file diff --git a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs index 8697be9..0be6664 100644 --- a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs +++ b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs @@ -7,6 +7,8 @@ using CoreAgent.Domain.Interfaces.System.Command; using CoreAgent.Domain.Models.Network; using CoreAgent.Domain.Models.System; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System.IO; namespace CoreAgent.Infrastructure.Services.Network; @@ -19,6 +21,7 @@ public class CellularNetworkService : ICellularNetworkService private readonly ISystemCommandExecutor _commandExecutor; private readonly INetworkConfigurationService _configService; private readonly CellularNetworkContext _context; + private readonly AppSettings _appSettings; private static readonly SemaphoreSlim _startLock = new(1, 1); private const int MaxConnectionAttempts = 30; private const int ConnectionCheckDelayMs = 1000; @@ -27,11 +30,13 @@ public class CellularNetworkService : ICellularNetworkService public CellularNetworkService( ILogger logger, ISystemCommandExecutor commandExecutor, - INetworkConfigurationService configService) + INetworkConfigurationService configService, + IOptions appSettings) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); _commandExecutor = commandExecutor ?? throw new ArgumentNullException(nameof(commandExecutor)); _configService = configService ?? throw new ArgumentNullException(nameof(configService)); + _appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings)); _context = CellularNetworkContext.Instance; } @@ -211,6 +216,13 @@ public class CellularNetworkService : ICellularNetworkService { try { + // 复制配置值到临时目录并更新配置路径 + if (!await CopyConfigValuesToTempAsync(networkConfig)) + { + _logger.LogError("复制配置值到临时目录失败"); + return false; + } + var commands = _context.GetNetworkCommandConfig(); var startCommand = commands.NetworkCommands.FirstOrDefault(s => s.Type == NetworkCommandType.Start); if (startCommand == null) @@ -243,6 +255,57 @@ public class CellularNetworkService : ICellularNetworkService } } + /// + /// 将配置值复制到临时目录并更新配置路径 + /// + private async Task CopyConfigValuesToTempAsync(NetworkConfiguration networkConfig) + { + try + { + // 确保临时目录存在 + if (!Directory.Exists(_appSettings.TempDirectory)) + { + Directory.CreateDirectory(_appSettings.TempDirectory); + } + + // 复制 RAG 配置值并更新路径 + if (!string.IsNullOrEmpty(networkConfig.RagConfig)) + { + var ragFileName = Path.GetFileName(networkConfig.RagConfig); + var ragTempPath = $"{_appSettings.TempDirectory}{ragFileName}"; + await File.WriteAllTextAsync(ragTempPath, networkConfig.RagConfig); + networkConfig.RagConfig = ragTempPath; + } + + // 复制核心网络配置值和IMS配置值并更新路径 + foreach (var config in networkConfig.CoreOrImsConfigs) + { + if (!string.IsNullOrEmpty(config.CoreNetworkConfig)) + { + var coreFileName = Path.GetFileName(config.CoreNetworkConfig); + var coreTempPath = $"{_appSettings.TempDirectory}{coreFileName}"; + await File.WriteAllTextAsync(coreTempPath, config.CoreNetworkConfig); + config.CoreNetworkConfig = coreTempPath; + } + + if (!string.IsNullOrEmpty(config.ImsConfig)) + { + var imsFileName = Path.GetFileName(config.ImsConfig); + var imsTempPath = $"{_appSettings.TempDirectory}{imsFileName}"; + await File.WriteAllTextAsync(imsTempPath, config.ImsConfig); + config.ImsConfig = imsTempPath; + } + } + + return true; + } + catch (Exception ex) + { + _logger.LogError(ex, "复制配置值到临时目录时发生错误"); + return false; + } + } + private bool HasBothRagAndCoreConfigs(NetworkConfiguration config) { return !string.IsNullOrWhiteSpace(config.RagConfig) && config.CoreOrImsConfigs.Any(); diff --git a/CoreAgent.Infrastructure/Services/Network/Extensions/ServiceCollectionExtensions.cs b/CoreAgent.Infrastructure/Services/Network/Extensions/ServiceCollectionExtensions.cs deleted file mode 100644 index 36e153a..0000000 --- a/CoreAgent.Infrastructure/Services/Network/Extensions/ServiceCollectionExtensions.cs +++ /dev/null @@ -1,13 +0,0 @@ -using CoreAgent.Domain.Interfaces.Network; -using Microsoft.Extensions.DependencyInjection; - -namespace CoreAgent.Infrastructure.Services.Network.Extensions; - -public static class ServiceCollectionExtensions -{ - public static IServiceCollection AddNetworkServices(this IServiceCollection services) - { - services.AddScoped(); - return services; - } -} \ No newline at end of file