Example #1
0
        bool ConnectToDeviceSSL(string deviceIpAddress, ref UInt32 deviceID)
        {
            UInt16 port = Util.GetInput((UInt16)BS2Environment.BS2_TCP_DEVICE_PORT_DEFAULT);

            int nCnt = 0;

            while (true)
            {
                BS2ErrorCode result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, deviceIpAddress, port, out deviceID);

                if (nCnt > 7)
                {
                    Console.WriteLine("Can't connect to device(errorCode : {0}).", result);
                    return(false);
                }

                if (result != BS2ErrorCode.BS_SDK_SUCCESS)
                {
                    nCnt++;
                    continue;
                }
                else
                {
                    break;
                }
            }

            Console.WriteLine(">>>> Successfully connected to the device[{0}].", deviceID);
            return(true);
        }
Example #2
0
        bool ConnectToDevice(ref UInt32 deviceID)
        {
            Console.WriteLine("Enter the IP Address to connect device");
            Console.Write(">>>> ");
            string    deviceIpAddress = Console.ReadLine();
            IPAddress ipAddress;

            if (!IPAddress.TryParse(deviceIpAddress, out ipAddress))
            {
                Console.WriteLine("Invalid ip : " + deviceIpAddress);
                return(false);
            }

            Console.WriteLine("Enter the port number to connect device : default[{0}]", BS2Environment.BS2_TCP_DEVICE_PORT_DEFAULT);
            Console.Write(">>>> ");
            UInt16 port = Util.GetInput((UInt16)BS2Environment.BS2_TCP_DEVICE_PORT_DEFAULT);

            Console.WriteLine("Trying to connect to device [ip :{0}, port : {1}]", deviceIpAddress, port);


            BS2ErrorCode result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, deviceIpAddress, port, out 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);
        }
Example #3
0
 public bool ConnectDevice(string deviceIP, ref UInt32 deviceID)
 {
     try
     {
         _log.InfoFormat("正在连接 {0}", deviceIP);
         BS2ErrorCode result = (BS2ErrorCode)API.BS2_ConnectDeviceViaIP(sdkContext, deviceIP, 51211, out deviceID);
         if (BS2ErrorCode.BS_SDK_SUCCESS != result)
         {
             _log.ErrorFormat("连接设备失败, 错误码 : {0} 设备IP {1}", result, deviceIP);
             return(false);
         }
         if (!idMapIp.ContainsKey(deviceID))
         {
             idMapIp.Add(deviceID, deviceIP);
         }
         _log.ErrorFormat("连接设备成功 设备IP : {0}", deviceID);
         return(true);
     }
     catch (Exception)
     {
         _log.ErrorFormat("连接设备异常, 设备IP {0}", deviceIP);
         return(false);
     }
 }
Example #4
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;
            }
        }