zjj
2025-04-10 d8f676ad00f7053180bcbdb91b8abe511eae208e
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/service/impl/MainServiceImpl.java
@@ -8,17 +8,22 @@
import com.zy.asrs.common.domain.param.SearchLocParam;
import com.zy.asrs.common.utils.HttpHandler;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.framework.common.R;
import com.zy.asrs.framework.common.SnowflakeIdWorker;
import com.zy.asrs.framework.exception.CoolException;
import com.zy.asrs.wcs.core.action.LiftAction;
import com.zy.asrs.wcs.core.domain.dto.MatDto;
import com.zy.asrs.wcs.core.domain.dto.RedisMapDto;
import com.zy.asrs.wcs.core.domain.dto.StaDto;
import com.zy.asrs.wcs.core.domain.param.CreateInTaskParam;
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.model.command.LiftAssignCommand;
import com.zy.asrs.wcs.core.model.command.LiftCommand;
import com.zy.asrs.wcs.core.model.enums.*;
import com.zy.asrs.wcs.core.service.*;
import com.zy.asrs.wcs.core.utils.OpenUtils;
import com.zy.asrs.wcs.core.utils.RedisUtil;
import com.zy.asrs.wcs.core.utils.ShuttleDispatcher;
import com.zy.asrs.wcs.core.utils.Utils;
@@ -29,13 +34,11 @@
import com.zy.asrs.wcs.rcs.model.command.LedCommand;
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.LiftProtocol;
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.thread.BarcodeThread;
import com.zy.asrs.wcs.rcs.thread.DevpThread;
import com.zy.asrs.wcs.rcs.thread.LedThread;
import com.zy.asrs.wcs.rcs.thread.ShuttleThread;
import com.zy.asrs.wcs.rcs.thread.*;
import com.zy.asrs.wcs.system.entity.Dict;
import com.zy.asrs.wcs.system.service.DictService;
import lombok.extern.slf4j.Slf4j;
@@ -81,7 +84,17 @@
    @Autowired
    private BasConveyorStaService basConveyorStaService;
    @Autowired
    private BasConveyorPathService basConveyorPathService;
    @Autowired
    private BasLedService basLedService;
    @Autowired
    private DeviceBarcodeService deviceBarcodeService;
    @Autowired
    private ShuttleChargeStaService shuttleChargeStaService;
    @Autowired
    private OpenUtils openUtils;
    @Autowired
    private LiftAction liftAction;
    /**
     * 组托
@@ -97,6 +110,11 @@
                BasConveyor basConveyor = basConveyorService.getOne(new LambdaQueryWrapper<BasConveyor>().eq(BasConveyor::getDeviceId, devp.getId()).eq(BasConveyor::getHostId, devp.getHostId()));
                // 遍历入库口
                for (StaDto inSta : JSON.parseArray(basConveyor.getInSta(), StaDto.class)) {
                    BasConveyorSta basConveyorSta = basConveyorStaService.selectBySiteNo(inSta.getStaNo().toString());
                    if(basConveyorSta == null) {
                        continue;
                    }
                    // 获取入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, devp.getId().intValue());
                    StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
@@ -151,16 +169,22 @@
                    if (staProtocol.isAutoing() && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && !staProtocol.isEmptyMk() && (workNo == 0 || (workNo >= 9990 && workNo <= 9999))
                            && staProtocol.isPakMk()
                    ) {
                        // 获取条码扫描仪信息
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, inSta.getBarcode());
                        DeviceBarcode deviceBarcode = deviceBarcodeService.getById(basConveyorSta.getBarcodeId());
                        if(deviceBarcode == null) {
                            continue;
                        }
                        BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, Integer.parseInt(deviceBarcode.getDeviceId()));
                        if (barcodeThread == null) {
                            continue;
                        }
                        String barcode = barcodeThread.getBarcode();
                        if (!Cools.isEmpty(barcode)) {
                            News.info("{}号条码扫描器检测条码信息:{}", inSta.getBarcode(), barcode);
                            News.info("{}号条码扫描器检测条码信息:{}", deviceBarcode.getId(), barcode);
                            if ("NG".endsWith(barcode) || "NoRead".equals(barcode) || "empty".equals(barcode) || "00000000".equals(barcode)) {
                                staProtocol.setWorkNo((short) 32002);
                                staProtocol.setStaNo(inSta.getBackSta().shortValue());
@@ -177,19 +201,18 @@
                            }
                        }
                        //获取入库任务类型
                        TaskCtg taskCtg = taskCtgService.getOne(new LambdaQueryWrapper<TaskCtg>()
                                .eq(TaskCtg::getFlag, "IN")
                                .eq(TaskCtg::getStatus, 1));
                        // 判断重复工作档
                        Task task1 = taskService.getOne(new LambdaQueryWrapper<Task>()
                                .eq(Task::getOriginSite, inSta.getStaNo())
                                .eq(Task::getTaskCtg, taskCtg.getId())
                                .in(Task::getTaskSts, 1, 2, 3)
                                .in(Task::getTaskSts, TaskStsType.NEW_INBOUND.sts, TaskStsType.ANALYZE_INBOUND.sts, TaskStsType.EXECUTE_INBOUND.sts)
                                .eq(Task::getZpallet, barcode));
                        if (task1 != null) {
                            News.error("工作档已存在,工作号={}", task1.getTaskNo());
                            if (staProtocol.getWorkNo().intValue() != Integer.parseInt(task1.getTaskNo())) {
                                devpThread.writeWorkSta(staProtocol.getSiteId(), Short.parseShort(task1.getTaskNo()), Short.parseShort(task1.getDestSite()));
                                devpThread.setPakMk(staProtocol.getSiteId(), false);
                                News.info("输送线入库命令下发,任务数据={}", JSON.toJSON(task1));
                            }
                            continue;
                        }
@@ -209,7 +232,7 @@
                            param.setLocType1(staProtocol.getLocType1().shortValue());
                            String response = new HttpHandler.Builder()
                                    .setUri(wmsUrl)
                                    .setPath("/rpc/pakin/loc/v2")
                                    .setPath("/rpc/pakin/loc/v1")
                                    .setJson(JSON.toJSONString(param))
                                    .build()
                                    .doPost();
@@ -218,35 +241,183 @@
                            Integer code = jsonObject.getInteger("code");
                            if (code.equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                                devpThread.writeWorkSta(staProtocol.getSiteId(), dto.getWorkNo().shortValue(), dto.getStaNo().shortValue());
                                devpThread.setPakMk(staProtocol.getSiteId(), false);
                            } else if (code == 500) {
                                if (ledThread != null) {
                                    String errorMsg = jsonObject.getString("msg");
                                    if (!Cools.isEmpty(errorMsg)) {
                                        ledThread.error(errorMsg);
                                        ledThread.setLedMk(false);
                                    }
                                }
                                News.error("请求接口失败!!!url:{};request:{};response:{}", wmsUrl + "/rpc/pakin/loc/v2", JSON.toJSONString(param), response);
                            } else if (code == 700) {
//                            staProtocol.setWorkNo((short) 32002);
//                            staProtocol.setRollback102(1);//102站回退信号
//                            devpThread.setPakMk(staProtocol.getSiteId(), false);
//                            MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(5, staProtocol));
                                String wmsLocNo = dto.getLocNo();
                                int row = Integer.parseInt(wmsLocNo.substring(0, 2));
                                int bay = Integer.parseInt(wmsLocNo.substring(2, 5));
                                int lev = Integer.parseInt(wmsLocNo.substring(5, 7));
                                row -= 16;
                                String wcsLocNo = Utils.getLocNo(row, bay, lev);
                                // led 异常显示
                                if (ledThread != null) {
                                    String errorMsg = barcode + "托盘识别异常,请先进行组托!";
                                    ledThread.error(errorMsg);
                                    ledThread.setLedMk(false);
                                CreateInTaskParam createInTaskParam = new CreateInTaskParam();
                                createInTaskParam.setTaskNo(String.valueOf(dto.getWorkNo()));
                                createInTaskParam.setDestLoc(wcsLocNo);
                                createInTaskParam.setOriginSite("31002");
                                createInTaskParam.setDestSite("31004");
                                createInTaskParam.setPriority(11);
                                createInTaskParam.setBarcode(barcode);
                                R result = openUtils.createInTask(createInTaskParam);
                                News.info("创建入库任务,任务数据={},WMS响应={},请求响应={}", JSON.toJSON(param), JSON.toJSON(jsonObject), JSON.toJSON(result));
                                try{
                                    String msg = "";
                                    HashMap<String, String> hashMap = new HashMap<>();
                                    hashMap.put("msg", msg);
                                    hashMap.put("sta", "31001");
                                    new HttpHandler.Builder()
                                            .setUri(wmsUrl)
                                            .setPath("/rpc/led/getError")
                                            .setJson(JSON.toJSONString(hashMap))
                                            .build()
                                            .doPost();
                                }catch (Exception e){
                                }
                            }else {
                                String msg = jsonObject.getString("msg");
                                HashMap<String, String> hashMap = new HashMap<>();
                                hashMap.put("msg", msg);
                                hashMap.put("sta", "31001");
                                new HttpHandler.Builder()
                                        .setUri(wmsUrl)
                                        .setPath("/rpc/led/getError")
                                        .setJson(JSON.toJSONString(hashMap))
                                        .build()
                                        .doPost();
                                News.error("入库申请失败,任务数据={},请求响应={}", JSON.toJSON(param), JSON.toJSON(jsonObject));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 入库-托盘进库
     */
    public synchronized void palletInbound() {
        try {
            // 根据输送线plc遍历
            List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                    .eq(Device::getDeviceType, DeviceCtgType.CONVEYOR.val())
                    .eq(Device::getStatus, 1));
            for (Device devp : list) {
                BasConveyor basConveyor = basConveyorService.getOne(new LambdaQueryWrapper<BasConveyor>().eq(BasConveyor::getDeviceId, devp.getId()).eq(BasConveyor::getHostId, devp.getHostId()));
                // 遍历入库口
                for (StaDto inSta : JSON.parseArray(basConveyor.getInSta(), StaDto.class)) {
                    BasConveyorSta basConveyorSta = basConveyorStaService.selectBySiteNo(inSta.getDeviceStaNo().toString());
                    if(basConveyorSta == null) {
                        continue;
                    }
                    // 获取入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, devp.getId().intValue());
                    StaProtocol staProtocol = devpThread.getStation().get(inSta.getDeviceStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    int workNo = staProtocol.getWorkNo().intValue();
                    // 判断是否满足入库条件
                    if (staProtocol.isAutoing()
                            && staProtocol.isLoading()
                            && staProtocol.isInEnable()
                            && (workNo >= 0)
                            && staProtocol.isPakMk()
                    ) {
                        Object object = redisUtil.get(DeviceRedisConstant.LIFT_PALLET_INBOUND + workNo);
                        if (object != null) {
                            continue;
                        }
                        Task task = taskService.getOne(new LambdaQueryWrapper<Task>()
                                .eq(Task::getDestSite, inSta.getDeviceStaNo())
                                .in(Task::getTaskSts, TaskStsType.NEW_INBOUND.sts)
                                .eq(Task::getTaskNo, workNo));
                        if (task != null) {
                            int lev = Utils.getLev(task.getDestLoc());
                            Integer targetSite = null;
                            if (lev == 1) {
                                targetSite = 31006;
                            }else if (lev == 2) {
                                targetSite = 31007;
                            }else if (lev == 3) {
                                targetSite = 31008;
                            }
                            if(targetSite == null) {
                                throw new CoolException(lev + "层站点信息不存在");
                            }
                            StaProtocol staProtocol1 = devpThread.getStation().get(targetSite);
                            if (staProtocol1 == null) {
                                break;
                            }
                            if (!staProtocol1.isAutoing()) {
                                break;
                            }
                            if (staProtocol1.isLoading()) {
                                break;
                            }
                            if (staProtocol1.getWorkNo().intValue() != 0) {
                                break;
                            }
                            LiftThread liftThread = (LiftThread) SlaveConnection.get(SlaveType.Lift, inSta.getLiftNo());
                            if (liftThread == null) {
                                break;
                            }
                            LiftProtocol liftProtocol = liftThread.getStatus();
                            if (liftProtocol == null) {
                                break;
                            }
                            if (!liftThread.isIdle()) {
                                break;
                            }
                            if (motionService.count(new LambdaQueryWrapper<Motion>()
                                    .eq(Motion::getDeviceCtg, DeviceCtgType.LIFT.val())
                                    .eq(Motion::getDevice, inSta.getLiftNo())
                                    .eq(Motion::getMotionSts, MotionStsType.EXECUTING.val())) > 0) {
                                break;
                            }
                            //检测提升机是否有任务绑定
                            boolean liftResult = Utils.checkLiftHasBinding(liftProtocol.getLiftNo(), String.valueOf(workNo));
                            if (liftResult) {
                                //存在任务,禁止执行
                                break;
                            }
                            //每次执行提升机指令都绑定提升机
                            task.setLiftNo(liftProtocol.getLiftNo());
                            task.setUpdateTime(new Date());
                            if (!taskService.updateById(task)) {
                                break;
                            }
                            List<LiftCommand> command = liftThread.getPalletInOutCommand(workNo, 1, lev, inSta.getDeviceStaNo(), targetSite, LiftCommandModeType.PALLET_INOUT);
                            LiftAssignCommand assignCommand = new LiftAssignCommand();
                            assignCommand.setLiftNo(liftProtocol.getLiftNo());
                            assignCommand.setTaskNo(workNo);
                            assignCommand.setDeviceTaskNo(liftThread.generateDeviceTaskNo(workNo, MotionCtgType.LIFT_WITH_GOODS));
                            assignCommand.setCommands(command);
                            liftAction.assignWork(liftThread.getDevice(), assignCommand);
                            redisUtil.set(DeviceRedisConstant.LIFT_PALLET_INBOUND + workNo, "send", 60 * 60 * 6);
                            break;
                        }
                    }
                }
            }
@@ -268,6 +439,11 @@
            for (Dict dict : dicts) {
                String[] split = dict.getFlag().split("-");
                int lev = Integer.parseInt(split[1]);
                Object data = redisUtil.get(DeviceRedisConstant.MAP + lev);
                if (data != null) {
                    continue;
                }
                TreeMap<Integer, List<JSONObject>> rows = new TreeMap<>();
                //排序Row
@@ -302,6 +478,17 @@
                        mapNode.setNo(object.getString("row") + "-" + object.getString("bay"));
                        mapNode.setXBase(object.getInteger("refx"));
                        mapNode.setYBase(object.getInteger("refy"));
                        if(mapNode.getValue() == MapNodeType.CONVEYOR.id) {
                            //输送线,判断小车是否可走
                            if (object.containsKey("conveyorHasGo")) {
                                if(object.getBoolean("conveyorHasGo")) {
                                    //小车可走
                                    mapNode.setValue(MapNodeType.CONVEYOR_CAR_GO.id);
                                }
                            }
                        }
                        nodes.add(mapNode);
                    }
@@ -324,7 +511,7 @@
                        //存在空缺节点,自动补足
                        for (int i = defaultBay; i < node.getBay(); i++) {
                            MapNode mapNode = new MapNode();
                            mapNode.setValue(-1);
                            mapNode.setValue(MapNodeType.DISABLE.id);
                            mapNode.setTop(1000);
                            mapNode.setBottom(1000);
                            mapNode.setLeft(1000);
@@ -351,7 +538,7 @@
                ArrayList<ArrayList<MapNode>> lists = entry.getValue();//获取地图
                MapNode mapNode = new MapNode();
                mapNode.setValue(-1);
                mapNode.setValue(MapNodeType.DISABLE.id);
                mapNode.setTop(1000);
                mapNode.setBottom(1000);
                mapNode.setLeft(1000);
@@ -405,11 +592,9 @@
                map.setUpdateTime(now);
                map.setLev(lev);
                Object data = redisUtil.get(DeviceRedisConstant.MAP + lev);
                if (data == null) {
                    //将地图数据存入redis
                    redisUtil.set(DeviceRedisConstant.MAP + lev, JSON.toJSONString(map));
                }
                //将地图数据存入redis
                redisUtil.set(DeviceRedisConstant.MAP + lev, JSON.toJSONString(map));
                redisUtil.set(DeviceRedisConstant.BASE_MAP + lev, JSON.toJSONString(map));
            }
        } catch (Exception e) {
            e.printStackTrace();
@@ -418,27 +603,182 @@
    // 解析入库工作档
    public synchronized void analyzeInBoundTask() {
        for (Task task : taskService.selectWaitAnalyzeInBoundTask()) {
            // generate motion list
            List<Motion> motionList = analyzeService.generateMotion(task);
            if (motionList.isEmpty()) {
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()));
        try {
            // 根据输送线plc遍历
            List<Device> list = deviceService.list(new LambdaQueryWrapper<Device>()
                    .eq(Device::getDeviceType, DeviceCtgType.CONVEYOR.val())
                    .eq(Device::getStatus, 1));
            for (Device devp : list) {
                BasConveyor basConveyor = basConveyorService.getOne(new LambdaQueryWrapper<BasConveyor>().eq(BasConveyor::getDeviceId, devp.getId()).eq(BasConveyor::getHostId, devp.getHostId()));
                // 遍历入库口
                for (StaDto inSta : JSON.parseArray(basConveyor.getDeviceInSta(), StaDto.class)) {
                    BasConveyorSta basConveyorSta = basConveyorStaService.selectBySiteNo(inSta.getStaNo().toString());
                    if(basConveyorSta == null) {
                        continue;
                    }
            // 更新工作主档
            task.setTaskSts(TaskStsType.ANALYZE_INBOUND.sts); // 工作状态
            task.setUpdateTime(new Date());
            if (!taskService.updateById(task)) {
                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
                    // 获取入库站信息
                    DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, devp.getId().intValue());
                    StaProtocol staProtocol = devpThread.getStation().get(inSta.getStaNo());
                    if (staProtocol == null) {
                        continue;
                    } else {
                        staProtocol = staProtocol.clone();
                    }
                    int workNo = staProtocol.getWorkNo().intValue();
                    // 判断是否满足入库条件
                    if (staProtocol.isAutoing()
                            && staProtocol.isInEnable()
                            && (workNo >= 0)
                            && staProtocol.isPakMk()
                    ) {
                        Task task = taskService.getOne(new LambdaQueryWrapper<Task>()
                                .in(Task::getTaskSts, TaskStsType.NEW_INBOUND.sts)
                                .eq(Task::getTaskNo, workNo));
                        if (task != null) {
                            if (!Cools.isEmpty(task.getLiftNo())) {
                                if (task.getLiftNo() > 0) {
                                    task.setLiftNo(0);
                                    task.setUpdateTime(new Date());
                                    if (!taskService.updateById(task)) {
                                        News.info("{}任务更新释放提升机失败", task.getTaskNo());
                                    }
                                }
                            }
                            if (Cools.isEmpty(task.getShuttleNo())) {
                                //分配小车
                                //搜索空闲车
                                ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
                                if (shuttleThread == null) {
                                    News.info("{}任务未找到空闲穿梭车", task.getTaskNo());
                                    continue;
                                }
                                task.setShuttleNo(Integer.valueOf(shuttleThread.getDevice().getDeviceNo()));//保存穿梭车号
                                task.setUpdateTime(new Date());
                                if (!taskService.updateById(task)) {
                                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                                }
                                continue;
                            }
                            // generate motion list
                            List<Motion> motionList = analyzeService.generateMotion(task);
                            if (motionList.isEmpty()) {
                                task.setShuttleNo(null);//保存穿梭车号
                                task.setUpdateTime(new Date());
                                if (!taskService.updateById(task)) {
                                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                                }
                                continue;
                            }
                            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
                            // 更新工作主档
                            task.setTaskSts(TaskStsType.ANALYZE_INBOUND.sts); // 工作状态
                            task.setUpdateTime(new Date());
                            if (!taskService.updateById(task)) {
                                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
                            }
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        for (Task task : taskService.selectWaitAnalyzeInBoundTask()) {
//            BasConveyorSta basConveyorSta = basConveyorStaService.getOne(new LambdaQueryWrapper<BasConveyorSta>().eq(BasConveyorSta::getSiteNo, task.getDestSite()));
//            if (basConveyorSta == null) {
//                continue;
//            }
//            BasConveyor basConveyor = basConveyorService.getById(basConveyorSta.getConveyorId());
//            if (basConveyor == null) {
//                continue;
//            }
////            DeviceBarcode deviceBarcode = deviceBarcodeService.getById(basConveyorSta.getBarcodeId());
////            if (deviceBarcode == null) {
////                continue;
////            }
////            BarcodeThread barcodeThread = (BarcodeThread) SlaveConnection.get(SlaveType.Barcode, Integer.parseInt(deviceBarcode.getDeviceId()));
////            if (barcodeThread == null) {
////                continue;
////            }
////            if (!barcodeThread.getBarcode().equals(task.getZpallet())) {
////                continue;
////            }
//            if (Cools.isEmpty(basConveyorSta.getTaskNo())){
//                continue;
//            }
//            if (!basConveyorSta.getTaskNo().toString().equals(task.getTaskNo())){
//                continue;
//            }
//            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, basConveyor.getDeviceId().intValue());
//            if (devpThread == null) {
//                continue;
//            }
//            StaProtocol staProtocol = devpThread.getStation().get(basConveyorSta.getSiteNo());
//            if (staProtocol == null) {
//                continue;
//            }
//            if (!(staProtocol.isAutoing()
//                    && staProtocol.isLoading()
//                    && staProtocol.isInEnable())) {
//                continue;
//            }
//
//            //解析前判断内部目标值是否有物 有工作号
//            StaProtocol staProtocolInside = devpThread.getStation().get(Utils.getStaByLev(Utils.getLev(task.getDestLoc())));
//            if (staProtocolInside == null) {
//                continue;
//            }
//            if (staProtocolInside.isLoading() || staProtocolInside.getWorkNo() !=0) {
//                continue;
//            }
//
//            if (Cools.isEmpty(task.getShuttleNo())) {
//                //分配小车
//                //搜索空闲车
//                ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
//                if (shuttleThread == null) {
//                    News.info("{}任务未找到空闲穿梭车", task.getTaskNo());
//                    continue;
//                }
//
//                task.setShuttleNo(Integer.valueOf(shuttleThread.getDevice().getDeviceNo()));//保存穿梭车号
//                task.setUpdateTime(new Date());
//                if (!taskService.updateById(task)) {
//                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
//                }
//                continue;
//            }
//
//            // generate motion list
//            List<Motion> motionList = analyzeService.generateMotion(task);
//            if (motionList.isEmpty()) {
//                continue;
//            }
//            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
//
//            // 更新工作主档
//            task.setTaskSts(TaskStsType.ANALYZE_INBOUND.sts); // 工作状态
//            task.setUpdateTime(new Date());
//            if (!taskService.updateById(task)) {
//                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
//            }
//        }
    }
    /**
     * 出库 ====>> 同一时间一台穿梭车只能有一个出库任务
     */
    public synchronized void generateOutboundWrkMast() {
    public synchronized void analyzeOutBoundTask() {
        List<Task> tasks = taskService.selectPakOut();
        if (tasks.isEmpty()) {
            return;
@@ -450,21 +790,26 @@
                continue;
            }
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, originStaObj.getConveyorId().intValue());
            BasConveyor basConveyor = basConveyorService.getById(originStaObj.getConveyorId());
            if(basConveyor == null) {
                continue;
            }
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, basConveyor.getDeviceId().intValue());
            StaProtocol staProtocol = devpThread.getStation().get(Integer.parseInt(task.getOriginSite()));//源站
            StaProtocol staProtocol1 = devpThread.getStation().get(Integer.parseInt(task.getDestSite()));//目标站
            if (staProtocol == null || staProtocol1 == null) {
//            StaProtocol staProtocol1 = devpThread.getStation().get(Integer.parseInt(task.getDestSite()));//目标站
            if (staProtocol == null) {
                continue;
            } else {
                staProtocol = staProtocol.clone();
                staProtocol1 = staProtocol1.clone();
//                staProtocol1 = staProtocol1.clone();
            }
            // 判断堆垛机出库站状态
            // 判断出库站状态
            if (staProtocol.isAutoing() && !staProtocol.isLoading() && staProtocol.getWorkNo() == 0 && staProtocol.isOutEnable()) {
                if (!(staProtocol1.isAutoing() && !staProtocol1.isLoading() && staProtocol1.getWorkNo() == 0 && staProtocol1.isOutEnable())) {
                    continue;
                }
//                if (!(staProtocol1.isAutoing() && !staProtocol1.isLoading() && staProtocol1.getWorkNo() == 0 && staProtocol1.isOutEnable())) {
//                    continue;
//                }
//                //同库位组校验
//                List<String> outerLoc = Utils.getGroupOuterLoc(wrkMast.getSourceLocNo());
@@ -474,13 +819,35 @@
//                    continue;//浅库位存在未执行任务
//                }
                if (Cools.isEmpty(task.getShuttleNo())) {
                    //分配小车
                    //搜索空闲车
                    ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
                    if (shuttleThread == null) {
                        News.info("{}任务未找到空闲穿梭车", task.getTaskNo());
                        continue;
                    }
                    task.setShuttleNo(Integer.valueOf(shuttleThread.getDevice().getDeviceNo()));//保存穿梭车号
                    task.setUpdateTime(new Date());
                    if (!taskService.updateById(task)) {
                        News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                    }
                    continue;
                }
                // generate motion list
                List<Motion> motionList = analyzeService.generateMotion(task);
                if (Cools.isEmpty(motionList)) {
                    task.setShuttleNo(null);//保存穿梭车号
                    task.setUpdateTime(new Date());
                    if (!taskService.updateById(task)) {
                        News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                    }
                    log.error("出库 ===>> 暂时没有空闲小车, 任务号={}", task.getTaskNo());
                    continue;
                }
                motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()));
                motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
                // 更新工作主档
                task.setTaskSts(TaskStsType.ANALYZE_OUTBOUND.sts); // 工作状态
@@ -493,21 +860,54 @@
        }
    }
    // 解析小车移动工作档
    public synchronized void analyzeMoveTask() {
        for (Task task : taskService.selectWaitAnalyzeMoveTask()) {
            if (Cools.isEmpty(task.getShuttleNo())) {
                //分配小车
                //搜索空闲车
                ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
                if (shuttleThread == null) {
                    News.info("{}任务未找到空闲穿梭车", task.getTaskNo());
                    continue;
                }
                task.setShuttleNo(Integer.valueOf(shuttleThread.getDevice().getDeviceNo()));//保存穿梭车号
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                }
                continue;
            }
            // generate motion list
            List<Motion> motionList = analyzeService.generateShuttleMoveMotion(task);
            if (motionList.isEmpty()) {
                task.setShuttleNo(null);//保存穿梭车号
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                }
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
            // 更新工作主档
            task.setTaskSts(TaskStsType.ANALYZE_MOVE.sts); // 工作状态
            task.setUpdateTime(new Date());
            if (!taskService.updateById(task)) {
                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
            }
        }
    }
    /**
     * 四向穿梭车电量检测 ===>> 发起充电
     */
    public synchronized void loopShuttleCharge() {
        // 获取充电桩库位类型
        LocCtg locCtg = locCtgService.getOne(new LambdaQueryWrapper<LocCtg>()
                .eq(LocCtg::getFlag, "CHARGE")
                .eq(LocCtg::getStatus, 1));
        if (locCtg == null) {
            return;
        }
        //获取充电任务类型
        TaskCtg taskCtg = taskCtgService.getOne(new LambdaQueryWrapper<TaskCtg>()
                .eq(TaskCtg::getFlag, "CHARGE")
                .eq(TaskCtg::getFlag, String.valueOf(TaskCtgType.CHARGE))
                .eq(TaskCtg::getStatus, 1));
        if (taskCtg == null) {
            return;
@@ -539,20 +939,19 @@
            String currentLocNo = shuttleProtocol.getCurrentLocNo();
            int lev = Utils.getLev(currentLocNo);//获取小车楼层
            //搜索小车当前楼层充电桩
            ArrayList<Loc> allChargeLoc = new ArrayList<>();
            List<Loc> list1 = locService.list(new LambdaQueryWrapper<Loc>()
                    .eq(Loc::getLocCtg, locCtg.getId())
                    .eq(Loc::getStatus, 1)
                    .eq(Loc::getLev, lev));
            ArrayList<ShuttleChargeSta> allChargeLoc = new ArrayList<>();
            List<ShuttleChargeSta> list1 = shuttleChargeStaService.list(new LambdaQueryWrapper<ShuttleChargeSta>()
                    .eq(ShuttleChargeSta::getDeviceLev, lev)
                    .eq(ShuttleChargeSta::getStatus, 1));
            if (!list1.isEmpty()) {
                allChargeLoc.addAll(list1);
            }
            //搜索其他楼层充电桩
            List<Loc> list2 = locService.list(new LambdaQueryWrapper<Loc>()
                    .eq(Loc::getLocCtg, locCtg.getId())
                    .eq(Loc::getStatus, 1)
                    .notIn(Loc::getLev, lev));
            List<ShuttleChargeSta> list2 = shuttleChargeStaService.list(new LambdaQueryWrapper<ShuttleChargeSta>()
                    .notIn(ShuttleChargeSta::getDeviceLev, lev)
                    .eq(ShuttleChargeSta::getStatus, 1));
            if (!list2.isEmpty()) {
                allChargeLoc.addAll(list2);
            }
@@ -563,24 +962,34 @@
            }
            //选择空闲充电桩
            Loc chargeLoc = null;
            for (Loc loc : allChargeLoc) {
            ShuttleChargeSta chargeSta = null;
            for (ShuttleChargeSta shuttleChargeSta : allChargeLoc) {
                // 判断充电位是否被占用(车辆位置)
                if (Utils.hasShuttleInLoc(loc.getLocNo(), device.getId())) {
                if (Utils.hasShuttleInLoc(shuttleChargeSta.getDeviceLoc(), device.getId())) {
                    continue;
                }
                // 盘点充电位是否存在任务档
                List<Task> tasks = taskService.hasChargeInLoc(loc.getLocNo());
                List<Task> tasks = taskService.hasChargeInLoc(shuttleChargeSta.getDeviceLoc());
                if (!tasks.isEmpty()) {
                    continue;
                }
                chargeLoc = loc;
                Loc loc = locService.getOne(new LambdaQueryWrapper<Loc>()
                        .eq(Loc::getLocNo, shuttleChargeSta.getDeviceLoc()));
                if (loc == null) {
                    continue;
                }
                if (!loc.getLocSts().equals(LocStsType.C.val())) {
                    continue;//库位不是充电桩
                }
                chargeSta = shuttleChargeSta;
                break;
            }
            if (chargeLoc == null) {
            if (chargeSta == null) {
                continue;//未找到充电桩
            }
@@ -601,7 +1010,7 @@
                continue;
            }
            String chargeLocNo = chargeLoc.getLocNo();
            String chargeLocNo = chargeSta.getDeviceLoc();
            Task task = new Task();
            task.setUuid(String.valueOf(snowflakeIdWorker.nextId()));
            task.setTaskNo(String.valueOf(Utils.getTaskNo("CHARGE")));
@@ -625,7 +1034,7 @@
                News.error("保存{}号四向穿梭车充电任务失败!!!", device.getDeviceNo());
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()));
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
            task.setTaskSts(TaskStsType.ANALYZE_CHARGE.sts);
            if (!taskService.save(task)) {
@@ -641,17 +1050,9 @@
     * 四向穿梭车电量检测 ===>> 满电后回到待机位
     */
    public synchronized void loopShuttleToStandbyCauseCharge() {
        Integer enoughPower = 90;
        Dict dict = dictService.getOne(new LambdaQueryWrapper<Dict>()
                .eq(Dict::getFlag, "chargeMaxValue")
                .eq(Dict::getStatus, 1));
        if (dict != null) {
            enoughPower = Integer.parseInt(dict.getValue());
        }
        //获取迁移任务类型
        TaskCtg taskCtg = taskCtgService.getOne(new LambdaQueryWrapper<TaskCtg>()
                .eq(TaskCtg::getFlag, "MOVE")
                .eq(TaskCtg::getFlag, String.valueOf(TaskCtgType.MOVE))
                .eq(TaskCtg::getStatus, 1));
        if (taskCtg == null) {
            return;
@@ -694,8 +1095,21 @@
                continue;
            }
            ShuttleChargeSta chargeSta = shuttleChargeStaService.getOne(new LambdaQueryWrapper<ShuttleChargeSta>()
                    .eq(ShuttleChargeSta::getDeviceLoc, chargeTask.getDestLoc())
                    .eq(ShuttleChargeSta::getStatus, 1));
            if (chargeSta == null) {
                continue;//充电桩不存在,异常
            }
            String standbyLoc = chargeSta.getStandbyLoc();
            if (standbyLoc == null) {
                continue;//避让位置数据不存在
            }
            List<String> availableLoc = JSON.parseArray(standbyLoc, String.class);
            //获取避让位置
            String standByLocNo = shuttleDispatcher.searchStandByLocNo(Integer.valueOf(device.getDeviceNo()), device.getHostId(), shuttleThread.getStatus().getCurrentLocNo());
            String standByLocNo = shuttleDispatcher.searchAvailableLocNo(Integer.valueOf(device.getDeviceNo()), device.getHostId(), shuttleThread.getStatus().getCurrentLocNo(), availableLoc);
            Task task = new Task();
            task.setUuid(String.valueOf(snowflakeIdWorker.nextId()));
@@ -720,7 +1134,7 @@
                News.error("保存{}号四向穿梭车迁移任务失败!!!", device.getDeviceNo());
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()));
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
            task.setTaskSts(TaskStsType.ANALYZE_MOVE.sts);
@@ -749,8 +1163,14 @@
                    .eq(BasLed::getDeviceId, ledDevice.getId()));
            List<Integer> staArr = JSON.parseArray(led.getSta(), Integer.class);
            BasConveyor basConveyor = basConveyorService.getOne(new LambdaQueryWrapper<BasConveyor>()
                    .eq(BasConveyor::getDeviceId, led.getConveyorId().intValue()));
            if (basConveyor == null) {
                continue;
            }
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, led.getConveyorId().intValue());
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, basConveyor.getDeviceId().intValue());
            // 命令集合
            List<LedCommand> commands = new ArrayList<>();
            // 工作档集合
@@ -758,26 +1178,26 @@
            for (Integer staNo : staArr) {
                // 获取叉车站点
                StaProtocol staProtocol = devpThread.getStation().get(staNo);
                if (null == staProtocol || null == staProtocol.getWorkNo() || 0 == staProtocol.getWorkNo() || !staProtocol.isLoading()) {
                if (null == staProtocol || null == staProtocol.getWorkNo()) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                // 获取工作档数据
                Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskNo, staProtocol.getWorkNo()));
                if (null == task) {
                    continue;
                }
                String taskNo = "0";
                if (staProtocol.isOutEnable() && staProtocol.getSiteId() == 31001 ){
                    if (0 != staProtocol.getWorkNo()){
                        Motion motion = motionService.getOne(new LambdaQueryWrapper<Motion>().eq(Motion::getMotionCtg, 9).eq(Motion::getTemp, staProtocol.getWorkNo()));
                        // 获取工作档数据
                        Task task = taskService.getOne(new LambdaQueryWrapper<Task>().eq(Task::getTaskNo, motion.getTaskNo()));
                        if (null == task) {
                            continue;
                        }
                        taskNo = task.getWmsTaskNo();
                    }
                tasks.add(task);
                // 组装命令
                LedCommand ledCommand = new LedCommand();
                ledCommand.setWorkNo(task.getTaskNo());
                ledCommand.setIoType(task.getTaskCtg().intValue());
                ledCommand.setTitle(task.getTaskCtg$());
                ledCommand.setSourceLocNo(task.getOriginLoc());
                ledCommand.setLocNo(task.getDestLoc());
                ledCommand.setStaNo(Integer.parseInt(task.getDestSite()));
                } else if (staProtocol.isInEnable() && staProtocol.getSiteId() == 31002) {
                    taskNo = staProtocol.getStaNo().toString();
                }
                try {
                    //获取WMS地址
@@ -786,32 +1206,24 @@
                        String wmsUrl = dict.getValue();
                        HashMap<String, Object> param = new HashMap<>();
                        param.put("taskNo", task.getTaskNo());
                        param.put("taskNo", taskNo);
                        param.put("sta",staNo);
                        String response = new HttpHandler.Builder()
                                .setUri(wmsUrl)
                                .setPath("/queryTask")
                                .setPath("/rpc/led/getTask")
                                .setJson(JSON.toJSONString(param))
                                .build()
                                .doPost();
                        JSONObject jsonObject = JSON.parseObject(response);
                        Integer code = jsonObject.getInteger("code");
                        if (code.equals(200)) {
                            List<MatDto> matDtos = JSON.parseArray(jsonObject.getString("data"), MatDto.class);
                            ledCommand.setMatDtos(matDtos);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                commands.add(ledCommand);
            }
            // 获取LED线程
            LedThread ledThread = (LedThread) SlaveConnection.get(SlaveType.Led, ledDevice.getId().intValue());
            // 命令下发 -------------------------------------------------------------------------------
            if (!commands.isEmpty()) {
                ledThread.write(commands);
                ledThread.setLedMk(false);
            }
        }
    }
@@ -830,8 +1242,13 @@
                    .eq(BasLed::getDeviceId, ledDevice.getId()));
            List<Integer> staArr = JSON.parseArray(led.getSta(), Integer.class);
            BasConveyor basConveyor = basConveyorService.getById(led.getConveyorId().intValue());
            if (basConveyor == null) {
                continue;
            }
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, led.getConveyorId().intValue());
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, basConveyor.getDeviceId().intValue());
            // 命令集合
            boolean reset = true;
            for (Integer staNo : staArr) {
@@ -860,8 +1277,13 @@
                    .eq(BasLed::getDeviceId, ledDevice.getId()));
            List<Integer> staArr = JSON.parseArray(led.getSta(), Integer.class);
            BasConveyor basConveyor = basConveyorService.getById(led.getConveyorId());
            if (basConveyor == null) {
                continue;
            }
            // 获取输送线plc线程
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, led.getConveyorId().intValue());
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Conveyor, basConveyor.getDeviceId().intValue());
            // 命令集合
            boolean reset = true;
            for (Integer staNo : staArr) {
@@ -883,4 +1305,39 @@
        }
    }
    // 解析小车载货移动工作档
    public synchronized void analyzeLadenMoveTask() {
        for (Task task : taskService.selectWaitAnalyzeLadenMoveTask()) {
            if (Cools.isEmpty(task.getShuttleNo())) {
                //分配小车
                //搜索空闲车
                ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
                if (shuttleThread == null) {
                    News.info("{}任务未找到空闲穿梭车", task.getTaskNo());
                    continue;
                }
                task.setShuttleNo(Integer.valueOf(shuttleThread.getDevice().getDeviceNo()));//保存穿梭车号
                task.setUpdateTime(new Date());
                if (!taskService.updateById(task)) {
                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                }
                continue;
            }
            // generate motion list
            List<Motion> motionList = analyzeService.generateShuttleLadenMoveMotion(task);
            if (motionList.isEmpty()) {
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
            // 更新工作主档
            task.setTaskSts(TaskStsType.ANALYZE_LADEN_MOVE.sts); // 工作状态
            task.setUpdateTime(new Date());
            if (!taskService.updateById(task)) {
                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
            }
        }
    }
}