| | |
| | | import com.zy.acs.manager.core.service.astart.MapDataDispatcher; |
| | | import com.zy.acs.manager.core.service.astart.RetreatNavigateNode; |
| | | import com.zy.acs.manager.core.service.astart.WaveNodeType; |
| | | import com.zy.acs.manager.core.service.astart.domain.DynamicNode; |
| | | import com.zy.acs.manager.manager.entity.*; |
| | | import com.zy.acs.manager.manager.enums.JamStateType; |
| | | import com.zy.acs.manager.manager.enums.SegmentStateType; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.transaction.interceptor.TransactionAspectSupport; |
| | | |
| | | import java.util.*; |
| | | import java.util.ArrayList; |
| | | import java.util.Arrays; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | |
| | | public static final Integer MIN_SLICE_PATH_LENGTH = 3; |
| | | |
| | | public static final Integer timeoutDuration = 5 * 1000; |
| | | public static final Integer MAX_JAM_TIMEOUT = 5 * 1000; |
| | | |
| | | private final RedisSupport redis = RedisSupport.defaultRedisSupport; |
| | | |
| | |
| | | @Autowired |
| | | private AvoidWaveCalculator avoidWaveCalculator; |
| | | |
| | | |
| | | // todo |
| | | // 1.故障 |
| | | @Transactional |
| | | public synchronized void trigger(Segment segment) { |
| | | try { |
| | |
| | | if (!Cools.isEmpty(pathList)) { |
| | | |
| | | if (!pathList.get(pathList.size() - 1).equals(endCode.getData())) { |
| | | |
| | | assert !Cools.isEmpty(blockVehicleList); |
| | | if (blockVehicleList.stream().anyMatch(blockVehicleDto -> !blockVehicleDto.isAvoidable()) |
| | | && pathList.size() <= MIN_SLICE_PATH_LENGTH) { |
| | | |
| | | boolean hasUnavoidableBlocks = blockVehicleList.stream().anyMatch(blockVehicleDto -> !blockVehicleDto.isAvoidable()); |
| | | if (hasUnavoidableBlocks && pathList.size() <= MIN_SLICE_PATH_LENGTH) { |
| | | log.info("AGV[{}] waiting in place, because the path list is too short...", agvNo); |
| | | pathList.clear(); |
| | | } |
| | | } |
| | |
| | | // 无可走行路径 |
| | | } else { |
| | | |
| | | // 阻塞车辆列表 |
| | | assert !Cools.isEmpty(blockVehicleList); |
| | | Integer maxJamTimeoutFactor = null; |
| | | |
| | | String blockAgvNo = blockVehicleList.stream() |
| | | .filter(BlockVehicleDto::isAvoidable) |
| | | .map(BlockVehicleDto::getVehicle) |
| | | .findFirst().orElse(null); |
| | | // 如果全是运行中的阻塞agv,则不进行避让逻辑 todo |
| | | if (Cools.isEmpty(blockAgvNo)) { |
| | | return pathList; |
| | | } |
| | | // persist jam data |
| | | jam = this.createOrUpdateJam(agv, startCode, segment, jam); |
| | | |
| | | if (null == jam) { |
| | | jam = new Jam(); |
| | | jam.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); |
| | | jam.setJamAgv(agv.getId()); |
| | | jam.setJamCode(startCode.getId()); |
| | | jam.setJamSeg(segment.getId()); |
| | | jam.setStartTime(now); |
| | | jam.setState(JamStateType.RUNNING.toString()); |
| | | if (!jamService.save(jam)) { |
| | | log.error("{}号车辆在{}定位被阻塞,记录阻塞状态失败!!!", agvNo, startCode.getData()); |
| | | return pathList; |
| | | } |
| | | // ? has unAvoidable block vehicles |
| | | if (blockVehicleList.stream().anyMatch(blockVehicleDto -> !blockVehicleDto.isAvoidable())) { |
| | | |
| | | // set factor of jam timeout |
| | | maxJamTimeoutFactor = 1; |
| | | } else { |
| | | jam.setDuration(System.currentTimeMillis() - jam.getStartTime().getTime()); |
| | | if (!jamService.updateById(jam)) { |
| | | log.error("{}编号阻塞记录更新失败!!!", jam.getUuid()); |
| | | |
| | | // ? already do notify to avoid |
| | | if (!Cools.isEmpty(jam.getAvoAgv()) |
| | | && BlockVehicleDto.customContain(blockVehicleList, agvService.getById(jam.getAvoAgv()).getUuid())) { |
| | | |
| | | maxJamTimeoutFactor = 10; |
| | | |
| | | } else { |
| | | |
| | | // select optimal block vehicle |
| | | String blockAgvNo = blockVehicleList.stream() |
| | | .filter(BlockVehicleDto::isAvoidable) |
| | | .map(BlockVehicleDto::getVehicle) |
| | | .findFirst().orElse(null); |
| | | |
| | | // block vehicle info |
| | | Agv blockAgv = agvService.selectByUuid(blockAgvNo); |
| | | String blockAgvCode = codeService.getById(agvDetailService.selectByAgvId(blockAgv.getId()).getRecentCode()).getData(); |
| | | |
| | | do { |
| | | |
| | | // 阻塞车辆正在原地作业,等待 ===>> 超过等待时间,绕路 |
| | | List<Segment> runningSegList = segmentService.getByAgvAndState(blockAgv.getId(), SegmentStateType.RUNNING.toString()); |
| | | if (!Cools.isEmpty(runningSegList)) { |
| | | maxJamTimeoutFactor = 1; |
| | | break; |
| | | } |
| | | |
| | | // 判断下个任务是否为原地任务,如果是则等待 ===>> 超过等待时间,绕路;如果不是,让阻塞车辆避让 |
| | | Segment waitingSeg = segmentService.getJustWaitingSeg(blockAgv.getId()); |
| | | if (null != waitingSeg && waitingSeg.getEndNode().equals(codeService.selectByData(blockAgvCode).getId())) { |
| | | maxJamTimeoutFactor = 1; |
| | | break; |
| | | } |
| | | |
| | | // notify block vehicle to avoid |
| | | if (this.notifyVehicleAvoid(blockAgvNo, blockAgvCode, unlockPathList, agvNo, jam)) { |
| | | if (jam.getCycleAvo() == 1) { |
| | | jam.setCycleCode(endCode.getId()); |
| | | } |
| | | jam.setAvoAgv(blockAgv.getId()); |
| | | jam.setNotifyTime(new Date()); |
| | | if (!jamService.updateById(jam)) { |
| | | throw new CoolException(jam.getUuid() + "-jam failed to update!!!"); |
| | | } |
| | | } else { |
| | | |
| | | maxJamTimeoutFactor = 1; |
| | | } |
| | | |
| | | } while (false); |
| | | } |
| | | |
| | | } |
| | | long previousTimestamp = jam.getStartTime().getTime(); |
| | | |
| | | // jam vehicle info |
| | | Agv blockAgv = agvService.selectByUuid(blockAgvNo); |
| | | String blockAgvCode = codeService.getById(agvDetailService.selectByAgvId(blockAgv.getId()).getRecentCode()).getData(); |
| | | |
| | | // jam vehicle dynamic on map matrix |
| | | List<String> blockDynamicList = mapDataDispatcher.queryCodeListFromDynamicNode(lev, blockAgvNo); |
| | | |
| | | // 路径阻塞 |
| | | if (blockDynamicList.size() > 1) { |
| | | if (System.currentTimeMillis() - previousTimestamp > timeoutDuration) { |
| | | // handle jam timeout |
| | | if (null != maxJamTimeoutFactor) { |
| | | if (System.currentTimeMillis() - jam.getStartTime().getTime() > MAX_JAM_TIMEOUT * maxJamTimeoutFactor) { |
| | | |
| | | if (!Cools.isEmpty(lockPathList)) { |
| | | |
| | |
| | | , agvNo, startCode.getData(), endCode.getData(), "路径阻塞超时"); |
| | | } |
| | | } else { |
| | | log.info("{}号车辆正在等待交通堵塞,阻塞车辆:【{}】", agvNo, blockAgvNo); |
| | | log.warn("{}号车辆正在等待交通堵塞,阻塞车辆:【{}】" |
| | | , agvNo |
| | | , blockVehicleList.stream().map(BlockVehicleDto::getVehicle).collect(Collectors.toList()).toString() |
| | | ); |
| | | } |
| | | |
| | | // 车辆阻塞 |
| | | } else { |
| | | |
| | | // 已经通知车辆避让 |
| | | if (!Cools.isEmpty(jam.getAvoAgv())) { |
| | | assert !Cools.isEmpty(jam.getNotifyTime()); |
| | | |
| | | if (System.currentTimeMillis() - jam.getNotifyTime().getTime() > timeoutDuration) { |
| | | log.warn("{}号车辆阻塞时间过长!!!已通知避让车辆【{}】...", agvNo, jam.getAvoAgv$()); |
| | | } |
| | | return pathList; |
| | | } |
| | | |
| | | do { |
| | | |
| | | // 阻塞车辆正在作业,等待 ===>> 超过等待时间,绕路 |
| | | List<Segment> runningSegList = segmentService.getByAgvAndState(blockAgv.getId(), SegmentStateType.RUNNING.toString()); |
| | | if (!Cools.isEmpty(runningSegList)) { |
| | | |
| | | if (System.currentTimeMillis() - previousTimestamp > timeoutDuration) { |
| | | |
| | | if (!Cools.isEmpty(lockPathList)) { |
| | | pathList = lockPathList; |
| | | } else { |
| | | log.error("{}号车辆检索[{}] ===>> [{}]路径失败,原因:{}" |
| | | , agvNo, startCode.getData(), endCode.getData(), "车辆阻塞超时"); |
| | | } |
| | | } else { |
| | | log.info("{}号车辆正在等待交通堵塞,阻塞车辆:【{}】", agvNo, blockAgvNo); |
| | | } |
| | | |
| | | break; |
| | | } |
| | | |
| | | // 判断下个任务是否为原地任务,如果是则等待 ===>> 超过等待时间,绕路;如果不是,让阻塞车辆避让 |
| | | Segment waitingSeg = segmentService.getJustWaitingSeg(blockAgv.getId()); |
| | | if (null != waitingSeg) { |
| | | |
| | | // 如果阻塞车辆待执行任务处于原地,则不能通知它避让 |
| | | if (waitingSeg.getEndNode().equals(codeService.selectByData(blockAgvCode).getId())) { |
| | | |
| | | if (System.currentTimeMillis() - previousTimestamp > Math.max((timeoutDuration / 30), (5 * 1000))) { |
| | | |
| | | if (!Cools.isEmpty(lockPathList)) { |
| | | pathList = lockPathList; |
| | | } else { |
| | | log.error("{}号车辆检索[{}] ===>> [{}]路径失败,原因:{}" |
| | | , agvNo, startCode.getData(), endCode.getData(), "车辆阻塞超时"); |
| | | } |
| | | } else { |
| | | log.info("{}号车辆正在等待交通堵塞,阻塞车辆:【{}】", agvNo, blockAgvNo); |
| | | } |
| | | |
| | | break; |
| | | } |
| | | } |
| | | |
| | | // 通知阻塞车辆避让 |
| | | if (this.notifyVehicleAvoid(blockAgvNo, blockAgvCode, unlockPathList, agvNo, jam)) { |
| | | |
| | | if (jam.getCycleAvo() == 1) { |
| | | jam.setCycleCode(endCode.getId()); |
| | | } |
| | | jam.setAvoAgv(blockAgv.getId()); |
| | | jam.setNotifyTime(new Date()); |
| | | if (!jamService.updateById(jam)) { |
| | | // log.error("{}编号阻塞记录更新失败!!!", jam.getUuid()); |
| | | throw new CoolException(jam.getUuid() + "编号阻塞记录更新失败!!!"); |
| | | } |
| | | } else { |
| | | |
| | | if (System.currentTimeMillis() - previousTimestamp > timeoutDuration) { |
| | | |
| | | if (!Cools.isEmpty(lockPathList)) { |
| | | pathList = lockPathList; |
| | | } else { |
| | | log.error("{}号车辆检索[{}] ===>> [{}]路径失败,原因:{}" |
| | | , agvNo, startCode.getData(), endCode.getData(), "车辆阻塞超时"); |
| | | } |
| | | } else { |
| | | log.info("{}号车辆正在等待交通堵塞,阻塞车辆:【{}】", agvNo, blockAgvNo); |
| | | } |
| | | } |
| | | |
| | | } while (false); |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | jam.setUpdateTime(now); |
| | | jam.setState(JamStateType.FINISH.toString()); |
| | | if (!jamService.updateById(jam)) { |
| | | log.error("{}编号阻塞记录完成修改失败!!!", jam.getUuid()); |
| | | log.error("Jam[{}] failed to update!!!", jam.getUuid()); |
| | | } |
| | | } |
| | | // deal expired jam |
| | |
| | | expiredJam.setUpdateTime(now); |
| | | expiredJam.setState(JamStateType.FINISH.toString()); |
| | | if (!jamService.updateById(expiredJam)) { |
| | | log.error("{}编号阻塞记录完成修改失败!!!", expiredJam.getUuid()); |
| | | log.error("Jam[{}] failed to update!!!", expiredJam.getUuid()); |
| | | } |
| | | } |
| | | |
| | |
| | | private List<BlockVehicleDto> slicePathAndReturnBlockVehicleList(Integer lev, List<String> fullPathList, String agvNo, List<String> pathList) { |
| | | List<BlockVehicleDto> blockVehicleList = new ArrayList<>(); |
| | | |
| | | DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev); |
| | | // DynamicNode[][] dynamicMatrix = mapDataDispatcher.getDynamicMatrix(lev); |
| | | String[][] waveMatrix = mapDataDispatcher.getWaveMatrix(lev); |
| | | for (String code : fullPathList) { |
| | | int[] node = mapDataDispatcher.getCodeMatrixIdx(lev, code); |
| | |
| | | return true; |
| | | } |
| | | |
| | | private Jam createOrUpdateJam(Agv agv, Code startCode, Segment segment, Jam jam) { |
| | | if (jam == null) { |
| | | jam = new Jam(); |
| | | jam.setUuid(String.valueOf(snowflakeIdWorker.nextId()).substring(3)); |
| | | jam.setJamAgv(agv.getId()); |
| | | jam.setJamCode(startCode.getId()); |
| | | jam.setJamSeg(segment.getId()); |
| | | jam.setStartTime(new Date()); |
| | | jam.setState(JamStateType.RUNNING.toString()); |
| | | if (!jamService.save(jam)) { |
| | | log.error("AGV[{}] failed to save jam", agv.getUuid()); |
| | | throw new CoolException("failed to save jam"); |
| | | } |
| | | } else { |
| | | jam.setDuration(System.currentTimeMillis() - jam.getStartTime().getTime()); |
| | | if (!jamService.updateById(jam)) { |
| | | log.error("AGV[{}] failed to update jam", agv.getUuid()); |
| | | } |
| | | } |
| | | return jam; |
| | | } |
| | | |
| | | } |