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.

137 lines
4.3 KiB

using System.Runtime.InteropServices;
namespace EC.Helper.CameraSDK;
public class DaHuaSDK : ICameraSDK
{
#region Attr
private IntPtr LoginId { get; set; } = IntPtr.Zero;
#endregion Attr
public DaHuaSDK(CameraInfo cameraInfo) : base(cameraInfo)
{
}
#region Base Method
public override bool Init()
{
bool ret = ConnectSuccess();
if (ret) return true;
DaHuaOriSDK.NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY stuInParam = new();
stuInParam.dwSize = (uint)Marshal.SizeOf(stuInParam);
stuInParam.szIP = CameraInfo.Ip;
stuInParam.nPort = CameraInfo.Port;
stuInParam.szUserName = CameraInfo.UserName;
stuInParam.szPassword = CameraInfo.Password;
stuInParam.emSpecCap = DaHuaOriSDK.EM_LOGIN_SPAC_CAP_TYPE.TCP;
stuInParam.pCapParam = IntPtr.Zero;
DaHuaOriSDK.NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY stuOutParam = new();
stuOutParam.dwSize = (uint)Marshal.SizeOf(stuOutParam);
LoginId = DaHuaOriSDK.CLIENT_LoginWithHighLevelSecurity(ref stuInParam, ref stuOutParam);
ret = ConnectSuccess();
if (ret) DaHuaOriSDK.CLIENT_SetAutoReconnect(delegate (IntPtr lLoginID, IntPtr pchDVRIP, int nDVRPort, IntPtr dwUser)
{
LoginId = lLoginID;
}, IntPtr.Zero);
return ret;
}
public override bool Destory()
{
bool ret = ConnectSuccess();
if (!ret) return true;
ret = DaHuaOriSDK.CLIENT_Logout(LoginId);
if (ret) LoginId = IntPtr.Zero;
return ret;
}
public override bool ConnectSuccess()
{
return LoginId != IntPtr.Zero;
}
public override void BuildException()
{
uint errCode = (uint)DaHuaOriSDK.CLIENT_GetLastError();
if (errCode == 0) return;
errCode -= 0x80000000;
throw CameraException.New(CameraType.DaHua, (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()
{
DaHuaOriSDK.DH_PTZ_LOCATION_INFO info = new();
Size = Marshal.SizeOf(info);
Type = info.GetType();
}
}
public override PtzInfo GetPtzInfo()
{
bool ret = ConnectSuccess();
if (!ret) return PtzInfo.Default;
DaHuaOriSDK.DH_PTZ_LOCATION_INFO entity = new();
int nBufLen = GPIParams.Size;
int pRetLen = 0;
IntPtr ptrBuf = Marshal.AllocHGlobal(GPIParams.Size);
Marshal.StructureToPtr(entity, ptrBuf, true);
try
{
ret = DaHuaOriSDK.CLIENT_QueryDevState(LoginId, (int)DaHuaOriSDK.EM_DEVICE_STATE.PTZ_LOCATION, ptrBuf, nBufLen, ref pRetLen, 3000);
if (!ret) { BuildException(); return PtzInfo.Default; }
object? objBuf = Marshal.PtrToStructure(ptrBuf, GPIParams.Type);
if (objBuf == null) return PtzInfo.Default;
entity = (DaHuaOriSDK.DH_PTZ_LOCATION_INFO)objBuf;
return PtzInfo.New(entity.nPTZPan, entity.nPTZTilt, entity.nPTZZoom);
}
finally
{
Marshal.FreeHGlobal(ptrBuf);
}
}
public override bool TryGetPtzInfo(out PtzInfo ptzInfo)
{
bool ret = ConnectSuccess();
if (!ret) { ptzInfo = PtzInfo.Default; return false; }
DaHuaOriSDK.DH_PTZ_LOCATION_INFO entity = new();
int nBufLen = GPIParams.Size;
int pRetLen = 0;
IntPtr ptrBuf = Marshal.AllocHGlobal(GPIParams.Size);
Marshal.StructureToPtr(entity, ptrBuf, true);
try
{
ret = DaHuaOriSDK.CLIENT_QueryDevState(LoginId, (int)DaHuaOriSDK.EM_DEVICE_STATE.PTZ_LOCATION, ptrBuf, nBufLen, ref pRetLen, 3000);
if (!ret) { BuildException(); ptzInfo = PtzInfo.Default; return false; }
object? objBuf = Marshal.PtrToStructure(ptrBuf, GPIParams.Type);
if (objBuf == null) { ptzInfo = PtzInfo.Default; return false; }
entity = (DaHuaOriSDK.DH_PTZ_LOCATION_INFO)objBuf;
ptzInfo = PtzInfo.New(entity.nPTZPan, entity.nPTZTilt, entity.nPTZZoom);
return true;
}
finally
{
Marshal.FreeHGlobal(ptrBuf);
}
}
#endregion Main Method
}