diff --git a/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs b/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs index 9777e87..bbc5679 100644 --- a/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs +++ b/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs @@ -119,9 +119,21 @@ public class StartDeviceRuntimeCommandHandler : IRequestHandler异步任务,表示网络启动操作的完成状态 /// 当instrumentNumber为null或空时抛出 /// 当instrumentNumber格式无效时抛出 - Task StartNetworkAsync( + Task StartNetworkAsync( CellularNetworkConfiguration cellular, RequestOptions? options = null, CancellationToken cancellationToken = default); @@ -40,7 +40,7 @@ namespace X1.DynamicClientCore.Features /// 异步任务,表示网络停止操作的完成状态 /// 当instrumentNumber为null或空时抛出 /// 当instrumentNumber格式无效时抛出 - Task StopNetworkAsync( + Task StopNetworkAsync( string instrumentNumber, RequestOptions? options = null, CancellationToken cancellationToken = default); diff --git a/src/X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs b/src/X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs index 20cc538..2064d79 100644 --- a/src/X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs +++ b/src/X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs @@ -102,7 +102,7 @@ namespace X1.DynamicClientCore.Features.Service /// 取消令牌 /// 异步任务 /// 当instrumentNumber为空或null时抛出 - public async Task StartNetworkAsync( + public async Task StartNetworkAsync( CellularNetworkConfiguration cellular, RequestOptions? options = null, CancellationToken cancellationToken = default) @@ -114,20 +114,37 @@ namespace X1.DynamicClientCore.Features.Service try { _logger.LogInformation("开始启动网络连接,设备编号:{InstrumentNumber}", instrumentNumber); - var response = await _dynamicHttpClient.PostAsync>( + var response = await _dynamicHttpClient.PostAsync>( instrumentNumber, "CellularNetwork/generalStart", cellular, options, cancellationToken); - await Task.CompletedTask.ConfigureAwait(false); - + if (response == null) + { + _logger.LogWarning("获取启动网络连接失败:响应为空,端点:{EndpointName}", instrumentNumber); + return false; + } + + if (!response.IsSuccess) + { + _logger.LogWarning("获取启动网络连接失败:请求未成功,端点:{EndpointName},错误:{ErrorMessage}", + instrumentNumber, response.Message ?? "未知错误"); + return false; + } + if (response.Data != NetworkStatus.Connected) + { + _logger.LogWarning("获取启动网络连接:请求未成功,端点:{EndpointName},错误:{ErrorMessage}", + instrumentNumber, response.Message ?? "未知错误"); + return false; + } _logger.LogInformation("网络连接启动完成,设备编号:{InstrumentNumber}", instrumentNumber); + return true; } catch (Exception ex) { _logger.LogError(ex, "启动网络连接时发生异常,设备编号:{InstrumentNumber}", instrumentNumber); - throw; + return false; } } diff --git a/src/modify.md b/src/modify.md index e302260..846c7ee 100644 --- a/src/modify.md +++ b/src/modify.md @@ -1,5 +1,68 @@ # 修改记录 +## 2025-01-29 - 修复StartDeviceRuntimeCommandHandler中网络启动返回值记录问题 + +### 修改原因 +`IInstrumentProtocolClient.StartNetworkAsync` 方法返回 `Task` 表示网络启动是否成功,但原代码没有记录这个返回值,导致无法准确判断网络启动的实际结果。 + +### 修改文件 +- `X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs` - 修复网络启动返回值记录 + +### 修改内容 + +#### 1. 记录网络启动返回值 +- **获取返回值**:将 `await _protocolClient.StartNetworkAsync(networkRequest)` 的返回值保存到 `startResult` 变量 +- **记录启动结果**:添加日志记录网络启动的实际结果(成功/失败) +- **条件处理**:根据返回值决定是否将设备标记为成功启动 + +#### 2. 具体修改 +```csharp +// 修改前 +await _protocolClient.StartNetworkAsync(networkRequest); +_logger.LogDebug("网络启动成功,设备代码: {DeviceCode}", networkRequest.DeviceCode); +networkResults.Add((true, networkRequest.DeviceCode, string.Empty)); + +// 修改后 +var startResult = await _protocolClient.StartNetworkAsync(networkRequest); +_logger.LogDebug("网络启动结果,设备代码: {DeviceCode}, 启动成功: {StartResult}", + networkRequest.DeviceCode, startResult); + +if (startResult) +{ + _logger.LogDebug("网络启动成功,设备代码: {DeviceCode}", networkRequest.DeviceCode); + networkResults.Add((true, networkRequest.DeviceCode, string.Empty)); +} +else +{ + var errorMessage = "网络启动返回失败状态"; + _logger.LogWarning("网络启动返回失败状态,设备代码: {DeviceCode}", networkRequest.DeviceCode); + networkResults.Add((false, networkRequest.DeviceCode, errorMessage)); +} +``` + +#### 3. 错误处理增强 +- **返回值验证**:检查 `StartNetworkAsync` 的返回值,只有返回 `true` 才认为启动成功 +- **失败状态记录**:当返回 `false` 时,记录警告日志并将设备标记为失败 +- **异常处理保持**:保持原有的异常处理逻辑不变 + +### 技术特性 +- **返回值处理**:正确处理异步方法的返回值 +- **条件判断**:根据返回值进行条件分支处理 +- **日志记录**:详细记录网络启动的实际结果 +- **错误分类**:区分网络启动失败和异常情况 + +### 业务价值 +- **准确性**:准确反映网络启动的实际结果 +- **可追踪性**:通过日志可以追踪每个设备的网络启动状态 +- **错误诊断**:能够区分网络启动失败和异常情况,便于问题诊断 + +### 影响范围 +- **日志记录**:增加了网络启动结果的详细日志 +- **成功统计**:更准确地统计成功启动的设备数量 +- **错误处理**:更精确地处理网络启动失败的情况 + +--- + ## 2025-01-29 - 重构StartDeviceRuntimeCommandHandler,提取网络配置请求构建逻辑 ### 修改原因