拍照 视频 采用延时队列

merge-requests/3/head
qiushui 4 years ago
parent 4259649407
commit fbf22449e5

@ -292,9 +292,8 @@ public class CameraControlController {
@ApiOperation(value = "立即拍照")
public Result pic(@PathVariable Integer id) {
checkLogin(id);
String path = "D:\\work\\qqq.jpeg";
cameraControlModule.pic(id,0,path);
String path = "D:\\work\\"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMDDHHmmss"))+".jpeg";
TaskDelayExecutor.addPicDelayTask(id,path,2000L);
return Result.success();
}

@ -9,7 +9,7 @@ import java.util.concurrent.TimeUnit;
@Data
@AllArgsConstructor
public class DownloadMp4Delayed implements Delayed {
public class CameraDelayTask implements Delayed {
private Integer cameraId;
@ -19,6 +19,11 @@ public class DownloadMp4Delayed implements Delayed {
private String path;
/**
* 0 : mp4 1:jpeg
*/
private Integer type;
private long executeTime;
@Override

@ -1,9 +0,0 @@
package com.zhehekeji.web.lib;
import org.springframework.stereotype.Component;
@Component
public class DownloadConsumer {
}

@ -1,42 +0,0 @@
package com.zhehekeji.web.lib;
import com.zhehekeji.common.util.SpringContextUtil;
import java.time.LocalDateTime;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class DownloadMp4Executor {
private static ExecutorService exec = Executors.newFixedThreadPool(1);
private static DelayQueue<DownloadMp4Delayed> queue = new DelayQueue<>();
public static void addDelayTask(Integer cameraId, String path,LocalDateTime startTime, LocalDateTime endTime, Long delayTime) {
DownloadMp4Delayed downloadMp4Delayed = new DownloadMp4Delayed(cameraId, startTime, endTime,path, delayTime);
queue.add(downloadMp4Delayed);
}
public static void runMp4DownloadExecutor(){
exec.execute(new Consumer());
}
private static class Consumer implements Runnable {
@Override
public void run() {
while (true) {
try {
DownloadMp4Delayed downloadMp4Delayed = queue.take();
CameraControlModule cameraControlModule = SpringContextUtil.getBean(CameraControlModule.class);
cameraControlModule.downloadMp4(downloadMp4Delayed.getCameraId(), downloadMp4Delayed.getPath(),downloadMp4Delayed.getStartTime(),downloadMp4Delayed.getEndTime());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

@ -0,0 +1,54 @@
package com.zhehekeji.web.lib;
import com.zhehekeji.common.util.SpringContextUtil;
import java.time.LocalDateTime;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TaskDelayExecutor {
private static ExecutorService exec = Executors.newFixedThreadPool(1);
private static DelayQueue<CameraDelayTask> queue = new DelayQueue<>();
public static void addMp4DelayTask(Integer cameraId, String path, LocalDateTime startTime, LocalDateTime endTime, Long delayTime) {
CameraDelayTask cameraDelayTask = new CameraDelayTask(cameraId, startTime, endTime,path, 0,delayTime);
queue.add(cameraDelayTask);
}
public static void addPicDelayTask(Integer cameraId, String path, Long delayTime) {
CameraDelayTask cameraDelayTask = new CameraDelayTask(cameraId, null, null,path, 1,delayTime);
queue.add(cameraDelayTask);
}
public static void runMp4DownloadExecutor(){
exec.execute(new Consumer());
}
private static class Consumer implements Runnable {
@Override
public void run() {
while (true) {
try {
CameraControlModule cameraControlModule = SpringContextUtil.getBean(CameraControlModule.class);
CameraDelayTask cameraDelayTask = queue.take();
if(cameraDelayTask != null){
if(cameraDelayTask.getType() == 0){
cameraControlModule.downloadMp4(cameraDelayTask.getCameraId(), cameraDelayTask.getPath(), cameraDelayTask.getStartTime(), cameraDelayTask.getEndTime());
}else if(cameraDelayTask.getType() == 1){
cameraControlModule.pic(cameraDelayTask.getCameraId(),0, cameraDelayTask.getPath());
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

@ -105,7 +105,7 @@ public class InitService implements ApplicationRunner {
log.error("ksec no config");
}
}
DownloadMp4Executor.runMp4DownloadExecutor();
TaskDelayExecutor.runMp4DownloadExecutor();
}

@ -3,22 +3,18 @@ package com.zhehekeji.web.service;
import com.aijiao.test.JNIDemo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhehekeji.common.util.PathUtil;
import com.zhehekeji.core.util.Assert;
import com.zhehekeji.web.config.ConfigProperties;
import com.zhehekeji.web.entity.*;
import com.zhehekeji.web.lib.*;
import com.zhehekeji.web.mapper.*;
import com.zhehekeji.web.pojo.OrderVO;
import com.zhehekeji.web.pojo.stock.StockStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
/**
@ -141,11 +137,11 @@ public class PlcService {
update.setColumn2(plcCmdInfo.getColumn2());
update.setRow2(plcCmdInfo.getRow2());
if (street.getCamera1Id() != null) {
String path = cameraVideo(street.getCamera1Id(),order.getStartTime(),endTime.plusSeconds(2));
String path = cameraVideo(street.getCamera1Id(),order.getStartTime(),endTime.plusSeconds(5));
update.setVideoPath1(path);
}
if (street.getCamera2Id() != null) {
String path = cameraVideo(street.getCamera2Id(),order.getStartTime(),endTime.plusSeconds(2));
String path = cameraVideo(street.getCamera2Id(),order.getStartTime(),endTime.plusSeconds(5));
update.setVideoPath2(path);
}
orderMapper.updateById(update);
@ -169,7 +165,7 @@ public class PlcService {
public String cameraVideo(Integer cameraId, LocalDateTime startTime, LocalDateTime endTime) {
String path = PathUtil.createFileName("mp4",cameraId);
String realPath = configProperties.getSavePath().getMp4Path() + path;
DownloadMp4Executor.addDelayTask(cameraId,realPath,startTime,endTime,5000L);
TaskDelayExecutor.addMp4DelayTask(cameraId,realPath,startTime,endTime,7000L);
//cameraControlModule.downloadMp4(cameraId,realPath,startTime,endTime);
return path;
}
@ -205,14 +201,11 @@ public class PlcService {
Integer cameraId = getCameraByPlcCmd(plcCmdInfo,orderInfo.getLeftRight());
gyrateCameraByCode(cameraId,code);
if(needCapture){
Boolean delay = false;
if(code.startsWith("C1") || code.startsWith("C3")){
try {
Thread.sleep(1200);
} catch (InterruptedException e) {
e.printStackTrace();
delay = true;
}
}
path = cameraCapture(cameraId);
path = cameraCapture(cameraId,delay,2000L);
}
//update order info after capture
if (path != null && needCapture) {
@ -268,10 +261,14 @@ public class PlcService {
*
* @Return pic address
*/
public String cameraCapture(Integer cameraId) {
public String cameraCapture(Integer cameraId,Boolean delay,Long delayTime) {
String path = PathUtil.createFileName("jpg",cameraId);
String realPath = configProperties.getSavePath().getMediaPath() + path;
if(delay){
TaskDelayExecutor.addPicDelayTask(cameraId,realPath,delayTime);
}else {
cameraControlModule.pic(cameraId,0,realPath);
}
return path;
}
@ -444,13 +441,7 @@ public class PlcService {
Street street = streetService.getStreetByPlcId(plcCmdInfo.getPlcId());
Integer cameraId = getCameraByPlcCmd(plcCmdInfo,1);
gyrateCameraByCode(cameraId, cmdCode);
//暂停2500ms再抓图
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
String path = cameraCapture(cameraId);
String path = cameraCapture(cameraId,true,2500L);
OrderInfo orderInfo = new OrderInfo(street,plcCmdInfo,1,cmdCode);
Stock stock = stockMapper.getByShelveIdAndRowColumn(orderInfo.getShelveId(),orderInfo.getRow(),orderInfo.getColumn());

Loading…
Cancel
Save