| | |
| | | package com.whyc.schedule; |
| | | |
| | | import com.whyc.constant.YamlProperties; |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.dto.Response4Http; |
| | | import com.whyc.factory.ThreadPoolExecutorFactory; |
| | | import com.whyc.service.VideoService; |
| | | import com.whyc.util.DateUtil; |
| | | import com.whyc.util.FileUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.EnableScheduling; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.File; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @EnableScheduling |
| | | @Component |
| | |
| | | @Autowired |
| | | private VideoService service; |
| | | |
| | | /**凌晨0点执行一次,录制视频流*/ |
| | | @Scheduled(cron = "0 0 0 * * ? ") |
| | | public void getAndSaveB247(){ |
| | | /** |
| | | * 1.监控录制,频次为2s. |
| | | * 2.视频录制的判断逻辑是: |
| | | * 是否存在流 |
| | | * 是否在录制 |
| | | * 3.考虑意外情况:突然程序死机或者电脑断电重启,需要延时启动录像,因为视频推流服务还未完成,延时20s开始检测 |
| | | * |
| | | * */ |
| | | //@Scheduled(cron = "0/2 * * * * ? ") |
| | | @Scheduled(fixedRate = 2000,initialDelay = 20000) |
| | | public void startRecordAndCheck(){ |
| | | ThreadPoolExecutorFactory.getPoolExecutor().execute(()->{ |
| | | //service.getAndSaveB247(); |
| | | List<String> videoStreamIds = YamlProperties.videoStreamIds; |
| | | Response4Http streamIdsOnlineRes = service.getVideoStreamIds(); |
| | | List<String> streamIdsOnline = (List<String>) streamIdsOnlineRes.getData2()==null?new ArrayList<>():(List<String>) streamIdsOnlineRes.getData2(); |
| | | |
| | | if(streamIdsOnlineRes.getCode() == 1 && ((boolean) streamIdsOnlineRes.getData())){ |
| | | videoStreamIds.forEach(streamId->{ |
| | | if(!streamIdsOnline.contains(streamId)){ //流不存在 |
| | | //添加流 |
| | | Response4Http<String> response = service.addStreamProxy(streamId); |
| | | |
| | | Response4Http response2 = service.startRecord(streamId); |
| | | if(!(response2.getCode() ==1 && ((boolean) response2.getData()))){ |
| | | System.out.println("流id为:"+streamId+"的信息:"+response2.getMsg()); |
| | | }else{ |
| | | System.out.println("流id为:"+streamId+"的信息:"+response2.getData()); |
| | | } |
| | | }else{ //流存在 |
| | | //检查是否在录制状态 |
| | | Response4Http recordingRes = service.isRecording(streamId); |
| | | if(!(boolean)recordingRes.getData()){ //不在录制,则进行录制 |
| | | Response4Http response2 = service.startRecord(streamId); |
| | | if(!(response2.getCode() ==1 && ((boolean) response2.getData()))){ |
| | | System.out.println("流id为:"+streamId+"的信息:"+response2.getMsg()); |
| | | }else{ |
| | | System.out.println("流id为:"+streamId+"的信息:"+response2.getData()); |
| | | } |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /**凌晨0点0分2秒执行一次,停止上次的录制视频流*/ |
| | | @Scheduled(cron = "2 0 0 * * ? ") |
| | | public void getAndSaveA250(){ |
| | | ThreadPoolExecutorFactory.getPoolExecutor().execute(()->{ |
| | | //service.getAndSaveA250(); |
| | | /** |
| | | * 录像保持3天 |
| | | */ |
| | | @Scheduled(cron= "0 0 3 * * ?") |
| | | public void recordRecycle(){ |
| | | System.out.println("-----------执行录像循环----------:"+ DateUtil.YYYY_MM_DD_HH_MM_SS.format(new Date())); |
| | | //文件夹路径 /battery_system/video_system/Debug/www/record/rtp/{streamId}/{day}} |
| | | List<String> videoStreamIds = YamlProperties.videoStreamIds; |
| | | videoStreamIds.forEach(streamId ->{ |
| | | String dirPath = "/battery_system/video_system/Debug/www/record/rtp/"+streamId; |
| | | File dirFile = new File(dirPath); |
| | | String[] dirList = dirFile.list(); |
| | | List<String> dirList2 = Arrays.asList(dirList); |
| | | |
| | | for (int i = 0; i < dirList2.size()-3; i++) { |
| | | //按顺序删除,保留最后3个day的视频录像文件夹 |
| | | String dirPath2Remove = dirPath + File.separator + dirList2.get(i); |
| | | File dir2Remove = new File(dirPath2Remove); |
| | | FileUtil.deleteFile(dir2Remove); |
| | | } |
| | | }); |
| | | } |
| | | |