Ejemplo n.º 1
0
        /// <summary>
        /// 开始拍照
        /// </summary>
        public async void StartProcess()
        {
            if (!Directory.Exists(_picsPath))
            {
                Directory.CreateDirectory(_picsPath);
            }
            var cameraStations = (await _cameraStationsService.GetAllAsny()).ToList();

            cameraStations.ForEach(async station =>
            {
                var dep = await _sysDepartmentService.GetByIdAsync(station.SysDepartmentSerialnum);
                if (dep != null)
                {
                    var depPath = GetDepPath(dep);
                    //if (!Directory.Exists(depPath))
                    //    Directory.CreateDirectory(depPath);

                    try
                    {
                        CHCNetSDK.NET_DVR_Init();
                        var userHandle = Login(station.IP, station.DataPort, station.UserID, station.UserPwd);
                        //var userHandle = Login("120.209.96.139", 20001, "admin", "smt12345");

                        if (userHandle != -1)
                        {
                            //从设备获取预置点
                            var points = await GetCameraPresetPoints(userHandle, station);

                            var cameraStationPath         = Path.Combine(await depPath, station.IP);
                            var cameraStationPresetPoints = points as CameraStationPresetPointDto[] ?? points.ToArray();
                            if (!cameraStationPresetPoints.Any())
                            {
                                var fileName = TakePhoto(userHandle, station.Channel, Path.Combine(_picsPath, cameraStationPath));
                                cameraStationPresetPoints.ToList().ForEach(cameraStationPresetPoint =>
                                {
                                    Process(fileName, cameraStationPresetPoint);
                                });
                            }
                            else
                            {
                                for (int i = 0; i < cameraStationPresetPoints.Count(); i++)
                                {
                                    var point = cameraStationPresetPoints.ElementAt(i);
                                    //跳转到预置点
                                    bool success = GotoCameraPreset(userHandle, Convert.ToUInt32(point.PresetPoint));
                                    if (success)
                                    {
                                        //等3秒聚焦
                                        Thread.Sleep(3000);
                                        //拍照
                                        var fileName = TakePhoto(userHandle, station.Channel, Path.Combine(_picsPath, cameraStationPath), i + 1);
                                        //处理文件对应的记录
                                        cameraStationPresetPoints.ToList().ForEach(cameraStationPresetPoint =>
                                        {
                                            Process(fileName, cameraStationPresetPoint);
                                        });
                                    }
                                }
                            }
                            CHCNetSDK.NET_DVR_Logout(userHandle);
                        }
                        //(待处理。。。。。。)
                        //设备运行记录
                        CameraStationRunLogDto record  = new CameraStationRunLogDto();
                        record.CameraStationsSerialnum = station.Serialnum;
                        record.Status = userHandle > -1;
                        await _cameraStationRunLogService.AddAsny(record);

                        //运行统计数据
                        var list = await _cameraStationOnlineStatisticsService.GetByCameraStationIdAndYearAndMonthAsny(station.Serialnum,
                                                                                                                       DateTime.Now.Year, DateTime.Now.Month);

                        CameraStationOnlineStatisticsDto statistics = null;
                        if (list == null || !list.Any())
                        {
                            statistics = new CameraStationOnlineStatisticsDto();
                            statistics.CameraStationsSerialnum = station.Serialnum;
                            statistics.Year  = DateTime.Now.Year;
                            statistics.Month = DateTime.Now.Month;
                        }
                        else
                        {
                            statistics = list.ToList()[0];
                        }

                        statistics.AllCount++;
                        if (userHandle > -1)
                        {
                            statistics.OnlineCount++;
                        }
                        statistics.OnlinePercent = Math.Round((statistics.OnlineCount * 1M) / (statistics.AllCount * 1M), 1) * 100M;
                        await _cameraStationOnlineStatisticsService.AddAsny(statistics);
                    }
                    catch (Exception ex)
                    {
                        XTrace.WriteException(ex);
                    }
                    finally
                    {
                        // CHCNetSDK.NET_DVR_Cleanup();
                    }
                }
            });
        }