zyh
昨天 e98d4b1133ed058bfe1e6af75be9975aebfc32d8
src/main/java/com/zy/asrs/service/impl/RcsServiceImpl.java
@@ -1,9 +1,11 @@
package com.zy.asrs.service.impl;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.zy.asrs.entity.*;
import com.zy.asrs.enums.RcsRetMethodEnum;
import com.zy.asrs.service.AgvTaskService;
@@ -33,9 +35,19 @@
@Slf4j
@Service
public class RcsServiceImpl implements RcsService {
    @Value("${mes.url}")
    public String MES_URL;
    // 海康RCS地址
    @Value("${hik.url}")
    private String HIK_URL;
    // 人员入侵系统检查
    @Value("${hik.renyuan}")
    private String HIK_RENYUAN;
    // 不需要发送end的站点
    @Value("${hik.station}")
    private String HIK_STATION;
    @Autowired
    private AgvTaskService taskService;
@@ -65,23 +77,29 @@
        String slotCategory = values.getString("slotCategory");
        String slotCode = values.getString("slotCode");
        EntityWrapper<AgvTask> wrapper = new EntityWrapper<>();
        wrapper.eq("task_no", robotTaskCode);
        // 修改后的逻辑:有"-"则去掉最后一个"-"及后面内容,没有则保持原样
        String taskNo = rcsReporterTask.getRobotTaskCode();
        if (taskNo.contains("-")) {
            taskNo = taskNo.substring(0, taskNo.lastIndexOf("-"));
        }
        wrapper.eq("task_no", taskNo);
        AgvTask task = taskService.selectOne(wrapper);
        if (!Cools.isEmpty(task)) {
            try {
                switch (Objects.requireNonNull(RcsRetMethodEnum.getEnum(method))) {
                    //放货申请
                    case APPLY_PUT: {
                        Integer sourceStaNo = Integer.valueOf(task.getStaNo());
                        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                        //wcs反馈rcs继续执行
                        if (staProtocol != null && !staProtocol.isLoading() && !staProtocol.isEmptyOutType()) {
                                if(task.getTaskType().equals("AGV补空料架")){
                        if (task.getStaNo().equals("401") || task.getStaNo().equals("402") || task.getStaNo().equals("307")) {
                            Integer sourceStaNo = Integer.valueOf(task.getStaNo());
                            SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                            StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                            //wcs反馈rcs继续执行
                            if (staProtocol != null && !staProtocol.isLoading() && !staProtocol.isEmptyOutType()) {
                                if (task.getTaskType().equals("AGV补空料架")) {
                                    RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                                    rcsTaskContinue.setRobotTaskCode(task.getTaskNo()+"-"+(task.getCtnType()-1));
                                    rcsTaskContinue.setRobotTaskCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                                    rcsTaskContinue.setTriggerType("TASK");
                                    rcsTaskContinue.setTriggerCode(task.getTaskNo()+"-"+(task.getCtnType()-1));
                                    rcsTaskContinue.setTriggerCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                                    String url = HIK_URL + "api/robot/controller/task/extend/continue";
                                    String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
@@ -94,7 +112,7 @@
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                }else {
                                } else {
                                    //出发PLC站点的扫码器扫码
                                    boolean result = MessageQueue.offer(SlaveType.Devp, 1, new com.zy.core.model.Task(3, staProtocol));
                                    if (result) {
@@ -106,9 +124,10 @@
                                        data.put("robotTaskCode", robotTaskCode);
                                        rcsReturn.setData(data);
                                        RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                                        rcsTaskContinue.setRobotTaskCode(task.getTaskNo()+"-"+(task.getCtnType()-1));
                                        rcsTaskContinue.setAgvFactory("1");
                                        rcsTaskContinue.setRobotTaskCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                                        rcsTaskContinue.setTriggerType("TASK");
                                        rcsTaskContinue.setTriggerCode(task.getTaskNo()+"-"+(task.getCtnType()-1));
                                        rcsTaskContinue.setTriggerCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                                        String url = HIK_URL + "api/robot/controller/task/extend/continue";
                                        String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
@@ -124,36 +143,38 @@
                                        rcsReturn.setData(data);
                                    }
                                }
                        }
                    }
                    break;
                            }
                        } else {
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(task.getStaNo());
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            int i = applyInStation(transParent);
                    //放货完成 --》agv已经离开
                    case TASK_END: {
                        Integer sourceStaNo = Integer.valueOf(task.getStaNo());
                        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                        Boolean boo=false;
                        Short wrkNo = 9991;
                        //放货完成 --》agv已经离开 --》给PLC站点写9991工作号
                        //补空料架 --》agv已经立库 --》给PLC站点写9995工作号
                        if(task.getTaskType().equals("AGV补空料架") && staProtocol != null && !staProtocol.isLoading() && staProtocol.isEmptyOutType() && staProtocol.getWorkNo() == 0){
                            boo=true;
                            wrkNo = 9995;
                        }else if(!task.getTaskType().equals("AGV补空料架") && staProtocol != null && staProtocol.isLoading() && staProtocol.isEmptyOutType() && staProtocol.getWorkNo() == 0){
                            boo=true;
                        }
                        if (boo) {
                            staProtocol.setWorkNo(wrkNo);//空料架到位信号点
                            staProtocol.setStaNo(Short.valueOf("0"));
                            boolean result = MessageQueue.offer(SlaveType.Devp, 1, new com.zy.core.model.Task(2, staProtocol));
                            log.info("AGV放货完成,给站点写9991工作号,下发任务:{},站点:{},agv任务号:{}", result, task.getStaNo(), task.getTaskNo());
                            if (result) {
                                // 更新任务状态等内部逻辑
                                task.setWrkSts(304L);   // 301 任务下发、302 任务执行、303 任务中断、304 任务结束
                                task.setModiTime(new Date());
                                taskService.updateById(task);
                                // 返回RCS
                            if (HIK_RENYUAN.contains(task.getStaNo())){
                                // 延迟处理,确保到站完成信号优先处理
                                try {
                                    Thread.sleep(20000); // 睡眠20秒
                                } catch (InterruptedException e)
                                {
                                    Thread.currentThread().interrupt();
                                }
                            }
                            if (i == 1) {
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
@@ -167,34 +188,224 @@
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }else {
                        }
                    }
                    break;
                    //放货完成 --》agv已经离开
                    case TASK_END: {
                        if (task.getIsPda()!=null && task.getIsPda().equals("Y")){
                            // 更新任务状态等内部逻辑
                            task.setWrkSts(305L);   // 301 任务下发、302 任务执行、303 任务中断、304 任务结束
                            task.setModiTime(new Date());
                            taskService.updateById(task);
                            // 返回RCS
                            rcsReturn.setCode("Err_Internal");
                            rcsReturn.setCode("SUCCESS");
                            rcsReturn.setMessage("");
                            JSONObject data = new JSONObject();
                            data.put("robotTaskCode", robotTaskCode);
                            rcsReturn.setData(data);
                        }
                        if (task.getStaNo().equals("Q-E2") || task.getStaNo().equals("Q-E3") || HIK_STATION.contains(task.getStaNo())){
                            // 更新任务状态等内部逻辑
                            task.setWrkSts(304L);   // 301 任务下发、302 任务执行、303 任务中断、304 任务结束
                            task.setModiTime(new Date());
                            taskService.updateById(task);
                            // 返回RCS
                            rcsReturn.setCode("SUCCESS");
                            rcsReturn.setMessage("");
                            JSONObject data = new JSONObject();
                            data.put("robotTaskCode", robotTaskCode);
                            rcsReturn.setData(data);
                        }
                         else if (task.getStaNo().equals("401") || task.getStaNo().equals("402") || task.getStaNo().equals("307")) {
                            Integer sourceStaNo = Integer.valueOf(task.getStaNo());
                            SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                            StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                            Boolean boo = false;
                            Short wrkNo = 9991;
                            if (task.getStaNo().equals("307")) {
                                wrkNo = 9997;
                            }
                            //放货完成 --》agv已经离开 --》给PLC站点写9991工作号
                            //补空料架 --》agv已经立库 --》给PLC站点写9995工作号
                            if (task.getTaskType().equals("AGV补空料架") && staProtocol != null && !staProtocol.isLoading() && staProtocol.isEmptyOutType() && staProtocol.getWorkNo() == 0) {
                                boo = true;
                                wrkNo = 9998;
                            } else if (!task.getTaskType().equals("AGV补空料架") && staProtocol != null && staProtocol.isLoading() && staProtocol.isEmptyOutType()) {
                                boo = true;
                            }
                            if (boo) {
                                staProtocol.setWorkNo(wrkNo);//空料架到位信号点
                                staProtocol.setStaNo(Short.valueOf("0"));
                                boolean result = MessageQueue.offer(SlaveType.Devp, 1, new com.zy.core.model.Task(2, staProtocol));
                                log.info("AGV放货完成,给站点写9991工作号,下发任务:{},站点:{},agv任务号:{}", result, task.getStaNo(), task.getTaskNo());
                                if (result) {
                                    // 更新任务状态等内部逻辑
                                    task.setWrkSts(304L);   // 301 任务下发、302 任务执行、303 任务中断、304 任务结束
                                    task.setModiTime(new Date());
                                    taskService.updateById(task);
                                    // 返回RCS
                                    rcsReturn.setCode("SUCCESS");
                                    rcsReturn.setMessage("");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                } else {
                                    // 返回RCS
                                    rcsReturn.setCode("Err_Internal");
                                    rcsReturn.setMessage("");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                }
                            } else {
                                // 返回RCS
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        } else {
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(task.getStaNo());
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            //放货离开站点
                            int i = AGVDepartureCompleted(transParent);
                            if (i == 1) {
                                // 更新任务状态等内部逻辑
                                task.setWrkSts(304L);   // 301 任务下发、302 任务执行、303 任务中断、304 任务结束
                                task.setModiTime(new Date());
                                taskService.updateById(task);
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            } else {
                                // 返回RCS
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }
                    }
                    break;
                    //rcs请求wms取货申请
                    case APPLY_PICK: {
                        Integer sourceStaNo = Integer.valueOf(task.getSourceStaNo());
                        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                        //判断站点是否有料架和托盘
                        if (staProtocol != null && (staProtocol.isLoading()||task.getTaskType().equals("AGV空料架回缓存区"))
                                && staProtocol.isEmptyOutType()) {
                            RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                            rcsTaskContinue.setRobotTaskCode(task.getTaskNo()+"-"+(task.getCtnType()-1));
                            rcsTaskContinue.setTriggerType("TASK");
                            rcsTaskContinue.setTriggerCode(task.getTaskNo()+"-"+(task.getCtnType()-1));
                        if (task.getSourceStaNo().equals("401") || task.getSourceStaNo().equals("402") || task.getSourceStaNo().equals("307")) {
                            Integer sourceStaNo = Integer.valueOf(task.getSourceStaNo());
                            SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                            StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                            //判断站点是否有料架和托盘
                            if (staProtocol != null && (staProtocol.isLoading() || task.getTaskType().equals("AGV空料架回缓存区"))
                                    && staProtocol.isEmptyOutType()) {
                                RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                                rcsTaskContinue.setRobotTaskCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                                rcsTaskContinue.setTriggerType("TASK");
                                rcsTaskContinue.setTriggerCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            String url = HIK_URL + "api/robot/controller/task/extend/continue";
                            String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
                            if (!StringUtils.isEmpty(response) && response.contains("code")) {
                                RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
                                if ("200".equals(rcsReturn1.getCode())) {
                                String url = HIK_URL + "api/robot/controller/task/extend/continue";
                                String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
                                if (!StringUtils.isEmpty(response) && response.contains("code")) {
                                    RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
                                    if ("SUCCESS".equals(rcsReturn1.getCode())) {
                                        // 返回RCS
                                        rcsReturn.setCode("SUCCESS");
                                        rcsReturn.setMessage("");
                                        JSONObject data = new JSONObject();
                                        data.put("robotTaskCode", robotTaskCode);
                                        rcsReturn.setData(data);
                                    } else {
                                        // 返回RCS
                                        rcsReturn.setCode("Err_Internal");
                                        rcsReturn.setMessage("");
                                        JSONObject data = new JSONObject();
                                        data.put("robotTaskCode", robotTaskCode);
                                        rcsReturn.setData(data);
                                    }
                                }
                            }
                        } else {
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(task.getSourceStaNo());
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            int i = applyInStation(transParent);
                            if (HIK_RENYUAN.contains(task.getStaNo())){
                                // 延迟处理,确保到站完成信号优先处理
                                try {
                                    Thread.sleep(20000); // 睡眠20秒
                                } catch (InterruptedException e)
                                {
                                    Thread.currentThread().interrupt();
                                }
                            }
                            if (i == 1) {
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            } else {
                                // 返回RCS
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }
                    }
                    break;
                    //rcs取货完成,已退出输送线
                    case PICK_COMPLETE: {
                        if (task.getSourceStaNo().equals("401") || task.getSourceStaNo().equals("402") || task.getSourceStaNo().equals("307")) {
                            Integer sourceStaNo = Integer.valueOf(task.getSourceStaNo());
                            SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                            StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                            //放货完成 --》agv已经离开 --》给PLC站点写9991工作号
                            if (staProtocol != null && !staProtocol.isLoading() && !staProtocol.isEmptyOutType()) {
                                staProtocol.setWorkNo((short) 0);
                                staProtocol.setStaNo((short) 0);
                                boolean result = MessageQueue.offer(SlaveType.Devp, 1, new com.zy.core.model.Task(2, staProtocol));
                                log.info("AGV取货完成,给站点写0工作号,下发任务:{},站点:{},agv任务号:{}", result, task.getStaNo(), task.getTaskNo());
                                if (result) {
                                    // 返回RCS
                                    rcsReturn.setCode("SUCCESS");
                                    rcsReturn.setMessage("");
@@ -210,22 +421,27 @@
                                    rcsReturn.setData(data);
                                }
                            }
                        }
                    }
                    break;
                    //rcs取货完成,已退出输送线
                    case PICK_COMPLETE: {
                        Integer sourceStaNo = Integer.valueOf(task.getSourceStaNo());
                        SiemensDevpThread siemensDevpThread = (SiemensDevpThread) SlaveConnection.get(SlaveType.Devp, 1);
                        StaProtocol staProtocol = siemensDevpThread.getStation().get(sourceStaNo).clone();
                        //放货完成 --》agv已经离开 --》给PLC站点写9991工作号
                        if (staProtocol != null && !staProtocol.isLoading() && !staProtocol.isEmptyOutType()) {
                            staProtocol.setWorkNo((short) 0);
                            staProtocol.setStaNo((short) 0);
                            boolean result = MessageQueue.offer(SlaveType.Devp, 1, new com.zy.core.model.Task(2, staProtocol));
                            log.info("AGV取货完成,给站点写0工作号,下发任务:{},站点:{},agv任务号:{}", result, task.getStaNo(), task.getTaskNo());
                            if (result) {
                                // 返回RCS
                        } else {
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(task.getSourceStaNo());
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            //取货离开站点
                            int i = AGVDepartureCompleted(transParent);
                            if (i == 1) {
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
@@ -239,10 +455,205 @@
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }
                    }
                    break;
                    //AGV取货离站请求
                    case APPLY_IN_OFF_STATION: {
                        //如果是立库站点直接允许离站
                        if (task.getSourceStaNo().equals("401") || task.getSourceStaNo().equals("402") || task.getSourceStaNo().equals("307")) {
                            RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                            rcsTaskContinue.setRobotTaskCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            rcsTaskContinue.setTriggerType("TASK");
                            rcsTaskContinue.setTriggerCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            String url = HIK_URL + "api/robot/controller/task/extend/continue";
                            String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
                            if (!StringUtils.isEmpty(response) && response.contains("code")) {
                                RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
                                if ("SUCCESS".equals(rcsReturn1.getCode())) {
                                    // 返回RCS
                                    rcsReturn.setCode("SUCCESS");
                                    rcsReturn.setMessage("");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                } else {
                                    // 返回RCS
                                    rcsReturn.setCode("Err_Internal");
                                    rcsReturn.setMessage("");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                }
                            }
                        } else {
                            // 延迟处理,确保到站完成信号优先处理
                            try {
                                Thread.sleep(5000); // 睡眠5秒
                            } catch (InterruptedException e)
                            {
                                Thread.currentThread().interrupt();
                            }
                            //如果是工位需要像mes请求离站
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(values.getString("slotName"));
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            int i = applyOutStation(transParent);
                            if (i == 1) {
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            } else {
                                // 返回RCS
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }
                        break;
                    }
                    //AGV放货离站请求
                    case APPLY_OFF_STATION: {
                        //如果是立库站点直接允许离站
                        if (task.getStaNo().equals("401") || task.getStaNo().equals("402") || task.getStaNo().equals("307")) {
                            RcsTaskContinue rcsTaskContinue = new RcsTaskContinue();
                            rcsTaskContinue.setRobotTaskCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            rcsTaskContinue.setTriggerType("TASK");
                            rcsTaskContinue.setTriggerCode(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            String url = HIK_URL + "api/robot/controller/task/extend/continue";
                            String response = sendPost(url, JSONObject.toJSONString(rcsTaskContinue));
                            if (!StringUtils.isEmpty(response) && response.contains("code")) {
                                RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
                                if ("SUCCESS".equals(rcsReturn1.getCode())) {
                                    // 返回RCS
                                    rcsReturn.setCode("SUCCESS");
                                    rcsReturn.setMessage("");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                } else {
                                    // 返回RCS
                                    rcsReturn.setCode("Err_Internal");
                                    rcsReturn.setMessage("");
                                    JSONObject data = new JSONObject();
                                    data.put("robotTaskCode", robotTaskCode);
                                    rcsReturn.setData(data);
                                }
                            }
                        } else {
                            // 延迟处理,确保到站完成信号优先处理
                            try {
                                Thread.sleep(5000); // 睡眠5秒
                            } catch (InterruptedException e)
                            {
                                Thread.currentThread().interrupt();
                            }
                            //如果是工位需要像mes请求离站
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(values.getString("slotName"));
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            int i = applyOutStation(transParent);
                            if (i == 1) {
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            } else {
                                // 返回RCS
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }
                        break;
                    }
                    //到站完成
                    case ARRIVE_ON_STATION: {
                        if ( values.getString("slotName").equals("401") || values.getString("slotName").equals("402") || values.getString("slotName").equals("307")) {
                            // 返回RCS
                            rcsReturn.setCode("SUCCESS");
                            rcsReturn.setMessage("");
                            JSONObject data = new JSONObject();
                            data.put("robotTaskCode", robotTaskCode);
                            rcsReturn.setData(data);
                        } else {
                            TransParent transParent = new TransParent();
                            transParent.setTaskno(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setTaskname(task.getTaskNo() + "-" + (task.getCtnType() - 1));
                            transParent.setStationId(values.getString("slotName"));
                            transParent.setAgvFactory(1);
                            transParent.setAgvCode("1");//连接器库
                            //08-1是空托盘转序任务
                            //08-2是物料转序任务
                            if (task.getTaskType().equals("ZX-AGV-08-1")) {
                                transParent.setTransType("08-1");
                            } else if (task.getTaskType().equals("ZX-AGV-08-2")) {
                                transParent.setTransType("08-2");
                            } else if (task.getTaskType().equals("PP-AGV")) {
                                transParent.setTransType("10");
                            } else if (task.getTaskType().equals("ZP-AGV")) {
                                transParent.setTransType("11");
                            }
                            int i = AGVArrivalCompletedFit(transParent);
                            if (i == 1) {
                                rcsReturn.setCode("SUCCESS");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            } else {
                                // 返回RCS
                                rcsReturn.setCode("Err_Internal");
                                rcsReturn.setMessage("");
                                JSONObject data = new JSONObject();
                                data.put("robotTaskCode", robotTaskCode);
                                rcsReturn.setData(data);
                            }
                        }
                        break;
                    }
                    default: {
                    }
@@ -262,6 +673,105 @@
        return rcsReturn;
    }
    /**
     * 到站完成:AGV到达工位里面--》mes到位完成
     *
     * @param apply
     * @return
     */
    public int AGVArrivalCompletedFit(TransParent apply) {
        String url = MES_URL + "AGVArrivalCompletedFit";
        String response = RcsServiceImpl.sendPost(url, JSONObject.toJSONString(apply));
        if (!StringUtils.isEmpty(response) && response.contains("Success")) {
            MesReturn mesReturn = JSONObject.parseObject(response, MesReturn.class);
            if ("1".equals(mesReturn.getSuccess())) {
                return 1;
            }
        }
        return 0;
    }
    /**
     * 离站完成:wms->mes
     * AGV离开接驳位后发送离开状态给MES
     *
     * @param apply
     * @return
     */
    public int AGVDepartureCompleted(TransParent apply) {
        String url = MES_URL + "AGVDepartureCompleted";
        String response = RcsServiceImpl.sendPost(url, JSONObject.toJSONString(apply));
        if (!StringUtils.isEmpty(response) && response.contains("Success")) {
            MesReturn mesReturn = JSONObject.parseObject(response, MesReturn.class);
            if ("1".equals(mesReturn.getSuccess())) {
                return 1;
            }
        }
        return 0;
    }
    /**
     * 入站请求:转发AGV->入站请求->给MES
     *
     * @param apply
     * @return
     */
    public int applyInStation(TransParent apply) {
        String path = "AGVTransportPalletNotice";
        String url = MES_URL + path;
        String response = RcsServiceImpl.sendPost(url, JSONObject.toJSONString(apply));
        if (!StringUtils.isEmpty(response) && response.contains("Success")) {
            MesReturn mesReturn = JSONObject.parseObject(response, MesReturn.class);
            if ("1".equals(mesReturn.getSuccess())) {
                return 1;
            }
        }
        return 0;
    }
    /**
     * 离站请求:转发AGV->离站请求->给MES
     *
     * @param apply
     * @return
     */
    public int applyOutStation(TransParent apply) {
        String url = MES_URL + "AGVDepartureRequest";
        String response = RcsServiceImpl.sendPost(url, JSONObject.toJSONString(apply));
        if (!StringUtils.isEmpty(response) && response.contains("Success")) {
            MesReturn mesReturn = JSONObject.parseObject(response, MesReturn.class);
            if ("1".equals(mesReturn.getSuccess())) {
                return 1;
            }
        }
        return 0;
    }
    @Override
    public R bindOrUnbind (BindOrUnbindParam param) {
        //1.先进行绑定
        String url = HIK_URL + "api/robot/controller/carrier/bind";
        String response = sendPost(url, JSONObject.toJSONString(param));
        if (!StringUtils.isEmpty(response) && response.contains("code")) {
            RcsReturn rcsReturn1 = JSONObject.parseObject(response, RcsReturn.class);
            if ("SUCCESS".equals(rcsReturn1.getCode())) {
                //2.绑定成功,对站定进行解绑
                String url1 = HIK_URL + "api/robot/controller/carrier/unbind";
                String response1 = sendPost(url, JSONObject.toJSONString(param));
                return R.ok();
            } else {
                R.error("绑定失败,该区域满了");
            }
        }else {
            return R.error();
        }
        return R.ok();
    }
    /**
     * 向指定 URL 发送POST方法的请求
     *
@@ -322,4 +832,55 @@
        }
        return result.toString();
    }
    public void agvPause(StaProtocol staProtocol){
        if (staProtocol.isHighError()) {
            JSONObject params = new JSONObject();
            if (staProtocol.getSiteId()==402) {
                params.put("zoneCode", "GK402");
                params.put("mapCode", "BB");
                params.put("invoke", "FREEZE");
                System.out.println("402超高报警");
            }else if (staProtocol.getSiteId() == 401){
                params.put("zoneCode", "GK401");
                params.put("mapCode", "BB");
                params.put("invoke", "FREEZE");
                System.out.println("401超高报警");
            }
            String url = HIK_URL + "api/robot/controller/zone/pause";
            try {
                String response = RcsServiceImpl.sendPost(url, JSONObject.toJSONString(params));
                JSONObject jsonObject = JSON.parseObject(response);
                if (!StringUtils.isEmpty(response) && (jsonObject.getString("code").equals("SUCCESS") || jsonObject.getInteger("code") == 200)) {
                    System.out.println("暂停成功");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        if (!staProtocol.isHighError()) {
            JSONObject params = new JSONObject();
            if (staProtocol.getSiteId()==402) {
                params.put("zoneCode", "GK402");
                params.put("mapCode", "BB");
                params.put("invoke", "RUN");
            }else if (staProtocol.getSiteId() == 401){
                params.put("zoneCode", "GK401");
                params.put("mapCode", "BB");
                params.put("invoke", "RUN");
                System.out.println("尝试启动401口AGV");
            }
            String url = HIK_URL + "api/robot/controller/zone/pause";
            try {
                String response = RcsServiceImpl.sendPost(url, JSONObject.toJSONString(params));
                JSONObject jsonObject = JSON.parseObject(response);
                if (!StringUtils.isEmpty(response) && (jsonObject.getString("code").equals("SUCCESS") || jsonObject.getInteger("code") == 200)) {
                    System.out.println("AGV启动成功");
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}