2026-01-07 10:59:03 +08:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2026-01-12 18:27:58 +08:00
|
|
|
|
using SHH.CameraSdk;
|
2026-01-07 10:59:03 +08:00
|
|
|
|
using SHH.Contracts;
|
|
|
|
|
|
|
|
|
|
|
|
namespace SHH.CameraService;
|
|
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 同步设备配置处理器
|
|
|
|
|
|
/// </summary>
|
2026-01-15 11:04:38 +08:00
|
|
|
|
public class DeviceConfigHandler : ICommandHandler
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
private readonly CameraManager _cameraManager;
|
|
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 命令名称
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public string ActionName => ProtocolHeaders.Sync_Camera;
|
2026-01-07 10:59:03 +08:00
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 构造函数
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="cameraManager"></param>
|
2026-01-15 11:04:38 +08:00
|
|
|
|
public DeviceConfigHandler(CameraManager cameraManager)
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
|
|
|
|
|
_cameraManager = cameraManager;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 执行处理
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="payload"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2026-01-07 10:59:03 +08:00
|
|
|
|
public async Task ExecuteAsync(JToken payload)
|
|
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// 1. 反序列化配置 DTO
|
2026-01-07 10:59:03 +08:00
|
|
|
|
var dto = payload.ToObject<CameraConfigDto>();
|
|
|
|
|
|
if (dto == null) return;
|
|
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// 2. 尝试获取现有设备
|
|
|
|
|
|
var device = _cameraManager.GetDevice(dto.Id);
|
|
|
|
|
|
|
|
|
|
|
|
if (device != null)
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// =========================================================
|
|
|
|
|
|
// 场景 A: 设备已存在 -> 执行智能更新 (Smart Update)
|
|
|
|
|
|
// =========================================================
|
|
|
|
|
|
Console.WriteLine($"[Sync] 更新设备配置: {dto.Id} ({dto.Name})");
|
2026-01-07 10:59:03 +08:00
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// 将全量配置映射为部分更新 DTO
|
|
|
|
|
|
var updateDto = new DeviceUpdateDto
|
|
|
|
|
|
{
|
|
|
|
|
|
// --- 冷更新参数 (变更会触发重启) ---
|
|
|
|
|
|
IpAddress = dto.IpAddress,
|
|
|
|
|
|
Port = dto.Port,
|
|
|
|
|
|
Username = dto.Username,
|
|
|
|
|
|
Password = dto.Password,
|
|
|
|
|
|
ChannelIndex = dto.ChannelIndex,
|
|
|
|
|
|
Brand = dto.Brand,
|
|
|
|
|
|
RtspPath = dto.RtspPath,
|
|
|
|
|
|
RenderHandle = dto.RenderHandle, // long 类型直接赋值
|
|
|
|
|
|
|
|
|
|
|
|
// --- 热更新参数 (变更立即生效) ---
|
|
|
|
|
|
Name = dto.Name,
|
|
|
|
|
|
Location = dto.Location,
|
|
|
|
|
|
StreamType = dto.StreamType,
|
|
|
|
|
|
|
|
|
|
|
|
MainboardIp = dto.MainboardIp,
|
|
|
|
|
|
MainboardPort = dto.MainboardPort,
|
|
|
|
|
|
|
|
|
|
|
|
// --- 图像处理参数 (热更新) ---
|
|
|
|
|
|
AllowCompress = dto.AllowCompress,
|
|
|
|
|
|
AllowExpand = dto.AllowExpand,
|
|
|
|
|
|
TargetResolution = dto.TargetResolution,
|
|
|
|
|
|
EnhanceImage = dto.EnhanceImage,
|
|
|
|
|
|
UseGrayscale = dto.UseGrayscale
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 调用 Manager 的核心更新逻辑 (它会自动判断是 Stop->Start 还是直接应用)
|
|
|
|
|
|
await _cameraManager.UpdateDeviceConfigAsync(dto.Id, updateDto);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// =========================================================
|
|
|
|
|
|
// 场景 B: 设备不存在 -> 执行新增 (Add New)
|
|
|
|
|
|
// =========================================================
|
|
|
|
|
|
Console.WriteLine($"[Sync] 新增设备: {dto.Id} ({dto.Name})");
|
|
|
|
|
|
|
|
|
|
|
|
// 构造全新的设备配置
|
|
|
|
|
|
var newConfig = new VideoSourceConfig
|
|
|
|
|
|
{
|
|
|
|
|
|
Id = dto.Id,
|
|
|
|
|
|
Name = dto.Name,
|
|
|
|
|
|
Brand = (DeviceBrand)dto.Brand, // int -> Enum 强转
|
|
|
|
|
|
IpAddress = dto.IpAddress,
|
|
|
|
|
|
Port = dto.Port,
|
|
|
|
|
|
Username = dto.Username,
|
|
|
|
|
|
Password = dto.Password,
|
|
|
|
|
|
ChannelIndex = dto.ChannelIndex,
|
|
|
|
|
|
StreamType = dto.StreamType,
|
|
|
|
|
|
RtspPath = dto.RtspPath,
|
|
|
|
|
|
MainboardIp = dto.MainboardIp,
|
|
|
|
|
|
MainboardPort = dto.MainboardPort,
|
|
|
|
|
|
RenderHandle = (IntPtr)dto.RenderHandle, // long -> IntPtr 转换
|
|
|
|
|
|
ConnectionTimeoutMs = 5000 // 默认超时
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 添加到管理器池
|
|
|
|
|
|
_cameraManager.AddDevice(newConfig);
|
|
|
|
|
|
|
|
|
|
|
|
// 重新获取引用以进行后续操作
|
|
|
|
|
|
device = _cameraManager.GetDevice(dto.Id);
|
|
|
|
|
|
|
2026-01-07 10:59:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// ★★★ 核心修复:统一处理“运行意图” ★★★
|
|
|
|
|
|
if (device != null)
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// 将 DTO 的立即执行标志直接同步给设备的运行意图
|
|
|
|
|
|
device.IsRunning = dto.ImmediateExecution;
|
|
|
|
|
|
|
|
|
|
|
|
if (dto.ImmediateExecution)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 情况 1: 收到“启动”指令
|
|
|
|
|
|
if (!device.IsOnline) // 只有没在线时才点火
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"[Sync] 指令:立即启动设备 {dto.Id}");
|
|
|
|
|
|
_ = device.StartAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 情况 2: 收到“停止”指令 (即 ImmediateExecution = false)
|
|
|
|
|
|
if (device.IsOnline) // 只有在线时才熄火
|
|
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine($"[Sync] 指令:立即停止设备 {dto.Id}");
|
|
|
|
|
|
_ = device.StopAsync();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-07 10:59:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// =========================================================
|
|
|
|
|
|
// 3. 处理自动订阅策略 (Auto Subscriptions)
|
|
|
|
|
|
// =========================================================
|
|
|
|
|
|
// 无论新增还是更新,都确保订阅策略是最新的
|
|
|
|
|
|
if (device != null && dto.AutoSubscriptions != null)
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
var controller = device.Controller;
|
|
|
|
|
|
if (controller != null)
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
foreach (var sub in dto.AutoSubscriptions)
|
2026-01-07 10:59:03 +08:00
|
|
|
|
{
|
2026-01-12 18:27:58 +08:00
|
|
|
|
// 如果没有 AppId,生成一个临时的(通常 Dashboard 会下发固定的 AppId)
|
|
|
|
|
|
string appId = string.IsNullOrWhiteSpace(sub.AppId)
|
|
|
|
|
|
? $"AUTO_{Guid.NewGuid().ToString("N")[..8]}"
|
|
|
|
|
|
: sub.AppId;
|
|
|
|
|
|
|
|
|
|
|
|
// 构造流控需求
|
|
|
|
|
|
var req = new FrameRequirement
|
|
|
|
|
|
{
|
|
|
|
|
|
AppId = appId,
|
|
|
|
|
|
TargetFps = sub.TargetFps,
|
|
|
|
|
|
Type = (SubscriptionType)sub.Type, // int -> Enum
|
|
|
|
|
|
Memo = sub.Memo ?? "Sync Auto",
|
|
|
|
|
|
|
|
|
|
|
|
// 自动订阅通常不包含具体的 Handle 或 SavePath,除非协议里带了
|
|
|
|
|
|
// 如果需要支持网络转发,这里可以扩展映射 sub.TargetIp 等
|
|
|
|
|
|
Handle = "",
|
|
|
|
|
|
SavePath = ""
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 注册到帧控制器
|
|
|
|
|
|
controller.Register(req);
|
|
|
|
|
|
}
|
2026-01-07 10:59:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|