69 lines
2.2 KiB
C#
69 lines
2.2 KiB
C#
using Ayay.SerilogLogs;
|
||
using Newtonsoft.Json.Linq;
|
||
using Serilog;
|
||
using SHH.CameraSdk;
|
||
using SHH.Contracts;
|
||
|
||
namespace SHH.CameraService;
|
||
|
||
/// <summary>
|
||
/// 设备重启指令处理器
|
||
/// 响应 gRpc 指令:ProtocolCodes.Device_Reboot
|
||
/// </summary>
|
||
public class DeviceRebootHandler : ICommandHandler
|
||
{
|
||
private readonly ILogger _sysLog = Log.ForContext("SourceContext", LogModules.Core);
|
||
private readonly CameraManager _cameraManager;
|
||
|
||
/// <summary>指令名称(需在 ProtocolCodes 中定义 Device_Reboot)</summary>
|
||
public string ActionName => ProtocolCodes.Device_Reboot;
|
||
|
||
public DeviceRebootHandler(CameraManager cameraManager)
|
||
{
|
||
_cameraManager = cameraManager ?? throw new ArgumentNullException(nameof(cameraManager));
|
||
}
|
||
|
||
public async Task ExecuteAsync(JToken payload)
|
||
{
|
||
// 1. 解析参数(假设重启指令至少包含 DeviceId)
|
||
// Optimized: 使用通用的基础 DTO 或直接解析 DeviceId,避免为简单的重启创建过多复杂 DTO
|
||
var deviceId = payload["DeviceId"]?.Value<int>() ?? 0;
|
||
|
||
if (deviceId <= 0)
|
||
{
|
||
_sysLog.Warning("[Reboot] 无效指令:设备ID非法");
|
||
return;
|
||
}
|
||
|
||
// 2. 获取设备实例
|
||
var device = _cameraManager.GetDevice(deviceId);
|
||
if (device == null)
|
||
{
|
||
_sysLog.Warning($"[Reboot] 设备 {deviceId} 不存在");
|
||
return;
|
||
}
|
||
|
||
// 3. 校验重启能力 (参考 PtzControlHandler 模式)
|
||
// Modified: 根据 ISyncFeature.cs 定义,检查是否实现了 IRebootFeature 接口
|
||
if (!(device is IRebootFeature rebootFeature))
|
||
{
|
||
_sysLog.Warning($"[Reboot] 设备 {deviceId} 不支持远程重启接口");
|
||
return;
|
||
}
|
||
|
||
// 4. 执行重启
|
||
try
|
||
{
|
||
_sysLog.Information($"[Reboot] 正在向设备 {deviceId} 发送重启指令...");
|
||
|
||
// 调用接口定义的异步方法
|
||
await rebootFeature.RebootAsync();
|
||
|
||
_sysLog.Information($"[Reboot] 设备 {deviceId} 重启指令发送成功");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
_sysLog.Error($"[Reboot] 设备 {deviceId} 重启失败: {ex.Message}");
|
||
}
|
||
}
|
||
} |