diff --git a/src/main/java/com/genersoft/iot/vmp/conf/CloudRecordTimer.java b/src/main/java/com/genersoft/iot/vmp/conf/CloudRecordTimer.java index 9424750c..7e11833f 100644 --- a/src/main/java/com/genersoft/iot/vmp/conf/CloudRecordTimer.java +++ b/src/main/java/com/genersoft/iot/vmp/conf/CloudRecordTimer.java @@ -3,8 +3,10 @@ package com.genersoft.iot.vmp.conf; import com.alibaba.fastjson2.JSONObject; import com.genersoft.iot.vmp.media.zlm.AssistRESTfulUtils; +import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils; import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem; import com.genersoft.iot.vmp.service.IMediaServerService; +import com.genersoft.iot.vmp.service.bean.CloudRecordItem; import com.genersoft.iot.vmp.storager.dao.CloudRecordServiceMapper; import com.genersoft.iot.vmp.vmanager.cloudRecord.CloudRecordController; import org.slf4j.Logger; @@ -13,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; +import java.io.File; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; @@ -33,12 +36,13 @@ public class CloudRecordTimer { private CloudRecordServiceMapper cloudRecordServiceMapper; @Autowired - private AssistRESTfulUtils assistRESTfulUtils; + private ZLMRESTfulUtils zlmresTfulUtils; /** * 定时查询待删除的录像文件 */ @Scheduled(cron = "0 0 0 * * ?") //每天的0点执行 +// @Scheduled(fixedRate = 5000) public void execute(){ logger.info("[录像文件定时清理] 开始清理过期录像文件"); // 获取配置了assist的流媒体节点 @@ -55,18 +59,29 @@ public class CloudRecordTimer { // 获取保存的最后截至日期,因为每个节点都有一个日期,也就是支持每个节点设置不同的保存日期, lastCalendar.add(Calendar.DAY_OF_MONTH, -mediaServerItem.getRecordDate()); Long lastDate = lastCalendar.getTimeInMillis(); + // 获取到截至日期之前的录像文件列表,文件列表满足未被收藏和保持的。这两个字段目前共能一致, // 为我自己业务系统相关的代码,大家使用的时候直接使用收藏(collect)这一个类型即可 - List filePathList = cloudRecordServiceMapper.queryRecordFilePathListForDelete(lastDate, mediaServerItem.getId()); - if (filePathList.isEmpty()) { + List cloudRecordItemList = cloudRecordServiceMapper.queryRecordListForDelete(lastDate, mediaServerItem.getId()); + if (cloudRecordItemList.isEmpty()) { continue; } - // 先调用assist删除磁盘文件,删除成功后再删除数据库记录 - JSONObject jsonObject = assistRESTfulUtils.deleteFiles(mediaServerItem, filePathList); - if (jsonObject != null && jsonObject.getInteger("code") == 0 && jsonObject.getInteger("data") > 0) { - result += jsonObject.getInteger("data"); - cloudRecordServiceMapper.deleteByFileList(filePathList, mediaServerItem.getId()); + List cloudRecordItemIdList = new ArrayList<>(); + for (CloudRecordItem cloudRecordItem : cloudRecordItemList) { + String date = new File(cloudRecordItem.getFilePath()).getParentFile().getName(); + JSONObject jsonObject = zlmresTfulUtils.deleteRecordDirectory(mediaServerItem, cloudRecordItem.getApp(), + cloudRecordItem.getStream(), date, cloudRecordItem.getFileName()); + if (jsonObject.getInteger("code") == 0) { + cloudRecordItemIdList.add(cloudRecordItem.getId()); + }else { + logger.warn("[录像文件定时清理] 删除磁盘文件错误: {}", jsonObject); + } } + if (cloudRecordItemIdList.isEmpty()) { + continue; + } + cloudRecordServiceMapper.deleteList(cloudRecordItemIdList, mediaServerItem.getId()); + result += cloudRecordItemIdList.size(); } } logger.info("[录像文件定时清理] 共清理{}个过期录像文件", result); diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java index fd63ce68..4f6d3e92 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java @@ -268,14 +268,4 @@ public class AssistRESTfulUtils { return sendGet(mediaServerItem, "api/record/file/download/task/list", param, null); } - - public JSONObject addCollect(MediaServerItem mediaServerItem, JSONObject jsonObject) { - return sendPost(mediaServerItem, "api/record/file/collection/add", jsonObject, null, 30); - } - - public JSONObject deleteFiles(MediaServerItem mediaServerItem, List filePathList) { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("filePathList", filePathList); - return sendPost(mediaServerItem, "api/record/file/delete", jsonObject, null, 15*60); - } } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java index 52bc9028..ea0b2241 100755 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java @@ -25,8 +25,6 @@ public class ZLMRESTfulUtils { private OkHttpClient client; - - public interface RequestCallback{ void run(JSONObject response); } @@ -398,4 +396,14 @@ public class ZLMRESTfulUtils { param.put("stream_id", streamId); return sendPost(mediaServerItem, "updateRtpServerSSRC",param, null); } + + public JSONObject deleteRecordDirectory(MediaServerItem mediaServerItem, String app, String stream, String date, String fileName) { + Map param = new HashMap<>(1); + param.put("vhost", "__defaultVhost__"); + param.put("app", app); + param.put("stream", stream); + param.put("period", date); + param.put("name", fileName); + return sendPost(mediaServerItem, "deleteRecordDirectory",param, null); + } } diff --git a/src/main/java/com/genersoft/iot/vmp/storager/dao/CloudRecordServiceMapper.java b/src/main/java/com/genersoft/iot/vmp/storager/dao/CloudRecordServiceMapper.java index 5fdf2f5c..775fe295 100644 --- a/src/main/java/com/genersoft/iot/vmp/storager/dao/CloudRecordServiceMapper.java +++ b/src/main/java/com/genersoft/iot/vmp/storager/dao/CloudRecordServiceMapper.java @@ -82,25 +82,28 @@ public interface CloudRecordServiceMapper { int updateCollectList(@Param("collect") boolean collect, List cloudRecordItemList); @Delete(" ") void deleteByFileList(List filePathList, @Param("mediaServerId") String mediaServerId); @Select(" ") - List queryRecordFilePathListForDelete(@Param("endTimeStamp")Long endTimeStamp, String mediaServerId); + List queryRecordListForDelete(@Param("endTimeStamp")Long endTimeStamp, String mediaServerId); @Update(" ") int changeCollectById(@Param("collect") boolean collect, @Param("recordId") Integer recordId); + @Delete(" ") + int deleteList(List cloudRecordItemIdList, @Param("mediaServerId") String mediaServerId); + }