示例#1
0
        public void run()
        {
            while (running)
            {
                UInt32 deviceID = 0;

                lock (locker)
                {
                    if (deviceIDQueue.Count > 0)
                    {
                        deviceID = deviceIDQueue.Dequeue();
                    }
                }

                if (deviceID != 0)
                {
                    Console.WriteLine("trying to reconnect Device[{0, 10}].", deviceID);
                    BS2ErrorCode result = (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, deviceID);
                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        if (result != BS2ErrorCode.BS_SDK_ERROR_CANNOT_CONNECT_SOCKET)
                        {
                            Console.WriteLine("Can't connect to device(errorCode : {0}).", result);
                            return;
                        }
                        else
                        {
                            enqueue(deviceID);
                        }
                    }
                }
                else
                {
                    eventWaitHandle.WaitOne();
                }
            }
        }
示例#2
0
        public void StartGlobalAPBZone(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            zoneManager = new GlobalAPBZoneManager();

            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)
            {
                BS2AuthConfig authConfig;
                Console.WriteLine("Getting auth config. reader[{0}]", zoneReaderID);
                result = (BS2ErrorCode)API.BS2_GetAuthConfig(sdkContext, zoneReaderID, out authConfig);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                    return;
                }
                authConfig.useGlobalAPB        = 1;
                authConfig.globalAPBFailAction = (byte)networkFailAction;
                Console.WriteLine("Setting auth config. reader[{0}]", zoneReaderID);
                result = (BS2ErrorCode)API.BS2_SetAuthConfig(sdkContext, zoneReaderID, ref authConfig);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                    return;
                }
            }

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

            Console.WriteLine("Attaching a global apb zone handler.");
            cbCheckGlobalAPBViolation = new API.OnCheckGlobalAPBViolation(onCheckGlobalAPBViolation);
            result = (BS2ErrorCode)API.BS2_SetCheckGlobalAPBViolationHandler(sdkContext, cbCheckGlobalAPBViolation);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            taskExecutor.start();

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

            taskExecutor.stop();

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

            cbCheckGlobalAPBViolation = null;
            taskExecutor = null;
            zoneManager  = null;
        }
示例#3
0
        bool SearchAndConnectDevice(ref UInt32 deviceID)
        {
            Console.WriteLine("Trying to broadcast on the network");

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_SearchDevices(sdkContext);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error : {0}.", result);
                return(false);
            }

            IntPtr deviceListObj = IntPtr.Zero;
            UInt32 numDevice     = 0;

            const UInt32 LONG_TIME_STANDBY_7S = 7;

            result = (BS2ErrorCode)API.BS2_SetDeviceSearchingTimeout(sdkContext, LONG_TIME_STANDBY_7S);
            if (BS2ErrorCode.BS_SDK_SUCCESS != result)
            {
                Console.WriteLine("Got error : {0}.", result);
                return(false);
            }

            result = (BS2ErrorCode)API.BS2_GetDevices(sdkContext, out deviceListObj, out numDevice);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error : {0}.", result);
                return(false);
            }

            if (numDevice > 0)
            {
                BS2SimpleDeviceInfo deviceInfo;

                Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
                for (UInt32 idx = 0; idx < numDevice; ++idx)
                {
                    deviceID = Convert.ToUInt32(Marshal.ReadInt32(deviceListObj, (int)idx * sizeof(UInt32)));
                    result   = (BS2ErrorCode)API.BS2_GetDeviceInfo(sdkContext, deviceID, out deviceInfo);
                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        Console.WriteLine("Can't get device information(errorCode : {0}).", result);
                        return(false);
                    }

                    Console.WriteLine("[{0, 3:##0}] ==> ID[{1, 10}] Type[{2, 16}] Connection mode[{3}] Ip[{4, 16}] port[{5, 5}]",
                                      idx,
                                      deviceID,
                                      API.productNameDictionary[(BS2DeviceTypeEnum)deviceInfo.type],
                                      (BS2ConnectionModeEnum)deviceInfo.connectionMode,
                                      new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString(),
                                      deviceInfo.port);
                }
                Console.WriteLine("+----------------------------------------------------------------------------------------------------------+");
                Console.WriteLine("Please, choose the index of the Device which you want to connect to. [-1: quit]");
                Console.Write(">>>> ");

                deviceID = 0;
                Int32 selection = Util.GetInput();

                if (selection >= 0)
                {
                    if (selection < numDevice)
                    {
                        deviceID = Convert.ToUInt32(Marshal.ReadInt32(deviceListObj, (int)selection * sizeof(UInt32)));
                    }
                    else
                    {
                        Console.WriteLine("Invalid selection[{0}]", selection);
                    }
                }

                API.BS2_ReleaseObject(deviceListObj);
                if (deviceID > 0)
                {
                    Console.WriteLine("Trying to connect to device[{0}]", deviceID);
                    result = (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, deviceID);

                    if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                    {
                        Console.WriteLine("Can't connect to device(errorCode : {0}).", result);
                        return(false);
                    }

                    Console.WriteLine(">>>> Successfully connected to the device[{0}].", deviceID);
                    return(true);
                }
            }
            else
            {
                Console.WriteLine("There is no device to launch.");
            }

            return(false);
        }
示例#4
0
        public void run()
        {
            UInt32 deviceID   = 0;
            IntPtr versionPtr = API.BS2_Version();

            if (title.Length > 0)
            {
                Console.Title = title;
            }

            Console.WriteLine("SDK version : " + Marshal.PtrToStringAnsi(versionPtr));

            sdkContext = API.BS2_AllocateContext();
            if (sdkContext == IntPtr.Zero)
            {
                Console.WriteLine("Can't allocate sdk context.");
                return;
            }

            Console.WriteLine("Do you want to set up ssl configuration? [Y/n]");
            Console.Write(">>>> ");
            if (Util.IsYes())
            {
                cbPreferMethod                = new API.PreferMethod(PreferMethodHandle);
                cbGetRootCaFilePath           = new API.GetRootCaFilePath(GetRootCaFilePathHandle);
                cbGetServerCaFilePath         = new API.GetServerCaFilePath(GetServerCaFilePathHandle);
                cbGetServerPrivateKeyFilePath = new API.GetServerPrivateKeyFilePath(GetServerPrivateKeyFilePathHandle);
                cbGetPassword    = new API.GetPassword(GetPasswordHandle);
                cbOnErrorOccured = new API.OnErrorOccured(OnErrorOccuredHandle);

                BS2ErrorCode sdkResult = (BS2ErrorCode)API.BS2_SetSSLHandler(sdkContext, cbPreferMethod, cbGetRootCaFilePath, cbGetServerCaFilePath, cbGetServerPrivateKeyFilePath, cbGetPassword, null);
                if (sdkResult != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("BS2_SetSSLHandler failed with : {0}", sdkResult);
                    API.BS2_ReleaseContext(sdkContext);
                    return;
                }
            }

            BS2ErrorCode result = (BS2ErrorCode)API.BS2_Initialize(sdkContext);

            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("SDK initialization failed with : {0}", result);
                API.BS2_ReleaseContext(sdkContext);
                return;
            }

            cbOnDeviceFound        = new API.OnDeviceFound(DeviceFound);
            cbOnDeviceAccepted     = new API.OnDeviceAccepted(DeviceAccepted);
            cbOnDeviceConnected    = new API.OnDeviceConnected(DeviceConnected);
            cbOnDeviceDisconnected = new API.OnDeviceDisconnected(DeviceDisconnected);

            result = (BS2ErrorCode)API.BS2_SetDeviceEventListener(sdkContext,
                                                                  cbOnDeviceFound,
                                                                  cbOnDeviceAccepted,
                                                                  cbOnDeviceConnected,
                                                                  cbOnDeviceDisconnected);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Can't register a callback function/method to a sdk.({0})", result);
                API.BS2_ReleaseContext(sdkContext);
                return;
            }

#if SDK_AUTO_CONNECTION
            result = (BS2ErrorCode)API.BS2_SetAutoConnection(sdkContext, 1);
#endif

            Console.WriteLine("+-----------------------------------------------------------+");
            Console.WriteLine("| 1. Search and connect device                              |");
            Console.WriteLine("| 2. Connect to device via Ip                               |");
            Console.WriteLine("| 3. Server mode test                                       |");
            Console.WriteLine("+-----------------------------------------------------------+");
            Console.WriteLine("How to connect to device? [2(default)]");
            Console.Write(">>>> ");
            int selection = Util.GetInput(2);

            switch (selection)
            {
            case 1:
                if (!SearchAndConnectDevice(ref deviceID))
                {
                    deviceID = 0;
                }
                break;

            case 2:
                if (!ConnectToDevice(ref deviceID))
                {
                    deviceID = 0;
                }
                break;

            case 3:
            {
                if (deviceIDForServerMode == 0)
                {
                    Console.WriteLine("Waiting for client connection");
                    eventWaitHandle.WaitOne();
                }

                deviceID = deviceIDForServerMode;
                result   = (BS2ErrorCode)API.BS2_ConnectDevice(sdkContext, deviceID);

                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Can't connect to device(errorCode : {0}).", result);
                    deviceID = 0;
                }
                else
                {
                    Console.WriteLine(">>>> Successfully connected to the device[{0}].", deviceID);
                }
            }
            break;

            default:
                Console.WriteLine("Invalid parameter : {0}", selection);
                break;
            }

            if (deviceID > 0)
            {
                Console.Title = String.Format("{0} connected deviceID[{1}]", title, deviceID);

#if !SDK_AUTO_CONNECTION
                reconnectionTask = new ReconnectionTask(sdkContext);
                reconnectionTask.start();
#endif
                runImpl(deviceID);
#if !SDK_AUTO_CONNECTION
                reconnectionTask.stop();
                reconnectionTask = null;
#endif

                Console.WriteLine("Trying to discconect device[{0}].", deviceID);
                result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext, deviceID);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                }
            }

            eventWaitHandle.Close();
            API.BS2_ReleaseContext(sdkContext);
            sdkContext = IntPtr.Zero;

            cbOnDeviceFound        = null;
            cbOnDeviceAccepted     = null;
            cbOnDeviceConnected    = null;
            cbOnDeviceDisconnected = null;
        }