自动化立体仓库 - WMS系统
chen.llin
2 天以前 2307db8fc3abd03227f54e24f73d87fb34908dc2
src/main/java/com/zy/asrs/task/handler/AgvHandler.java
@@ -12,17 +12,19 @@
import com.zy.asrs.service.TaskLogService;
import com.zy.asrs.service.TaskService;
import com.zy.common.constant.ApiInterfaceConstant;
import com.zy.common.properties.AgvProperties;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
@@ -49,15 +51,15 @@
    @Resource
    private BasStationMapper basStationMapper;
    @Value("${Agv.sendTask}")
    private boolean agvSendTask;
    @Resource
    private AgvProperties agvProperties;
    /**
     * 呼叫agv搬运
     */
    public void callAgv(List<Task> taskList) {
        if (!agvSendTask) {
        if (!agvProperties.isSendTask()) {
            return;
        }
@@ -86,6 +88,9 @@
                default:
            }
            String body = getRequest(task,namespace);
            // 打印请求信息
            log.info("{}呼叫agv搬运 - 请求地址:{}", namespace, url);
            log.info("{}呼叫agv搬运 - 请求参数:{}", namespace, body);
            try {
                // 使用仙工M4接口
                response = new HttpHandler.Builder()
@@ -94,17 +99,35 @@
                        .setJson(body)
                        .build()
                        .doPost();
                // 打印返回参数
                log.info("{}呼叫agv搬运 - 返回参数:{}", namespace, response);
                // 检查响应是否为空
                if (response == null || response.trim().isEmpty()) {
                    log.error("{}呼叫agv搬运失败 - 任务ID:{},AGV接口返回为空", namespace, task.getId());
                    continue;
                }
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code").equals(200)) {
                if (jsonObject == null) {
                    log.error("{}呼叫agv搬运失败 - 任务ID:{},响应JSON解析失败,响应内容:{}", namespace, task.getId(), response);
                    continue;
                }
                Integer code = jsonObject.getInteger("code");
                if (code != null && code.equals(200)) {
                    success = true;
                    task.setWrkSts(8L);
                    taskService.updateById(task);
                    log.info(namespace + "呼叫agv搬运成功:{}", task.getId());
                    log.info("{}呼叫agv搬运成功 - 任务ID:{}", namespace, task.getId());
                } else {
                    log.error(namespace + "呼叫agv搬运失败!!!url:{};request:{};response:{}", url, body, response);
                    String message = jsonObject.getString("message");
                    log.error("{}呼叫agv搬运失败 - 任务ID:{},错误码:{},错误信息:{}",
                            namespace, task.getId(), code, message);
                }
            } catch (Exception e) {
                log.error(namespace + "呼叫agv搬运异常", e);
                log.error("{}呼叫agv搬运异常 - 任务ID:{},请求地址:{},请求参数:{},异常信息:{}",
                        namespace, task.getId(), url, body, e.getMessage(), e);
            } finally {
                try {
                    // 保存接口日志
@@ -131,19 +154,34 @@
        JSONObject object = new JSONObject();
        // taskId使用任务ID,格式:T + 任务ID
        object.put("taskId", "T" + task.getId());
        object.put("fromBin", task.getSourceStaNo());
        // fromBin使用源库位编号(sourceLocNo),如果为空则使用源站点编号(sourceStaNo)作为备选
        String fromBin = task.getSourceLocNo();
        if (fromBin == null || fromBin.isEmpty()) {
            fromBin = task.getSourceStaNo();
        }
        if (fromBin == null || fromBin.isEmpty() || "0".equals(fromBin)) {
            log.warn("任务{}的源库位和源站点都为空,使用默认值", task.getId());
            fromBin = "0";
        }
        object.put("fromBin", fromBin);
        // toBin使用目标站点编号
        object.put("toBin", task.getStaNo());
        // robotGroup从invWh字段获取,如果没有则使用默认值
        // robotGroup从invWh字段获取,如果没有则根据站点编号判断
        String robotGroup = task.getInvWh();
        if (robotGroup == null || robotGroup.isEmpty()) {
            robotGroup = "Group-001"; // 默认机器人组
            robotGroup = determineRobotGroupByStation(task.getStaNo());
        }
        object.put("robotGroup", robotGroup);
        // kind根据任务类型映射
        String kind = "";
        switch (nameSpace) {
            case "入库":
                kind = "实托入库";
                // 判断是否为空托入库:ioType=10 或 emptyMk="Y"
                if (task.getIoType() == 10 || "Y".equals(task.getEmptyMk())) {
                    kind = "空托入库";
                } else {
                    kind = "实托入库";
                }
                break;
            case "出库":
                kind = "实托出库";
@@ -156,6 +194,31 @@
        }
        object.put("kind", kind);
        return object.toJSONString();
    }
    /**
     * 根据站点编号判断机器人组
     * @param staNo 站点编号
     * @return 机器人组名称
     */
    private String determineRobotGroupByStation(String staNo) {
        if (staNo == null || staNo.isEmpty()) {
            return agvProperties.getRobotGroupEast(); // 默认使用东侧机器人组
        }
        // 从配置中获取站点列表
        Set<String> eastStations = new HashSet<>(agvProperties.getEastStations());
        Set<String> westStations = new HashSet<>(agvProperties.getWestStations());
        // 判断站点属于哪一侧
        if (eastStations.contains(staNo)) {
            return agvProperties.getRobotGroupEast(); // 东侧机器人
        } else if (westStations.contains(staNo)) {
            return agvProperties.getRobotGroupWest(); // 西侧机器人
        } else {
            log.warn("站点编号不在配置列表中,使用默认机器人组:{}", staNo);
            return agvProperties.getRobotGroupEast(); // 默认使用东侧机器人组
        }
    }
    /**
@@ -231,7 +294,7 @@
     * @return 是否成功
     */
    public boolean cancelAgvTask(Task task) {
        if (!agvSendTask) {
        if (!agvProperties.isSendTask()) {
            return false;
        }