Camera Information System
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.5 KiB

using System.Runtime.InteropServices;
namespace EC.Helper.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
}