增加云台移动、缩放、聚集、光圈、校时、重启对 AiVideo 项目的支持
This commit is contained in:
@@ -5,15 +5,51 @@
|
||||
/// </summary>
|
||||
public enum PtzAction
|
||||
{
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
ZoomIn, // 放大
|
||||
ZoomOut, // 缩小
|
||||
FocusNear, // 聚焦近
|
||||
FocusFar, // 聚焦远
|
||||
IrisOpen, // 光圈大
|
||||
IrisClose, // 光圈小
|
||||
Wiper, // 雨刷
|
||||
/// <summary>云台向左</summary>
|
||||
Left = 0,
|
||||
|
||||
/// <summary>云台向上</summary>
|
||||
Up = 1,
|
||||
|
||||
/// <summary>云台向右</summary>
|
||||
Right = 2,
|
||||
|
||||
/// <summary>云台向下</summary>
|
||||
Down = 3,
|
||||
|
||||
/// <summary>云台左上</summary>
|
||||
LeftUp = 4,
|
||||
|
||||
/// <summary>云台左下</summary>
|
||||
LeftDown = 5,
|
||||
|
||||
/// <summary>云台右上</summary>
|
||||
RightUp = 6,
|
||||
|
||||
/// <summary>云台右下</summary>
|
||||
RightDown = 7,
|
||||
|
||||
/// <summary>云台自动</summary>
|
||||
Auto = 8,
|
||||
|
||||
/// <summary>放大</summary>
|
||||
ZoomIn = 9,
|
||||
|
||||
/// <summary>缩小</summary>
|
||||
ZoomOut = 10,
|
||||
|
||||
/// <summary>聚焦近</summary>
|
||||
FocusNear = 11,
|
||||
|
||||
/// <summary>聚焦远</summary>
|
||||
FocusFar = 12,
|
||||
|
||||
/// <summary>光圈大</summary>
|
||||
IrisOpen = 13,
|
||||
|
||||
/// <summary>光圈小</summary>
|
||||
IrisClose = 14,
|
||||
|
||||
/// <summary>雨刷</summary>
|
||||
Wiper = 15,
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
namespace SHH.CameraSdk.HikFeatures;
|
||||
using Serilog;
|
||||
|
||||
namespace SHH.CameraSdk.HikFeatures;
|
||||
|
||||
public class HikPtzProvider : IPtzFeature
|
||||
{
|
||||
@@ -9,6 +11,15 @@ public class HikPtzProvider : IPtzFeature
|
||||
_context = context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断该动作是否支持速度参数
|
||||
/// </summary>
|
||||
private static bool IsSpeedSupported(PtzAction action)
|
||||
{
|
||||
// Optimized: 方向和缩放支持速度,其余(雨刷/聚焦/光圈)为开关量或标准调节
|
||||
return action <= PtzAction.ZoomOut;
|
||||
}
|
||||
|
||||
public async Task PtzControlAsync(PtzAction action, bool stop, int speed)
|
||||
{
|
||||
int userId = _context.GetUserId();
|
||||
@@ -17,10 +28,18 @@ public class HikPtzProvider : IPtzFeature
|
||||
// 1. 映射指令
|
||||
uint hikCommand = action switch
|
||||
{
|
||||
PtzAction.Up => HikNativeMethods.TILT_UP,
|
||||
PtzAction.Down => HikNativeMethods.TILT_DOWN,
|
||||
PtzAction.Left => HikNativeMethods.PAN_LEFT,
|
||||
PtzAction.Up => HikNativeMethods.TILT_UP,
|
||||
PtzAction.Right => HikNativeMethods.PAN_RIGHT,
|
||||
PtzAction.Down => HikNativeMethods.TILT_DOWN,
|
||||
|
||||
PtzAction.LeftUp => HikNativeMethods.UP_LEFT, // 海康 SDK: 左上
|
||||
PtzAction.LeftDown => HikNativeMethods.DOWN_LEFT, // 海康 SDK: 左下
|
||||
PtzAction.RightUp => HikNativeMethods.UP_RIGHT, // 海康 SDK: 右上
|
||||
PtzAction.RightDown => HikNativeMethods.DOWN_RIGHT, // 海康 SDK: 右下
|
||||
|
||||
PtzAction.Auto => HikNativeMethods.PAN_AUTO,
|
||||
|
||||
PtzAction.ZoomIn => HikNativeMethods.ZOOM_IN,
|
||||
PtzAction.ZoomOut => HikNativeMethods.ZOOM_OUT,
|
||||
PtzAction.FocusNear => HikNativeMethods.FOCUS_NEAR,
|
||||
@@ -36,19 +55,28 @@ public class HikPtzProvider : IPtzFeature
|
||||
// 2. 转换停止标志 (海康: 0=开始, 1=停止)
|
||||
uint dwStop = stop ? 1u : 0u;
|
||||
|
||||
// 3. 限制速度范围 (1-7)
|
||||
uint dwSpeed = (uint)Math.Clamp(speed, 1, 7);
|
||||
|
||||
// 4. 调用 SDK
|
||||
// 3. 调用 SDK
|
||||
await Task.Run(() =>
|
||||
{
|
||||
// Channel 默认为 1
|
||||
bool result = HikNativeMethods.NET_DVR_PTZControlWithSpeed_Other(userId, 1, hikCommand, dwStop, dwSpeed);
|
||||
bool result;
|
||||
// Optimized: 接口分流逻辑,确保不同类型的动作调用正确的 SDK 接口
|
||||
if (IsSpeedSupported(action))
|
||||
{
|
||||
uint dwSpeed = (uint)Math.Clamp(speed, 1, 7);
|
||||
result = HikNativeMethods.NET_DVR_PTZControlWithSpeed_Other(userId, 1, hikCommand, dwStop, dwSpeed);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 对于雨刷、聚焦、光圈,使用不带速度的接口
|
||||
result = HikNativeMethods.NET_DVR_PTZControl_Other(userId, 1, hikCommand, dwStop);
|
||||
}
|
||||
|
||||
if (!result)
|
||||
{
|
||||
// 这里通常不抛异常,因为云台繁忙或到头是常态,记录日志即可
|
||||
// Console.WriteLine($"PTZ Error: {HikNativeMethods.NET_DVR_GetLastError()}");
|
||||
// Modified: 统一记录到 D:\Logs
|
||||
uint errorCode = HikNativeMethods.NET_DVR_GetLastError();
|
||||
Log.Warning("Ayay.AiVideo: PTZ {Action} failed. Stop: {Stop}, ErrorCode: {ErrorCode}",
|
||||
action, stop, errorCode);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -431,6 +431,18 @@ public static partial class HikNativeMethods
|
||||
#endregion
|
||||
|
||||
#region --- PTZ 控制接口 (PTZ Control Interfaces) ---
|
||||
|
||||
/// <summary>
|
||||
/// 云台控制
|
||||
/// 功能:控制云台旋转、镜头缩放、光圈调节等操作
|
||||
/// </summary>
|
||||
/// <param name="lUserID"></param>
|
||||
/// <param name="lChannel"></param>
|
||||
/// <param name="dwPTZCommand"></param>
|
||||
/// <param name="dwStop"></param>
|
||||
/// <returns></returns>
|
||||
[DllImport(DllName)]
|
||||
public static extern bool NET_DVR_PTZControl_Other(int lUserID, int lChannel, uint dwPTZCommand, uint dwStop);
|
||||
|
||||
/// <summary>
|
||||
/// 云台控制(带速度)
|
||||
|
||||
Reference in New Issue
Block a user