示例#1
0
        public async Task <OResult <Boolean> > BackupByTimeAsync(DateTime from, DateTime to, CancellationToken cancellationToken = default)
        {
            try
            {
                var lpDiskList = new CHCNetSDK.NET_DVR_DISKABILITY_LIST();
                lpDiskList.dwSize = (UInt32)Marshal.SizeOf(lpDiskList);

                var succ = CHCNetSDK.NET_DVR_GetDiskList(_userId, ref lpDiskList);
                if (!succ)
                {
                    return(new OResult <Boolean>(false, HkvsErrorCode.GetLastErrorCode(), $"获取磁盘列表失败:{HkvsErrorCode.GetLastErrorMessage()}"));
                }

                if (!lpDiskList.struDescNode.Any())
                {
                    return(new OResult <Boolean>(false, "可用磁盘列表为空"));
                }

                var availableDesc = lpDiskList.struDescNode.Where(x => x.dwFreeSpace > 0).Select(x => x.byDescribe).FirstOrDefault();
                if (availableDesc == null || !availableDesc.Any())
                {
                    return(new OResult <Boolean>(false, "磁盘空间不足"));
                }

                var lpBackupByTime = new CHCNetSDK.NET_DVR_BACKUP_TIME_PARAM();

                lpBackupByTime.lChannel = _loginRequest.ChannelNo;

                lpBackupByTime.struStartTime.dwYear   = (UInt32)from.Year;
                lpBackupByTime.struStartTime.dwMonth  = (UInt32)from.Month;
                lpBackupByTime.struStartTime.dwDay    = (UInt32)from.Day;
                lpBackupByTime.struStartTime.dwHour   = (UInt32)from.Hour;
                lpBackupByTime.struStartTime.dwMinute = (UInt32)from.Minute;
                lpBackupByTime.struStartTime.dwSecond = (UInt32)from.Second;

                lpBackupByTime.struStopTime.dwYear   = (UInt32)to.Year;
                lpBackupByTime.struStopTime.dwMonth  = (UInt32)to.Month;
                lpBackupByTime.struStopTime.dwDay    = (UInt32)to.Day;
                lpBackupByTime.struStopTime.dwHour   = (UInt32)to.Hour;
                lpBackupByTime.struStopTime.dwMinute = (UInt32)to.Minute;
                lpBackupByTime.struStopTime.dwSecond = (UInt32)to.Second;

                lpBackupByTime.byDiskDes    = availableDesc; //备份磁盘的描述
                lpBackupByTime.byWithPlayer = 0;             //是否备份播放器,0-不备份,1-备份
                lpBackupByTime.byContinue   = 0;             //是否继续备份,0-不继续,1-继续
                lpBackupByTime.byDrawFrame  = 0;             //0-不抽帧,1-抽帧

                var backUpHandle = CHCNetSDK.NET_DVR_BackupByTime(_userId, ref lpBackupByTime);
                if (backUpHandle < 0)
                {
                    return(new OResult <Boolean>(false, $"按时间备份录像失败:{HkvsErrorCode.GetLastErrorMessage()}"));
                }

                var tcs = new TaskCompletionSource <Boolean>();

                _ = Task.Run(async() =>
                {
                    UInt32 iPos   = 0;
                    IntPtr pState = Marshal.AllocHGlobal((Int32)iPos);
                    Marshal.StructureToPtr(iPos, pState, false);

                    try
                    {
                        while (!cancellationToken.IsCancellationRequested)
                        {
                            var succ = CHCNetSDK.NET_DVR_GetBackupProgress(backUpHandle, pState);
                            if (succ)
                            {
                                iPos = (UInt32)Marshal.PtrToStructure(pState, typeof(UInt32));

                                if (iPos == 100)
                                {
                                    CHCNetSDK.NET_DVR_StopBackup(backUpHandle);
                                    tcs.SetResult(true);
                                    return;
                                }
                                else if (iPos == 101 || iPos >= 400)
                                {
                                    CHCNetSDK.NET_DVR_StopBackup(backUpHandle);
                                    return;
                                }
                            }

                            await Task.Delay(1000);
                        }
                    }
                    finally
                    {
                        Marshal.FreeHGlobal(pState);
                    }
                }, CancellationToken.None);

                succ = await tcs.Task;

                if (!succ)
                {
                    return(new OResult <Boolean>(false, "按时间下载失败"));
                }

                return(new OResult <Boolean>(true));
            }
            catch (Exception ex)
            {
                return(new OResult <Boolean>(false, ex));
            }
        }