执行任务的流程逻辑 变更
devhuozheluoji
周文涛 2 years ago
parent 132976ba13
commit b88eafe8ab

@ -133,7 +133,7 @@ public class ApiController {
}
Thread.sleep(1000L);
//3.请求python ocr识别异步执行
taskService.postSemantic(ocrIdentify,fileList);
//taskService.postSemantic(ocrIdentify,fileList);
//ocrIdentifyService.postSemantic(ocrIdentify, fileList);
return Result.OK("请求成功");
}

@ -182,7 +182,7 @@ public class OcrIdentifyController extends JeecgController<OcrIdentify, IOcrIden
ocrIdentify.setStatus("0");
ocrIdentifyService.save(ocrIdentify);
//3.请求python ocr识别异步执行
taskService.postSemantic(ocrIdentify, fileList);
//taskService.postSemantic(ocrIdentify, fileList);
return Result.OK("添加成功!");
}

@ -0,0 +1,146 @@
package org.jeecg.modules.ocr.init;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.constant.OcrConstant;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.AssertUtils;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.ocr.dto.OcrIdentifyDTO;
import org.jeecg.modules.ocr.entity.OcrIdentify;
import org.jeecg.modules.ocr.service.IOcrIdentifyService;
import org.jeecg.modules.ocr.service.impl.TaskService;
import org.jeecg.modules.ocr.utils.FileOUtils;
import org.jeecg.modules.system.entity.SysLog;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysLogService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @Description
* @Author ZhouWenTao
* @Date 2023/9/20 17:53
*/
@Configuration
@Slf4j
public class HandleTaskInit implements ApplicationRunner {
@Resource
IOcrIdentifyService ocrIdentifyService;
@Resource
TaskService taskService;
@Resource
private ISysDictService sysDictService;
@Resource
private ISysLogService sysLogService;
@Value("${system.project.enableHandleTask}")
private boolean enableHandleTask;
@Override
public void run(ApplicationArguments args) throws Exception {
if(enableHandleTask){
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
while (true) {
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("---------检测是否有任务...");
//获取任务
List<OcrIdentify> list= ocrIdentifyService.getSemanticTaskList();
if (CollectionUtils.isEmpty(list)) {
continue;
}
//获取相对路径
List<DictModel> ocr_relative_path = sysDictService.queryDictItemsByCode("ocr_relative_path");
String relativePath = null;
if (ocr_relative_path != null && ocr_relative_path.size() > 0) {
for (DictModel dictModel : ocr_relative_path) {
if (dictModel.getValue().equals("0")) {
relativePath = dictModel.getText();
}
}
}
//获取识别的图片
List<String> fileList=null;
//临时变量
List<String> fileUrlList;
String image;//图片
//执行获取到的任务
identifyFor: for (OcrIdentify ocrIdentify : list) {
image=null;
fileList=new ArrayList<>();
//识别的图片路径
String imageUrl = ocrIdentify.getIdentifyUrl();
//判断是不是网络图片
Boolean onlineFile = FileOUtils.isOnlineFile(ocrIdentify.getIdentifyUrl());
//最终要请求ocr识别的图片对象
JSONObject requestBody = new JSONObject();
if (onlineFile) {
//如果是网络图片,则将图片下载
image = FileOUtils.downLoadFromUrl(imageUrl, FileOUtils.getFileName(imageUrl), OcrConstant.FILE_DOWNLOAD_URL_PREFIX);
fileList.add(image);
}else{
//路径下识别到的图片集合
fileUrlList = FileOUtils.fileLists(relativePath, ocrIdentify.getIdentifyUrl());
AssertUtils.notNull(fileUrlList, "图片地址不存在");
for (String fileUrl : fileUrlList) {
//判断附件是否是 图片格式
if (fileUrl.lastIndexOf(".png") != -1 || fileUrl.lastIndexOf(".jpg") != -1 || fileUrl.lastIndexOf(".jpeg") != -1) {
fileList.add(fileUrl);
}
}
}
//最终要识别哪些图片
int i=0;
for (String img : fileList) {
i++;
requestBody.put("task_id", ocrIdentify.getId()+"_"+i);
requestBody.put("img_path", image);
log.info("----------------------请求参数");
log.info(requestBody.toJSONString());
try {
log.info("----------------------请求参数");
log.info(requestBody.toJSONString());
JSONObject semanticResponseJson = RestUtil.post(OcrConstant.api_test2_identify_url, requestBody);
semanticResponseJson.put("identifyId", ocrIdentify.getId());
log.info("ocr识别返回数据:");
log.info(semanticResponseJson.toJSONString());
ocrIdentifyService.getSemanticInfo(semanticResponseJson);
}catch (Exception e){
log.error("识别图片失败:");
SysLog sysLog=new SysLog();
sysLog.setLogType(2);
sysLog.setLogContent(img+"_识别图片失败:"+e.getMessage());
sysLog.setOperateType(2);
sysLogService.save(sysLog);
log.error(e.getMessage());
log.error("正在重试...");
continue identifyFor;
}
}
//更改任务状态
ocrIdentifyService.updateOcrIdentifyStatus(ocrIdentify.getId(), "1");
}
}
}
}
}

@ -48,8 +48,9 @@ public class HandleTransInit implements ApplicationRunner {
@Override
@Async
@Deprecated
public void run(ApplicationArguments args) throws UnknownHostException {
if (enableHandleTask) {
if (false) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {

@ -62,4 +62,10 @@ public interface IOcrIdentifyService extends IService<OcrIdentify> {
* @param simulateChecksVO
*/
JSONObject simulateChecks(SimulateChecksVO simulateChecksVO);
/**
*
* @return
*/
List<OcrIdentify> getSemanticTaskList();
}

@ -10,6 +10,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.OcrConstant;
import org.jeecg.common.util.AssertUtils;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.ocr.dto.OcrIdentifyDTO;
@ -87,13 +88,8 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
log.debug("打印 ocr 结果:" + responseBody.toString());
String identifyId = responseBody.getString("identifyId");//任务id
String imgPath = responseBody.getString("img_path");//图片路径
String imgName = null;//图片名称
if (StringUtils.isNotBlank(imgPath)) {
imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.length());
} else {
log.error("异常,识别图片地址为空");
return;
}
AssertUtils.notEmpty(imgPath,"异常,识别图片地址为空");
String imgName = imgPath.substring(imgPath.lastIndexOf("/") + 1, imgPath.length());//图片名称
String message = responseBody.getString("message");//描述
String taskStatus = responseBody.getString("taskStatus");//任务是否完成
Double ocrTime = responseBody.getDouble("ocr_time");//OCR识别时长
@ -355,7 +351,6 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
@Async
public void updateOcrIdentifyStatus(String id, String status) {
//4.更新主任务状态
OcrIdentifyDTO ocrIdentifyDTO = this.findById(id);
LambdaUpdateWrapper<OcrIdentify> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(OcrIdentify::getId, id);
List<OcrIdentifyDetail> identifyDetailList = ocrIdentifyDetailService.listByIdentifyId(id);
@ -1026,6 +1021,15 @@ public class OcrIdentifyServiceImpl extends ServiceImpl<OcrIdentifyMapper, OcrId
return responseBody;
}
@Override
public List<OcrIdentify> getSemanticTaskList() {
LambdaQueryWrapper<OcrIdentify> queryWrapper=new LambdaQueryWrapper<>();
queryWrapper.in(OcrIdentify::getStatus,0,2);
queryWrapper.orderByDesc(OcrIdentify::getPriority);//优先 加急 1>0 不加急
queryWrapper.ne(OcrIdentify::getTaskSource,"模拟实验");//不获取模拟实验室的数据
return this.list(queryWrapper);
}
// 自定义的脱敏方法,对字段进行半脱敏
private String desensitizeText(String text) {
StringBuilder sb = new StringBuilder(text);

@ -100,7 +100,7 @@ public class TaskService {
}
//获取全部任务
public List<TaskModel> getTaskList() {
public List<TaskModel> getTaskList(String ss) {
String task_0 = (String) redisUtil.get("task_identify_0");
String task_1 = (String) redisUtil.get("task_identify_1");
/*String task_2 = (String) redisUtil.get("task_2");

Loading…
Cancel
Save