You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
2.0 KiB
51 lines
2.0 KiB
1 week ago
|
using CoreAgent.Application.Commands.System;
|
||
|
using CoreAgent.Domain.Interfaces.System;
|
||
|
using CoreAgent.Domain.Models.Common;
|
||
|
using CoreAgent.Domain.Models.System;
|
||
|
using MediatR;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
|
||
|
namespace CoreAgent.Application.Handlers.System;
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取设备序列号命令处理器
|
||
|
/// </summary>
|
||
|
public class GetSerialNumberCommandHandler : IRequestHandler<GetSerialNumberCommand, ApiActionResult<DeviceInfo>>
|
||
|
{
|
||
|
private readonly IDeviceService _deviceService;
|
||
|
private readonly ILogger<GetSerialNumberCommandHandler> _logger;
|
||
|
|
||
|
public GetSerialNumberCommandHandler(
|
||
|
IDeviceService deviceService,
|
||
|
ILogger<GetSerialNumberCommandHandler> logger)
|
||
|
{
|
||
|
_deviceService = deviceService ?? throw new ArgumentNullException(nameof(deviceService));
|
||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||
|
}
|
||
|
|
||
|
public async Task<ApiActionResult<DeviceInfo>> Handle(GetSerialNumberCommand request, CancellationToken cancellationToken)
|
||
|
{
|
||
|
try
|
||
|
{
|
||
|
_logger.LogInformation("开始处理获取设备序列号命令");
|
||
|
|
||
|
var deviceInfo = await _deviceService.GetSerialNumberAsync();
|
||
|
|
||
|
if (deviceInfo.IsSuccess)
|
||
|
{
|
||
|
_logger.LogInformation("成功获取设备序列号: {SerialNumber}", deviceInfo.SerialNumber);
|
||
|
return ApiActionResult<DeviceInfo>.Ok(deviceInfo, "获取设备序列号成功");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
_logger.LogWarning("获取设备序列号失败,返回的序列号: {SerialNumber}", deviceInfo.SerialNumber);
|
||
|
return ApiActionResult<DeviceInfo>.Error("获取设备序列号失败", "DEVICE_SN_NOT_FOUND");
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
_logger.LogError(ex, "处理获取设备序列号命令时发生异常");
|
||
|
return ApiActionResult<DeviceInfo>.Error("获取设备序列号失败", "DEVICE_SN_ERROR");
|
||
|
}
|
||
|
}
|
||
|
}
|