using System.Runtime.InteropServices; namespace EC.Util.CameraSDK; public class HiKSDK : ICameraSDK { #region Attr private int LoginId { get; set; } = -1; #endregion Attr public HiKSDK(CameraInfo cameraInfo) : base(cameraInfo) { } #region Base Method public override bool Init() { bool ret = ConnectSuccess(); if (ret) return true; HiKOriSDK.NET_DVR_DEVICEINFO_V30 deviceInfo = new(); LoginId = HiKOriSDK.NET_DVR_Login_V30(CameraInfo.Ip, CameraInfo.Port, CameraInfo.UserName, CameraInfo.Password, ref deviceInfo); ret = ConnectSuccess(); return ret; } public override bool Destory() { bool ret = ConnectSuccess(); if (!ret) return true; ret = HiKOriSDK.NET_DVR_Logout(LoginId); if (ret) LoginId = -1; return ret; } public override bool ConnectSuccess() { return LoginId >= 0; } public override void BuildException() { uint errCode = HiKOriSDK.NET_DVR_GetLastError(); if (errCode == 0) return; throw CameraException.New(CameraManufactor.HiK, (int)errCode); } #endregion Base Method #region Main Method private static class GPIParams { public static int Size { get; private set; } public static Type Type { get; private set; } static GPIParams() { HiKOriSDK.NET_DVR_PTZPOS ptzPos = new(); Size = Marshal.SizeOf(ptzPos); Type = ptzPos.GetType(); } } public override PtzInfo GetPtzInfo() { bool ret = ConnectSuccess(); if (!ret) return PtzInfo.Default; HiKOriSDK.NET_DVR_PTZPOS entity = new(); int dwSize = GPIParams.Size; uint dwReturned = 0; IntPtr ptrBuf = Marshal.AllocHGlobal(dwSize); Marshal.StructureToPtr(entity, ptrBuf, true); try { ret = HiKOriSDK.NET_DVR_GetDVRConfig(LoginId, HiKOriSDK.NET_DVR_GET_PTZPOS, 0, ptrBuf, (uint)dwSize, ref dwReturned); if (!ret) { BuildException(); return PtzInfo.Default; } object? objBuf = Marshal.PtrToStructure(ptrBuf, GPIParams.Type); if (objBuf == null) return PtzInfo.Default; entity = (HiKOriSDK.NET_DVR_PTZPOS)objBuf; return PtzInfo.New(entity.wPanPos, entity.wTiltPos, entity.wZoomPos); } finally { Marshal.FreeHGlobal(ptrBuf); } } public override bool TryGetPtzInfo(out PtzInfo ptzInfo) { bool ret = ConnectSuccess(); if (!ret) { ptzInfo = PtzInfo.Default; return false; } HiKOriSDK.NET_DVR_PTZPOS entity = new(); int dwSize = GPIParams.Size; uint dwReturned = 0; IntPtr ptrBuf = Marshal.AllocHGlobal(dwSize); Marshal.StructureToPtr(entity, ptrBuf, true); try { ret = HiKOriSDK.NET_DVR_GetDVRConfig(LoginId, HiKOriSDK.NET_DVR_GET_PTZPOS, 0, ptrBuf, (uint)dwSize, ref dwReturned); if (!ret) { BuildException(); ptzInfo = PtzInfo.Default; return false; } object? objBuf = Marshal.PtrToStructure(ptrBuf, GPIParams.Type); if (objBuf == null) { ptzInfo = PtzInfo.Default; return false; } entity = (HiKOriSDK.NET_DVR_PTZPOS)objBuf; ptzInfo = PtzInfo.New(entity.wPanPos, entity.wTiltPos, entity.wZoomPos); return true; } finally { Marshal.FreeHGlobal(ptrBuf); } } #endregion Main Method #region Ptz Method public void PtzMove(uint[] args) { bool ret = ConnectSuccess(); if (!ret || args == null || args.Length < 3) return; int lChannel = 1; HiKOriSDK.NET_DVR_PTZControlWithSpeed_Other(LoginId, lChannel, args[0], args[1], args[2]); } public void PtzPreset(uint[] args) { bool ret = ConnectSuccess(); if (!ret || args == null || args.Length < 2) return; int lChannel = 1; HiKOriSDK.NET_DVR_PTZPreset_Other(LoginId, lChannel, args[0], args[1]); } #endregion Ptz Method }