自动化立体仓库 - WMS系统
skyouc
2 天以前 707d414c0e6a8baff3ce7affec820f0fc1a13c19
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package com.zy.api.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.api.controller.params.AgvCallBackParam;
import com.zy.api.controller.params.AgvCarriesParam;
import com.zy.api.controller.params.AgvRequestParam;
import com.zy.api.entity.CallAgvParams;
import com.zy.api.enums.OrderType;
import com.zy.api.service.AgvScheduleService;
import com.zy.asrs.entity.LocCache;
import com.zy.asrs.entity.Task;
import com.zy.asrs.entity.result.ForwardAGVTaskDTO;
import com.zy.asrs.entity.result.HIKApiDTO;
import com.zy.asrs.entity.result.HIKResultDTO;
import com.zy.asrs.enums.LocStsType;
import com.zy.asrs.enums.TaskIOType;
import com.zy.asrs.enums.TaskStatusType;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.LocCacheService;
import com.zy.asrs.service.TaskService;
import com.zy.asrs.service.impl.LocCacheServiceImpl;
import com.zy.common.constant.HIKApiConstant;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
@Slf4j
@Service
public class AgvScheduleServiceImpl implements AgvScheduleService {
 
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private LocCacheService locCacheService;
 
    /**
     * @author Ryan
     * @date 2025/11/3
     * @description: 呼叫AGV搬运
     * @version 1.0
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R callAgvCarry(CallAgvParams params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        if (Objects.isNull(params.getWrkNo())) {
            return R.error("任务号不能为空!!");
        }
 
        Task task = taskService.selectOne(new EntityWrapper<Task>().eq("wrk_no", params.getWrkNo()));
        if (Objects.isNull(task)) {
            return R.error("任务信息不存在!!");
        }
 
        if (Arrays.asList(TaskIOType.ALL_IN.type, TaskIOType.PICK_IN.type, TaskIOType.MERGE_IN.type)
                .contains(task.getIoType())) {
            // 入库
            task.setWrkSts(TaskStatusType.AGV_TASK_ISSUED_IN.type);
        } else if (Arrays.asList(TaskIOType.ALL_OUT.type, TaskIOType.PICK_OUT.type, TaskIOType.MERGE_OUT.type, TaskIOType.EMPTY_OUT.type)
                .contains(task.getIoType())) {
            // 出库
            task.setWrkSts(TaskStatusType.AGV_TASK_ISSUED_OUT.type);
        }
 
        HIKResultDTO hikResultDTO = sendAgvTask(params, HIKApiConstant.TASK_SUBMIT);
 
        if (hikResultDTO.isSuccess()) {
            if (!taskService.updateById(task)) {
                throw new CoolException("任务状态更新失败!!");
            }
        }
 
        return R.ok();
    }
 
    /**
     * 获取自定义请求头
     *
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @author Ryan
     * @date 2025/12/29 9:11
     */
    private Map<String, Object> getHeaderParam() {
        Map<String, Object> headerParam = new HashMap<>();
        //设置请求连接的Token
        headerParam.put("X-LR-REQUEST-ID", new Date().getTime() + "");
        return headerParam;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R callback(AgvCallBackParam params) {
        if (Objects.isNull(params)) {
            return R.error("参数不能为空!!");
        }
        if (Objects.isNull(params.getRobotTaskCode())) {
            return R.error("任务号不能为空!!");
        }
        Task task = taskService.selectOne(new EntityWrapper<Task>().eq("wrk_no", params.getRobotTaskCode()));
        if (Objects.isNull(task)) {
            return R.error("任务信息不存在!!");
        }
 
        if (Arrays.asList(TaskIOType.ALL_IN.type, TaskIOType.PICK_IN.type, TaskIOType.MERGE_IN.type)
                .contains(task.getIoType())) {
            // 入库
            if (params.getExtra().getValues().getMethod().equals("start")) {
                task.setWrkSts(TaskStatusType.AGV_TASK_RUNNING_IN.type);
            } else if (params.getExtra().getValues().getMethod().equals("end")){
                task.setWrkSts(TaskStatusType.AGV_TASK_FINISHED_IN.type);
            }
        } else if (Arrays.asList(TaskIOType.ALL_OUT.type, TaskIOType.PICK_OUT.type, TaskIOType.MERGE_OUT.type, TaskIOType.EMPTY_OUT.type)
                .contains(task.getIoType())) {
            // 出库p
            if (params.getExtra().getValues().getMethod().equals("start")) {
                task.setWrkSts(TaskStatusType.AGV_TASK_RUNNING_OUT.type);
            } else if (params.getExtra().getValues().getMethod().equals("end")){
                task.setWrkSts(TaskStatusType.AGV_TASK_FINISHED_OUT.type);
            }
        }
 
        if (!taskService.updateById(task)) {
            throw new CoolException("状态修改失败!!");
        }
        return R.ok("执行完成 !!");
    }
 
    public HIKResultDTO sendAgvTask(CallAgvParams param, String path) {
        HIKResultDTO result = new HIKResultDTO();
        AgvRequestParam requestParam = new AgvRequestParam();
        requestParam.setTaskType(param.getTaskType());
        List<AgvCarriesParam> agvCarriesParams = new ArrayList<>();
 
        AgvCarriesParam carriesParam = new AgvCarriesParam();
        carriesParam.setSeq(0);
        carriesParam.setCode(param.getOrgSite());
 
        agvCarriesParams.add(carriesParam);
 
        AgvCarriesParam carriesParam2 = new AgvCarriesParam();
        carriesParam2.setSeq(1);
        carriesParam2.setCode(param.getTarSite());
 
        agvCarriesParams.add(carriesParam2);
 
        requestParam.setRobotTaskCode(param.getWrkNo());
        requestParam.setTargetRoute(agvCarriesParams);
 
        String body = JSON.toJSONString(requestParam);
        String response = "";
        try {
            response = new HttpHandler.Builder()
                    .setUri(HIKApiConstant.AGV_IP)
                    .setPath(path)
                    .setHeaders(getHeaderParam())
                    .setJson(body)
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getString("code").equals("SUCCESS")) {
                result.setSuccess(true);
            } else {
                result.setMessage(jsonObject.getString("message"));
                log.error("发送agv任务失败!!!url:{};request:{};response:{}", HIKApiConstant.AGV_IP + path, body, response);
            }
            // {"code":"1","data":"","interrupt":false,"message":"重复提交","msgErrCode":"0x3a80D012","reqCode":"fa92b49481a44627ae4d80c1400f28f6"}
        } catch (Exception e) {
            result.setMessage(e.getMessage());
            log.error("发送agv任务异常", e);
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        "发送agv任务",
                        HIKApiConstant.AGV_IP + path,
                        null,
                        "127.0.0.1",
                        body,
                        response,
                        result.isSuccess());
            } catch (Exception e) {
                log.error("", e);
            }
        }
        return result;
    }
 
}