/// <summary> /// 一天一天删除文件 /// </summary> /// <param name="days"></param> private void DeleteFileByDay(List <string> days, StreamDvrPlan sdp) { var mediaServer = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(sdp.MediaServerId)); if (mediaServer == null || !mediaServer.IsRunning) { return; } foreach (var day in days) { var deleteList = OrmService.Db.Select <RecordFile>().Where(x => x.RecordDate == day).ToList(); if (deleteList != null && deleteList.Count > 0) { var deleteFileList = deleteList.Select(x => x.VideoPath).ToList(); OrmService.Db.Update <RecordFile>().Set(x => x.UpdateTime, DateTime.Now) .Set(x => x.Deleted, true) .Where(x => x.RecordDate == day).ExecuteAffrows(); mediaServer.DeleteFileList(deleteFileList, out _); LogWriter.WriteLog("要删除除一天的文件,数据库标记为删除", day !); } Thread.Sleep(100); } }
private decimal getDvrPlanFileSize(StreamDvrPlan sdp) { try { lock (SrsManageCommon.Common.LockDbObjForDvrVideo) { return(OrmService.Db.Select <DvrVideo>() .WhereIf(!string.IsNullOrEmpty(sdp.DeviceId), x => x.App !.Trim().ToLower().Equals(sdp.App !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.VhostDomain), x => x.Vhost !.Trim().ToLower().Equals(sdp.VhostDomain !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.DeviceId), x => x.Device_Id !.Trim().ToLower().Equals(sdp.DeviceId !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.Stream), x => x.Stream !.Trim().ToLower().Equals(sdp.Stream !.Trim().ToLower())) .Where(x => x.Deleted == false) .Sum(x => x.FileSize)); } } catch (Exception ex) { Console.WriteLine(ex.Message); return(-1); } }
private bool getDvrOnorOff(StreamDvrPlan sdp) { var dvr = VhostDvrApis.GetVhostDvr(sdp.DeviceId, sdp.VhostDomain, out ResponseStruct rs); if (dvr == null) { return(false); } string?dvrApply = dvr.Dvr_apply !; dvrApply = dvrApply.Replace(";", ""); List <string> dvrStreams = new List <string>(); if (dvrApply.Trim().ToLower().Equals("all")) { dvrApply = ""; } if (!string.IsNullOrEmpty(dvrApply)) { dvrStreams = Regex.Split(dvrApply, @"[\s]+").ToList(); if (!dvrStreams.Contains((sdp.App + "/" + sdp.Stream).Trim())) { return(false); } return(true); } return(false); }
private List <string> getDvrPlanFileDataList(StreamDvrPlan plan) { List <string?> ret = null !; lock (SrsManageCommon.Common.LockDbObjForDvrVideo) { ret = OrmService.Db.Select <DvrVideo>() .WhereIf(!string.IsNullOrEmpty(plan.DeviceId), x => x.App !.Trim().ToLower().Equals(plan.App !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(plan.VhostDomain), x => x.Vhost !.Trim().ToLower().Equals(plan.VhostDomain !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(plan.DeviceId), x => x.Device_Id !.Trim().ToLower().Equals(plan.DeviceId !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(plan.Stream), x => x.Stream !.Trim().ToLower().Equals(plan.Stream !.Trim().ToLower())) .Where(x => x.Deleted == false) .GroupBy(x => x.RecordDate) .OrderBy(x => x.Value.RecordDate) .ToList(a => a.Value.RecordDate); } if (ret != null && ret.Count > 0) { return(ret !); } return(null !); }
private void setDvrOnorOff(StreamDvrPlan sdp, bool eanble) { var dvr = VhostDvrApis.GetVhostDvr(sdp.DeviceId, sdp.VhostDomain, out ResponseStruct rs); if (dvr != null) { string? dvrApply = dvr.Dvr_apply !; List <string> dvrStreams = new List <string>(); if (!string.IsNullOrEmpty(dvrApply)) { dvrStreams = Regex.Split(dvrApply, @"[\s]+").ToList(); } if (dvrStreams.Count == 0) { dvrStreams.Add(""); } for (int i = 0; i <= dvrStreams.Count - 1; i++) { dvrStreams[i] = dvrStreams[i].TrimEnd(';').Trim(); } bool needWrite = false; switch (eanble) { case true: if (!dvrStreams.Contains((sdp.App + "/" + sdp.Stream).Trim())) { dvrStreams.Add((sdp.App + "/" + sdp.Stream).Trim()); needWrite = true; } break; case false: if (dvrStreams.Contains((sdp.App + "/" + sdp.Stream).Trim())) { dvrStreams.Remove((sdp.App + "/" + sdp.Stream).Trim()); needWrite = true; } break; } if (needWrite) { dvr.Dvr_apply = ""; foreach (var str in dvrStreams) { dvr.Dvr_apply += str + "\t"; } dvr.Dvr_apply = dvr.Dvr_apply.TrimEnd('\t'); VhostDvrApis.SetVhostDvr(sdp.DeviceId, sdp.VhostDomain, dvr, out rs); SystemApis.RefreshSrsObject(sdp.DeviceId, out rs); } } }
private void deleteFileOneByOne(decimal videoSize, StreamDvrPlan sdp) { long deleteSize = 0; List <OrderByStruct> orderBy = new List <OrderByStruct>(); orderBy.Add(new OrderByStruct() { FieldName = "starttime", OrderByDir = OrderByDir.ASC, }); ReqGetDvrVideo rgdv = new ReqGetDvrVideo() { App = sdp.App, DeviceId = sdp.DeviceId, EndTime = null, IncludeDeleted = false, OrderBy = orderBy, PageIndex = 1, PageSize = 10, StartTime = null, Stream = sdp.Stream, VhostDomain = sdp.VhostDomain, }; while (videoSize - deleteSize > sdp.LimitSpace) { DvrVideoResponseList videoList = DvrPlanApis.GetDvrVideoList(rgdv, out ResponseStruct rs); if (videoList != null && videoList.DvrVideoList != null && videoList.DvrVideoList.Count > 0) { lock (SrsManageCommon.Common.LockDbObjForDvrVideo) { OrmService.Db.Transaction(() => { foreach (var ret in videoList.DvrVideoList) { if (ret != null) { if (File.Exists(ret.VideoPath)) { File.Delete(ret.VideoPath); deleteSize += (long)ret.FileSize !; LogWriter.WriteLog("删除录制文件", ret.VideoPath !); Thread.Sleep(20); } OrmService.Db.Update <DvrVideo>().Set(x => x.UpdateTime, DateTime.Now) .Set(x => x.Deleted, true) .Where(x => x.Id == ret !.Id).ExecuteAffrows(); } if ((videoSize - deleteSize) < sdp.LimitSpace) { break; } } }); } } } }
/// <summary> /// 清除24小时前软删除的文件 /// </summary> /// <param name="sdp"></param> private void doDeleteFor24HourAgo(StreamDvrPlan sdp) { List <DvrVideo> retList = null !; lock (SrsManageCommon.Common.LockDbObjForDvrVideo) { retList = OrmService.Db.Select <DvrVideo>() .WhereIf(!string.IsNullOrEmpty(sdp.DeviceId), x => x.App !.Trim().ToLower().Equals(sdp.App !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.VhostDomain), x => x.Vhost !.Trim().ToLower().Equals(sdp.VhostDomain !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.DeviceId), x => x.Device_Id !.Trim().ToLower().Equals(sdp.DeviceId !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.Stream), x => x.Stream !.Trim().ToLower().Equals(sdp.Stream !.Trim().ToLower())) .Where(x => x.Deleted == true) .Where(x => ((DateTime)x.UpdateTime !).AddHours(24) <= DateTime.Now) .ToList(); } if (retList != null && retList.Count > 0) { lock (SrsManageCommon.Common.LockDbObjForDvrVideo) { OrmService.Db.Transaction(() => { foreach (var ret in retList) { if (ret != null) { if (ret.UpdateTime >= DateTime.Now) { if (File.Exists(ret.VideoPath)) { File.Delete(ret.VideoPath); LogWriter.WriteLog("删除被软删除的录制文件(24小时)", ret.VideoPath !); Thread.Sleep(20); } OrmService.Db.Update <DvrVideo>().Set(x => x.UpdateTime, DateTime.Now) .Set(x => x.Undo, false) .Where(x => x.Id == ret !.Id).ExecuteAffrows(); } } } }); } } }
/// <summary> /// 获取文件大小 /// </summary> /// <param name="sdp"></param> /// <returns></returns> private decimal getDvrPlanFileSize(StreamDvrPlan sdp) { try { return(OrmService.Db.Select <RecordFile>() .WhereIf(!string.IsNullOrEmpty(sdp.MediaServerId), x => x.PushMediaServerId !.Trim().ToLower().Equals(sdp.MediaServerId !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.CameraId), x => x.CameraId !.Trim().ToLower().Equals(sdp.CameraId !.Trim().ToLower())) .Where(x => x.Deleted == false) .Sum(x => x.FileSize)); } catch (Exception ex) { Console.WriteLine(ex.Message); return(-1); } }
/// <summary> /// 获取录制状态 /// </summary> /// <param name="sdp"></param> /// <returns></returns> private bool getDvrOnorOff(StreamDvrPlan sdp) { var mediaServer = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(sdp.MediaServerId)); if (mediaServer != null && mediaServer.IsRunning) { var sessionList = MediaServerApis.GetCameraSessionList(mediaServer.MediaServerId, out _); if (sessionList != null) { var obj = sessionList.FindLast(x => x.CameraId.Equals(sdp.CameraId)); if (obj != null) { var ret = mediaServer.WebApi.GetRecordStatus(new ReqZLMediaKitStopRecord() { App = obj.App, Secret = "", Stream = obj.StreamId, Vhost = obj.Vhost, }, out _); if (ret.Code == 0 && ret.Status != null) { try { lock (Common.CameraSessionLock) { Common.CameraSessions.FindLast(x => x.CameraId.Equals(obj.CameraId) && x.MediaServerId.Equals(obj.MediaServerId)) .IsRecord = ret.Status; } } catch { // } return((bool)ret.Status); } } } } return(false); }
/// <summary> /// 执行删除指令 /// </summary> /// <param name="sdp"></param> private void ExecDelete(StreamDvrPlan sdp) { doDeleteFor24HourAgo(sdp); //删除24小时过期的软删除文件 if (sdp.OverStepPlan == OverStepPlan.DeleteFile) { if (sdp.LimitDays > 0) //处理有时间限制的 { List <string?> dateList = null !; dateList = getDvrPlanFileDataList(sdp) !; if (dateList != null) { Common.RemoveNull(dateList); } if (dateList != null && dateList.Count > sdp.LimitDays) { //执行一天一天删除 int?loopCount = dateList.Count - sdp.LimitDays; List <string> willDeleteDays = new List <string>(); for (int i = 0; i < loopCount; i++) { willDeleteDays.Add(dateList[i] !); } DeleteFileByDay(willDeleteDays, sdp); } } if (sdp.LimitSpace > 0) //处理有容量限制的情况 { decimal videoSize = getDvrPlanFileSize(sdp); if (videoSize > sdp.LimitSpace) { //一个一个删除文件 deleteFileOneByOne(videoSize, sdp); } } } }
/// <summary> /// 检查是否在时间范围内 /// </summary> /// <param name="sdp"></param> /// <returns></returns> private bool checkTimeRange(StreamDvrPlan sdp) { if (sdp.TimeRangeList != null && sdp.TimeRangeList.Count > 0) { var t = sdp.TimeRangeList.FindLast(x => x.WeekDay == DateTime.Now.DayOfWeek); if (t != null && isTimeRange(t)) //有当天计划并在时间反问内返回true { return(true); } if (t != null && !isTimeRange(t)) //只有设置当天计划并且不在当天计划时间内,返回false { return(false); } return(true); //如果没有设置当天计划也返回true } return(true); //如果是空的,就直接返回可运行 }
private void execDvrOnOrOffPlan(StreamDvrPlan sdp) { if (sdp.Enable) { if (sdp.TimeRangeList != null && sdp.TimeRangeList.Count > 0) { var t = sdp.TimeRangeList.FindLast(x => x.WeekDay == DateTime.Now.DayOfWeek); if (t != null && isTimeRange(t)) { if (sdp.LimitSpace != null && sdp.LimitSpace > 0 && sdp.LimitDays != null && sdp.LimitDays > 0 && !getDvrOnorOff(sdp)) { var videoSize = getDvrPlanFileSize(sdp); var dateList = getDvrPlanFileDataList(sdp); if (videoSize < sdp.LimitSpace && dateList.Count < sdp.LimitDays) { LogWriter.WriteLog("录制计划需要" + (true ? "启用" : "停用") + sdp.App + "/" + sdp.Stream + "的录制计划," + " VideoSize:" + videoSize + "::LimitSpace:" + sdp.LimitSpace + " 天数:" + dateList !.Count + "::LimitDays:" + sdp.LimitDays); setDvrOnorOff(sdp, true); //录制时间在范围内,开始录制 } } else if (!getDvrOnorOff(sdp)) { LogWriter.WriteLog("录制计划需要启用" + sdp.App + "/" + sdp.Stream + "录制计划,因为已经到了录制时间范围"); setDvrOnorOff(sdp, true); //录制时间在范围内,开始录制 } } else { if (getDvrOnorOff(sdp)) { LogWriter.WriteLog("录制计划需要停用" + sdp.App + "/" + sdp.Stream + "录制计划,因为不在录制的时间范围内"); setDvrOnorOff(sdp, false); //不在录制时间范围内,停止录制 } } } } }
static void Main(string[] args) { DBManager.fsql.Delete <StreamDvrPlan>().Where("1=1").ExecuteAffrows(); DBManager.fsql.Delete <DvrDayTimeRange>().Where("1=1").ExecuteAffrows(); var a = new StreamDvrPlan(); a.App = "live"; a.Enable = true; a.Stream = "stream"; a.DeviceId = "device_id123"; a.LimitDays = 10; a.LimitSpace = 99999999; a.VhostDomain = "vhost"; a.OverStepPlan = OverStepPlan.DeleteFile; a.TimeRangeList = new List <DvrDayTimeRange>(); DvrDayTimeRange b = new DvrDayTimeRange(); b.EndTime = DateTime.Now.AddDays(10); b.StartTime = DateTime.Now; b.WeekDay = DateTime.Now.DayOfWeek; a.TimeRangeList.Add(b); DvrDayTimeRange c = new DvrDayTimeRange(); c.EndTime = DateTime.Now.AddDays(15); c.StartTime = DateTime.Now.AddDays(-5); c.WeekDay = DayOfWeek.Monday; a.TimeRangeList.Add(c); /*联同子类一起插入*/ var repo = DBManager.fsql.GetRepository <StreamDvrPlan>(); repo.DbContextOptions.EnableAddOrUpdateNavigateList = true; //需要手工开启 repo.Insert(a); /*联同子类一起插入*/ /*联同子类一起查出*/ var ret = DBManager.fsql.Select <StreamDvrPlan>().IncludeMany(a => a.TimeRangeList) .ToList(); /*联同子类一起查出*/ Console.WriteLine("Hello World!"); }
/// <summary> /// 检查是否在时间范围内 /// </summary> /// <param name="sdp"></param> /// <returns></returns> private bool checkTimeRange(StreamDvrPlan sdp) { if (sdp.TimeRangeList != null && sdp.TimeRangeList.Count > 0) { bool haveFalse = false; foreach (var sdpTimeRange in sdp.TimeRangeList) { if (sdpTimeRange != null && sdpTimeRange.WeekDay == DateTime.Now.DayOfWeek && isTimeRange(sdpTimeRange)) { return(true); //有当天计划并在时间反问内返回true } if (sdpTimeRange != null && sdpTimeRange.WeekDay == DateTime.Now.DayOfWeek && !isTimeRange(sdpTimeRange)) { haveFalse = true; //当天计划存在,但不在范围,先做个标记,因为也许会有多个星期n的情况 } } if (haveFalse) { return(false); //如果循环以外,haveFalse为true,说明真的不在范围内 } /*var t = sdp.TimeRangeList.FindLast(x => x.WeekDay == DateTime.Now.DayOfWeek); * if (t != null && isTimeRange(t)) //有当天计划并在时间反问内返回true * { * return true; * } * * if (t != null && !isTimeRange(t)) //只有设置当天计划并且不在当天计划时间内,返回false * { * return false; * } * * return true; //如果没有设置当天计划也返回true*/ } return(true); //如果是空的,就直接返回可运行 }
/// <summary> /// 清除24小时前软删除的文件 /// </summary> /// <param name="sdp"></param> private void doDeleteFor24HourAgo(StreamDvrPlan sdp) { try { List <RecordFile> retList = null !; retList = OrmService.Db.Select <RecordFile>() .WhereIf(!string.IsNullOrEmpty(sdp.MediaServerId), x => x.PushMediaServerId !.Trim().ToLower().Equals(sdp.MediaServerId !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(sdp.CameraId), x => x.CameraId !.Trim().ToLower().Equals(sdp.CameraId !.Trim().ToLower())) .Where(x => x.Deleted == true) .Where(x => ((DateTime)x.UpdateTime !).AddHours(24) <= DateTime.Now) .ToList(); if (retList != null && retList.Count > 0) { var deleteFileList = retList.Select(x => x.VideoPath).ToList(); var deleteFileIdList = retList.Select(x => x.Id).ToList(); var mediaServer = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(retList[0].PushMediaServerId)); if (mediaServer != null && mediaServer.IsRunning) { var delRet = mediaServer.DeleteFileList(deleteFileList, out _); if (delRet) { var a = OrmService.Db.Update <RecordFile>().Set(x => x.UpdateTime, DateTime.Now) .Set(x => x.Undo, false) .Where(x => deleteFileIdList.Contains(x.Id)).ExecuteAffrows(); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } }
/// <summary> /// 获取记录日期列表 /// </summary> /// <param name="plan"></param> /// <returns></returns> private List <string> getDvrPlanFileDataList(StreamDvrPlan plan) { List <string?> ret = null !; ret = OrmService.Db.Select <RecordFile>() .WhereIf(!string.IsNullOrEmpty(plan.MediaServerId), x => x.PushMediaServerId !.Trim().ToLower().Equals(plan.MediaServerId !.Trim().ToLower())) .WhereIf(!string.IsNullOrEmpty(plan.CameraId), x => x.CameraId !.Trim().ToLower().Equals(plan.CameraId !.Trim().ToLower())) .Where(x => x.Deleted == false) .GroupBy(x => x.RecordDate) .OrderBy(x => x.Value.RecordDate) .ToList(a => a.Value.RecordDate); if (ret != null && ret.Count > 0) { return(ret !); } return(null !); }
private bool checkTimeRange(StreamDvrPlan sdp) { if (sdp.Enable) { if (sdp.TimeRangeList != null && sdp.TimeRangeList.Count > 0) { var t = sdp.TimeRangeList.FindLast(x => x.WeekDay == DateTime.Now.DayOfWeek); if (t != null && isTimeRange(t)) { return(true); } else { return(false); } } return(true); //如果是空的,就直接返回可运行 } return(false); }
/// <summary> /// 设置是否启用录制 /// </summary> /// <param name="sdp"></param> /// <param name="enable"></param> private void setDvrOnorOff(StreamDvrPlan sdp, bool enable) { var mediaServer = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(sdp.MediaServerId)); if (mediaServer != null && mediaServer.IsRunning) { var sessionList = MediaServerApis.GetCameraSessionList(mediaServer.MediaServerId, out _); if (sessionList != null) { var obj = sessionList.FindLast(x => x.CameraId.Equals(sdp.CameraId)); if (obj != null) { if (enable) { mediaServer.WebApi.StartRecord(new ReqZLMediaKitStartRecord() { App = obj.App, Secret = "", Stream = obj.StreamId, Vhost = obj.Vhost, Customized_Path = mediaServer.RecordFilePath, }, out _); } else { mediaServer.WebApi.StopRecord(new ReqZLMediaKitStopRecord() { App = obj.App, Secret = "", Stream = obj.StreamId, Vhost = obj.Vhost, }, out _); } } } } }
/// <summary> /// 执行启动和关闭指令 /// </summary> /// <param name="dvrPlan"></param> private void execOnOrOff(StreamDvrPlan sdp) { bool isEnable = true; int dateCount = 0; decimal videoSize = 0; List <string?> dateList = null !; videoSize = getDvrPlanFileSize(sdp) !; dateList = getDvrPlanFileDataList(sdp) !; if (dateList != null && dateList.Count > 0) { Common.RemoveNull(dateList); dateCount = dateList.Count; } else { dateCount = 0; } if (sdp.OverStepPlan == OverStepPlan.StopDvr) { if (sdp.LimitDays > 0) //处理有天数限制的情况 { if (sdp.LimitDays < dateCount) { //停掉 isEnable = false; } } if (sdp.LimitSpace > 0) //处理有天数限制的情况 { if (videoSize > sdp.LimitSpace) { //停掉 isEnable = false; } } } bool isTime = checkTimeRange(sdp); isEnable = isEnable && sdp.Enable; //要处理计划停用的状态 if (isTime && isEnable) { if (!getDvrOnorOff(sdp)) { LogWriter.WriteLog("录制计划即将启动录制,因为视频流没有达到受限条件,已经进入计划规定时间内并且录制程序处于关闭状态", sdp.MediaServerId + "->" + sdp.CameraId + "->" + sdp.CameraId + "\t" + "空间限制:" + sdp.LimitSpace.ToString() + "字节::实际空间占用:" + videoSize.ToString() + "字节 \t时间限制:" + sdp.LimitDays.ToString() + "天::实际录制天数:" + dateCount.ToString() + "\t录制计划启用状态:" + sdp.Enable.ToString()); setDvrOnorOff(sdp, true); } } else { if (getDvrOnorOff(sdp)) { LogWriter.WriteLog("录制计划即将关闭录制,因为视频流可能达到受限条件或者已经离开计划规定时间内并且录制程序处于启动状态", sdp.MediaServerId + "->" + sdp.CameraId + "->" + "\t" + "空间限制:" + sdp.LimitSpace.ToString() + "字节::实际空间占用:" + videoSize.ToString() + "字节 \t时间限制:" + sdp.LimitDays.ToString() + "天::实际录制天数:" + dateCount.ToString() + "\t录制计划启用状态:" + sdp.Enable.ToString()); setDvrOnorOff(sdp, false); } } }
private void execDvrDeletePlan(StreamDvrPlan sdp) { doDeleteFor24HourAgo(sdp); //处理24小时后要删除的文件 if (sdp.OverStepPlan == OverStepPlan.StopDvr) { bool isEnable = true; List <string?> dateList = null !; //停止录制操作 if (sdp.LimitDays != null && sdp.LimitDays > 0) { //有日期限制 dateList = getDvrPlanFileDataList(sdp) !; if (dateList != null && dateList.Count > sdp.LimitDays) { isEnable = false; } else { isEnable = true; } } decimal videoSize = 0; if (sdp.LimitSpace != null && sdp.LimitSpace > 0) { videoSize = getDvrPlanFileSize(sdp); if (videoSize > sdp.LimitSpace) { isEnable = false; } else { if (isEnable) { isEnable = true; } } } isEnable = checkTimeRange(sdp) && isEnable && sdp.Enable; if (isEnable != getDvrOnorOff(sdp)) { LogWriter.WriteLog("录制计划需要" + (isEnable == true ? "启用" : "停用") + sdp.App + "/" + sdp.Stream + "的录制计划," + " VideoSize:" + videoSize + "::LimitSpace:" + sdp.LimitSpace + " 天数:" + dateList !.Count + "::LimitDays:" + sdp.LimitDays); setDvrOnorOff(sdp, isEnable); //启用或停用录制 } } else { if (sdp.Enable && !getDvrOnorOff(sdp) && checkTimeRange(sdp) ) //sdp的enable为真,并且没在运行,同时还需要在时间范围内(或者时间范围为空) { LogWriter.WriteLog("录制计划需要" + (true ? "启用" : "停用") + sdp.App + "/" + sdp.Stream + "的录制计划状态为启用,同时因为录制计划超限是文件删除模式"); setDvrOnorOff(sdp, true); //启用或停用录制 } else if (!sdp.Enable && getDvrOnorOff(sdp)) { LogWriter.WriteLog( "录制计划需要" + (false ? "启用" : "停用") + sdp.App + "/" + sdp.Stream + "的录制计划,录制计划状态为停用"); setDvrOnorOff(sdp, false); //启用或停用录制 } //文件删除操作 //停止录制操作 if (sdp.LimitDays != null && sdp.LimitDays > 0) { //有日期限制 List <string?> dateList = getDvrPlanFileDataList(sdp) !; if (dateList != null && dateList.Count > sdp.LimitDays) { //一天一天删除文件 int? loopCount = dateList.Count - sdp.LimitDays; List <string> willDeleteDays = new List <string>(); for (int i = 0; i < loopCount; i++) { willDeleteDays.Add(dateList[i] !); } deleteFileByDay(willDeleteDays); } } if (sdp.LimitSpace != null && sdp.LimitSpace > 0) { decimal videoSize = getDvrPlanFileSize(sdp); if (videoSize > sdp.LimitSpace) { //一个一个删除文件 deleteFileOneByOne(videoSize, sdp); } } } }
/// <summary> /// 文件一个一个删除 /// </summary> /// <param name="videoSize"></param> /// <param name="sdp"></param> private void deleteFileOneByOne(decimal videoSize, StreamDvrPlan sdp) { var mediaServer = Common.MediaServerList.FindLast(x => x.MediaServerId.Equals(sdp.MediaServerId)); if (mediaServer == null || !mediaServer.IsRunning) { return; } long deleteSize = 0; List <OrderByStruct> orderBy = new List <OrderByStruct>(); orderBy.Add(new OrderByStruct() { FieldName = "starttime", OrderByDir = OrderByDir.ASC, }); ReqGetDvrVideo rgdv = new ReqGetDvrVideo() { CameraId = sdp.CameraId, MediaServerId = sdp.MediaServerId, EndTime = null, IncludeDeleted = false, OrderBy = orderBy, PageIndex = 1, PageSize = 10, StartTime = null, }; while (videoSize - deleteSize > sdp.LimitSpace) { DvrVideoResponseList videoList = MediaServerApis.GetDvrVideoList(rgdv, out ResponseStruct rs); if (videoList != null && videoList.DvrVideoList != null && videoList.DvrVideoList.Count > 0) { OrmService.Db.Transaction(() => { foreach (var ret in videoList.DvrVideoList) { if (ret != null) { if (mediaServer.CheckFileExists(ret.VideoPath, out _)) { mediaServer.DeleteFile(ret.VideoPath, out _); deleteSize += (long)ret.FileSize !; OrmService.Db.Update <RecordFile>().Set(x => x.UpdateTime, DateTime.Now) .Set(x => x.Deleted, true) .Where(x => x.Id == ret !.Id).ExecuteAffrows(); LogWriter.WriteLog("删除录制文件", ret.VideoPath !); Thread.Sleep(20); } } if ((videoSize - deleteSize) < sdp.LimitSpace) { break; } } }); } } }