|  |  |  | 
|---|
|  |  |  | package com.zy.asrs.wcs.core.service.impl; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONArray; | 
|---|
|  |  |  | import com.alibaba.fastjson.JSONObject; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.Cools; | 
|---|
|  |  |  | import com.zy.asrs.framework.common.SnowflakeIdWorker; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.domain.dto.RedisMapDto; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.entity.*; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.kernel.AnalyzeService; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.model.MapNode; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.model.enums.DeviceCtgType; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.model.enums.MotionStsType; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.model.enums.TaskStsType; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.service.*; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.utils.RedisUtil; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.utils.ShuttleDispatcher; | 
|---|
|  |  |  | import com.zy.asrs.wcs.core.utils.Utils; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.News; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.cache.SlaveConnection; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.entity.Device; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.entity.DeviceType; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.model.enums.ShuttleProtocolStatusType; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.model.enums.SlaveType; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.model.protocol.ShuttleProtocol; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.model.protocol.StaProtocol; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.service.DeviceService; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.service.DeviceTypeService; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.thread.DevpThread; | 
|---|
|  |  |  | import com.zy.asrs.wcs.rcs.thread.ShuttleThread; | 
|---|
|  |  |  | import com.zy.asrs.wcs.system.entity.Dict; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.*; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 立体仓库WCS系统主流程业务 | 
|---|
|  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DeviceService deviceService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DeviceTypeService deviceTypeService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocCtgService locCtgService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private LocService locService; | 
|---|
|  |  |  | 
|---|
|  |  |  | private TaskCtgService taskCtgService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private DictService dictService; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private ShuttleDispatcher shuttleDispatcher; | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | private RedisUtil redisUtil; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 组托 | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public synchronized void generateInboundWrk() { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 初始化实时地图 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public synchronized void initRealtimeBasMap() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | List<Dict> dicts = dictService.list(new LambdaQueryWrapper<Dict>() | 
|---|
|  |  |  | .like(Dict::getFlag, "map") | 
|---|
|  |  |  | .eq(Dict::getStatus, 1)); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TreeMap<Integer, ArrayList<ArrayList<MapNode>>> levData = new TreeMap<>(); | 
|---|
|  |  |  | for (Dict dict : dicts) { | 
|---|
|  |  |  | String[] split = dict.getFlag().split("-"); | 
|---|
|  |  |  | int lev = Integer.parseInt(split[1]); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TreeMap<Integer, List<JSONObject>> rows = new TreeMap<>(); | 
|---|
|  |  |  | //排序Row | 
|---|
|  |  |  | JSONArray value = JSON.parseArray(dict.getValue()); | 
|---|
|  |  |  | for (Object o : value) { | 
|---|
|  |  |  | JSONObject item = JSON.parseObject(o.toString()); | 
|---|
|  |  |  | if (item.getString("type").equals("SHELF")) { | 
|---|
|  |  |  | JSONObject property = JSON.parseObject(item.getString("property")); | 
|---|
|  |  |  | Integer row1 = property.getInteger("row"); | 
|---|
|  |  |  | ArrayList<JSONObject> bays = new ArrayList<>(); | 
|---|
|  |  |  | if (rows.containsKey(row1)) { | 
|---|
|  |  |  | bays.addAll(rows.get(row1)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | bays.add(property); | 
|---|
|  |  |  | rows.put(row1, bays); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ArrayList<ArrayList<MapNode>> list = new ArrayList<>(); | 
|---|
|  |  |  | //排序Bay | 
|---|
|  |  |  | for (Map.Entry<Integer, List<JSONObject>> entry : rows.entrySet()) { | 
|---|
|  |  |  | ArrayList<MapNode> nodes = new ArrayList<>(); | 
|---|
|  |  |  | for (JSONObject object : entry.getValue()) { | 
|---|
|  |  |  | MapNode mapNode = new MapNode(); | 
|---|
|  |  |  | mapNode.setValue(object.getInteger("shelfType")); | 
|---|
|  |  |  | mapNode.setTop(object.getInteger("top")); | 
|---|
|  |  |  | mapNode.setBottom(object.getInteger("bottom")); | 
|---|
|  |  |  | mapNode.setLeft(object.getInteger("left")); | 
|---|
|  |  |  | mapNode.setRight(object.getInteger("right")); | 
|---|
|  |  |  | mapNode.setRow(object.getInteger("row")); | 
|---|
|  |  |  | mapNode.setBay(object.getInteger("bay")); | 
|---|
|  |  |  | mapNode.setNo(object.getString("row") + "-" + object.getString("bay")); | 
|---|
|  |  |  | nodes.add(mapNode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Collections.sort(nodes, new Comparator<MapNode>() { | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public int compare(MapNode o1, MapNode o2) { | 
|---|
|  |  |  | return Integer.compare(o1.getBay(), o2.getBay()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | list.add(nodes); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | levData.put(lev, list); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | for (Map.Entry<Integer, ArrayList<ArrayList<MapNode>>> entry : levData.entrySet()) { | 
|---|
|  |  |  | ArrayList<ArrayList<MapNode>> lists = entry.getValue();//获取地图 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | MapNode mapNode = new MapNode(); | 
|---|
|  |  |  | mapNode.setValue(-1); | 
|---|
|  |  |  | mapNode.setTop(1000); | 
|---|
|  |  |  | mapNode.setBottom(1000); | 
|---|
|  |  |  | mapNode.setLeft(1000); | 
|---|
|  |  |  | mapNode.setRight(1000); | 
|---|
|  |  |  | mapNode.setRow(0); | 
|---|
|  |  |  | mapNode.setBay(0); | 
|---|
|  |  |  | mapNode.setNo("0-0"); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取最长row | 
|---|
|  |  |  | int row = 0; | 
|---|
|  |  |  | //给每个row首尾增加-1节点 | 
|---|
|  |  |  | for (ArrayList<MapNode> list : lists) { | 
|---|
|  |  |  | if (list.size() > row) { | 
|---|
|  |  |  | row = list.size(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | list.add(0, mapNode.clone()); | 
|---|
|  |  |  | list.add(mapNode.clone()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ArrayList<MapNode> headNodes = new ArrayList<>(); | 
|---|
|  |  |  | ArrayList<MapNode> footerNodes = new ArrayList<>(); | 
|---|
|  |  |  | for (int i = 0; i < row+2; i++) { | 
|---|
|  |  |  | headNodes.add(mapNode.clone()); | 
|---|
|  |  |  | footerNodes.add(mapNode.clone()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | lists.add(0, headNodes); | 
|---|
|  |  |  | lists.add(footerNodes); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Integer lev = entry.getKey(); | 
|---|
|  |  |  | Date now = new Date(); | 
|---|
|  |  |  | RedisMapDto map = new RedisMapDto(); | 
|---|
|  |  |  | map.setData(JSON.toJSONString(lists)); | 
|---|
|  |  |  | map.setCreateTime(now); | 
|---|
|  |  |  | map.setUpdateTime(now); | 
|---|
|  |  |  | map.setLev(lev); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //将地图数据存入redis | 
|---|
|  |  |  | redisUtil.set(DeviceRedisConstant.MAP + lev, JSON.toJSONString(map)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 解析入库工作档 | 
|---|
|  |  |  | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | DeviceType deviceType = deviceTypeService.getOne(new LambdaQueryWrapper<DeviceType>() | 
|---|
|  |  |  | .eq(DeviceType::getFlag, String.valueOf(SlaveType.Shuttle)) | 
|---|
|  |  |  | .eq(DeviceType::getStatus, 1)); | 
|---|
|  |  |  | if (deviceType == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>() | 
|---|
|  |  |  | .eq(Device::getDeviceType, deviceType.getId()) | 
|---|
|  |  |  | .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val()) | 
|---|
|  |  |  | .eq(Device::getStatus, 1)); | 
|---|
|  |  |  | for (Device device : list) { | 
|---|
|  |  |  | //获取四向穿梭车线程 | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | ShuttleProtocol shuttleProtocol = shuttleThread.getStatus(); | 
|---|
|  |  |  | if (shuttleProtocol == null) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!shuttleProtocol.getProtocolStatusType().equals(ShuttleProtocolStatusType.IDLE)) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (!shuttleThread.isRequireCharge()) { | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | DeviceType deviceType = deviceTypeService.getOne(new LambdaQueryWrapper<DeviceType>() | 
|---|
|  |  |  | .eq(DeviceType::getFlag, String.valueOf(SlaveType.Shuttle)) | 
|---|
|  |  |  | .eq(DeviceType::getStatus, 1)); | 
|---|
|  |  |  | if (deviceType == null) { | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>() | 
|---|
|  |  |  | .eq(Device::getDeviceType, deviceType.getId()) | 
|---|
|  |  |  | .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val()) | 
|---|
|  |  |  | .eq(Device::getStatus, 1)); | 
|---|
|  |  |  | for (Device device : list) { | 
|---|
|  |  |  | //获取四向穿梭车线程 | 
|---|
|  |  |  | 
|---|
|  |  |  | continue; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //获取避让位置 | 
|---|
|  |  |  | String standByLocNo = shuttleDispatcher.searchStandByLocNo(Integer.valueOf(device.getDeviceNo()), device.getHostId(), shuttleThread.getStatus().getCurrentLocNo()); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Task task = new Task(); | 
|---|
|  |  |  | task.setUuid(String.valueOf(snowflakeIdWorker.nextId())); | 
|---|
|  |  |  | task.setTaskNo(String.valueOf(Utils.getTaskNo("MOVE"))); | 
|---|
|  |  |  | 
|---|
|  |  |  | task.setOriginSite(null); | 
|---|
|  |  |  | task.setOriginLoc(null); | 
|---|
|  |  |  | task.setDestSite(null); | 
|---|
|  |  |  | task.setDestLoc("1-1-1"); // 暂时未定 | 
|---|
|  |  |  | task.setDestLoc(standByLocNo); // 避让位置 | 
|---|
|  |  |  | task.setIoTime(new Date()); | 
|---|
|  |  |  | task.setStartTime(new Date()); | 
|---|
|  |  |  | task.setHostId(device.getHostId()); | 
|---|