Example #1
0
        public bool DisConnectDevice(ref UInt32 deviceID)
        {
            try
            {
                _log.InfoFormat("正在断开 {0}", parseIdToIp(deviceID));
                BS2ErrorCode result = (BS2ErrorCode)API.BS2_DisconnectDevice(sdkContext, deviceID);
                if (BS2ErrorCode.BS_SDK_SUCCESS != result)
                {
                    _log.ErrorFormat("断开设备失败 : {0} 设备IP : {1}", result, parseIdToIp(deviceID));
                    return(false);
                }

                _log.ErrorFormat("断开设备成功 : {0} 设备IP : {1}", result, parseIdToIp(deviceID));
                return(true);
            }
            catch (Exception)
            {
                _log.ErrorFormat("断开设备异常,设备IP : {0}", parseIdToIp(deviceID));
                return(false);
            }
        }
Example #2
0
        void getImageLog(IntPtr sdkContext, UInt32 deviceID, bool isMasterDevice)
        {
            BS2SimpleDeviceInfo deviceInfo;
            int            structSize        = Marshal.SizeOf(typeof(BS2Event));
            UInt16         imageLogEventCode = (UInt16)BS2EventCodeEnum.DEVICE_TCP_CONNECTED;
            BS2EventConfig eventConfig       = Util.AllocateStructure <BS2EventConfig>();

            eventConfig.numImageEventFilter = 1;
            eventConfig.imageEventFilter[0].mainEventCode = (byte)(imageLogEventCode >> 8);
            eventConfig.imageEventFilter[0].scheduleID    = (UInt32)BS2ScheduleIDEnum.ALWAYS;

            Console.WriteLine("Trying to get the device[{0}] information.", deviceID);
            BS2ErrorCode 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;
            }

            Console.WriteLine("Trying to activate image log.");
            result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext, deviceID, ref eventConfig);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            Console.WriteLine("Trying to clear log for quick test.");
            result = (BS2ErrorCode)API.BS2_ClearLog(sdkContext, deviceID);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

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

            Thread.Sleep(500); //waiting for socket close

            Console.WriteLine("Trying to connect device[{0}].", deviceID);
            result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, new IPAddress(BitConverter.GetBytes(deviceInfo.ipv4Address)).ToString(), deviceInfo.port, out deviceID);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            IntPtr outEventLogObjs = IntPtr.Zero;
            UInt32 outNumEventLogs = 0;

            result = (BS2ErrorCode)API.BS2_GetLog(sdkContext, deviceID, 0, 1024, out outEventLogObjs, out outNumEventLogs);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }

            if (outNumEventLogs > 0)
            {
                IntPtr curEventLogObjs = outEventLogObjs;
                for (int idx = 0; idx < outNumEventLogs; idx++)
                {
                    BS2Event eventLog = (BS2Event)Marshal.PtrToStructure(curEventLogObjs, typeof(BS2Event));
                    if (Convert.ToBoolean(eventLog.image))
                    {
                        Console.WriteLine("Trying to get image log[{0}].", eventLog.id);

                        IntPtr imageObj  = IntPtr.Zero;
                        UInt32 imageSize = 0;

                        result = (BS2ErrorCode)API.BS2_GetImageLog(sdkContext, deviceID, eventLog.id, out imageObj, out imageSize);
                        if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                        {
                            Console.WriteLine("Got error({0}).", result);
                        }
                        else
                        {
                            int        written = 0;
                            FileStream file    = new FileStream(String.Format("{0}.jpg", eventLog.id), FileMode.Create, FileAccess.Write);

                            Console.WriteLine("Trying to save image log[{0}].", eventLog.id);
                            WriteFile(file.Handle, imageObj, (int)imageSize, out written, IntPtr.Zero);
                            file.Close();

                            if (written != imageSize)
                            {
                                Console.WriteLine("Got error({0}).", result);
                            }
                            else
                            {
                                Console.WriteLine("Successfully saved the image log[{0}].", eventLog.id);
                                Process.Start(file.Name);
                            }
                        }
                        break;
                    }

                    curEventLogObjs = (IntPtr)((long)curEventLogObjs + structSize);
                }

                API.BS2_ReleaseObject(outEventLogObjs);
            }

            eventConfig.numImageEventFilter = 0;

            Console.WriteLine("Trying to deactivate image log.");
            result = (BS2ErrorCode)API.BS2_SetEventConfig(sdkContext, deviceID, ref eventConfig);
            if (result != BS2ErrorCode.BS_SDK_SUCCESS)
            {
                Console.WriteLine("Got error({0}).", result);
                return;
            }
        }
Example #3
0
        public void run()
        {
            UInt32 deviceID   = 0;
            IntPtr versionPtr = API.BS2_Version();

            //bool bSsl = false;

            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);
                //ServicePointManager.SecurityProtocol = (SecurityProtocolType)SecurityProtocolType.Ssl3;

                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;
                }
                else
                {
                    //bSsl = true;
                }
            }

            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 (bSsl)
             * {
             *  cbOnSendRootCA = new API.OnSendRootCA(SendRootCA);
             *  result = (BS2ErrorCode)API.BS2_SetDeviceSSLEventListener(sdkContext, cbOnSendRootCA);
             * }
             */

            cbDebugExPrint = null;
            Console.WriteLine("Do you want print debug message? [y/n]");
            Console.Write(">>>> ");
            if (Util.IsYes())
            {
                cbDebugExPrint = new API.CBDebugExPrint(DebugExPrint);
                result         = (BS2ErrorCode)API.BS2_SetDebugExCallback(cbDebugExPrint, Constants.DEBUG_LOG_ALL, Constants.DEBUG_MODULE_ALL);
                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    Console.WriteLine("Got error({0}).", result);
                    return;
                }

                //Trace.Listeners.Add(new TextWriterTraceListener(Console.Out));
                //Trace.AutoFlush = true;
                //Trace.Indent();
            }

#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;
            cbOnSendRootCA         = null;
        }