From 8a7cd4332c9f0ff10e9b589d8f51aaa88de9d391 Mon Sep 17 00:00:00 2001 From: gongbaoxiong <751575283@qq.com> Date: Fri, 1 May 2026 17:00:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=A4=9A=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E9=94=81=E5=AE=9A?= =?UTF-8?q?=E5=90=8C=E6=8E=92=E7=AB=99=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/quartz/task/AutoCreateInst.java | 618 +++++++++++------- .../src/main/resources/config/application.yml | 2 +- .../wms/sch_manage/service/util/AutoTask.java | 129 ++-- .../system_manage/enums/SysParamConstant.java | 4 + .../service/impl/IWmsToWmsServiceImpl.java | 21 +- .../service/impl/WmsToIWmsServiceImpl.java | 2 +- .../resources/config/application-prod.yml | 5 +- .../src/main/resources/config/application.yml | 2 +- 8 files changed, 459 insertions(+), 324 deletions(-) diff --git a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java index a7043ce..3743497 100644 --- a/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java +++ b/acs/nladmin-system/nlsso-server/src/main/java/org/nl/system/service/quartz/task/AutoCreateInst.java @@ -3,16 +3,14 @@ package org.nl.system.service.quartz.task; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; +import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.ObjectUtils; -import org.nl.acs.common.base.CommonFinalParam; +import org.apache.commons.collections4.CollectionUtils; import org.nl.acs.instruction.domain.Instruction; import org.nl.acs.instruction.enums.InstructionStatusEnum; import org.nl.acs.instruction.service.InstructionService; import org.nl.acs.opc.DeviceAppService; import org.nl.acs.opc.DeviceAppServiceImpl; -import org.nl.acs.route.service.RouteLineService; -import org.nl.acs.route.service.dto.RouteLineDto; import org.nl.acs.task.enums.TaskStatusEnum; import org.nl.acs.task.enums.TaskTypeEnum; import org.nl.acs.task.service.TaskService; @@ -22,11 +20,15 @@ import org.nl.config.SpringContextHolder; import org.nl.system.service.lucene.LuceneExecuteLogService; import org.nl.system.service.lucene.dto.LuceneLogDto; import org.nl.system.service.lucene.impl.LuceneExecuteLogServiceImpl; +import org.redisson.api.RLock; +import org.redisson.api.RedissonClient; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.util.Arrays; -import java.util.Comparator; import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** @@ -34,252 +36,392 @@ import java.util.stream.Collectors; */ @Slf4j @Component +@RequiredArgsConstructor public class AutoCreateInst { + private final RedissonClient redissonClient; /** * 根据任务状态创建指令、生成下一条指令 * 创建指令前需要判断是否条件具备:起始位置是否有货、目标位置是否有货 */ - public void run() throws Exception { - log.info("自动生成指令" + DateUtil.now()); - TaskService taskserver = SpringContextHolder.getBean(TaskService.class); - InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); - RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class); - DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class); - LuceneExecuteLogService luceneExecuteLogService = SpringContextHolder.getBean(LuceneExecuteLogServiceImpl.class); - List list = taskserver.queryAllByStatus("0"); - for (int i = 0; i < list.size(); i++) { - TaskDto acsTask = list.get(i); - if (StrUtil.equals(acsTask.getTask_type(), TaskTypeEnum.AGV_Task.getIndex()) && !StrUtil.startWith(acsTask.getTask_code(), "-")) { - continue; - } - if (StrUtil.equals(acsTask.getTask_type(), TaskTypeEnum.Truss_Task.getIndex()) && !StrUtil.startWith(acsTask.getTask_code(), "-")) { - continue; - } - Boolean flag=false; - String taskid = acsTask.getTask_id(); - String taskcode = acsTask.getTask_code(); - String task_type = acsTask.getTask_type(); - String vehiclecode = acsTask.getVehicle_code(); - String priority = acsTask.getPriority(); - String is_send = acsTask.getIs_send(); - - String start_device_code = acsTask.getStart_device_code(); - String start_point=null; - if(start_device_code.contains("BCPRK")){ - String[] parts = start_device_code.split("-", 2); - start_point=parts[0]; - }else if(start_device_code.contains("CPRK")){ - String[] parts = start_device_code.split("-", 2); - start_point=parts[0]; - } - if(start_point != null) { - if (start_point.equals("CPRK1")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("CPRK1")) { - flag = true; - } - } - } else if (start_point.equals("CPRK2")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("CPRK2")) { - flag = true; - } - } - } else if (start_point.equals("CPRK3")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("CPRK3")) { - flag = true; - } - } - } else if (start_point.equals("CPRK4")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("CPRK4")) { - flag = true; - } - } - } else if (start_point.equals("CPRK5")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("CPRK5")) { - flag = true; - } - } - }else if (start_point.equals("BCPRK1")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("BCPRK1")) { - flag = true; - } - } - } else if (start_point.equals("BCPRK2")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("BCPRK2")) { - flag = true; - } - } - } else if (start_point.equals("BCPRK3")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("BCPRK3")) { - flag = true; - } - } - } else if (start_point.equals("BCPRK4")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("BCPRK4")) { - flag = true; - } - } - } else if (start_point.equals("BCPRK5")) { - List list1 = taskserver.queryAllByStatus("1"); - for (int j = 0; j < list1.size(); j++) { - String start_device_code1 = list1.get(j).getStart_device_code(); - String[] parts = start_device_code1.split("-", 2); - start_point = parts[0]; - if (start_point.equals("BCPRK5")) { - flag = true; - } - } - } - } - if (flag){ - continue; - } - String start_point_code = acsTask.getStart_point_code(); - - String put_device_code = acsTask.getPut_device_code(); - String put_point_code = acsTask.getPut_point_code(); - - String next_device_code = acsTask.getNext_device_code(); - String next_point_code = acsTask.getNext_point_code(); - - String route_plan_code = acsTask.getRoute_plan_code(); - String vehicleType = acsTask.getVehicle_type(); - String agv_system_type = acsTask.getAgv_system_type(); - - String start_height = acsTask.getStart_height(); - String next_height = acsTask.getNext_height(); - String car_type=acsTask.getCar_type(); - String car_width=acsTask.getCar_width(); - - - if (StrUtil.equals(is_send, "0")) { - continue; - } - -// //校验路由关系 -// List shortPathsList = routeLineService.getShortPathLines(start_device_code, next_device_code, route_plan_code); -// if (ObjectUtils.isEmpty(shortPathsList)) { -// acsTask.setRemark("路由不通无法生成指令"); -// taskserver.updateByCodeFromCache(acsTask); +// public void run() throws Exception { +// log.info("自动生成指令" + DateUtil.now()); +// TaskService taskserver = SpringContextHolder.getBean(TaskService.class); +// InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); +// RouteLineService routeLineService = SpringContextHolder.getBean(RouteLineService.class); +// DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class); +// LuceneExecuteLogService luceneExecuteLogService = SpringContextHolder.getBean(LuceneExecuteLogServiceImpl.class); +// List list = taskserver.queryAllByStatus("0"); +// for (int i = 0; i < list.size(); i++) { +// TaskDto acsTask = list.get(i); +// if (StrUtil.equals(acsTask.getTask_type(), TaskTypeEnum.AGV_Task.getIndex()) && !StrUtil.startWith(acsTask.getTask_code(), "-")) { +// continue; +// } +// if (StrUtil.equals(acsTask.getTask_type(), TaskTypeEnum.Truss_Task.getIndex()) && !StrUtil.startWith(acsTask.getTask_code(), "-")) { +// continue; +// } +// Boolean flag=false; +// String taskid = acsTask.getTask_id(); +// String taskcode = acsTask.getTask_code(); +// String task_type = acsTask.getTask_type(); +// String vehiclecode = acsTask.getVehicle_code(); +// String priority = acsTask.getPriority(); +// String is_send = acsTask.getIs_send(); +// +// String start_device_code = acsTask.getStart_device_code(); +// String start_point=null; +// if(start_device_code.contains("BCPRK")){ +// String[] parts = start_device_code.split("-", 2); +// start_point=parts[0]; +// }else if(start_device_code.contains("CPRK")){ +// String[] parts = start_device_code.split("-", 2); +// start_point=parts[0]; +// } +// if(start_point != null) { +// if (start_point.equals("CPRK1")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("CPRK1")) { +// flag = true; +// } +// } +// } else if (start_point.equals("CPRK2")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("CPRK2")) { +// flag = true; +// } +// } +// } else if (start_point.equals("CPRK3")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("CPRK3")) { +// flag = true; +// } +// } +// } else if (start_point.equals("CPRK4")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("CPRK4")) { +// flag = true; +// } +// } +// } else if (start_point.equals("CPRK5")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("CPRK5")) { +// flag = true; +// } +// } +// }else if (start_point.equals("BCPRK1")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("BCPRK1")) { +// flag = true; +// } +// } +// } else if (start_point.equals("BCPRK2")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("BCPRK2")) { +// flag = true; +// } +// } +// } else if (start_point.equals("BCPRK3")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("BCPRK3")) { +// flag = true; +// } +// } +// } else if (start_point.equals("BCPRK4")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("BCPRK4")) { +// flag = true; +// } +// } +// } else if (start_point.equals("BCPRK5")) { +// List list1 = taskserver.queryAllByStatus("1"); +// for (int j = 0; j < list1.size(); j++) { +// String start_device_code1 = list1.get(j).getStart_device_code(); +// String[] parts = start_device_code1.split("-", 2); +// start_point = parts[0]; +// if (start_point.equals("BCPRK5")) { +// flag = true; +// } +// } +// } +// } +// if (flag){ +// continue; +// } +// String start_point_code = acsTask.getStart_point_code(); +// +// String put_device_code = acsTask.getPut_device_code(); +// String put_point_code = acsTask.getPut_point_code(); +// +// String next_device_code = acsTask.getNext_device_code(); +// String next_point_code = acsTask.getNext_point_code(); +// +// String route_plan_code = acsTask.getRoute_plan_code(); +// String vehicleType = acsTask.getVehicle_type(); +// String agv_system_type = acsTask.getAgv_system_type(); +// +// String start_height = acsTask.getStart_height(); +// String next_height = acsTask.getNext_height(); +// String car_type=acsTask.getCar_type(); +// String car_width=acsTask.getCar_width(); +// +// +// if (StrUtil.equals(is_send, "0")) { // continue; // } // -// if (!StrUtil.equals(shortPathsList.get(0).getType(), CommonFinalParam.ONE)) { +//// //校验路由关系 +//// List shortPathsList = routeLineService.getShortPathLines(start_device_code, next_device_code, route_plan_code); +//// if (ObjectUtils.isEmpty(shortPathsList)) { +//// acsTask.setRemark("路由不通无法生成指令"); +//// taskserver.updateByCodeFromCache(acsTask); +//// continue; +//// } +//// +//// if (!StrUtil.equals(shortPathsList.get(0).getType(), CommonFinalParam.ONE)) { +//// continue; +//// } +//// RouteLineDto routeLineDto = shortPathsList.get(0); +//// String path = routeLineDto.getPath(); +//// String type = routeLineDto.getType(); +//// String[] str = path.split("->"); +//// List pathlist = Arrays.asList(str); +//// int index = 0; +//// for (int m = 0; m < pathlist.size(); m++) { +//// if (pathlist.get(m).equals(start_device_code)) { +//// index = m + 1; +//// break; +//// } +//// } +//// next_device_code = pathlist.get(index); +// +// if (StrUtil.equals(appService.findDeviceTypeByCode(next_device_code), "storage")) { +// next_point_code = next_device_code + "-" + acsTask.getTo_y() + "-" + acsTask.getTo_z(); +// } else { +// next_point_code = next_device_code; +// } +// +// Instruction instdto = new Instruction(); +// instdto.setInstruction_type(task_type); +// instdto.setInstruction_id(IdUtil.simpleUUID()); +// instdto.setRoute_plan_code(route_plan_code); +// instdto.setRemark(acsTask.getRemark()); +// instdto.setMaterial(acsTask.getMaterial()); +// instdto.setQuantity(acsTask.getQuantity()); +// instdto.setTask_id(taskid); +// instdto.setTask_code(taskcode); +// instdto.setVehicle_code(vehiclecode); +// String now = DateUtil.now(); +// instdto.setCreate_time(now); +// instdto.setCreate_by(SecurityUtils.getCurrentNickName()); +// +// instdto.setStart_device_code(start_point_code); +// instdto.setStart_point_code(start_point_code); +// instdto.setPut_device_code(put_device_code); +// instdto.setPut_point_code(put_point_code); +// instdto.setNext_device_code(next_device_code); +// instdto.setNext_point_code(next_point_code); +// instdto.setCar_type(car_type); +// instdto.setCar_width(car_width); +// instdto.setPriority(priority); +// instdto.setInstruction_status(InstructionStatusEnum.READY.getIndex()); +// instdto.setExecute_device_code(start_point_code); +// instdto.setVehicle_type(vehicleType); +// instdto.setAgv_system_type(agv_system_type); +// instdto.setStart_height(start_height); +// instdto.setNext_height(next_height); +// +// try { +// instructionService.create(instdto); +// } catch (Exception e) { +// acsTask.setRemark(e.getMessage()); +// taskserver.updateByCodeFromCache(acsTask); +// LuceneLogDto logDto = LuceneLogDto.builder() +// .device_code("定时创建指令失败") +// .content(e.getMessage()) +// .build(); +// logDto.setLog_level(2); +// luceneExecuteLogService.deviceExecuteLog(logDto); // continue; // } -// RouteLineDto routeLineDto = shortPathsList.get(0); -// String path = routeLineDto.getPath(); -// String type = routeLineDto.getType(); -// String[] str = path.split("->"); -// List pathlist = Arrays.asList(str); -// int index = 0; -// for (int m = 0; m < pathlist.size(); m++) { -// if (pathlist.get(m).equals(start_device_code)) { -// index = m + 1; -// break; -// } -// } -// next_device_code = pathlist.get(index); +// //创建指令后修改任务状态 +// acsTask.setTask_status(TaskStatusEnum.BUSY.getIndex()); +// acsTask.setUpdate_time(DateUtil.now()); +// taskserver.update(acsTask); +// +// } +// } - if (StrUtil.equals(appService.findDeviceTypeByCode(next_device_code), "storage")) { - next_point_code = next_device_code + "-" + acsTask.getTo_y() + "-" + acsTask.getTo_z(); - } else { - next_point_code = next_device_code; + /** + * 根据任务状态创建指令、生成下一条指令 + */ + public void run() throws Exception { + log.info("自动生成指令开始: {}", DateUtil.now()); + RLock lock = redissonClient.getLock("autoGenInstructionLock"); + boolean locked = false; + try { + locked = lock.tryLock(0, 5, TimeUnit.SECONDS); // 不等待,锁持有5秒自动释放 + if (!locked) { + log.debug("未获取到分布式锁,跳过本次执行"); + return; } - - Instruction instdto = new Instruction(); - instdto.setInstruction_type(task_type); - instdto.setInstruction_id(IdUtil.simpleUUID()); - instdto.setRoute_plan_code(route_plan_code); - instdto.setRemark(acsTask.getRemark()); - instdto.setMaterial(acsTask.getMaterial()); - instdto.setQuantity(acsTask.getQuantity()); - instdto.setTask_id(taskid); - instdto.setTask_code(taskcode); - instdto.setVehicle_code(vehiclecode); - String now = DateUtil.now(); - instdto.setCreate_time(now); - instdto.setCreate_by(SecurityUtils.getCurrentNickName()); - - instdto.setStart_device_code(start_point_code); - instdto.setStart_point_code(start_point_code); - instdto.setPut_device_code(put_device_code); - instdto.setPut_point_code(put_point_code); - instdto.setNext_device_code(next_device_code); - instdto.setNext_point_code(next_point_code); - instdto.setCar_type(car_type); - instdto.setCar_width(car_width); - instdto.setPriority(priority); - instdto.setInstruction_status(InstructionStatusEnum.READY.getIndex()); - instdto.setExecute_device_code(start_point_code); - instdto.setVehicle_type(vehicleType); - instdto.setAgv_system_type(agv_system_type); - instdto.setStart_height(start_height); - instdto.setNext_height(next_height); - - try { - instructionService.create(instdto); - } catch (Exception e) { - acsTask.setRemark(e.getMessage()); - taskserver.updateByCodeFromCache(acsTask); - LuceneLogDto logDto = LuceneLogDto.builder() - .device_code("定时创建指令失败") - .content(e.getMessage()) - .build(); - logDto.setLog_level(2); - luceneExecuteLogService.deviceExecuteLog(logDto); - continue; + TaskService taskserver = SpringContextHolder.getBean(TaskService.class); + InstructionService instructionService = SpringContextHolder.getBean(InstructionService.class); + DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class); + LuceneExecuteLogService luceneExecuteLogService = SpringContextHolder.getBean(LuceneExecuteLogServiceImpl.class); + //按优先级降序task_code升序 + List list = taskserver.queryAllByStatus("0"); + if (CollectionUtils.isEmpty(list)) { + return; + } + Set busyStartPoints = taskserver.queryAllByStatus("1").stream() + .map(t -> extractStartPoint(t.getStart_device_code())) + .filter(Objects::nonNull) + .collect(Collectors.toSet()); + for (TaskDto acsTask : list) { + if (isNonBlockingTaskType(acsTask)) { + continue; // AGV/Truss且不以"-"开头,跳过,继续看下一个 + } + if (StrUtil.equals(acsTask.getIs_send(), "0")) { + break; + } + //起始点冲突(同一起点已有执行中任务) + String startPoint = extractStartPoint(acsTask.getStart_device_code()); + if (startPoint != null && busyStartPoints.contains(startPoint)) { + break; + } + try { + createInstruction(acsTask, appService, instructionService, taskserver); + } catch (Exception e) { + acsTask.setRemark(e.getMessage()); + taskserver.updateByCodeFromCache(acsTask); + LuceneLogDto logDto = LuceneLogDto.builder() + .device_code("定时创建指令失败") + .content(e.getMessage()) + .build(); + logDto.setLog_level(2); + luceneExecuteLogService.deviceExecuteLog(logDto); + break; + } + break; + } + } catch (Exception e) { + log.error("自动生成指令异常", e); + } finally { + if (locked && lock.isHeldByCurrentThread()) { + try { + lock.unlock(); + } catch (Exception e) { + log.error("释放分布式锁异常", e); + } } - //创建指令后修改任务状态 - acsTask.setTask_status(TaskStatusEnum.BUSY.getIndex()); - acsTask.setUpdate_time(DateUtil.now()); - taskserver.update(acsTask); - } } + + /** + * 判断是否属于无需生成指令的任务类型 + */ + private boolean isNonBlockingTaskType(TaskDto acsTask) { + String taskType = acsTask.getTask_type(); + String taskCode = acsTask.getTask_code(); + boolean isAgv = StrUtil.equals(taskType, TaskTypeEnum.AGV_Task.getIndex()) + && !StrUtil.startWith(taskCode, "-"); + boolean isTruss = StrUtil.equals(taskType, TaskTypeEnum.Truss_Task.getIndex()) + && !StrUtil.startWith(taskCode, "-"); + return isAgv || isTruss; + } + + /** + * 从设备编码中提取起点区域CPRK1, BCPRK2 + */ + private String extractStartPoint(String startDeviceCode) { + if (startDeviceCode == null) { + return null; + } + if (startDeviceCode.contains("BCPRK") || startDeviceCode.contains("CPRK")) { + return startDeviceCode.split("-")[0]; + } + return null; + } + + /** + * 封装指令创建与任务状态更新 + */ + private void createInstruction(TaskDto acsTask, DeviceAppService appService, + InstructionService instructionService, TaskService taskserver) throws Exception { + String next_device_code = acsTask.getNext_device_code(); + String next_point_code; + if (StrUtil.equals(appService.findDeviceTypeByCode(next_device_code), "storage")) { + next_point_code = next_device_code + "-" + acsTask.getTo_y() + "-" + acsTask.getTo_z(); + } else { + next_point_code = next_device_code; + } + Instruction instdto = new Instruction(); + instdto.setInstruction_type(acsTask.getTask_type()); + instdto.setInstruction_id(IdUtil.simpleUUID()); + instdto.setRoute_plan_code(acsTask.getRoute_plan_code()); + instdto.setRemark(acsTask.getRemark()); + instdto.setMaterial(acsTask.getMaterial()); + instdto.setQuantity(acsTask.getQuantity()); + instdto.setTask_id(acsTask.getTask_id()); + instdto.setTask_code(acsTask.getTask_code()); + instdto.setVehicle_code(acsTask.getVehicle_code()); + instdto.setCreate_time(DateUtil.now()); + instdto.setCreate_by(SecurityUtils.getCurrentNickName()); + instdto.setStart_device_code(acsTask.getStart_point_code()); + instdto.setStart_point_code(acsTask.getStart_point_code()); + instdto.setPut_device_code(acsTask.getPut_device_code()); + instdto.setPut_point_code(acsTask.getPut_point_code()); + instdto.setNext_device_code(next_device_code); + instdto.setNext_point_code(next_point_code); + instdto.setCar_type(acsTask.getCar_type()); + instdto.setCar_width(acsTask.getCar_width()); + instdto.setPriority(acsTask.getPriority()); + instdto.setInstruction_status(InstructionStatusEnum.READY.getIndex()); + instdto.setExecute_device_code(acsTask.getStart_point_code()); + instdto.setVehicle_type(acsTask.getVehicle_type()); + instdto.setAgv_system_type(acsTask.getAgv_system_type()); + instdto.setStart_height(acsTask.getStart_height()); + instdto.setNext_height(acsTask.getNext_height()); + instructionService.create(instdto); + // 更新任务状态为执行中 + acsTask.setTask_status(TaskStatusEnum.BUSY.getIndex()); + acsTask.setUpdate_time(DateUtil.now()); + taskserver.update(acsTask); + } } diff --git a/acs/nladmin-system/nlsso-server/src/main/resources/config/application.yml b/acs/nladmin-system/nlsso-server/src/main/resources/config/application.yml index 5828d14..4dff837 100644 --- a/acs/nladmin-system/nlsso-server/src/main/resources/config/application.yml +++ b/acs/nladmin-system/nlsso-server/src/main/resources/config/application.yml @@ -6,7 +6,7 @@ spring: freemarker: check-template-location: false profiles: - active: dev + active: prod # active: prod jackson: time-zone: GMT+8 diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch_manage/service/util/AutoTask.java b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch_manage/service/util/AutoTask.java index 7b898dd..0406bc3 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch_manage/service/util/AutoTask.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/sch_manage/service/util/AutoTask.java @@ -1,11 +1,8 @@ package org.nl.wms.sch_manage.service.util; - -import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.ObjectUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.nl.wms.sch_manage.enums.TaskStatus; @@ -17,7 +14,8 @@ import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.sql.Time; +import javax.annotation.PostConstruct; +import javax.annotation.PreDestroy; import java.util.Comparator; import java.util.List; import java.util.Map; @@ -30,83 +28,68 @@ import java.util.stream.Collectors; @Component @RequiredArgsConstructor public class AutoTask { - private static final ScheduledExecutorService DELAY_EXECUTOR = Executors.newSingleThreadScheduledExecutor(); + /** - * 任务服务 + * 延迟任务调度线程池,从单线程改为适中并发,避免任务堆积 */ + private ScheduledExecutorService delayExecutor; + @Autowired private ISchBaseTaskService taskService; - /** - * 任务工厂服务 - */ @Autowired private TaskFactory taskFactory; private final RedissonClient redissonClient; - //定时任务 - @SneakyThrows - public void run() { - RLock lock = redissonClient.getLock(this.getClass().getName()); - boolean tryLock = lock.tryLock(0, TimeUnit.SECONDS); - try { - if (tryLock) { - sendTask(); - } - } catch (Exception e) { - if (tryLock) { - lock.unlock(); + + @PostConstruct + public void init() { + //无界线程池,线程数 + this.delayExecutor = Executors.newScheduledThreadPool(4); + } + + @PreDestroy + public void destroy() { + if (delayExecutor != null && !delayExecutor.isShutdown()) { + delayExecutor.shutdown(); + try { + if (!delayExecutor.awaitTermination(5, TimeUnit.SECONDS)) { + delayExecutor.shutdownNow(); + } + } catch (InterruptedException e) { + delayExecutor.shutdownNow(); + Thread.currentThread().interrupt(); } } } /** - * 定时下发任务 + * 定时任务 */ -// private void sendTask() { -// List taskList = taskService.list(new LambdaQueryWrapper() -// .eq(SchBaseTask::getIs_delete, IOSConstant.IS_DELETE_NO) -// .eq(SchBaseTask::getTask_status, TaskStatus.CREATE.getCode())); -// if (ObjectUtil.isEmpty(taskList)) { -// return; -// } -// // 找出需要间隔的任务列表 -// List delayTaskList = taskList.stream() -// .filter(r -> "STInTask".equals(r.getConfig_code())) -// .collect(Collectors.toList()); -// List immediatelyTaskList = new ArrayList<>(taskList); -// immediatelyTaskList.removeAll(delayTaskList); -// //优先发送正常的任务列表 -// if (ObjectUtil.isNotEmpty(immediatelyTaskList)) { -// // 整理下发acs参数 -// for (SchBaseTask taskDao : immediatelyTaskList) { -// //任务优先级 -// taskDao.setPriority(StringUtils.isBlank(taskDao.getPriority()) ? "1" : taskDao.getPriority()); -// AbstractTask task = taskFactory.getTask(taskDao.getConfig_code()); -// task.sendTaskOne(taskDao.getTask_id()); -// } -// } -// //间隔3秒发一个任务 -// if (ObjectUtil.isNotEmpty(delayTaskList)) { -// DELAY_EXECUTOR.execute(() -> { -// try { -// for (SchBaseTask taskDao : delayTaskList) { -// //间隔3秒发一个任务 -// Thread.sleep(2000); -// //任务优先级 -// taskDao.setPriority(StringUtils.isBlank(taskDao.getPriority()) ? "1" : taskDao.getPriority()); -// AbstractTask task = taskFactory.getTask(taskDao.getConfig_code()); -// task.sendTaskOne(taskDao.getTask_id()); -// } -// } catch (InterruptedException e) { -// Thread.currentThread().interrupt(); -// log.error("延迟任务被中断", e); -// } catch (Exception e) { -// log.error("延迟下发任务执行失败", e); -// } -// }); -// } -// } + public void run() { + RLock lock = redissonClient.getLock(this.getClass().getName()); + boolean locked = false; + try { + //最多等待 1 秒,锁自动释放时间 30 秒 + locked = lock.tryLock(2, 30, TimeUnit.SECONDS); + if (locked) { + sendTask(); + } else { + log.debug("未获取到分布式锁,跳过本次执行"); + } + } catch (Exception e) { + log.error("获取分布式锁或执行任务异常,可能 Redis 不可用", e); + } finally { + // 确保释放锁,且只能释放自己持有的锁 + if (locked && lock.isHeldByCurrentThread()) { + try { + lock.unlock(); + } catch (Exception e) { + log.error("释放分布式锁失败", e); + } + } + } + } /** * 定时下发任务 @@ -119,6 +102,7 @@ public class AutoTask { if (ObjectUtil.isEmpty(taskList)) { return; } + // 2. 统一设置默认优先级并分组 Map> partitioned = taskList.stream() .peek(task -> { @@ -129,38 +113,35 @@ public class AutoTask { .collect(Collectors.partitioningBy(r -> "STInTask".equals(r.getConfig_code()))); List immediatelyTaskList = partitioned.get(false); List delayTaskList = partitioned.get(true); + // 3. 立即下发任务 if (ObjectUtil.isNotEmpty(immediatelyTaskList)) { - //log.info("开始下发立即任务, 数量: {}", immediatelyTaskList.size()); for (SchBaseTask task : immediatelyTaskList) { try { AbstractTask taskExecutor = taskFactory.getTask(task.getConfig_code()); taskExecutor.sendTaskOne(task.getTask_id()); - // log.info("任务下发成功: taskId={}, configCode={}", task.getTask_id(), task.getConfig_code()); } catch (Exception e) { log.error("任务下发失败: taskId={}, configCode={}", task.getTask_id(), task.getConfig_code(), e); } } - //log.info("立即任务下发完成"); } + // 4. 延迟下发任务(间隔 7 秒,异步执行) if (ObjectUtil.isNotEmpty(delayTaskList)) { delayTaskList.sort(Comparator.comparing(SchBaseTask::getTask_code)); - //log.info("开始调度延迟任务, 数量: {}, 间隔: 7秒", delayTaskList.size()); long delaySeconds = 0; for (SchBaseTask task : delayTaskList) { - DELAY_EXECUTOR.schedule(() -> { + delayExecutor.schedule(() -> { try { AbstractTask taskExecutor = taskFactory.getTask(task.getConfig_code()); taskExecutor.sendTaskOne(task.getTask_id()); - log.info("延迟任务下发成功: taskCode={}, configCode={}", task.getTask_code(), task.getConfig_code()); + //log.info("延迟任务下发成功: taskCode={}, configCode={}", task.getTask_code(), task.getConfig_code()); } catch (Exception e) { log.error("延迟任务下发失败: taskId={}, configCode={}", task.getTask_id(), task.getConfig_code(), e); } }, delaySeconds, TimeUnit.SECONDS); delaySeconds += 7; } - //log.info("延迟任务已全部提交调度"); } } -} +} \ No newline at end of file diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/system_manage/enums/SysParamConstant.java b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/system_manage/enums/SysParamConstant.java index 6e6ab33..29b3e60 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/system_manage/enums/SysParamConstant.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/system_manage/enums/SysParamConstant.java @@ -7,6 +7,10 @@ package org.nl.wms.system_manage.enums; */ public class SysParamConstant { + /** + * 是否反馈IWMS + */ + public final static String IS_CONNECT_IWMS = "is_connect_iwms"; /** * 是否连接ACS */ diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/IWmsToWmsServiceImpl.java b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/IWmsToWmsServiceImpl.java index 7109c7c..e7632c4 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/IWmsToWmsServiceImpl.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/IWmsToWmsServiceImpl.java @@ -1,11 +1,9 @@ package org.nl.wms.wbwms.service.impl; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.http.HttpRequest; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; import lombok.extern.slf4j.Slf4j; import org.nl.common.exception.BadRequestException; import org.nl.config.SpringContextHolder; @@ -14,14 +12,12 @@ import org.nl.wms.basedata_manage.service.dao.mapper.MdMeMaterialbaseMapper; import org.nl.wms.system_manage.enums.SysParamConstant; import org.nl.wms.system_manage.service.param.dao.Param; import org.nl.wms.system_manage.service.param.impl.SysParamServiceImpl; +import org.nl.wms.warehouse_manage.enums.IOSConstant; import org.nl.wms.warehouse_manage.inAndOut.service.dto.TOWMSMSG; import org.nl.wms.warehouse_manage.service.dto.CheckToWmsMsg; import org.nl.wms.warehouse_manage.service.dto.MoveToWmsMsg; import org.nl.wms.wbwms.enums.WMSConstant; import org.nl.wms.wbwms.service.IWmsToWmsService; -import org.nl.wms.wbwms.service.dto.IWmstoWmsResponse; -import org.nl.wms.wbwms.service.dto.WmstoIWmsResponse; -import org.slf4j.MDC; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Service; @@ -75,11 +71,20 @@ public class IWmsToWmsServiceImpl implements IWmsToWmsService { @Override public JSONObject FinishOutTask(TOWMSMSG towmsmsg) { - JSONObject result = new JSONObject(); + // 系统参数类 + SysParamServiceImpl sysParamService = SpringContextHolder.getBean(SysParamServiceImpl.class); + //判断是否反馈IWMS + Param isConnectAcs = sysParamService.findByCode(SysParamConstant.IS_CONNECT_IWMS); + if (ObjectUtil.isNotEmpty(isConnectAcs)) { + if (isConnectAcs.getValue().equals(IOSConstant.IS_DELETE_NO)) { + return null; + } + } + JSONObject result; // 系统参数类 JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(towmsmsg), JSONObject.class); log.info("FinishOutTask请求参数是:-------------------" + jsonObject.toString()); - String wmsUrl = SpringContextHolder.getBean(SysParamServiceImpl.class).findByCode(SysParamConstant.WMS_URL).getValue(); + String wmsUrl = sysParamService.findByCode(SysParamConstant.WMS_URL).getValue(); wmsUrl = wmsUrl + WMSConstant.INOUT_WMS_API; try { String resultMsg = HttpRequest.post(wmsUrl) diff --git a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/WmsToIWmsServiceImpl.java b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/WmsToIWmsServiceImpl.java index 3f0fcef..834e7c0 100644 --- a/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/WmsToIWmsServiceImpl.java +++ b/nladmin-system/nlsso-server/src/main/java/org/nl/wms/wbwms/service/impl/WmsToIWmsServiceImpl.java @@ -351,7 +351,7 @@ public class WmsToIWmsServiceImpl implements WmsToIWmsService { for (SchBasePoint point : list) { JSONObject pointData = new JSONObject(); pointData.put("point_code", point.getPoint_code()); - pointData.put("status", point.getPoint_status().equals(IOSEnum.POINT_STATUS.code("空位")) ? "0" : "1"); + pointData.put("status", point.getPoint_status().equals(PointStatusEnum.EMPTY_POINT.getCode()) && point.getLock_up()? "0" : "1"); dataList.add(pointData); } } diff --git a/nladmin-system/nlsso-server/src/main/resources/config/application-prod.yml b/nladmin-system/nlsso-server/src/main/resources/config/application-prod.yml index cc50aac..f2e875c 100644 --- a/nladmin-system/nlsso-server/src/main/resources/config/application-prod.yml +++ b/nladmin-system/nlsso-server/src/main/resources/config/application-prod.yml @@ -70,8 +70,11 @@ spring: connectionMinimumIdleSize: 8 connectionPoolSize: 8 address: redis://127.0.0.1:6379 - idleConnectionTimeout: 10000 + idleConnectionTimeout: 30000 timeout: 3000 + pingConnectionInterval: 30000 + retryAttempts: 3 + retryInterval: 1500 # 登录相关配置 login: diff --git a/nladmin-system/nlsso-server/src/main/resources/config/application.yml b/nladmin-system/nlsso-server/src/main/resources/config/application.yml index 9384616..c682a12 100644 --- a/nladmin-system/nlsso-server/src/main/resources/config/application.yml +++ b/nladmin-system/nlsso-server/src/main/resources/config/application.yml @@ -11,7 +11,7 @@ spring: freemarker: check-template-location: false profiles: - active: dev + active: prod jackson: time-zone: GMT+8730 885 969 data: