NetMQ 协议,支持摄像头增、删、改
This commit is contained in:
84
SHH.CameraService/Core/CmdClients/RemoveCameraHandler.cs
Normal file
84
SHH.CameraService/Core/CmdClients/RemoveCameraHandler.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
using SHH.CameraSdk;
|
||||
using SHH.Contracts;
|
||||
|
||||
namespace SHH.CameraService
|
||||
{
|
||||
/// <summary>
|
||||
/// 移除设备指令处理器
|
||||
/// </summary>
|
||||
public class RemoveCameraHandler : ICommandHandler
|
||||
{
|
||||
private readonly CameraManager _cameraManager;
|
||||
|
||||
/// <summary>
|
||||
/// 指令名称
|
||||
/// </summary>
|
||||
public string ActionName => ProtocolHeaders.Remove_Camera;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="cameraManager"></param>
|
||||
public RemoveCameraHandler(CameraManager cameraManager)
|
||||
{
|
||||
_cameraManager = cameraManager;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理指令
|
||||
/// </summary>
|
||||
/// <param name="payload"></param>
|
||||
public async Task ExecuteAsync(JToken payload)
|
||||
{
|
||||
long deviceId = 0;
|
||||
|
||||
try
|
||||
{
|
||||
// 1. 增强型 ID 解析
|
||||
if (payload.Type == JTokenType.Object)
|
||||
{
|
||||
// 兼容大小写不敏感的解析
|
||||
var idToken = payload["Id"] ?? payload["id"];
|
||||
if (idToken != null) deviceId = idToken.Value<long>();
|
||||
}
|
||||
else if (payload.Type == JTokenType.Integer || payload.Type == JTokenType.String)
|
||||
{
|
||||
// 兼容字符串形式的 ID
|
||||
long.TryParse(payload.ToString(), out deviceId);
|
||||
}
|
||||
|
||||
if (deviceId <= 0)
|
||||
{
|
||||
Console.WriteLine($"[{ActionName}] 收到无效指令: ID解析失败 ({payload})");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 预检查
|
||||
var device = _cameraManager.GetDevice(deviceId);
|
||||
if (device == null)
|
||||
{
|
||||
Console.WriteLine($"[{ActionName}] 设备 {deviceId} 已经不在管理池中,无需操作。");
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. 安全移除
|
||||
// 这里建议增加审计日志,记录谁触发了删除(如果协议里有用户信息的话)
|
||||
device.AddAuditLog("收到远程指令:彻底移除设备");
|
||||
Console.WriteLine($"[{ActionName}] 正在安全移除设备: {deviceId} ({device.Config.Name})");
|
||||
|
||||
// CameraManager 内部会:StopAsync -> DisposeAsync -> TryRemove -> SaveChanges
|
||||
await _cameraManager.RemoveDeviceAsync(deviceId);
|
||||
|
||||
Console.WriteLine($"[{ActionName}] 设备 {deviceId} 已彻底清理并从持久化库中移除。");
|
||||
|
||||
// 4. (可选) 此处可以调用 CommandDispatcher 发送 Success ACK
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 捕获异常,防止影响全局 Socket 轮询
|
||||
Console.WriteLine($"[{ActionName}] 移除设备 {deviceId} 过程中发生致命错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user