diff --git a/CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs b/CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs index 932e356..e8635eb 100644 --- a/CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs +++ b/CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs @@ -7,7 +7,7 @@ namespace CoreAgent.Application.Commands.System; /// /// 获取设备序列号命令 /// -public class GetSerialNumberCommand : IRequest> +public class GetSerialNumberCommand : IRequest> { // 命令不需要额外参数,直接获取当前设备的序列号 } \ No newline at end of file diff --git a/CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs b/CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs index 249c697..eda26db 100644 --- a/CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs +++ b/CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs @@ -10,7 +10,7 @@ namespace CoreAgent.Application.Handlers.System; /// /// 获取设备序列号命令处理器 /// -public class GetSerialNumberCommandHandler : IRequestHandler> +public class GetSerialNumberCommandHandler : IRequestHandler> { private readonly IDeviceService _deviceService; private readonly ILogger _logger; @@ -23,7 +23,7 @@ public class GetSerialNumberCommandHandler : IRequestHandler> Handle(GetSerialNumberCommand request, CancellationToken cancellationToken) + public async Task> Handle(GetSerialNumberCommand request, CancellationToken cancellationToken) { try { @@ -34,18 +34,26 @@ public class GetSerialNumberCommandHandler : IRequestHandler.Ok(deviceInfo, "获取设备序列号成功"); + + // 创建DeviceInfoResponse对象,不包含IsSuccess字段 + var resultDeviceInfo = new DeviceInfoResponse + { + SerialNumber = deviceInfo.SerialNumber, + Timestamp = deviceInfo.Timestamp + }; + + return ApiActionResult.Ok(resultDeviceInfo, "获取设备序列号成功"); } else { _logger.LogWarning("获取设备序列号失败,返回的序列号: {SerialNumber}", deviceInfo.SerialNumber); - return ApiActionResult.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND"); + return ApiActionResult.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND"); } } catch (Exception ex) { _logger.LogError(ex, "处理获取设备序列号命令时发生异常"); - return ApiActionResult.Error("获取设备序列号失败", "DEVICE_SN_ERROR"); + return ApiActionResult.Error("获取设备序列号失败", "DEVICE_SN_ERROR"); } } } \ No newline at end of file diff --git a/CoreAgent.Domain/Models/System/DeviceInfoResponse.cs b/CoreAgent.Domain/Models/System/DeviceInfoResponse.cs new file mode 100644 index 0000000..d936a8a --- /dev/null +++ b/CoreAgent.Domain/Models/System/DeviceInfoResponse.cs @@ -0,0 +1,17 @@ +namespace CoreAgent.Domain.Models.System; + +/// +/// 设备信息响应模型(用于API响应) +/// +public class DeviceInfoResponse +{ + /// + /// 设备序列号(SN) + /// + public string SerialNumber { get; set; } = string.Empty; + + /// + /// 获取时间 + /// + public DateTime Timestamp { get; set; } = DateTime.UtcNow; +} \ No newline at end of file diff --git a/modify.md b/modify.md index a54ea75..cc0801e 100644 --- a/modify.md +++ b/modify.md @@ -856,7 +856,9 @@ **修改时间**: 2024年 **修改文件**: - `CoreAgent.Domain/Models/System/DeviceInfo.cs` +- `CoreAgent.Domain/Models/System/DeviceInfoResponse.cs` (新建) - `CoreAgent.Infrastructure/Services/System/DeviceService.cs` +- `CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs` - `CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs` **修改内容**: @@ -866,6 +868,12 @@ - 用于标识是否成功获取到序列号 - 提供更准确的成功/失败状态判断 +2. **创建DeviceInfoResponse模型** + - 创建专门用于API响应的模型 + - 只包含 `SerialNumber` 和 `Timestamp` 字段 + - 不包含内部状态字段 `IsSuccess` + - 确保API响应简洁明了 + 2. **ParseSerialNumber方法优化** - 返回类型从 `string` 改为 `string?`(可空字符串) - 失败时返回 `null` 而不是固定字符串 @@ -879,15 +887,17 @@ - 失败时设置 `IsSuccess = false` - 添加更详细的日志记录 -4. **GetSerialNumberCommandHandler优化** +4. **GetSerialNumberCommand和GetSerialNumberCommandHandler优化** + - 修改命令返回类型为 `ApiActionResult` - 在Handle方法中检查 `deviceInfo.IsSuccess` 字段 - - 成功时返回 `ApiActionResult.Ok()` - - 失败时返回 `ApiActionResult.Error()` 并设置错误代码 `DEVICE_SN_NOT_FOUND` + - 成功时创建 `DeviceInfoResponse` 对象返回,不包含IsSuccess字段 + - 失败时返回 `ApiActionResult.Error()` 并设置错误代码 `DEVICE_SN_NOT_FOUND` - 提供更准确的错误信息 + - 确保API返回的响应模型不包含内部状态字段 5. **具体实现**: ```csharp - // DeviceInfo模型 + // DeviceInfo模型(内部使用) public class DeviceInfo { public bool IsSuccess { get; set; } @@ -895,6 +905,13 @@ public DateTime Timestamp { get; set; } = DateTime.UtcNow; } + // DeviceInfoResponse模型(API响应) + public class DeviceInfoResponse + { + public string SerialNumber { get; set; } = string.Empty; + public DateTime Timestamp { get; set; } = DateTime.UtcNow; + } + // ParseSerialNumber方法 private string? ParseSerialNumber(string output) { @@ -939,11 +956,17 @@ // GetSerialNumberCommandHandler中的使用 if (deviceInfo.IsSuccess) { - return ApiActionResult.Ok(deviceInfo, "获取设备序列号成功"); + // 创建DeviceInfoResponse对象,不包含IsSuccess字段 + var resultDeviceInfo = new DeviceInfoResponse + { + SerialNumber = deviceInfo.SerialNumber, + Timestamp = deviceInfo.Timestamp + }; + return ApiActionResult.Ok(resultDeviceInfo, "获取设备序列号成功"); } else { - return ApiActionResult.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND"); + return ApiActionResult.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND"); } ```