Example #1
0
        public void getDoor(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            IntPtr       doorObj = IntPtr.Zero;
            UInt32       numDoor = 0;
            BS2ErrorCode result  = BS2ErrorCode.BS_SDK_SUCCESS;

            Console.WriteLine("Do you want to get all doors? [Y/n]");
            Console.Write(">>>> ");
            if (Util.IsYes())
            {
                Console.WriteLine("Trying to get all doors from device.");
                result = (BS2ErrorCode)API.BS2_GetAllDoor(sdkContext, deviceID, out doorObj, out numDoor);
            }
            else
            {
                Console.WriteLine("Enter the ID of the door which you want to get: [ID_1,ID_2 ...]");
                Console.Write(">>>> ");
                char[]        delimiterChars = { ' ', ',', '.', ':', '\t' };
                string[]      doorIDs        = Console.ReadLine().Split(delimiterChars);
                List <UInt32> doorIDList     = new List <UInt32>();

                foreach (string doorID in doorIDs)
                {
                    if (doorID.Length > 0)
                    {
                        UInt32 item;
                        if (UInt32.TryParse(doorID, out item))
                        {
                            doorIDList.Add(item);
                        }
                    }
                }

                if (doorIDList.Count > 0)
                {
                    IntPtr doorIDObj    = Marshal.AllocHGlobal(4 * doorIDList.Count);
                    IntPtr curDoorIDObj = doorIDObj;
                    foreach (UInt32 item in doorIDList)
                    {
                        Marshal.WriteInt32(curDoorIDObj, (Int32)item);
                        curDoorIDObj = (IntPtr)((long)curDoorIDObj + 4);
                    }

                    Console.WriteLine("Trying to get doors from device.");
                    result = (BS2ErrorCode)API.BS2_GetDoor(sdkContext, deviceID, doorIDObj, (UInt32)doorIDList.Count, out doorObj, out numDoor);

                    Marshal.FreeHGlobal(doorIDObj);
                }
                else
                {
                    Console.WriteLine("Invalid parameter");
                }
            }

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
            }
            else if (numDoor > 0)
            {
                IntPtr curDoorObj = doorObj;
                int    structSize = Marshal.SizeOf(typeof(BS2Door));

                for (int idx = 0; idx < numDoor; ++idx)
                {
                    BS2Door item = (BS2Door)Marshal.PtrToStructure(curDoorObj, typeof(BS2Door));
                    print(sdkContext, item);
                    curDoorObj = (IntPtr)((long)curDoorObj + structSize);
                }

                API.BS2_ReleaseObject(doorObj);
            }
            else
            {
                Console.WriteLine(">>> There is no door in the device.");
            }
        }