From 11aa9440108326dc846afd4cc82daf25502327c3 Mon Sep 17 00:00:00 2001 From: root <295172551@qq.com> Date: Sat, 14 Jun 2025 02:03:33 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=B7=BB=E5=8A=A0=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=AA=8C=E8=AF=81=E5=92=8C=E6=8F=90=E5=8F=96=E5=B8=B8?= =?UTF-8?q?=E9=87=8F=EF=BC=8C=E6=8F=90=E9=AB=98=E4=BB=A3=E7=A0=81=E5=8F=AF?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Network/CellularNetworkService.cs | 60 ++++++++++++++++++- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs index d8255c3..f5fd078 100644 --- a/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs +++ b/CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs @@ -26,6 +26,8 @@ public class CellularNetworkService : ICellularNetworkService private const int MaxConnectionAttempts = 30; private const int ConnectionCheckDelayMs = 1000; private const int LockTimeoutSeconds = 5; + private const string NULL_CONFIG = "NULL"; + private const string KILL_COMMAND_TEMPLATE = "ps -ef | grep {0} | grep -v grep | awk '{{print $2}}' | xargs kill -9"; public CellularNetworkService( ILogger logger, @@ -207,7 +209,7 @@ public class CellularNetworkService : ICellularNetworkService private async Task KillProcessAsync(string template,int Timeout) { - string killCmd = $"ps -ef | grep {template} | grep -v grep | awk '{{print $2}}' | xargs kill -9"; + string killCmd = string.Format(KILL_COMMAND_TEMPLATE, template); var result = await _commandExecutor.ExecuteCommandAsync(killCmd, new CancellationTokenSource()); _logger.LogInformation("已终止进程: {Template}", template); return result.IsSuccess; @@ -217,6 +219,13 @@ public class CellularNetworkService : ICellularNetworkService { try { + // 验证配置 + if (!ValidateConfig(networkConfig)) + { + _logger.LogError("网络配置验证失败"); + return false; + } + // 复制配置值到临时目录并更新配置路径 if (!CopyConfigValuesToTempAsync(networkConfig)) { @@ -398,7 +407,7 @@ public class CellularNetworkService : ICellularNetworkService { var config = secondaryConfigs[i]; var command = string.Format(startCommand.Template, i + 1); - var fullCommand = $"{command} NULL {config.CoreNetworkConfig} {config.ImsConfig}"; + var fullCommand = $"{command} {NULL_CONFIG} {config.CoreNetworkConfig} {config.ImsConfig}"; _logger.LogInformation("并发执行次要配置启动命令: {Command}", fullCommand); secondaryTasks.Add(_commandExecutor.ExecuteCommandAsync(fullCommand, _context.token)); @@ -502,4 +511,51 @@ public class CellularNetworkService : ICellularNetworkService _logger.LogError("蜂窝网络连接超时"); return false; } + + /// + /// 验证网络配置是否有效 + /// + private bool ValidateConfig(NetworkConfiguration config) + { + if (config == null) + { + _logger.LogError("网络配置为空"); + return false; + } + + // 验证RAG配置 + if (!string.IsNullOrEmpty(config.RagConfig) && !File.Exists(config.RagConfig)) + { + _logger.LogError("RAG配置文件不存在: {FilePath}", config.RagConfig); + return false; + } + + // 验证核心网络和IMS配置 + if (config.CoreOrImsConfigs != null) + { + foreach (var coreConfig in config.CoreOrImsConfigs) + { + if (!string.IsNullOrEmpty(coreConfig.CoreNetworkConfig) && !File.Exists(coreConfig.CoreNetworkConfig)) + { + _logger.LogError("核心网络配置文件不存在: {FilePath}", coreConfig.CoreNetworkConfig); + return false; + } + + if (!string.IsNullOrEmpty(coreConfig.ImsConfig) && !File.Exists(coreConfig.ImsConfig)) + { + _logger.LogError("IMS配置文件不存在: {FilePath}", coreConfig.ImsConfig); + return false; + } + } + } + + // 验证至少有一个有效配置 + if (string.IsNullOrEmpty(config.RagConfig) && (config.CoreOrImsConfigs == null || !config.CoreOrImsConfigs.Any())) + { + _logger.LogError("没有有效的网络配置"); + return false; + } + + return true; + } } \ No newline at end of file