|
@ -153,50 +153,81 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public async Task<CellularNetworkOperationResult> StopAsync(string Key) |
|
|
/// <summary>
|
|
|
|
|
|
/// 停止蜂窝网络
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="key">网络配置键</param>
|
|
|
|
|
|
/// <returns>停止操作的结果</returns>
|
|
|
|
|
|
public async Task<CellularNetworkOperationResult> StopAsync(string key) |
|
|
{ |
|
|
{ |
|
|
string neConfigKey = _context.GetNeConfigKey(); |
|
|
string neConfigKey = _context.GetNeConfigKey(); |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
// 1. 检查网络状态
|
|
|
// 1. 检查当前网络状态
|
|
|
var state = _context.GetNetworkState(); |
|
|
var state = _context.GetNetworkState(); |
|
|
if (state.CurrentStatus == NetworkStatus.Disconnected || state.CurrentStatus == NetworkStatus.Unknown) |
|
|
if (state.CurrentStatus == NetworkStatus.Disconnected || state.CurrentStatus == NetworkStatus.Unknown) |
|
|
{ |
|
|
{ |
|
|
_logger.LogWarning("蜂窝网络已经处于断开或未知状态,无需停止"); |
|
|
_logger.LogWarning("蜂窝网络已经处于断开或未知状态,无需停止"); |
|
|
_context.Reset(); // 即使不需要停止,也重置状态
|
|
|
_context.Reset(); // 重置上下文状态
|
|
|
return CellularNetworkOperationResult.Success(NetworkStatus.Disconnected); |
|
|
return CellularNetworkOperationResult.Success(NetworkStatus.Disconnected); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 2. 执行初始化命令
|
|
|
// 2. 检查 RAN 退出状态(仅当配置类型为 BothRagAndCore 时)
|
|
|
|
|
|
if (_context.CurrentConfigType == NetworkConfigType.BothRagAndCore) |
|
|
|
|
|
{ |
|
|
|
|
|
var isRanQuit = await _statusMonitor.CheckRanQuitAsync(_context.NetworkIPEndPointManager.GetRanEndPoint()); |
|
|
|
|
|
if (!isRanQuit) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogWarning("RAN 退出状态检查失败"); |
|
|
|
|
|
return CellularNetworkOperationResult.Failure("RAN 退出状态检查失败"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 3. 执行网络接口初始化命令
|
|
|
var initResult = await _interfaceManager.ExecuteInitializeCommandsAsync(); |
|
|
var initResult = await _interfaceManager.ExecuteInitializeCommandsAsync(); |
|
|
if (!initResult.IsSuccess) |
|
|
if (!initResult.IsSuccess) |
|
|
{ |
|
|
{ |
|
|
_logger.LogWarning("执行初始化命令失败: {ErrorMessage}", initResult.ErrorMessage); |
|
|
_logger.LogWarning("执行初始化命令失败: {ErrorMessage}", initResult.ErrorMessage); |
|
|
} |
|
|
} |
|
|
// 4. 停止网络配置
|
|
|
|
|
|
|
|
|
// 4. 禁用网络配置
|
|
|
var disableResult = await _interfaceManager.DisableAsync(neConfigKey); |
|
|
var disableResult = await _interfaceManager.DisableAsync(neConfigKey); |
|
|
if (!disableResult.IsSuccess) |
|
|
if (!disableResult.IsSuccess) |
|
|
{ |
|
|
{ |
|
|
return CellularNetworkOperationResult.Failure(disableResult.ErrorMessage); |
|
|
return CellularNetworkOperationResult.Failure(disableResult.ErrorMessage); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 5. 更新状态
|
|
|
// 5. 收集所有网络端点信息
|
|
|
_logger.LogInformation($"蜂窝网络配置 {neConfigKey} 停止成功"); |
|
|
var endPoints = new NetworkIPEndPointCollection |
|
|
|
|
|
{ |
|
|
|
|
|
RanEndPoint = _context.NetworkIPEndPointManager.GetRanEndPoint(), |
|
|
|
|
|
ImsEndPoints = _context.NetworkIPEndPointManager.GetImsEndPoints(), |
|
|
|
|
|
CnEndPoints = _context.NetworkIPEndPointManager.GetCnEndPoints() |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// 6. 检查网络端点连接状态
|
|
|
|
|
|
_logger.LogInformation("开始检查所有网络端点的连接状态"); |
|
|
|
|
|
var statusCheckResult = await _statusMonitor.CheckAllEndPointsStatusAsync(endPoints, _context.CurrentConfigType); |
|
|
|
|
|
if (statusCheckResult.IsSuccess) |
|
|
|
|
|
{ |
|
|
|
|
|
_logger.LogWarning("网络端点仍然处于连接状态,停止操作失败"); |
|
|
|
|
|
return CellularNetworkOperationResult.Failure("网络端点仍然处于连接状态,停止操作失败"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 6. 重置上下文
|
|
|
// 7. 重置上下文并返回成功结果
|
|
|
_context.Reset(); |
|
|
_context.Reset(); |
|
|
return CellularNetworkOperationResult.Success(NetworkStatus.Disconnected); |
|
|
return CellularNetworkOperationResult.Success(NetworkStatus.Disconnected); |
|
|
} |
|
|
} |
|
|
catch (Exception ex) |
|
|
catch (Exception ex) |
|
|
{ |
|
|
{ |
|
|
_logger.LogError(ex, $"停止蜂窝网络配置 {neConfigKey} 失败"); |
|
|
_logger.LogError(ex, "停止蜂窝网络配置 {ConfigKey} 失败", neConfigKey); |
|
|
return CellularNetworkOperationResult.Failure($"停止蜂窝网络失败: {ex.Message}"); |
|
|
return CellularNetworkOperationResult.Failure($"停止蜂窝网络失败: {ex.Message}"); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private async Task<CellularNetworkOperationResult> StartNetworkAsync(string key) |
|
|
private async Task<CellularNetworkOperationResult> StartNetworkAsync(string key) |
|
|
{ |
|
|
{ |
|
|
// 1. 获取并验证配置
|
|
|
// 1. 获取并验证网络配置
|
|
|
var config = await _configService.GetByConfigKeyAsync(key); |
|
|
var config = await _configService.GetByConfigKeyAsync(key); |
|
|
if (config == null) |
|
|
if (config == null) |
|
|
{ |
|
|
{ |
|
@ -205,14 +236,14 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
return CellularNetworkOperationResult.Failure(message); |
|
|
return CellularNetworkOperationResult.Failure(message); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 2. 执行初始化命令
|
|
|
// 2. 执行网络接口初始化命令
|
|
|
var initResult = await _interfaceManager.ExecuteInitializeCommandsAsync(true); |
|
|
var initResult = await _interfaceManager.ExecuteInitializeCommandsAsync(true); |
|
|
if (!initResult.IsSuccess) |
|
|
if (!initResult.IsSuccess) |
|
|
{ |
|
|
{ |
|
|
_logger.LogWarning("执行初始化命令失败: {ErrorMessage}", initResult.ErrorMessage); |
|
|
_logger.LogWarning("执行初始化命令失败: {ErrorMessage}", initResult.ErrorMessage); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 3. 复制配置值到临时目录并更新配置路径
|
|
|
// 3. 复制配置值到临时目录
|
|
|
var copyResult = await _configCopier.CopyConfigValuesToTempAsync(config, _context.GetAppSettings()); |
|
|
var copyResult = await _configCopier.CopyConfigValuesToTempAsync(config, _context.GetAppSettings()); |
|
|
if (!copyResult.IsSuccess) |
|
|
if (!copyResult.IsSuccess) |
|
|
{ |
|
|
{ |
|
@ -221,7 +252,7 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
return CellularNetworkOperationResult.Failure(message); |
|
|
return CellularNetworkOperationResult.Failure(message); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 4. 获取 IP 端点信息
|
|
|
// 4. 获取并验证 IP 端点信息
|
|
|
var (endPoints, hasAnyEndPoint) = await _configCopier.GetComAddrInfoAsync(config); |
|
|
var (endPoints, hasAnyEndPoint) = await _configCopier.GetComAddrInfoAsync(config); |
|
|
if (!hasAnyEndPoint) |
|
|
if (!hasAnyEndPoint) |
|
|
{ |
|
|
{ |
|
@ -247,7 +278,7 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
_context.UpdateNetworkConfigType(enableResult.ConfigType); |
|
|
_context.UpdateNetworkConfigType(enableResult.ConfigType); |
|
|
_logger.LogInformation("更新网络配置类型: {ConfigType}", enableResult.ConfigType); |
|
|
_logger.LogInformation("更新网络配置类型: {ConfigType}", enableResult.ConfigType); |
|
|
|
|
|
|
|
|
// 8. 检查所有网络端点的连接状态
|
|
|
// 8. 检查网络端点连接状态
|
|
|
_logger.LogInformation("开始检查所有网络端点的连接状态"); |
|
|
_logger.LogInformation("开始检查所有网络端点的连接状态"); |
|
|
var statusCheckResult = await _statusMonitor.CheckAllEndPointsStatusAsync(endPoints, enableResult.ConfigType); |
|
|
var statusCheckResult = await _statusMonitor.CheckAllEndPointsStatusAsync(endPoints, enableResult.ConfigType); |
|
|
if (!statusCheckResult.IsSuccess) |
|
|
if (!statusCheckResult.IsSuccess) |
|
@ -258,7 +289,7 @@ public class CellularNetworkService : ICellularNetworkService |
|
|
} |
|
|
} |
|
|
_logger.LogInformation("网络端点状态检查完成,所有端点状态正常"); |
|
|
_logger.LogInformation("网络端点状态检查完成,所有端点状态正常"); |
|
|
|
|
|
|
|
|
// 9. 更新状态
|
|
|
// 9. 更新网络状态并返回结果
|
|
|
var state = _context.GetNetworkState(); |
|
|
var state = _context.GetNetworkState(); |
|
|
state.MarkAsStarted(); |
|
|
state.MarkAsStarted(); |
|
|
_logger.LogInformation("蜂窝网络配置 {ConfigKey} 启动成功,当前状态: {Status}", key, state.CurrentStatus); |
|
|
_logger.LogInformation("蜂窝网络配置 {ConfigKey} 启动成功,当前状态: {Status}", key, state.CurrentStatus); |
|
|