using System; using System.Collections.Generic; using System.Linq; using System.Text; using AccessBusiness.Common; using System.Runtime.InteropServices; namespace AccessBusiness { public class HIV { /// <summary> /// 初始化門禁SDK /// </summary> /// <returns>true:成功 false:失敗</returns> public static bool InitialSDK() { // 初始化SDK bool bInit = CHCNetSDK.NET_DVR_Init(); if (!bInit) { // 初始化失敗 CMMLog.Error("海康門禁SDK初始化失敗 !"); } else { CMMLog.Info("海康門禁SDK初始化成功!"); } return bInit; } /// <summary> /// 實例化對象 /// </summary> /// <param name="sLoginName">登陸名</param> /// <param name="sPwd">密碼</param> /// <param name="sIP">IP</param> /// <param name="sPort">端口號</param> public HIV(string sLoginName, string sPwd, string sIP, ushort uPort) { this.LoginName = sLoginName; this.Pwd = sPwd; this.IP = sIP; this.Port = uPort; } #region 屬性 /// <summary> /// 登陸名 /// </summary> public string LoginName { get; set; } /// <summary> /// 密碼 /// </summary> public string Pwd { get; set; } /// <summary> /// IP /// </summary> public string IP { get; set; } /// <summary> /// 端口號 /// </summary> public ushort Port { get; set; } /// <summary> /// 門序號 /// </summary> public int GateWayIndex { get; set; } private uint _OperationType = 5; /// <summary> /// 0- 關閉,1- 打開,2- 常開,3- 常關,4- 授權,5- 清除權限 /// </summary> public uint OperationType { get { return this._OperationType; } set { this._OperationType = value; } } /// <summary> /// 用戶登陸后的ID /// </summary> private int LoginUserID; #endregion /// <summary> /// 門操作 /// </summary> /// <param name="uType">0- 關閉,1- 打開,2- 常開,3- 常關 </param> /// <param name="iGateWayIndex">門序號</param> /// <returns>true:成功 false:失敗</returns> public bool Operate(uint uType, int iGateWayIndex) { // 獲取文字信息時使用 this.OperationType = uType; bool bResult = Login(out this.LoginUserID); if (bResult) { bResult = CHCNetSDK.NET_DVR_ControlGateway(this.LoginUserID, iGateWayIndex, uType); bResult = CheckAndRecord("NET_DVR_ControlGateway"); this.LogOut(); } return bResult; } /// <summary> /// 清除門禁控制器中所有卡 /// </summary> public bool DelAllCard() { bool bResult = this.Login(out this.LoginUserID); NET_DVR_ACS_PARAM_TYPE struAcsParam = new NET_DVR_ACS_PARAM_TYPE(); struAcsParam.dwSize = (uint)Marshal.SizeOf(struAcsParam); struAcsParam.dwParamType |= 1 << 12; uint dwSize = (uint)Marshal.SizeOf(struAcsParam); IntPtr ptrAcsParam = Marshal.AllocHGlobal((int)dwSize); Marshal.StructureToPtr(struAcsParam, ptrAcsParam, false); bResult = CHCNetSDK.NET_DVR_RemoteControl(this.LoginUserID, 2118, ptrAcsParam, dwSize); this.CheckAndRecord("NET_DVR_RemoteControl"); Marshal.FreeHGlobal(ptrAcsParam); this.LogOut(); return bResult; } /// <summary> /// 權限設置(測試中卡過期后仍然可以刷開門,需要清除權限) /// </summary> /// <param name="iDoorNum">門編號</param> /// <param name="sCardNo">卡號</param> /// <param name="flag">0:授權 1:清除權限</param> /// <param name="beginTime">開始時間(卡有效期)</param> /// <param name="endTime">截止時間(卡有效期)</param> /// <returns>true:成功 false:失敗</returns> public bool Authority(int iDoorNum, string sCardNo, int flag, DateTime beginTime, DateTime endTime) { this.GateWayIndex = iDoorNum; bool bResult = this.Login(out this.LoginUserID); if (!bResult) { // 登陸失敗 return false; } // 卡設置 NET_DVR_CARD_CFG_COND lpInBuffer = new NET_DVR_CARD_CFG_COND(); lpInBuffer.byCheckCardNo = 1; lpInBuffer.byRes = new byte[31]; lpInBuffer.dwCardNum = 1; lpInBuffer.dwSize = (uint)Marshal.SizeOf(lpInBuffer); // 回調實例 fRemoteConfigCallback callback = new fRemoteConfigCallback( (dwType, lpBuffer, dwBufLen, pUserData) => { } ); IntPtr lpInBufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(lpInBuffer)); Marshal.StructureToPtr(lpInBuffer, lpInBufferPtr, false); // 打開長連接 int iResult = CHCNetSDK.NET_DVR_StartRemoteConfig(this.LoginUserID, CHCNetSDK.NET_DVR_SET_CARD_CFG, lpInBufferPtr, lpInBuffer.dwSize, callback, IntPtr.Zero); bResult = CheckAndRecord("NET_DVR_StartRemoteConfig"); if (!bResult) { Marshal.FreeHGlobal(lpInBufferPtr); // 啟動長連接失敗 return false; } NET_DVR_TIME_EX begin = new NET_DVR_TIME_EX(); begin.wYear = (ushort)beginTime.Year; begin.byMonth = (byte)beginTime.Month; begin.byDay = (byte)beginTime.Day; begin.byHour = (byte)beginTime.Hour; begin.byMinute = (byte)beginTime.Minute; begin.bySecond = (byte)beginTime.Second; NET_DVR_TIME_EX end = new NET_DVR_TIME_EX(); end.wYear = (ushort)endTime.Year; end.byMonth = (byte)endTime.Month; end.byDay = (byte)endTime.Day; end.byHour = (byte)endTime.Hour; end.byMinute = (byte)endTime.Minute; end.bySecond = (byte)endTime.Second; NET_DVR_VALID_PERIOD_CFG struValid = new NET_DVR_VALID_PERIOD_CFG(); struValid.struBeginTime = begin; struValid.struEndTime = end; struValid.byEnable = 1; struValid.byRes1 = new byte[3]; struValid.byRes2 = new byte[32]; // 卡信息 NET_DVR_CARD_CFG pSendBuf = new NET_DVR_CARD_CFG(); pSendBuf.struValid = struValid; pSendBuf.byCardNo = new byte[32]; byte[] byTempCardNo = new byte[32]; byTempCardNo = System.Text.Encoding.UTF8.GetBytes(sCardNo); for (int i = 0; i < byTempCardNo.Length; i++) { pSendBuf.byCardNo[i] = byTempCardNo[i]; } pSendBuf.byCardValid = 1; pSendBuf.byCardType = 1; pSendBuf.dwModifyParamType = 1023; pSendBuf.byLeaderCard = 0; pSendBuf.byRes1 = 0; pSendBuf.dwBelongGroup = 0; pSendBuf.dwBelongGroup = 0; pSendBuf.byCardPassword = new byte[8]; pSendBuf.byCardRightPlan = new byte[128]; int index = iDoorNum - 1; for (int i = 0; i < 32; i++) { // 最多32個門 if (i == index) { // 當前門個數 pSendBuf.dwDoorRight |= (uint)(1 << i); pSendBuf.byCardRightPlan[i * 4] = 1; if (flag == 1) { // 清除權限 //pSendBuf.dwDoorRight = 0; pSendBuf.byCardRightPlan[i * 4] = 0; // pSendBuf.byCardValid = 0; } } } pSendBuf.dwMaxSwipeTime = 0; pSendBuf.wRoomNumber = 0; pSendBuf.wFloorNumber = 0; pSendBuf.byRes2 = new byte[32]; int iDwSize = Marshal.SizeOf(pSendBuf); pSendBuf.dwSize = (uint)iDwSize; IntPtr pSendBufPtr = Marshal.AllocHGlobal(iDwSize); Marshal.StructureToPtr(pSendBuf, pSendBufPtr, false); bResult = CHCNetSDK.NET_DVR_SendRemoteConfig(iResult, 3, pSendBufPtr, pSendBuf.dwSize); bResult = CheckAndRecord("NET_DVR_SendRemoteConfig"); if (!bResult) { Marshal.FreeHGlobal(lpInBufferPtr); Marshal.FreeHGlobal(pSendBufPtr); return false; } bResult = CHCNetSDK.NET_DVR_StopRemoteConfig(iResult); bResult = CheckAndRecord("NET_DVR_StopRemoteConfig"); if (!bResult) { Marshal.FreeHGlobal(lpInBufferPtr); Marshal.FreeHGlobal(pSendBufPtr); // 停止長連接失敗 return false; } Marshal.FreeHGlobal(lpInBufferPtr); Marshal.FreeHGlobal(pSendBufPtr); this.LogOut(); CMMLog.Info(string.Format("卡號:{0},門序號:{1},開始時間:{2},結束時間:{3}", sCardNo, index, beginTime, endTime)); return bResult; } /// <summary> /// 登陸門禁 /// </summary> /// <returns>true:登陸成功 false:登陸失敗</returns> private bool Login(out int iUserID) { CHCNetSDK.NET_DVR_DEVICEINFO deviceInfo = new CHCNetSDK.NET_DVR_DEVICEINFO(); iUserID = CHCNetSDK.NET_DVR_Login(this.IP, this.Port, this.LoginName, this.Pwd, ref deviceInfo); return CheckAndRecord("NET_DVR_Login"); } /// <summary> /// 檢查並記錄 /// </summary> /// <returns>true:成功 false:失敗</returns> private bool CheckAndRecord(string sAPIName = "") { // 獲取錯誤信息 uint errorCode = CHCNetSDK.NET_DVR_GetLastError(); if (errorCode != 0) { CMMLog.Error(string.Format("ErrorCode:{0},接口名:{1} \r\n {2}", errorCode, sAPIName, this.ToString())); return false; } else { CMMLog.Info(string.Format("接口名:{0},\r\n {1}", sAPIName, this.ToString())); return true; } } public override string ToString() { return string.Format("用戶名:{0},密碼:{1},IP:{2},端口:{3},門序號:{4},操作類型:{5}", this.LoginName, this.Pwd, this.IP, this.Port, this.GateWayIndex, GetNameByOperatorType()); } /// <summary> /// 注銷登陸 /// </summary> public void LogOut() { bool bResult = CHCNetSDK.NET_DVR_Logout(this.LoginUserID); this.CheckAndRecord("NET_DVR_Logout"); } /// <summary> /// 清理資源 /// </summary> public static void Cleanup() { bool bResult = CHCNetSDK.NET_DVR_Cleanup(); } /// <summary> /// 根據操作類型編號獲取中文描述 /// </summary> /// <returns>類型描述</returns> public string GetNameByOperatorType() { // 0- 關閉,1- 打開,2- 常開,3- 常關 switch (this.OperationType) { case 0: return "關門"; case 1: return "開門"; case 2: return "常開"; case 3: return "常關"; case 4: return "授權"; case 5: return "清除權限"; default: return "其它"; } } } }