|  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.core.common.Arith; | 
|---|
|  |  |  | import com.core.common.Cools; | 
|---|
|  |  |  | import com.core.common.SpringUtils; | 
|---|
|  |  |  | import com.zy.asrs.entity.LocMast; | 
|---|
|  |  |  | import com.zy.asrs.service.LocMastService; | 
|---|
|  |  |  | import com.zy.asrs.service.impl.MainServiceImpl; | 
|---|
|  |  |  | import com.zy.common.model.NavigateNode; | 
|---|
|  |  |  | import com.zy.common.model.enums.NavigationMapType; | 
|---|
|  |  |  | import com.zy.common.service.CommonService; | 
|---|
|  |  |  | import com.zy.common.utils.NavigateMapData; | 
|---|
|  |  |  | import com.zy.common.utils.NavigatePositionConvert; | 
|---|
|  |  |  | import com.zy.common.utils.NavigateUtils; | 
|---|
|  |  |  | import com.zy.core.Slave; | 
|---|
|  |  |  | import com.zy.core.cache.MessageQueue; | 
|---|
|  |  |  | import com.zy.core.cache.SlaveConnection; | 
|---|
|  |  |  | import com.zy.core.enums.ShuttleRunDirection; | 
|---|
|  |  |  | import com.zy.core.enums.ShuttleTaskModeType; | 
|---|
|  |  |  | import com.zy.core.enums.SlaveType; | 
|---|
|  |  |  | import com.zy.core.model.ShuttleSlave; | 
|---|
|  |  |  | import com.zy.core.model.Task; | 
|---|
|  |  |  | import com.zy.core.model.command.ShuttleAssignCommand; | 
|---|
|  |  |  | import com.zy.core.model.command.ShuttleCommand; | 
|---|
|  |  |  | import com.zy.core.model.protocol.ShuttleProtocol; | 
|---|
|  |  |  | import com.zy.core.properties.SlaveProperties; | 
|---|
|  |  |  | import com.zy.core.thread.ShuttleThread; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.text.DecimalFormat; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取除白名单外的指定楼层全部穿梭车xy坐标点 | 
|---|
|  |  |  | public static List<int[]> getShuttlePoints(Integer whiteShuttle, Integer lev) { | 
|---|
|  |  |  | SlaveProperties slaveProperties = SpringUtils.getBean(SlaveProperties.class); | 
|---|
|  |  |  | ArrayList<int[]> list = new ArrayList<>(); | 
|---|
|  |  |  | for (ShuttleSlave slave : slaveProperties.getShuttle()) { | 
|---|
|  |  |  | if (slave.getId().intValue() == whiteShuttle) { | 
|---|
|  |  |  | continue;//跳过白名单 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取穿梭车所在节点位置 | 
|---|
|  |  |  | ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId()); | 
|---|
|  |  |  | if (shuttleThread == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol(); | 
|---|
|  |  |  | if (shuttleProtocol == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (shuttleProtocol.getLocNo() == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (lev != Utils.getLev(shuttleProtocol.getLocNo())) { | 
|---|
|  |  |  | continue;//楼层不同 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int[] xyPosition = NavigatePositionConvert.positionToXY(shuttleProtocol.getLocNo());//通过库位号获取xy坐标 | 
|---|
|  |  |  | list.add(xyPosition); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return list; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //检测穿梭车行走路径,是否存在其他小车,如有其他小车则进行调离 | 
|---|
|  |  |  | public static boolean checkShuttlePath(List<NavigateNode> nodes, Integer shuttleId) { | 
|---|
|  |  |  | boolean flag = false; | 
|---|
|  |  |  | int shuttleX = -1; | 
|---|
|  |  |  | int shuttleY = -1; | 
|---|
|  |  |  | int shuttleZ = -1; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | LocMastService locMastService = SpringUtils.getBean(LocMastService.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (nodes == null) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (nodes.size() == 0) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | NavigateMapData mapData = new NavigateMapData(nodes.get(0).getZ());//获取地图数据 | 
|---|
|  |  |  | int[][] data = mapData.getData(-1, null, Utils.getShuttlePoints(shuttleId, nodes.get(0).getZ())); | 
|---|
|  |  |  | for (NavigateNode node : nodes) { | 
|---|
|  |  |  | int x = node.getX(); | 
|---|
|  |  |  | int y = node.getY(); | 
|---|
|  |  |  | if (data[x][y] == 66) {//判断该路径是否有小车 | 
|---|
|  |  |  | flag = true;//存在小车 | 
|---|
|  |  |  | shuttleX = x; | 
|---|
|  |  |  | shuttleY = y; | 
|---|
|  |  |  | shuttleZ = node.getZ(); | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (flag) { | 
|---|
|  |  |  | //检测到路径存在其他小车 | 
|---|
|  |  |  | //搜索一条没有小车的空巷道 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取小车坐标二维码 | 
|---|
|  |  |  | Short shuttlePosition = NavigatePositionConvert.xyToPosition(shuttleX, shuttleY, shuttleZ); | 
|---|
|  |  |  | LocMast shuttleLocMast = locMastService.queryByQrCode(String.valueOf(shuttlePosition)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取小车线程 | 
|---|
|  |  |  | SlaveProperties slaveProperties = SpringUtils.getBean(SlaveProperties.class); | 
|---|
|  |  |  | ShuttleThread currentShuttleThread = null; | 
|---|
|  |  |  | for (ShuttleSlave slave : slaveProperties.getShuttle()) { | 
|---|
|  |  |  | //获取穿梭车所在节点位置 | 
|---|
|  |  |  | ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, slave.getId()); | 
|---|
|  |  |  | if (shuttleThread == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol(); | 
|---|
|  |  |  | if (shuttleProtocol == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (shuttleProtocol.getCurrentCode().intValue() == Integer.parseInt(shuttleLocMast.getQrCodeValue())) { | 
|---|
|  |  |  | //小车坐标和线程获取的小车坐标一致 | 
|---|
|  |  |  | currentShuttleThread = shuttleThread; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (currentShuttleThread == null) { | 
|---|
|  |  |  | //没找到小车 | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ShuttleProtocol shuttleProtocol = currentShuttleThread.getShuttleProtocol(); | 
|---|
|  |  |  | if (!shuttleProtocol.isIdle()) { | 
|---|
|  |  |  | return false;//被调度的小车处于工作状态,禁止触发避让任务 | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //搜索一条没有小车的空巷道,并调度小车 | 
|---|
|  |  |  | ShuttleAssignCommand assignCommand = Utils.searchEmptyGroupToMoveShuttle(shuttleZ, shuttleId, currentShuttleThread, null);//shuttleId搜索时需要排除的车辆id,currentShuttleThread是需要被调度的车辆线程 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (assignCommand == null) { | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //下发任务 | 
|---|
|  |  |  | MessageQueue.offer(SlaveType.Shuttle, assignCommand.getShuttleNo().intValue(), new Task(3, assignCommand)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //搜索一条没有小车的空巷道,并返回移动小车的命令 | 
|---|
|  |  |  | public static ShuttleAssignCommand searchEmptyGroupToMoveShuttle(int z, Integer currentShuttleId, ShuttleThread shuttleThread, String startLocNo) { | 
|---|
|  |  |  | LocMastService locMastService = SpringUtils.getBean(LocMastService.class); | 
|---|
|  |  |  | MainServiceImpl mainServiceImpl = SpringUtils.getBean(MainServiceImpl.class); | 
|---|
|  |  |  | CommonService commonService = SpringUtils.getBean(CommonService.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (shuttleThread == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ShuttleProtocol shuttleProtocol = shuttleThread.getShuttleProtocol(); | 
|---|
|  |  |  | if (shuttleProtocol == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | NavigateMapData mapData = new NavigateMapData(z);//获取地图数据 | 
|---|
|  |  |  | int[][] data = mapData.getData(-1, null, currentShuttleId == null ? null : Utils.getShuttlePoints(0, z));//载入全部车辆 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | int distY = -1; | 
|---|
|  |  |  | int distX = -1; | 
|---|
|  |  |  | int distZ = -1; | 
|---|
|  |  |  | for (int y = 3; y <= 25; y++) { | 
|---|
|  |  |  | boolean searchFlag = true; | 
|---|
|  |  |  | for (int x = 20; x <= 23; x++) { | 
|---|
|  |  |  | if (data[x][y] < 0 || data[x][y] == 66) { | 
|---|
|  |  |  | searchFlag = false;//该巷道有禁用节点或有小车 | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (searchFlag) { | 
|---|
|  |  |  | //搜索出空巷道 | 
|---|
|  |  |  | distY = y; | 
|---|
|  |  |  | distX = 20; | 
|---|
|  |  |  | distZ = z; | 
|---|
|  |  |  | break; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (distY != -1) { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取避让坐标二维码 | 
|---|
|  |  |  | Short distPosition = NavigatePositionConvert.xyToPosition(distX, distY, distZ); | 
|---|
|  |  |  | LocMast distLocMast = locMastService.queryByQrCode(String.valueOf(distPosition)); | 
|---|
|  |  |  | if (distLocMast == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (startLocNo == null) { | 
|---|
|  |  |  | //获取小车坐标二维码 | 
|---|
|  |  |  | LocMast shuttleLocMast = locMastService.queryByQrCode(String.valueOf(shuttleProtocol.getCurrentCode())); | 
|---|
|  |  |  | if (shuttleLocMast == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | startLocNo = shuttleLocMast.getLocNo(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //创建分配命令 | 
|---|
|  |  |  | ShuttleAssignCommand assignCommand = new ShuttleAssignCommand(); | 
|---|
|  |  |  | assignCommand.setShuttleNo(shuttleProtocol.getShuttleNo());//四向穿梭车号 | 
|---|
|  |  |  | assignCommand.setTaskNo((short) commonService.getWorkNo(7));//任务号 | 
|---|
|  |  |  | assignCommand.setTaskMode(ShuttleTaskModeType.AVOID.id.shortValue());//避让任务 | 
|---|
|  |  |  | assignCommand.setSourceLocNo(startLocNo);//源库位 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //分配目标库位 | 
|---|
|  |  |  | shuttleProtocol.setLocNo(distLocMast.getLocNo()); | 
|---|
|  |  |  | //目标库位 | 
|---|
|  |  |  | assignCommand.setLocNo(distLocMast.getLocNo()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //小车移动到目标位置 | 
|---|
|  |  |  | List<ShuttleCommand> commands = mainServiceImpl.shuttleAssignCommand(startLocNo, distLocMast.getLocNo(), NavigationMapType.NORMAL.id, assignCommand, shuttleThread); | 
|---|
|  |  |  | if (commands == null) { | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | assignCommand.setCommands(commands); | 
|---|
|  |  |  | return assignCommand; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|