|
@ -7,6 +7,8 @@ using CoreAgent.Domain.Interfaces.System.Command; |
|
|
using CoreAgent.Domain.Models.Network; |
|
|
using CoreAgent.Domain.Models.Network; |
|
|
using CoreAgent.Domain.Models.System; |
|
|
using CoreAgent.Domain.Models.System; |
|
|
using Microsoft.Extensions.Logging; |
|
|
using Microsoft.Extensions.Logging; |
|
|
|
|
|
using Microsoft.Extensions.Options; |
|
|
|
|
|
using System.IO; |
|
|
|
|
|
|
|
|
namespace CoreAgent.Infrastructure.Services.Network; |
|
|
namespace CoreAgent.Infrastructure.Services.Network; |
|
|
|
|
|
|
|
@ -19,6 +21,7 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
private readonly ISystemCommandExecutor _commandExecutor; |
|
|
private readonly ISystemCommandExecutor _commandExecutor; |
|
|
private readonly INetworkConfigurationService _configService; |
|
|
private readonly INetworkConfigurationService _configService; |
|
|
private readonly CellularNetworkContext _context; |
|
|
private readonly CellularNetworkContext _context; |
|
|
|
|
|
private readonly AppSettings _appSettings; |
|
|
private static readonly SemaphoreSlim _startLock = new(1, 1); |
|
|
private static readonly SemaphoreSlim _startLock = new(1, 1); |
|
|
private const int MaxConnectionAttempts = 30; |
|
|
private const int MaxConnectionAttempts = 30; |
|
|
private const int ConnectionCheckDelayMs = 1000; |
|
|
private const int ConnectionCheckDelayMs = 1000; |
|
@ -27,11 +30,13 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
public CellularNetworkService( |
|
|
public CellularNetworkService( |
|
|
ILogger<CellularNetworkService> logger, |
|
|
ILogger<CellularNetworkService> logger, |
|
|
ISystemCommandExecutor commandExecutor, |
|
|
ISystemCommandExecutor commandExecutor, |
|
|
INetworkConfigurationService configService) |
|
|
INetworkConfigurationService configService, |
|
|
|
|
|
IOptions<AppSettings> appSettings) |
|
|
{ |
|
|
{ |
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
|
|
_commandExecutor = commandExecutor ?? throw new ArgumentNullException(nameof(commandExecutor)); |
|
|
_commandExecutor = commandExecutor ?? throw new ArgumentNullException(nameof(commandExecutor)); |
|
|
_configService = configService ?? throw new ArgumentNullException(nameof(configService)); |
|
|
_configService = configService ?? throw new ArgumentNullException(nameof(configService)); |
|
|
|
|
|
_appSettings = appSettings?.Value ?? throw new ArgumentNullException(nameof(appSettings)); |
|
|
_context = CellularNetworkContext.Instance; |
|
|
_context = CellularNetworkContext.Instance; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -211,6 +216,13 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
|
|
|
// 复制配置值到临时目录并更新配置路径
|
|
|
|
|
|
if (!await CopyConfigValuesToTempAsync(networkConfig)) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogError("复制配置值到临时目录失败"); |
|
|
|
|
|
return false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
var commands = _context.GetNetworkCommandConfig(); |
|
|
var commands = _context.GetNetworkCommandConfig(); |
|
|
var startCommand = commands.NetworkCommands.FirstOrDefault(s => s.Type == NetworkCommandType.Start); |
|
|
var startCommand = commands.NetworkCommands.FirstOrDefault(s => s.Type == NetworkCommandType.Start); |
|
|
if (startCommand == null) |
|
|
if (startCommand == null) |
|
@ -243,6 +255,57 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 将配置值复制到临时目录并更新配置路径
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
private async Task<bool> 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) |
|
|
private bool HasBothRagAndCoreConfigs(NetworkConfiguration config) |
|
|
{ |
|
|
{ |
|
|
return !string.IsNullOrWhiteSpace(config.RagConfig) && config.CoreOrImsConfigs.Any(); |
|
|
return !string.IsNullOrWhiteSpace(config.RagConfig) && config.CoreOrImsConfigs.Any(); |
|
|