Browse Source

refactor: 添加配置验证和提取常量,提高代码可维护性

master
root 4 days ago
parent
commit
11aa944010
  1. 60
      CoreAgent.Infrastructure/Services/Network/CellularNetworkService.cs

60
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<CellularNetworkService> logger,
@ -207,7 +209,7 @@ public class CellularNetworkService : ICellularNetworkService
private async Task<bool> 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;
}
/// <summary>
/// 验证网络配置是否有效
/// </summary>
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;
}
}
Loading…
Cancel
Save