Browse Source
- 修改CreateCellularNetworkConfigurationFile返回类型为元组 - 修改CreateCoreNetworkImsConfigurationFiles返回类型为元组 - 更新接口定义和调用代码 - 简化错误处理,提高代码可读性 - 更新modify.md记录修改内容feature/protocol-log-Perfect
9 changed files with 1394 additions and 209 deletions
@ -0,0 +1,25 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Models.Network; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Interfaces.Network |
||||
|
{ |
||||
|
public interface IGeneralCellularNetworkService |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 启动蜂窝网络
|
||||
|
/// </summary>
|
||||
|
/// <param name="Key">网络配置映射Key</param>
|
||||
|
/// <returns>启动结果</returns>
|
||||
|
Task<CellularNetworkOperationResult> StartAsync(CellularNetworkConfiguration cellular); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 停止蜂窝网络
|
||||
|
/// </summary>
|
||||
|
/// <returns>停止结果</returns>
|
||||
|
Task<CellularNetworkOperationResult> StopAsync(string RuntimeCode); |
||||
|
} |
||||
|
} |
@ -0,0 +1,60 @@ |
|||||
|
using System.ComponentModel.DataAnnotations; |
||||
|
|
||||
|
namespace CoreAgent.Domain.Models.Network; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 蜂窝网络配置实体
|
||||
|
/// </summary>
|
||||
|
public class CellularNetworkConfiguration |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 设备代码
|
||||
|
/// </summary>
|
||||
|
[Required(ErrorMessage = "设备代码不能为空")] |
||||
|
public string DeviceCode { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 运行时代码
|
||||
|
/// </summary>
|
||||
|
[Required(ErrorMessage = "运行时代码不能为空")] |
||||
|
public string RuntimeCode { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 无线接入网配置
|
||||
|
/// </summary>
|
||||
|
public string RadioAccessNetworkConfiguration { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 核心网IMS配置集合
|
||||
|
/// </summary>
|
||||
|
public List<CoreNetworkImsConfiguration> CoreNetworkImsConfigurations { get; set; } = new List<CoreNetworkImsConfiguration>(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 核心网IMS配置对象
|
||||
|
/// </summary>
|
||||
|
public class CoreNetworkImsConfiguration |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 索引
|
||||
|
/// </summary>
|
||||
|
public int Index { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// PLMN标识
|
||||
|
/// </summary>
|
||||
|
[Required(ErrorMessage = "PLMN标识不能为空")] |
||||
|
public string Plmn { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 核心网配置
|
||||
|
/// </summary>
|
||||
|
public string CoreNetworkConfiguration { get; set; } |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// IMS服务配置
|
||||
|
/// </summary>
|
||||
|
public string ImsServiceConfiguration { get; set; } |
||||
|
|
||||
|
} |
@ -0,0 +1,512 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using CoreAgent.Domain.Entities; |
||||
|
using CoreAgent.Domain.Interfaces.Network; |
||||
|
using CoreAgent.Domain.Interfaces.System.Command; |
||||
|
using CoreAgent.Domain.Interfaces; |
||||
|
using CoreAgent.Domain.Models.Network; |
||||
|
using CoreAgent.ProtocolClient.Interfaces; |
||||
|
using CoreAgent.ProtocolClient.ProtocolEngineCore; |
||||
|
using CoreAgent.WebSocketTransport.Interfaces; |
||||
|
using Microsoft.Extensions.Logging; |
||||
|
|
||||
|
namespace CoreAgent.Infrastructure.Services.Network |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// 蜂窝网络服务实现
|
||||
|
/// </summary>
|
||||
|
public class GeneralCellularNetworkService : IGeneralCellularNetworkService |
||||
|
{ |
||||
|
private readonly ILogger<CellularNetworkService> _logger; |
||||
|
private readonly ILoggerFactory _loggerFactory; |
||||
|
private readonly ISystemCommandExecutor _commandExecutor; |
||||
|
private readonly INetworkConfigurationService _configService; |
||||
|
private readonly ICellularNetworkContext _context; |
||||
|
private readonly INetworkConfigCopier _configCopier; |
||||
|
private readonly INetworkInterfaceManager _interfaceManager; |
||||
|
private readonly INetworkStatusMonitor _statusMonitor; |
||||
|
private readonly IWebSocketTransport _webSocketTransport; |
||||
|
private readonly IProtocolLogObserver _protocolLogObserver; |
||||
|
private readonly IProtocolWsClientManager _protocolWsClientManager; |
||||
|
private static readonly SemaphoreSlim _startLock = new(1, 1); |
||||
|
private const int LockTimeoutSeconds = 60; |
||||
|
|
||||
|
|
||||
|
public GeneralCellularNetworkService( |
||||
|
ILogger<CellularNetworkService> logger, |
||||
|
ILoggerFactory loggerFactory, |
||||
|
ISystemCommandExecutor commandExecutor, |
||||
|
INetworkConfigurationService configService, |
||||
|
ICellularNetworkContext context, |
||||
|
INetworkConfigCopier configCopier, |
||||
|
INetworkInterfaceManager interfaceManager, |
||||
|
INetworkStatusMonitor statusMonitor, |
||||
|
IWebSocketTransport webSocketTransport, |
||||
|
IProtocolLogObserver protocolLogObserver, |
||||
|
IProtocolWsClientManager protocolWsClientManager) |
||||
|
{ |
||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger)); |
||||
|
_loggerFactory = loggerFactory ?? throw new ArgumentNullException(nameof(loggerFactory)); |
||||
|
_commandExecutor = commandExecutor ?? throw new ArgumentNullException(nameof(commandExecutor)); |
||||
|
_configService = configService ?? throw new ArgumentNullException(nameof(configService)); |
||||
|
_context = context ?? throw new ArgumentNullException(nameof(context)); |
||||
|
_configCopier = configCopier ?? throw new ArgumentNullException(nameof(configCopier)); |
||||
|
_interfaceManager = interfaceManager ?? throw new ArgumentNullException(nameof(interfaceManager)); |
||||
|
_statusMonitor = statusMonitor; |
||||
|
_webSocketTransport = webSocketTransport ?? throw new ArgumentNullException(nameof(webSocketTransport)); |
||||
|
_protocolLogObserver = protocolLogObserver ?? throw new ArgumentNullException(nameof(protocolLogObserver)); |
||||
|
_protocolWsClientManager = protocolWsClientManager ?? throw new ArgumentNullException(nameof(protocolWsClientManager)); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 启动蜂窝网络
|
||||
|
/// </summary>
|
||||
|
/// <param name="key">网络配置键</param>
|
||||
|
/// <returns>启动结果</returns>
|
||||
|
public async Task<CellularNetworkOperationResult> StartAsync(CellularNetworkConfiguration cellular) |
||||
|
{ |
||||
|
string key = cellular.RuntimeCode; |
||||
|
// 1. 参数验证
|
||||
|
if (string.IsNullOrEmpty(key)) |
||||
|
{ |
||||
|
_logger.LogError("启动蜂窝网络失败:配置键为空"); |
||||
|
return CellularNetworkOperationResult.Failure("启动蜂窝网络失败:配置键为空"); |
||||
|
} |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
// 2. 获取启动锁,防止并发启动
|
||||
|
if (!await _startLock.WaitAsync(TimeSpan.FromSeconds(LockTimeoutSeconds))) |
||||
|
{ |
||||
|
_logger.LogWarning("蜂窝网络启动操作被锁定,可能已有其他启动操作正在进行"); |
||||
|
return CellularNetworkOperationResult.Failure("蜂窝网络启动操作被锁定,可能已有其他启动操作正在进行"); |
||||
|
} |
||||
|
|
||||
|
// 3. 状态检查
|
||||
|
// 如果是第二次启动(已初始化),需要先检查网络状态
|
||||
|
if (_context.IsInitialized) |
||||
|
{ |
||||
|
// 检查网络状态,确保没有冲突的配置
|
||||
|
var stateCheckResult = CheckNetworkState(key); |
||||
|
if (!stateCheckResult.IsSuccess) |
||||
|
{ |
||||
|
return stateCheckResult; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 4. 初始化网络上下文
|
||||
|
_context.Initialize(key); |
||||
|
|
||||
|
// 5. 启动网络
|
||||
|
var result = await StartNetworkAsync(cellular); |
||||
|
if (!result.IsSuccess) |
||||
|
{ |
||||
|
_logger.LogWarning("启动蜂窝网络内部操作失败,重置上下文"); |
||||
|
_context.Reset(); |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "启动蜂窝网络失败"); |
||||
|
// 重置上下文
|
||||
|
_context.Reset(); |
||||
|
return CellularNetworkOperationResult.Failure($"启动蜂窝网络失败: {ex.Message}"); |
||||
|
} |
||||
|
finally |
||||
|
{ |
||||
|
_startLock.Release(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 停止蜂窝网络
|
||||
|
/// </summary>
|
||||
|
/// <param name="key">网络配置键</param>
|
||||
|
/// <returns>停止操作的结果</returns>
|
||||
|
public async Task<CellularNetworkOperationResult> StopAsync(string key) |
||||
|
{ |
||||
|
string neConfigKey = _context.GetNeConfigKey(); |
||||
|
try |
||||
|
{ |
||||
|
// 检查是否是相同的配置
|
||||
|
if (key != neConfigKey) |
||||
|
{ |
||||
|
var message = $"停止操作失败:当前运行配置 {neConfigKey} 与请求停止的配置 {key} 不匹配"; |
||||
|
_logger.LogWarning(message); |
||||
|
return CellularNetworkOperationResult.Failure(message); |
||||
|
} |
||||
|
|
||||
|
// 1. 检查当前网络状态
|
||||
|
var state = _context.GetNetworkState(); |
||||
|
if (state.CurrentStatus == NetworkStatus.Disconnected || state.CurrentStatus == NetworkStatus.Unknown) |
||||
|
{ |
||||
|
_logger.LogWarning("蜂窝网络已经处于断开或未知状态,无需停止"); |
||||
|
_context.Reset(); // 重置上下文状态
|
||||
|
return CellularNetworkOperationResult.Success(NetworkStatus.Disconnected); |
||||
|
} |
||||
|
|
||||
|
// 2. 检查 RAN 退出状态(仅当配置类型为 BothRagAndCore 或 RagOnly 时)
|
||||
|
if (_context.CurrentConfigType is NetworkConfigType.BothRagAndCore or NetworkConfigType.RagOnly) |
||||
|
{ |
||||
|
var isRanQuit = await _statusMonitor.CheckRanQuitAsync(_context.NetworkIPEndPointManager.GetRanEndPoint()); |
||||
|
if (!isRanQuit) |
||||
|
{ |
||||
|
_logger.LogWarning("RAN 退出状态检查失败"); |
||||
|
return CellularNetworkOperationResult.Failure("RAN 退出状态检查失败"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 3. 执行网络接口初始化命令
|
||||
|
var initResult = await _interfaceManager.ExecuteInitializeCommandsAsync(); |
||||
|
if (!initResult.IsSuccess) |
||||
|
{ |
||||
|
_logger.LogWarning("执行初始化命令失败: {ErrorMessage}", initResult.ErrorMessage); |
||||
|
} |
||||
|
|
||||
|
// 4. 禁用网络配置
|
||||
|
var disableResult = await _interfaceManager.DisableAsync(neConfigKey); |
||||
|
if (!disableResult.IsSuccess) |
||||
|
{ |
||||
|
return CellularNetworkOperationResult.Failure(disableResult.ErrorMessage); |
||||
|
} |
||||
|
|
||||
|
// 5. 停止所有协议客户端
|
||||
|
try |
||||
|
{ |
||||
|
_logger.LogInformation("开始停止所有协议客户端"); |
||||
|
_protocolWsClientManager.StopAllClients(); |
||||
|
_logger.LogInformation("所有协议客户端停止完成"); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "停止协议客户端失败"); |
||||
|
// 不返回失败,继续执行后续步骤
|
||||
|
} |
||||
|
|
||||
|
// 6. 停止 WebSocket 传输连接
|
||||
|
var webSocketConnected = await StopWebSocketTransportAsync(); |
||||
|
if (!webSocketConnected) |
||||
|
{ |
||||
|
_logger.LogError("WebSocket 传输连接停止失败,服务端可能未启动"); |
||||
|
return CellularNetworkOperationResult.Failure("WebSocket 传输连接停止失败"); |
||||
|
} |
||||
|
_logger.LogInformation("WebSocket 传输连接停止成功"); |
||||
|
// 7. 收集所有网络端点信息
|
||||
|
var endPoints = new NetworkIPEndPointCollection |
||||
|
{ |
||||
|
RanEndPoint = _context.NetworkIPEndPointManager.GetRanEndPoint(), |
||||
|
ImsEndPoints = _context.NetworkIPEndPointManager.GetImsEndPoints(), |
||||
|
CnEndPoints = _context.NetworkIPEndPointManager.GetCnEndPoints() |
||||
|
}; |
||||
|
|
||||
|
// 8. 检查网络端点连接状态
|
||||
|
_logger.LogInformation("开始检查所有网络端点的连接状态"); |
||||
|
var statusCheckResult = await _statusMonitor.CheckAllEndPointsStatusAsync(endPoints, _context.CurrentConfigType, isStartOperation: false); |
||||
|
if (!statusCheckResult.IsSuccess) |
||||
|
{ |
||||
|
_logger.LogWarning("网络端点仍然处于连接状态,停止操作失败"); |
||||
|
return CellularNetworkOperationResult.Failure("网络端点仍然处于连接状态,停止操作失败"); |
||||
|
} |
||||
|
|
||||
|
// 9. 重置上下文并返回成功结果
|
||||
|
_context.Reset(); |
||||
|
return CellularNetworkOperationResult.Success(NetworkStatus.Disconnected); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "停止蜂窝网络配置 {ConfigKey} 失败", neConfigKey); |
||||
|
return CellularNetworkOperationResult.Failure($"停止蜂窝网络失败: {ex.Message}"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 检查网络状态
|
||||
|
/// </summary>
|
||||
|
/// <param name="newConfigKey">新的网络配置键</param>
|
||||
|
/// <returns>检查结果</returns>
|
||||
|
private CellularNetworkOperationResult CheckNetworkState(string newConfigKey) |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
// 获取当前网络状态
|
||||
|
var currentKey = _context.GetNeConfigKey(); |
||||
|
var currentState = _context.GetNetworkState(); |
||||
|
|
||||
|
// 检查是否已初始化
|
||||
|
if (!string.IsNullOrEmpty(currentKey)) |
||||
|
{ |
||||
|
// 检查是否是相同的配置
|
||||
|
if (currentKey == newConfigKey) |
||||
|
{ |
||||
|
// 检查当前状态
|
||||
|
if (currentState.CurrentStatus == NetworkStatus.Connected) |
||||
|
{ |
||||
|
return CellularNetworkOperationResult.Failure("当前网络配置已经处于连接状态"); |
||||
|
} |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
// 检查当前网络状态
|
||||
|
if (currentState.CurrentStatus == NetworkStatus.Connected) |
||||
|
{ |
||||
|
var message = $"检测到不同的网络配置,当前运行配置: {currentKey},新配置: {newConfigKey},请先停止当前网络"; |
||||
|
_logger.LogWarning(message); |
||||
|
return CellularNetworkOperationResult.Failure(message); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
return CellularNetworkOperationResult.Success(NetworkStatus.Unknown); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "检查网络状态失败"); |
||||
|
return CellularNetworkOperationResult.Failure($"检查网络状态失败: {ex.Message}"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private async Task<CellularNetworkOperationResult> StartNetworkAsync(CellularNetworkConfiguration cellular) |
||||
|
{ |
||||
|
string key =cellular.RuntimeCode; |
||||
|
var startTime = DateTime.UtcNow; |
||||
|
_logger.LogInformation("开始启动网络配置 {ConfigKey},开始时间: {StartTime}", key, startTime.ToString("yyyy-MM-dd HH:mm:ss.fff")); |
||||
|
|
||||
|
// 1. 获取并验证网络配置
|
||||
|
var step1Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤1开始:获取并验证网络配置"); |
||||
|
|
||||
|
var (isSuccess, errorMsg, config) = await _configCopier.CreateCellularNetworkConfigurationFile(cellular, _context.GetAppSettings()); |
||||
|
if (!isSuccess) |
||||
|
{ |
||||
|
var message = $"创建网络配置文件失败: {errorMsg}"; |
||||
|
_logger.LogError(message); |
||||
|
return CellularNetworkOperationResult.Failure(message); |
||||
|
} |
||||
|
|
||||
|
var step1Duration = (DateTime.UtcNow - step1Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤1完成:获取并验证网络配置,耗时: {Duration}ms", step1Duration.ToString("F2")); |
||||
|
|
||||
|
// 2. 执行网络接口初始化命令
|
||||
|
var step2Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤2开始:执行网络接口初始化命令"); |
||||
|
|
||||
|
var initResult = await _interfaceManager.ExecuteInitializeCommandsAsync(true); |
||||
|
if (!initResult.IsSuccess) |
||||
|
{ |
||||
|
_logger.LogWarning("执行初始化命令失败: {ErrorMessage}", initResult.ErrorMessage); |
||||
|
} |
||||
|
|
||||
|
var step2Duration = (DateTime.UtcNow - step2Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤2完成:执行网络接口初始化命令,耗时: {Duration}ms", step2Duration.ToString("F2")); |
||||
|
|
||||
|
// 3. 复制配置值到临时目录
|
||||
|
var step3Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤3开始:复制配置值到临时目录"); |
||||
|
|
||||
|
var copyResult = _configCopier.CopyConfigValuesToTempAsync(config, _context.GetAppSettings()); |
||||
|
if (!copyResult.IsSuccess) |
||||
|
{ |
||||
|
var message = $"复制配置值到临时目录失败: {copyResult.ErrorMessage}"; |
||||
|
_logger.LogError(message); |
||||
|
return CellularNetworkOperationResult.Failure(message); |
||||
|
} |
||||
|
|
||||
|
var step3Duration = (DateTime.UtcNow - step3Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤3完成:复制配置值到临时目录,耗时: {Duration}ms", step3Duration.ToString("F2")); |
||||
|
|
||||
|
// 4. 获取并验证 IP 端点信息
|
||||
|
var step4Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤4开始:获取并验证 IP 端点信息"); |
||||
|
|
||||
|
var (endPoints, hasAnyEndPoint) = await _configCopier.GetComAddrInfoAsync(config); |
||||
|
if (!hasAnyEndPoint) |
||||
|
{ |
||||
|
var message = "未获取到任何有效的 IP 端点信息"; |
||||
|
_logger.LogError(message); |
||||
|
return CellularNetworkOperationResult.Failure(message); |
||||
|
} |
||||
|
|
||||
|
var step4Duration = (DateTime.UtcNow - step4Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤4完成:获取并验证 IP 端点信息,耗时: {Duration}ms", step4Duration.ToString("F2")); |
||||
|
|
||||
|
// 5. 更新 IP 端点管理器
|
||||
|
var step5Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤5开始:更新 IP 端点管理器"); |
||||
|
|
||||
|
_context.NetworkIPEndPointManager.UpdateEndPoints(endPoints); |
||||
|
|
||||
|
var step5Duration = (DateTime.UtcNow - step5Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤5完成:更新 IP 端点管理器,耗时: {Duration}ms", step5Duration.ToString("F2")); |
||||
|
|
||||
|
// 6. 创建协议客户端配置
|
||||
|
var step6Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤6开始:创建协议客户端配置"); |
||||
|
|
||||
|
var protocolConfigFactory = new ProtocolClientConfigFactory(_loggerFactory.CreateLogger<ProtocolClientConfigFactory>(), _context); |
||||
|
var configCreated = protocolConfigFactory.CreateFromEntities(); |
||||
|
if (configCreated) |
||||
|
{ |
||||
|
_logger.LogInformation("协议客户端配置创建成功,共创建 {ConfigCount} 个配置", protocolConfigFactory.ConfigCount); |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
_logger.LogWarning("协议客户端配置创建失败"); |
||||
|
return CellularNetworkOperationResult.Failure("协议客户端配置创建失败"); |
||||
|
} |
||||
|
|
||||
|
var step6Duration = (DateTime.UtcNow - step6Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤6完成:创建协议客户端配置,耗时: {Duration}ms", step6Duration.ToString("F2")); |
||||
|
|
||||
|
// 7. 启动 WebSocket 传输连接
|
||||
|
var step7Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤7开始:启动 WebSocket 传输连接"); |
||||
|
|
||||
|
var webSocketConnected = await StartWebSocketTransportAsync(); |
||||
|
if (!webSocketConnected) |
||||
|
{ |
||||
|
_logger.LogError("WebSocket 传输连接启动失败,服务端可能未启动"); |
||||
|
return CellularNetworkOperationResult.Failure("WebSocket 传输连接启动失败,服务端可能未启动"); |
||||
|
} |
||||
|
_logger.LogInformation("WebSocket 传输连接启动成功"); |
||||
|
|
||||
|
var step7Duration = (DateTime.UtcNow - step7Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤7完成:启动 WebSocket 传输连接,耗时: {Duration}ms", step7Duration.ToString("F2")); |
||||
|
|
||||
|
// 8. 启动网络配置
|
||||
|
var step8Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤8开始:启动网络配置"); |
||||
|
_logger.LogInformation("正在启动蜂窝网络配置: {ConfigKey}", key); |
||||
|
|
||||
|
var enableResult = await _interfaceManager.EnableAsync(config); |
||||
|
if (!enableResult.IsSuccess) |
||||
|
{ |
||||
|
var message = $"启动网络配置失败: {enableResult.ErrorMessage}"; |
||||
|
_logger.LogError(message); |
||||
|
return CellularNetworkOperationResult.Failure(message); |
||||
|
} |
||||
|
|
||||
|
var step8Duration = (DateTime.UtcNow - step8Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤8完成:启动网络配置,耗时: {Duration}ms", step8Duration.ToString("F2")); |
||||
|
|
||||
|
// 9. 更新网络配置类型
|
||||
|
var step9Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤9开始:更新网络配置类型"); |
||||
|
|
||||
|
_context.UpdateNetworkConfigType(enableResult.ConfigType); |
||||
|
_logger.LogInformation("更新网络配置类型: {ConfigType}", enableResult.ConfigType); |
||||
|
|
||||
|
var step9Duration = (DateTime.UtcNow - step9Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤9完成:更新网络配置类型,耗时: {Duration}ms", step9Duration.ToString("F2")); |
||||
|
|
||||
|
// 10. 检查网络端点连接状态
|
||||
|
var step10Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤10开始:检查网络端点连接状态"); |
||||
|
_logger.LogInformation("开始检查所有网络端点的连接状态"); |
||||
|
|
||||
|
var statusCheckResult = await _statusMonitor.CheckAllEndPointsStatusAsync(endPoints, enableResult.ConfigType, isStartOperation: true); |
||||
|
if (!statusCheckResult.IsSuccess) |
||||
|
{ |
||||
|
var errorMessage = string.Join("; ", statusCheckResult.ErrorMessage); |
||||
|
_logger.LogWarning("网络端点状态检查未通过: {ErrorMessage}", errorMessage); |
||||
|
return CellularNetworkOperationResult.Failure($"网络端点状态检查失败: {errorMessage}"); |
||||
|
} |
||||
|
_logger.LogInformation("网络端点状态检查完成,所有端点状态正常"); |
||||
|
|
||||
|
var step10Duration = (DateTime.UtcNow - step10Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤10完成:检查网络端点连接状态,耗时: {Duration}ms", step10Duration.ToString("F2")); |
||||
|
|
||||
|
// 11. 更新网络状态
|
||||
|
var step11Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤11开始:更新网络状态"); |
||||
|
|
||||
|
var state = _context.GetNetworkState(); |
||||
|
state.MarkAsStarted(); |
||||
|
|
||||
|
var step11Duration = (DateTime.UtcNow - step11Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤11完成:更新网络状态,耗时: {Duration}ms", step11Duration.ToString("F2")); |
||||
|
|
||||
|
// 12. 启动所有协议客户端
|
||||
|
var step12Start = DateTime.UtcNow; |
||||
|
_logger.LogDebug("步骤12开始:启动所有协议客户端"); |
||||
|
|
||||
|
try |
||||
|
{ |
||||
|
var protocolConfigs = protocolConfigFactory.GetAllConfigs(); |
||||
|
_protocolWsClientManager.StartAllClients(protocolConfigs); |
||||
|
_logger.LogInformation("所有协议客户端启动完成"); |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "启动协议客户端失败"); |
||||
|
return CellularNetworkOperationResult.Failure($"启动协议客户端失败: {ex.Message}"); |
||||
|
} |
||||
|
|
||||
|
var step12Duration = (DateTime.UtcNow - step12Start).TotalMilliseconds; |
||||
|
_logger.LogDebug("步骤12完成:启动所有协议客户端,耗时: {Duration}ms", step12Duration.ToString("F2")); |
||||
|
|
||||
|
var endTime = DateTime.UtcNow; |
||||
|
var duration = endTime - startTime; |
||||
|
_logger.LogInformation("蜂窝网络配置 {ConfigKey} 启动成功,当前状态: {Status},总耗时: {Duration}ms", |
||||
|
key, state.CurrentStatus, duration.TotalMilliseconds.ToString("F2")); |
||||
|
|
||||
|
// 删除临时配置文件
|
||||
|
var deleteResult = _configCopier.DeleteCellularNetworkConfigurationFile(config); |
||||
|
if (!deleteResult.IsSuccess) |
||||
|
{ |
||||
|
_logger.LogWarning("删除临时配置文件失败: {ErrorMessage}", deleteResult.ErrorMessage); |
||||
|
// 不返回失败,因为网络已经启动成功
|
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
_logger.LogInformation("临时配置文件删除成功"); |
||||
|
} |
||||
|
|
||||
|
return CellularNetworkOperationResult.Success(state.CurrentStatus); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 启动 WebSocket 传输连接
|
||||
|
/// </summary>
|
||||
|
/// <returns>连接是否成功</returns>
|
||||
|
private async Task<bool> StartWebSocketTransportAsync() |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
_logger.LogInformation("开始启动 WebSocket 传输连接"); |
||||
|
await _webSocketTransport.ConnectAsync(); |
||||
|
return _webSocketTransport.IsConnected; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "WebSocket 传输连接启动失败"); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// 停止 WebSocket 传输连接
|
||||
|
/// </summary>
|
||||
|
/// <returns>停止是否成功</returns>
|
||||
|
private async Task<bool> StopWebSocketTransportAsync() |
||||
|
{ |
||||
|
try |
||||
|
{ |
||||
|
_logger.LogInformation("开始停止 WebSocket 传输连接"); |
||||
|
await _webSocketTransport.CloseAsync(); |
||||
|
return !_webSocketTransport.IsConnected; |
||||
|
} |
||||
|
catch (Exception ex) |
||||
|
{ |
||||
|
_logger.LogError(ex, "WebSocket 传输连接停止失败"); |
||||
|
return false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue