public CheckGlobalAPBByDoorOpenTask(IntPtr sdkContext, UInt32 deviceID, UInt16 seq, string userID1, string userID2, bool isDualAuth, bool isCheck, GlobalAPBByDoorOpenManager zoneManager)
 {
     this.sdkContext  = sdkContext;
     this.deviceID    = deviceID;
     this.seq         = seq;
     this.userID1     = userID1;
     this.userID2     = userID2;
     this.isDualAuth  = isDualAuth;
     this.isCheck     = isCheck;
     this.zoneManager = zoneManager;
 }
        public void StartGlobalAPBByDoorOpen(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            zoneManager = new GlobalAPBByDoorOpenManager();

            UInt32[] connectedDeviceIDs = null;
            uint     numDevice          = 0;
            IntPtr   deviceObjList      = IntPtr.Zero;

            API.BS2_GetDevices(sdkContext, out deviceObjList, out numDevice);
            connectedDeviceIDs = new UInt32[numDevice];
            for (int idx = 0; idx < numDevice; ++idx)
            {
                connectedDeviceIDs[idx] = Convert.ToUInt32(Marshal.ReadInt32(deviceObjList, (int)idx * sizeof(UInt32)));
            }
            API.BS2_ReleaseObject(deviceObjList);

            if (connectedDeviceIDs.Length < 2)
            {
                Console.WriteLine("Number of connected device is {0}. At least two terminals must be connected.", connectedDeviceIDs.Length);
                return;
            }

            Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
            for (UInt32 idx = 0; idx < numDevice; ++idx)
            {
                Console.WriteLine("[{0:000}] ==> ID[{1, 10}]",
                                  idx,
                                  connectedDeviceIDs[idx]);
            }
            Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");

            char[] delimiterChars = { ' ', ',', '.', ':', '\t' };
            Console.WriteLine("Enter index of the readers which are used as >>>>> IN <<<<< device of global apb zone: [0,1,2 ...]");
            Console.Write(">>>> ");
            string[] readerIndexs = Console.ReadLine().Split(delimiterChars);
            for (int idx = 0; idx < readerIndexs.Length; ++idx)
            {
                UInt32 indexOfReader;
                if (UInt32.TryParse(readerIndexs[idx], out indexOfReader) && indexOfReader < numDevice)
                {
                    if (BS2ErrorCode.BS_SDK_SUCCESS == (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, connectedDeviceIDs[indexOfReader]))
                    {
                        zoneManager.inDeviceIDs.Add(connectedDeviceIDs[indexOfReader]);
                    }
                }
                else
                {
                    Console.WriteLine("Got error(Invaild index({0})).", readerIndexs[idx]);
                    return;
                }
            }

            Console.WriteLine("Enter index of the readers which are used as <<<<< OUT >>>>> device of global apb zone: [0,1,2 ...]");
            Console.Write(">>>> ");
            readerIndexs = Console.ReadLine().Split(delimiterChars);
            for (int idx = 0; idx < readerIndexs.Length; ++idx)
            {
                UInt32 indexOfReader;
                if (UInt32.TryParse(readerIndexs[idx], out indexOfReader) && indexOfReader < numDevice)
                {
                    if (BS2ErrorCode.BS_SDK_SUCCESS == (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, connectedDeviceIDs[indexOfReader]))
                    {
                        if (!zoneManager.inDeviceIDs.Contains(connectedDeviceIDs[indexOfReader]))
                        {
                            zoneManager.outDeviceIDs.Add(connectedDeviceIDs[indexOfReader]);
                        }
                        else
                        {
                            Console.WriteLine("Got error(index({0}) is IN device already.).", readerIndexs[idx]);
                            return;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Got error(Invaild index({0})).", readerIndexs[idx]);
                    return;
                }
            }

            Console.WriteLine("Which type is this global apb zone? [{0} : {1}, {2} : {3}(default)]", (byte)BS2APBZoneTypeEnum.HARD, BS2APBZoneTypeEnum.HARD, (byte)BS2APBZoneTypeEnum.SOFT, BS2APBZoneTypeEnum.SOFT);
            Console.Write(">>>> ");
            zoneManager.APBZoneType = (BS2APBZoneTypeEnum)Util.GetInput((byte)BS2APBZoneTypeEnum.SOFT);

            BS2GlobalAPBFailActionTypeEnum networkFailAction = BS2GlobalAPBFailActionTypeEnum.NONE;

            Console.WriteLine("Which network fail action type is this global apb zone? [{0} : {1}(default), {2} : {3}, {4} : {5}]", (byte)BS2GlobalAPBFailActionTypeEnum.NONE, BS2GlobalAPBFailActionTypeEnum.NONE, (byte)BS2GlobalAPBFailActionTypeEnum.SOFT, BS2GlobalAPBFailActionTypeEnum.SOFT, (byte)BS2GlobalAPBFailActionTypeEnum.HARD, BS2GlobalAPBFailActionTypeEnum.HARD);
            Console.Write(">>>> ");
            networkFailAction = (BS2GlobalAPBFailActionTypeEnum)Util.GetInput((byte)BS2GlobalAPBFailActionTypeEnum.NONE);

            Console.WriteLine("Enter the ID for the zone which you want to set");
            Console.Write(">>>> ");
            zoneManager.zoneID = (UInt32)Util.GetInput();

            SortedSet <UInt32> zoneReaders = new SortedSet <UInt32>();

            zoneReaders.UnionWith(zoneManager.inDeviceIDs);
            zoneReaders.UnionWith(zoneManager.outDeviceIDs);
            BS2ErrorCode result = 0;

            foreach (UInt32 zoneReaderID in zoneReaders)
            {
                Console.WriteLine("Now check if the useGlobalAPB option is turned on in the AuthConfig settings.");
                if (!checkAPBOptionInAuthConfig(sdkContext, zoneReaderID, Convert.ToByte(networkFailAction)))
                {
                    return;
                }

                Console.WriteLine("Now check if the apbUseDoorSensor option is turned on in the door settings.");
                if (!checkAPBOptionInDoor(sdkContext, zoneReaderID))
                {
                    return;
                }
            }

            this.sdkContext = sdkContext;
            taskExecutor    = new CheckGlobalAPBByDoorOpenTaskExecutor();

            Console.WriteLine("Attaching a global apb by door open handler.");
            cbCheck  = new API.OnCheckGlobalAPBViolationByDoorOpen(onCheckGlobalAPBViolationByDoorOpen);
            cbUpdate = new API.OnUpdateGlobalAPBViolationByDoorOpen(onUpdateGlobalAPBViolationByDoorOpen);
            result   = (BS2ErrorCode)API.BS2_SetGlobalAPBViolationByDoorOpenHandler(sdkContext, cbCheck, cbUpdate);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            taskExecutor.start();

            Console.WriteLine("Press ESC to stop global apb by door open.");
            while (Console.ReadKey(true).Key != ConsoleKey.Escape)
            {
                Thread.Sleep(100);
            }

            taskExecutor.stop();

            Console.WriteLine("Detaching a global apb by door open handler.");
            result = (BS2ErrorCode)API.BS2_SetGlobalAPBViolationByDoorOpenHandler(sdkContext, null, null);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            cbCheck      = null;
            taskExecutor = null;
            zoneManager  = null;
        }