|
|
@ -20,7 +20,6 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
|
private readonly INetworkConfigurationService _configService; |
|
|
|
private readonly CellularNetworkContext _context; |
|
|
|
private static readonly SemaphoreSlim _startLock = new(1, 1); |
|
|
|
private const string DefaultInterfaceName = "Cellular"; |
|
|
|
private const int MaxConnectionAttempts = 30; |
|
|
|
private const int ConnectionCheckDelayMs = 1000; |
|
|
|
private const int LockTimeoutSeconds = 5; |
|
|
@ -133,7 +132,7 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
|
} |
|
|
|
|
|
|
|
// 2. 检查网络状态
|
|
|
|
var state = _context.GetOrCreateNetworkState(DefaultInterfaceName); |
|
|
|
var state = _context.GetOrCreateNetworkState(key); |
|
|
|
if (state.IsInitialized) |
|
|
|
{ |
|
|
|
_logger.LogWarning("蜂窝网络已经初始化,不能重复启动"); |
|
|
@ -144,21 +143,21 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
|
await ExecuteInitializeCommandsAsync(); |
|
|
|
|
|
|
|
// 4. 启动网络接口
|
|
|
|
_logger.LogInformation("正在启动蜂窝网络接口: {InterfaceName}", DefaultInterfaceName); |
|
|
|
_logger.LogInformation("正在启动蜂窝网络接口: {InterfaceName}", key); |
|
|
|
if (!await EnableNetworkInterfaceAsync(config)) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 5. 等待连接
|
|
|
|
if (!await WaitForConnectionAsync(DefaultInterfaceName)) |
|
|
|
if (!await WaitForConnectionAsync(key)) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 6. 更新状态
|
|
|
|
state.MarkAsStarted(); |
|
|
|
_logger.LogInformation("蜂窝网络接口 {InterfaceName} 启动成功", DefaultInterfaceName); |
|
|
|
_logger.LogInformation("蜂窝网络接口 {InterfaceName} 启动成功", key); |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
@ -281,26 +280,26 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
|
private async Task<bool> ExecuteCoreOnlyStartCommandsAsync(CommandTemplateConfig startCommand, NetworkConfiguration networkConfig) |
|
|
|
{ |
|
|
|
var commands = new List<string>(); |
|
|
|
var secondaryConfigs = networkConfig.CoreOrImsConfigs.Where(s => s.Index != 1).ToArray(); |
|
|
|
|
|
|
|
var secondaryConfigs = networkConfig.CoreOrImsConfigs.ToArray(); |
|
|
|
|
|
|
|
var secondaryTasks = new List<Task<CommandExecutionResult>>(); |
|
|
|
|
|
|
|
for (int i = 0; i < secondaryConfigs.Length; i++) |
|
|
|
{ |
|
|
|
var config = secondaryConfigs[i]; |
|
|
|
var command = string.Format(startCommand.Template, i + 1); |
|
|
|
commands.Add($"{command} NULL {config.CoreNetworkConfig} {config.ImsConfig}"); |
|
|
|
} |
|
|
|
var fullCommand = $"{command} NULL {config.CoreNetworkConfig} {config.ImsConfig}"; |
|
|
|
|
|
|
|
foreach (var command in commands) |
|
|
|
_logger.LogInformation("并发执行次要配置启动命令: {Command}", fullCommand); |
|
|
|
secondaryTasks.Add(_commandExecutor.ExecuteCommandAsync(fullCommand, _context.token)); |
|
|
|
} |
|
|
|
// 等待所有次要配置命令执行完成
|
|
|
|
var secondaryResults = await Task.WhenAll(secondaryTasks); |
|
|
|
if (secondaryResults.Any(r => !r.IsSuccess)) |
|
|
|
{ |
|
|
|
_logger.LogInformation("执行核心网配置启动命令: {Command}", command); |
|
|
|
var result = await _commandExecutor.ExecuteCommandAsync(command, _context.token); |
|
|
|
if (!result.IsSuccess) |
|
|
|
{ |
|
|
|
_logger.LogWarning("命令执行失败: {Command}", command); |
|
|
|
return false; |
|
|
|
} |
|
|
|
_logger.LogWarning("部分次要配置命令执行失败"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
@ -319,35 +318,40 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
|
{ |
|
|
|
var commands = new List<string>(); |
|
|
|
|
|
|
|
// 添加主配置命令
|
|
|
|
var primaryConfig = networkConfig.CoreOrImsConfigs.FirstOrDefault(s => s.Index == 1); |
|
|
|
if (primaryConfig != null) |
|
|
|
{ |
|
|
|
var primaryCommand = string.Format(startCommand.Template, 1); |
|
|
|
commands.Add($"{primaryCommand} {networkConfig.RagConfig} {primaryConfig.CoreNetworkConfig} {primaryConfig.ImsConfig}"); |
|
|
|
} |
|
|
|
|
|
|
|
// 添加次要配置命令
|
|
|
|
var secondaryConfigs = networkConfig.CoreOrImsConfigs.Where(s => s.Index != 1).ToArray(); |
|
|
|
var secondaryTasks = new List<Task<CommandExecutionResult>>(); |
|
|
|
|
|
|
|
for (int i = 0; i < secondaryConfigs.Length; i++) |
|
|
|
{ |
|
|
|
var config = secondaryConfigs[i]; |
|
|
|
var command = string.Format(startCommand.Template, i + 2); |
|
|
|
commands.Add($"{command} NULL {config.CoreNetworkConfig} {config.ImsConfig}"); |
|
|
|
var fullCommand = $"{command} NULL {config.CoreNetworkConfig} {config.ImsConfig}"; |
|
|
|
|
|
|
|
_logger.LogInformation("并发执行次要配置启动命令: {Command}", fullCommand); |
|
|
|
secondaryTasks.Add(_commandExecutor.ExecuteCommandAsync(fullCommand, _context.token)); |
|
|
|
} |
|
|
|
|
|
|
|
// 添加RAG配置命令
|
|
|
|
var ragCommand = string.Format(startCommand.Template, 1); |
|
|
|
commands.Add($"{ragCommand} {networkConfig.RagConfig}"); |
|
|
|
// 等待所有次要配置命令执行完成
|
|
|
|
var secondaryResults = await Task.WhenAll(secondaryTasks); |
|
|
|
if (secondaryResults.Any(r => !r.IsSuccess)) |
|
|
|
{ |
|
|
|
_logger.LogWarning("部分次要配置命令执行失败"); |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// 执行所有命令
|
|
|
|
foreach (var command in commands) |
|
|
|
// 执行主配置命令
|
|
|
|
var primaryConfig = networkConfig.CoreOrImsConfigs.FirstOrDefault(s => s.Index == 1); |
|
|
|
if (primaryConfig != null) |
|
|
|
{ |
|
|
|
_logger.LogInformation("执行多配置启动命令: {Command}", command); |
|
|
|
var result = await _commandExecutor.ExecuteCommandAsync(command, _context.token); |
|
|
|
var primaryCommand = string.Format(startCommand.Template, 1); |
|
|
|
var fullCommand = $"{primaryCommand} {networkConfig.RagConfig} {primaryConfig.CoreNetworkConfig} {primaryConfig.ImsConfig}"; |
|
|
|
|
|
|
|
_logger.LogInformation("执行主配置启动命令: {Command}", fullCommand); |
|
|
|
var result = await _commandExecutor.ExecuteCommandAsync(fullCommand, _context.token); |
|
|
|
if (!result.IsSuccess) |
|
|
|
{ |
|
|
|
_logger.LogWarning("命令执行失败: {Command}", command); |
|
|
|
_logger.LogWarning("主配置命令执行失败: {Command}", fullCommand); |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|