Exemplo n.º 1
0
        static void Main(string[] args)
        {
            if (ParseCommandLine())
            {
                MinimaxDictionaryInitialization();

                string filePathFinished = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName) + "/video/" + m_address.ToString().Replace(".", "") + ".finished";

                if (!File.Exists(filePathFinished))
                {
                    InitAvigilon();

                    m_endPoint = new IPEndPoint(m_address, m_controlCenter.DefaultNvrPortNumber);
                    AvgError result = m_controlCenter.AddNvr(m_endPoint);

                    if (ErrorHelper.IsError(result))
                    {
                        Console.WriteLine("An error occurred while adding the NVR." + m_endPoint.Address);
                    }
                    else
                    {
                        // Start m_controlCenter.GetNvr
                        Activity activity = new Activity();
                        activity.Setup().Wait();

                        if (nvr == null)
                        {
                            Console.WriteLine("An error occurred while connecting to the NVR.");
                        }
                        else
                        {
                            LoginResult loginResult = nvr.Login(m_userName, m_password);
                            if (loginResult != 0)
                            {
                                Console.WriteLine("Failed to login to NVR: " + loginResult);
                            }
                            else
                            {
                                DateTime waitEnd = DateTime.Now + new TimeSpan(0, 0, 10);

                                List <IDevice> devices = new List <IDevice>();
                                while (DateTime.Now < waitEnd)
                                {
                                    devices = nvr.Devices;

                                    if (devices.Count == m_cameraCount)
                                    {
                                        break;
                                    }

                                    Thread.Sleep(500);
                                }

                                bool deviceConnectedExists = false;

                                // Получен список камер. Начинаем получение скриншотов
                                foreach (IDevice device in devices)
                                {
                                    if (device.Connected)
                                    {
                                        if (!deviceConnectedExists)
                                        {
                                            deviceConnectedExists = true;
                                        }

                                        IEntity iEntity = device.Entities.FirstOrDefault();

                                        if (iEntity != null)
                                        {
                                            uint          logicalId = iEntity.LogicalId;
                                            IEntityCamera camera    = (IEntityCamera)device.GetEntityByLogicalId(logicalId);

                                            if (camera == null)
                                            {
                                                Console.WriteLine("The given camera with LogicalId {0} is not connected to the NVR.", logicalId);
                                            }
                                            else
                                            {
                                                IStreamGroup streamGroup = m_controlCenter.CreateStreamGroup(PlaybackMode.Live);

                                                if (m_controlCenter.CreateStreamCallback(camera, streamGroup, MediaCoding.Jpeg, out IStreamCallback stream) == AvgError.Success)
                                                {
                                                    stream.OutputSize = new Size(2048, 1536);
                                                    stream.Overlays   = Overlay.ImageTime;
                                                    stream.Enable     = true;
                                                    IFrame   frame     = stream.GetFrame(new TimeSpan(0, 1, 0));
                                                    DateTime timeStamp = DateTime.Now;

                                                    byte[] byteFrame = frame.GetAsArray();

                                                    stream.Enable = false;

                                                    CreateXmlFile(byteFrame, logicalId, timeStamp);
                                                }
                                            }
                                        }
                                    }
                                }

                                // отметка об окончании экспорта xml файлов при их наличии
                                if (deviceConnectedExists)
                                {
                                    File.Create(filePathFinished);
                                }
                            }
                        }
                    }

                    m_controlCenter?.Dispose();
                    m_sdk.Shutdown();
                }
            }
        }