#
Junjie
2025-04-02 664b83b2e10025a36b29ad086f68c66cb80e339c
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/service/impl/MainServiceImpl.java
@@ -8,25 +8,16 @@
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.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;
import com.zy.asrs.wcs.core.utils.*;
import com.zy.asrs.wcs.rcs.News;
import com.zy.asrs.wcs.rcs.cache.SlaveConnection;
import com.zy.asrs.wcs.rcs.constant.DeviceRedisConstant;
@@ -34,7 +25,6 @@
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;
@@ -45,7 +35,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import java.util.*;
/**
@@ -95,6 +84,10 @@
    private OpenUtils openUtils;
    @Autowired
    private LiftAction liftAction;
    @Autowired
    private LiftDispatcher liftDispatcher;
    @Autowired
    private ShuttleStandbyService shuttleStandbyService;
    /**
     * 组托
@@ -172,6 +165,11 @@
                            && staProtocol.isPakMk()
                    ) {
                        Object object = redisUtil.get(DeviceRedisConstant.LIFT_PALLET_INBOUND + inSta.getStaNo());
                        if (object != null) {
                            continue;
                        }
                        // 获取条码扫描仪信息
                        DeviceBarcode deviceBarcode = deviceBarcodeService.getById(basConveyorSta.getBarcodeId());
                        if(deviceBarcode == null) {
@@ -184,7 +182,6 @@
                        String barcode = barcodeThread.getBarcode();
                        if (!Cools.isEmpty(barcode)) {
                            News.info("{}号条码扫描器检测条码信息:{}", deviceBarcode.getId(), barcode);
                        }
                        // 判断重复工作档
@@ -228,37 +225,38 @@
                            if (code.equals(200)) {
                                StartupDto dto = jsonObject.getObject("data", StartupDto.class);
                                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));
                                String wcsLocNo = Utils.getLocNo(row, bay, lev);
//                                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));
//                                String wcsLocNo = Utils.getLocNo(row, bay, lev);
//
//                                CreateInTaskParam createInTaskParam = new CreateInTaskParam();
//                                createInTaskParam.setTaskNo(String.valueOf(dto.getWorkNo()));
//                                createInTaskParam.setDestLoc(wcsLocNo);
//                                createInTaskParam.setOriginSite(dto.getSourceStaNo().toString());
//                                createInTaskParam.setDestSite(dto.getStaNo().toString());
//                                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", inSta.getStaNo().toString());
//                                    new HttpHandler.Builder()
//                                            .setUri(wmsUrl)
//                                            .setPath("/rpc/led/getError")
//                                            .setJson(JSON.toJSONString(hashMap))
//                                            .build()
//                                            .doPost();
//                                }catch (Exception e){
//
//                                }
                                CreateInTaskParam createInTaskParam = new CreateInTaskParam();
                                createInTaskParam.setTaskNo(String.valueOf(dto.getWorkNo()));
                                createInTaskParam.setDestLoc(wcsLocNo);
                                createInTaskParam.setOriginSite(dto.getSourceStaNo().toString());
                                createInTaskParam.setDestSite(dto.getStaNo().toString());
                                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", inSta.getStaNo().toString());
                                    new HttpHandler.Builder()
                                            .setUri(wmsUrl)
                                            .setPath("/rpc/led/getError")
                                            .setJson(JSON.toJSONString(hashMap))
                                            .build()
                                            .doPost();
                                }catch (Exception e){
                                }
                                redisUtil.set(DeviceRedisConstant.LIFT_PALLET_INBOUND + inSta.getStaNo(), "in", 10);
                            }else {
                                String msg = jsonObject.getString("msg");
                                HashMap<String, String> hashMap = new HashMap<>();
@@ -275,134 +273,6 @@
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        } 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;
                        }
                    }
                }
            }
@@ -633,20 +503,46 @@
                                }
                            }
                            //获取距离目标位置最近的可换层提升机(可能不空闲)
                            LiftThread liftThread = liftDispatcher.searchLift(task.getDestLoc(), task.getHostId(), true);
                            if (liftThread == null) {
                                continue;
                            }
                            Device transferLiftDevice = liftThread.getDevice();
                            //获取小车待机库位 ==> 进提升机
                            ShuttleStandby shuttleStandbyTo = shuttleStandbyService.getOne(new LambdaQueryWrapper<ShuttleStandby>()
                                    .eq(ShuttleStandby::getDeviceId, transferLiftDevice.getId())
                                    .eq(ShuttleStandby::getDeviceLev, Utils.getLev(task.getDestLoc()))
                                    .eq(ShuttleStandby::getStatus, 1));
                            //穿梭车进提升机库位号
                            String liftLocNoTo = shuttleStandbyTo.getDeviceLoc();
                            //穿梭车进提升机待机位库位号  输送线位置
                            String standbyLocNoTo = shuttleStandbyTo.getDeviceStandbyLoc();
                            if (Cools.isEmpty(task.getShuttleNo())) {
                                //分配小车
                                //搜索空闲车
                                ShuttleThread shuttleThread = shuttleDispatcher.searchIdleShuttle(task);
                                if (shuttleThread == null) {
                                //调度空闲车去取货待机位
                                Device shuttleDevice = shuttleDispatcher.dispatchShuttle(task, standbyLocNoTo);
                                if (shuttleDevice == null) {
                                    News.info("{}任务未找到空闲穿梭车", task.getTaskNo());
                                    continue;
                                }
                                task.setShuttleNo(Integer.valueOf(shuttleThread.getDevice().getDeviceNo()));//保存穿梭车号
                                task.setShuttleNo(Integer.valueOf(shuttleDevice.getDeviceNo()));//保存穿梭车号
                                task.setUpdateTime(new Date());
                                if (!taskService.updateById(task)) {
                                    News.info("{}任务更新穿梭车号失败", task.getTaskNo());
                                }
                                continue;
                            }
                            //判断小车是否到达取货待机位置
                            String shuttleLocNo = shuttleDispatcher.findShuttleLocNo(task.getShuttleNo(), task.getHostId());
                            if (shuttleLocNo == null) {
                                continue;
                            }
                            if (!standbyLocNoTo.equals(shuttleLocNo)) {
                                continue;
                            }
@@ -838,23 +734,6 @@
    // 解析小车移动工作档
    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()) {
@@ -864,6 +743,54 @@
            // 更新工作主档
            task.setTaskSts(TaskStsType.ANALYZE_MOVE.sts); // 工作状态
            task.setUpdateTime(new Date());
            if (!taskService.updateById(task)) {
                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
            }
        }
    }
    // 解析小车充电工作档
    public synchronized void analyzeChargeTask() {
        List<Task> list = taskService.list(new LambdaQueryWrapper<Task>().eq(Task::getTaskSts, TaskStsType.NEW_CHARGE.sts));
        for (Task task : list) {
            String locNo = task.getDestLoc();
            Device shuttleDevice = deviceService.getOne(new LambdaQueryWrapper<Device>()
                    .eq(Device::getDeviceNo, task.getShuttleNo())
                    .eq(Device::getDeviceType, DeviceCtgType.SHUTTLE.val())
                    .eq(Device::getHostId, task.getHostId())
                    .eq(Device::getStatus, 1));
            if (shuttleDevice == null) {
                continue;
            }
            ShuttleThread shuttleThread = (ShuttleThread) SlaveConnection.get(SlaveType.Shuttle, shuttleDevice.getId().intValue());
            if (shuttleThread == null) {
                continue;
            }
            ShuttleProtocol shuttleProtocol = shuttleThread.getStatus();
            if (shuttleProtocol == null || shuttleProtocol.getShuttleNo() == null) {
                continue;
            }
            String shuttleLocNo = task.getOriginLoc();
            if (Utils.getLev(locNo) != Utils.getLev(shuttleLocNo)) {
                shuttleDispatcher.dispatchShuttle(task, task.getDestLoc());
                continue;
            }
            // generate motion list
            List<Motion> motionList = analyzeService.generateChargeMotion(task);
            if (Cools.isEmpty(motionList)) {
                News.error("保存{}号四向穿梭车充电任务失败!!!", task.getTaskNo());
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
            // 更新工作主档
            task.setTaskSts(TaskStsType.ANALYZE_CHARGE.sts);
            task.setUpdateTime(new Date());
            if (!taskService.updateById(task)) {
                News.error("更新工作档失败!!! [工作号:{}]", task.getTaskNo());
@@ -998,15 +925,6 @@
            task.setMemo("charge");
            task.setShuttleNo(Integer.valueOf(device.getDeviceNo()));
            // generate motion list
            List<Motion> motionList = analyzeService.generateChargeMotion(task);
            if (Cools.isEmpty(motionList)) {
                News.error("保存{}号四向穿梭车充电任务失败!!!", device.getDeviceNo());
                continue;
            }
            motionService.batchInsert(motionList, task.getUuid(), Integer.valueOf(task.getTaskNo()), task.getHostId());
            task.setTaskSts(TaskStsType.ANALYZE_CHARGE.sts);
            if (!taskService.save(task)) {
                News.error("保存{}号四向穿梭车充电任务失败!!!", device.getDeviceNo());
                continue;
@@ -1088,7 +1006,7 @@
            task.setTaskCtg(taskCtg.getId());
            task.setPriority(10);
            task.setOriginSite(null);
            task.setOriginLoc(null);
            task.setOriginLoc(shuttleProtocol.getCurrentLocNo());
            task.setDestSite(null);
            task.setDestLoc(standByLocNo); // 避让位置
            task.setIoTime(new Date());