海康门禁C# demo-开,关,常开,常关,授权,清权

c#xdemo海康门禁-开,关,常开,常关,授权,清权C

欢迎使用海康c#xdemo

你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Markdown编辑器, 可以仔细阅读这篇文章,了解一下Markdown的基本语法知识。

新的改变

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 "其它";
            }
        }
    }
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值