Browse Source

refactor: 创建DeviceInfoResponse模型,优化API响应结构

- 新增DeviceInfoResponse模型,专门用于API响应
- 移除API响应中的IsSuccess字段,确保响应简洁
- 修改GetSerialNumberCommand返回类型为ApiActionResult<DeviceInfoResponse>
- 更新GetSerialNumberCommandHandler使用DeviceInfoResponse
- 实现清晰的职责分离:DeviceInfo用于内部状态判断,DeviceInfoResponse用于API响应
- 确保API响应不包含内部实现细节
feature/protocol-log-Perfect
hyh 6 days ago
parent
commit
6aadfe9de5
  1. 2
      CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs
  2. 18
      CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs
  3. 17
      CoreAgent.Domain/Models/System/DeviceInfoResponse.cs
  4. 35
      modify.md

2
CoreAgent.Application/Commands/System/GetSerialNumberCommand.cs

@ -7,7 +7,7 @@ namespace CoreAgent.Application.Commands.System;
/// <summary>
/// 获取设备序列号命令
/// </summary>
public class GetSerialNumberCommand : IRequest<ApiActionResult<DeviceInfo>>
public class GetSerialNumberCommand : IRequest<ApiActionResult<DeviceInfoResponse>>
{
// 命令不需要额外参数,直接获取当前设备的序列号
}

18
CoreAgent.Application/Handlers/System/GetSerialNumberCommandHandler.cs

@ -10,7 +10,7 @@ namespace CoreAgent.Application.Handlers.System;
/// <summary>
/// 获取设备序列号命令处理器
/// </summary>
public class GetSerialNumberCommandHandler : IRequestHandler<GetSerialNumberCommand, ApiActionResult<DeviceInfo>>
public class GetSerialNumberCommandHandler : IRequestHandler<GetSerialNumberCommand, ApiActionResult<DeviceInfoResponse>>
{
private readonly IDeviceService _deviceService;
private readonly ILogger<GetSerialNumberCommandHandler> _logger;
@ -23,7 +23,7 @@ public class GetSerialNumberCommandHandler : IRequestHandler<GetSerialNumberComm
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
public async Task<ApiActionResult<DeviceInfo>> Handle(GetSerialNumberCommand request, CancellationToken cancellationToken)
public async Task<ApiActionResult<DeviceInfoResponse>> Handle(GetSerialNumberCommand request, CancellationToken cancellationToken)
{
try
{
@ -34,18 +34,26 @@ public class GetSerialNumberCommandHandler : IRequestHandler<GetSerialNumberComm
if (deviceInfo.IsSuccess)
{
_logger.LogInformation("成功获取设备序列号: {SerialNumber}", deviceInfo.SerialNumber);
return ApiActionResult<DeviceInfo>.Ok(deviceInfo, "获取设备序列号成功");
// 创建DeviceInfoResponse对象,不包含IsSuccess字段
var resultDeviceInfo = new DeviceInfoResponse
{
SerialNumber = deviceInfo.SerialNumber,
Timestamp = deviceInfo.Timestamp
};
return ApiActionResult<DeviceInfoResponse>.Ok(resultDeviceInfo, "获取设备序列号成功");
}
else
{
_logger.LogWarning("获取设备序列号失败,返回的序列号: {SerialNumber}", deviceInfo.SerialNumber);
return ApiActionResult<DeviceInfo>.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND");
return ApiActionResult<DeviceInfoResponse>.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND");
}
}
catch (Exception ex)
{
_logger.LogError(ex, "处理获取设备序列号命令时发生异常");
return ApiActionResult<DeviceInfo>.Error("获取设备序列号失败", "DEVICE_SN_ERROR");
return ApiActionResult<DeviceInfoResponse>.Error("获取设备序列号失败", "DEVICE_SN_ERROR");
}
}
}

17
CoreAgent.Domain/Models/System/DeviceInfoResponse.cs

@ -0,0 +1,17 @@
namespace CoreAgent.Domain.Models.System;
/// <summary>
/// 设备信息响应模型(用于API响应)
/// </summary>
public class DeviceInfoResponse
{
/// <summary>
/// 设备序列号(SN)
/// </summary>
public string SerialNumber { get; set; } = string.Empty;
/// <summary>
/// 获取时间
/// </summary>
public DateTime Timestamp { get; set; } = DateTime.UtcNow;
}

35
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<DeviceInfoResponse>`
- 在Handle方法中检查 `deviceInfo.IsSuccess` 字段
- 成功时返回 `ApiActionResult<DeviceInfo>.Ok()`
- 失败时返回 `ApiActionResult<DeviceInfo>.Error()` 并设置错误代码 `DEVICE_SN_NOT_FOUND`
- 成功时创建 `DeviceInfoResponse` 对象返回,不包含IsSuccess字段
- 失败时返回 `ApiActionResult<DeviceInfoResponse>.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<DeviceInfo>.Ok(deviceInfo, "获取设备序列号成功");
// 创建DeviceInfoResponse对象,不包含IsSuccess字段
var resultDeviceInfo = new DeviceInfoResponse
{
SerialNumber = deviceInfo.SerialNumber,
Timestamp = deviceInfo.Timestamp
};
return ApiActionResult<DeviceInfoResponse>.Ok(resultDeviceInfo, "获取设备序列号成功");
}
else
{
return ApiActionResult<DeviceInfo>.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND");
return ApiActionResult<DeviceInfoResponse>.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND");
}
```

Loading…
Cancel
Save