#
Junjie
2 天以前 6694bb8752aced4b818f2976442d66ae3a52e9e8
src/main/java/com/zy/core/thread/impl/TrafficControlImplThread.java
@@ -33,6 +33,9 @@
    private Long detectTime = System.currentTimeMillis();
    private HashMap<String, Integer> deviceMap = null;
    private HashMap<Integer, HashMap<String, List<Integer>>> levNodesMap = null;
    private HashMap<Integer,Long> applyRecordsMap = new HashMap<>();
    private HashMap<String, List<NavigateNode>> taskNodesMap = new HashMap<>();
    private List<TrafficControlDataModel> trafficControlDataList = new ArrayList<>();
@@ -42,6 +45,12 @@
    @Override
    public void run() {
        //从缓存恢复交管信息
        Object object = redisUtil.get(RedisKeyType.TRAFFIC_CONTROL_MAP.key);
        if(object != null) {
            trafficControlDataList = (List<TrafficControlDataModel>) object;
        }
//        List<Integer> shuttleNoList = new ArrayList<>();
//        while (true) {
//            try {
@@ -198,66 +207,66 @@
        detecting = false;
        detectTime = System.currentTimeMillis();
        //发布堵塞节点可用设备编号
        redisUtil.set(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key, trafficControlDataList);
//        //发布堵塞节点可用设备编号
//        redisUtil.set(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key, trafficControlDataList);
    }
    //分配堵塞节点可执行设备
    public void findDeviceByBlockList(List<List<String>> allLocList, List<List<Integer>> blockNodes) {
        HashMap<String, Integer> map = new HashMap<>();
        if (deviceMap == null) {
            Object object = redisUtil.get(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key);
            if (object != null) {
                map = (HashMap<String, Integer>) object;
            }
        } else {
            map = deviceMap;
        }
        HashMap<String, Integer> newMap = new HashMap<>();
        for (int i = 0; i < blockNodes.size(); i++) {
            List<Integer> blockNode = blockNodes.get(i);
            List<String> locs = allLocList.get(i);
            String key = JSON.toJSONString(locs);
            Integer value = -1;
            if (map.containsKey(key)) {
                value = map.get(key);
                map.remove(key);
                if (value > 0) {
                    ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, value);
                    if (shuttleThread == null) {
                        continue;
                    }
                    ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
                    if (shuttleProtocol == null) {
                        continue;
                    }
                    if (shuttleProtocol.getTaskNo() == 0) {
                        value = searchDevice(locs, blockNode, newMap);
                    }
                    if (!shuttleProtocol.getTrafficControl()) {
                        value = searchDevice(locs, blockNode, newMap);
                    }
                }else {
                    value = searchDevice(locs, blockNode, newMap);
                }
            } else {
                value = searchDevice(locs, blockNode, newMap);
            }
            newMap.put(key, value);
        }
        deviceMap = newMap;
        //发布堵塞节点可用设备编号
        redisUtil.set(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key, newMap);
    }
//    //分配堵塞节点可执行设备
//    public void findDeviceByBlockList(List<List<String>> allLocList, List<List<Integer>> blockNodes) {
//        HashMap<String, Integer> map = new HashMap<>();
//        if (deviceMap == null) {
//            Object object = redisUtil.get(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key);
//            if (object != null) {
//                map = (HashMap<String, Integer>) object;
//            }
//        } else {
//            map = deviceMap;
//        }
//
//        HashMap<String, Integer> newMap = new HashMap<>();
//
//        for (int i = 0; i < blockNodes.size(); i++) {
//            List<Integer> blockNode = blockNodes.get(i);
//            List<String> locs = allLocList.get(i);
//
//            String key = JSON.toJSONString(locs);
//
//            Integer value = -1;
//            if (map.containsKey(key)) {
//                value = map.get(key);
//                map.remove(key);
//
//                if (value > 0) {
//                    ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, value);
//                    if (shuttleThread == null) {
//                        continue;
//                    }
//
//                    ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
//                    if (shuttleProtocol == null) {
//                        continue;
//                    }
//
//                    if (shuttleProtocol.getTaskNo() == 0) {
//                        value = searchDevice(locs, blockNode, newMap);
//                    }
//
//                    if (!shuttleProtocol.getTrafficControl()) {
//                        value = searchDevice(locs, blockNode, newMap);
//                    }
//                }else {
//                    value = searchDevice(locs, blockNode, newMap);
//                }
//            } else {
//                value = searchDevice(locs, blockNode, newMap);
//            }
//            newMap.put(key, value);
//        }
//
//        deviceMap = newMap;
//        //发布堵塞节点可用设备编号
//        redisUtil.set(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key, newMap);
//    }
    public Integer searchDevice(List<String> locs, List<Integer> blockNode, HashMap<String, Integer> deviceMap) {
        NavigateUtils navigateUtils = null;
@@ -434,8 +443,24 @@
    @Override
    public synchronized boolean applyTrafficControl(List<NavigateNode> totalNodeList, List<NavigateNode> nodeList, Integer shuttleNo, Integer taskNo) {
        //发布堵塞节点可用设备编号
        redisUtil.set(RedisKeyType.TASK_BLOCK_ENABLE_DEVICE_MAP.key, trafficControlDataList);
        //更新交管信息
        redisUtil.set(RedisKeyType.TRAFFIC_CONTROL_MAP.key, trafficControlDataList);
        NavigateNode startNode = totalNodeList.get(0);
        List<int[]> shuttlePoints = Utils.getShuttlePoints(shuttleNo, startNode.getZ());
        int[] currentShuttlePoint = Utils.getShuttlePoint(shuttleNo);
        if(currentShuttlePoint == null) {
            return false;
        }
        String currentShuttleLoc = Utils.getLocNo(currentShuttlePoint[0], currentShuttlePoint[1], startNode.getZ());
        List<String> shuttleLocList = new ArrayList<>();
        for (int[] shuttlePoint : shuttlePoints) {
            String locNo = Utils.getLocNo(shuttlePoint[0], shuttlePoint[1], startNode.getZ());
            shuttleLocList.add(locNo);
        }
        //检测车子是否存在管制
        for (int i = 0; i < trafficControlDataList.size(); i++) {
            TrafficControlDataModel controlDataModel = trafficControlDataList.get(i);
@@ -445,27 +470,14 @@
                    return false;
                }
                //任务总数量不一致
                if (totalNodeList.size() != controlDataModel.getTotalNodeList().size()) {
                    return false;
                }
                int startIdx = 0;
                int targetIdx = totalNodeList.size() - 1;
                NavigateNode applyStartNode = totalNodeList.get(startIdx);
                NavigateNode applyTargetNode = totalNodeList.get(targetIdx);
                NavigateNode controlStartNode = controlDataModel.getTotalNodeList().get(startIdx);
                NavigateNode controlTargetNode = controlDataModel.getTotalNodeList().get(targetIdx);
                //起点不同
                if(!NavigatePositionConvert.equalsNode(applyStartNode, controlStartNode)) {
                    return false;
                }
                //终点不同
                if(!NavigatePositionConvert.equalsNode(applyTargetNode, controlTargetNode)) {
                    return false;
                for (NavigateNode node : nodeList) {
                    String locNo = Utils.getLocNo(node.getX(), node.getY(), node.getZ());
                    if(shuttleLocList.contains(locNo)) {
                        ShuttleThread shuttleThread = Utils.searchShuttle(locNo);
                        if(shuttleThread != null) {
                            shuttleThread.restartCalcPath();
                        }
                    }
                }
                News.info("traffic running {},{}", shuttleNo, taskNo);
@@ -473,30 +485,45 @@
            }
        }
        NavigateNode startNode = totalNodeList.get(0);
        List<int[]> shuttlePoints = Utils.getShuttlePoints(shuttleNo, startNode.getZ());
        List<String> shuttleLocList = new ArrayList<>();
        for (int[] shuttlePoint : shuttlePoints) {
            String locNo = Utils.getLocNo(shuttlePoint[0], shuttlePoint[1], startNode.getZ());
            shuttleLocList.add(locNo);
        ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleNo);
        if (shuttleThread == null) {
            return false;
        }
        if (!applyRecordsMap.containsKey(shuttleNo)) {
            applyRecordsMap.put(shuttleNo, System.currentTimeMillis());
        }
        Long applyRecordTime = applyRecordsMap.get(shuttleNo);
        if ((System.currentTimeMillis() - applyRecordTime) > 1000 * 10) {
            shuttleThread.restartCalcPath();
        }
        List<String> totalLocList = new ArrayList<>();
        for (NavigateNode node : totalNodeList) {
            String locNo = Utils.getLocNo(node.getX(), node.getY(), node.getZ());
            if(shuttleLocList.contains(locNo)) {
               return false;//node has shuttle
            totalLocList.add(locNo);
        }
        //检测路径是否有小车
        for (String loc : totalLocList) {
            if(shuttleLocList.contains(loc)) {
                return false;//node has shuttle
            }
        }
        //检测节点是否被使用
        for (TrafficControlDataModel controlDataModel : trafficControlDataList) {
            List<NavigateNode> list = controlDataModel.getTotalNodeList();
            for (NavigateNode node1 : list) {
                for (NavigateNode node2 : totalNodeList) {
                    if (NavigatePositionConvert.equalsNode(node1, node2)) {
                        return false;
                    }
            for (int i = 0; i < list.size(); i++) {
                NavigateNode node = list.get(i);
                String locNo = Utils.getLocNo(node.getX(), node.getY(), node.getZ());
                if (i == 0 && currentShuttleLoc.equals(locNo)) {
                    continue;//..todo wait watch problem
                }
                if(totalLocList.contains(locNo)) {
                    return false;
                }
            }
        }
@@ -508,20 +535,36 @@
        model.setNodeList(nodeList);
        model.setTotalNodeList(totalNodeList);
        trafficControlDataList.add(model);
        applyRecordsMap.remove(shuttleNo);
        News.info("receipt traffic {},{}", shuttleNo, taskNo);
        return true;
    }
    @Override
    public boolean trafficReport(List<NavigateNode> nodeList, Integer shuttleNo, Integer taskNo) {
    public synchronized boolean trafficReport(List<NavigateNode> nodeList, Integer shuttleNo, Integer taskNo) {
        //检测车子是否存在管制
        for (int i = 0; i < trafficControlDataList.size(); i++) {
            TrafficControlDataModel controlDataModel = trafficControlDataList.get(i);
            if(shuttleNo.equals(controlDataModel.getShuttleNo())) {
                if(controlDataModel.getTaskNo().equals(taskNo)) {
                    List<NavigateNode> newTotalNodeList = new ArrayList<>();
                    List<NavigateNode> totalNodeList = controlDataModel.getTotalNodeList();
                    totalNodeList.removeAll(nodeList);
                    controlDataModel.setTotalNodeList(totalNodeList);
                    List<String> reportList = new ArrayList<>();
                    for (NavigateNode node : nodeList) {
                        reportList.add(Utils.getLocNo(node.getX(), node.getY(), node.getZ()));
                    }
                    for (NavigateNode node : totalNodeList) {
                        String locNo = Utils.getLocNo(node.getX(), node.getY(), node.getZ());
                        if(reportList.contains(locNo)) {
                            continue;
                        }
                        newTotalNodeList.add(node);
                    }
                    controlDataModel.setTotalNodeList(newTotalNodeList);
                    trafficControlDataList.set(i, controlDataModel);
                    return true;
                }
@@ -546,6 +589,19 @@
    }
    @Override
    public boolean forceCancelTrafficControl(Integer shuttleNo) {
        //检测车子是否存在管制
        for (int i = 0; i < trafficControlDataList.size(); i++) {
            TrafficControlDataModel controlDataModel = trafficControlDataList.get(i);
            if(shuttleNo.equals(controlDataModel.getShuttleNo())) {
                trafficControlDataList.remove(i);//取消管制
                return true;
            }
        }
        return false;
    }
    @Override
    public TrafficControlDataModel queryTrafficControl(Integer shuttleNo) {
        //检测车子是否存在管制
        for (int i = 0; i < trafficControlDataList.size(); i++) {