diff --git a/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs b/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs index 5ee5bdc..9c657b4 100644 --- a/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs +++ b/src/X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs @@ -396,7 +396,13 @@ public class StartDeviceRuntimeCommandHandler : IRequestHandler /// 启动网络连接 /// - /// 仪器编号,用于标识特定的仪器设备 + /// 蜂窝网络启动请求 /// 请求选项,包含超时、请求头等配置 /// 取消令牌,用于取消异步操作 /// 异步任务,表示网络启动操作的完成状态 - /// 当instrumentNumber为null或空时抛出 - /// 当instrumentNumber格式无效时抛出 + /// 当request为null时抛出 + /// 当request中的设备编号为空时抛出 Task StartNetworkAsync( - CellularNetworkConfiguration cellular, + CellularNetworkRequest request, 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 0819ba5..c82e5c0 100644 --- a/src/X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs +++ b/src/X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs @@ -97,19 +97,23 @@ namespace X1.DynamicClientCore.Features.Service /// /// 启动网络连接 /// - /// 设备编号 + /// 蜂窝网络启动请求 /// 请求选项 /// 取消令牌 /// 异步任务 - /// 当instrumentNumber为空或null时抛出 + /// 当request为null时抛出 + /// 当request中的设备编号为空时抛出 public async Task StartNetworkAsync( - CellularNetworkConfiguration cellular, + CellularNetworkRequest request, RequestOptions? options = null, CancellationToken cancellationToken = default) { - string instrumentNumber = cellular.DeviceCode; + if (request == null) + throw new ArgumentNullException(nameof(request)); + + string instrumentNumber = request.CellularNetwork?.DeviceCode; if (string.IsNullOrWhiteSpace(instrumentNumber)) - throw new ArgumentException("设备编号不能为空", nameof(instrumentNumber)); + throw new ArgumentException("设备编号不能为空", nameof(request)); try { @@ -121,7 +125,7 @@ namespace X1.DynamicClientCore.Features.Service var response = await _dynamicHttpClient.PostAsync>( instrumentNumber, "CellularNetwork/generalStart", - cellular, + request, options, cancellationToken); if (response == null) diff --git a/src/X1.DynamicClientCore/Models/CellularNetworkRequest.cs b/src/X1.DynamicClientCore/Models/CellularNetworkRequest.cs new file mode 100644 index 0000000..54f7e58 --- /dev/null +++ b/src/X1.DynamicClientCore/Models/CellularNetworkRequest.cs @@ -0,0 +1,14 @@ +namespace X1.DynamicClientCore.Models +{ + /// + /// 蜂窝网络启动请求包装类 + /// 用于匹配API文档要求的JSON结构 + /// + public class CellularNetworkRequest + { + /// + /// 蜂窝网络配置 + /// + public CellularNetworkConfiguration CellularNetwork { get; set; } = new(); + } +} \ No newline at end of file diff --git a/src/modify.md b/src/modify.md index d304f6e..361936d 100644 --- a/src/modify.md +++ b/src/modify.md @@ -5838,4 +5838,96 @@ private static DeviceRuntimeDto MapToDto(CellularDeviceRuntime runtime) ### 影响范围 - **编译错误**:修复了导致编译失败的语法错误 - **PLMN处理**:改进了PLMN值的提取和处理逻辑 -- **错误处理**:增强了异常处理和日志记录功能 \ No newline at end of file +- **错误处理**:增强了异常处理和日志记录功能 + +--- + +## 2025-01-29 - 修复StartNetworkAsync API请求体结构不匹配问题 + +### 修改原因 +根据API文档要求,`StartNetworkAsync` 方法发送的JSON请求体需要包含外层的 `cellularNetwork` 包装,但当前实现直接发送 `CellularNetworkConfiguration` 对象,导致请求体结构不匹配。 + +### 修改文件 +- `X1.DynamicClientCore/Models/CellularNetworkRequest.cs` - 新增包装类 +- `X1.DynamicClientCore/Features/IInstrumentProtocolClient.cs` - 修改接口签名 +- `X1.DynamicClientCore/Features/Service/InstrumentProtocolClient.cs` - 修改实现方法 +- `X1.Application/Features/DeviceRuntimes/Commands/StartDeviceRuntime/StartDeviceRuntimeCommandHandler.cs` - 修改调用方 + +### 修改内容 + +#### 1. 新增包装类 +- **类名**: `CellularNetworkRequest` +- **功能**: 包装 `CellularNetworkConfiguration` 对象,匹配API文档要求的JSON结构 +- **属性**: `CellularNetwork` - 包含实际的网络配置对象 + +#### 2. 修改接口签名 +- **参数类型**: 将 `StartNetworkAsync` 方法的参数从 `CellularNetworkConfiguration` 改为 `CellularNetworkRequest` +- **参数验证**: 添加对 `request` 参数的 null 检查 +- **文档更新**: 更新方法注释和异常说明 + +#### 3. 修改实现方法 +- **参数处理**: 从 `request.CellularNetwork` 中提取设备编号 +- **请求传递**: 直接将 `request` 对象传递给 `PostAsync` 方法 +- **错误处理**: 增强参数验证和错误处理 + +#### 4. 修改调用方 +- **包装创建**: 在调用 `StartNetworkAsync` 前创建 `CellularNetworkRequest` 包装对象 +- **参数传递**: 将包装后的对象传递给协议客户端 + +#### 5. API文档匹配 +**API文档要求的JSON结构**: +```json +{ + "cellularNetwork": { + "deviceCode": "string", + "runtimeCode": "string", + "radioAccessNetworkConfiguration": "string", + "coreNetworkImsConfigurations": [...] + } +} +``` + +**修复后的实现**: +```csharp +// 调用方 +var request = new CellularNetworkRequest +{ + CellularNetwork = networkRequest +}; +var startResult = await _protocolClient.StartNetworkAsync(request); + +// 实现方 +public async Task StartNetworkAsync( + CellularNetworkRequest request, + RequestOptions? options = null, + CancellationToken cancellationToken = default) +{ + // 直接传递 request 对象给 PostAsync + var response = await _dynamicHttpClient.PostAsync>( + instrumentNumber, + "CellularNetwork/generalStart", + request, // 使用包装后的请求对象 + options, + cancellationToken); +} +``` + +### 技术要点 +- **接口设计**: 将包装类作为接口参数,确保类型安全 +- **序列化匹配**: 确保JSON序列化后的结构与API文档要求一致 +- **参数验证**: 增强参数验证,提高代码健壮性 +- **类型安全**: 使用强类型对象确保编译时类型检查 + +### 业务价值 +- **API兼容性**: 确保与外部API的正确通信 +- **请求成功**: 修复可能导致API调用失败的结构问题 +- **调试便利**: 提供清晰的请求体结构,便于API调试 +- **代码清晰**: 明确区分请求包装和实际配置对象 + +### 影响范围 +- **接口变更**: 修改了 `IInstrumentProtocolClient` 接口的 `StartNetworkAsync` 方法签名 +- **实现更新**: 更新了 `InstrumentProtocolClient` 的实现逻辑 +- **调用方修改**: 修改了 `StartDeviceRuntimeCommandHandler` 中的调用方式 +- **API调用**: 修复了网络启动API调用的请求体结构 +- **序列化**: 改进了JSON序列化的结构匹配 +- **兼容性**: 确保与外部系统的API兼容性 \ No newline at end of file