自动化立体仓库 - WMS系统
chen.llin
9 天以前 817683f9935a6343e954ffe7fd0eeae46e55d29c
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package com.zy.asrs.task.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.entity.Task;
import com.zy.asrs.entity.TaskLog;
import com.zy.asrs.entity.WrkMast;
import com.zy.asrs.mapper.BasStationMapper;
import com.zy.asrs.mapper.WrkMastMapper;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.TaskLogService;
import com.zy.asrs.service.TaskService;
import com.zy.common.constant.ApiInterfaceConstant;
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.List;
import java.util.stream.Collectors;
 
/**
 * @author pang.jiabao
 * @description AGV交互相关定时任务处理类
 * @createDate 2025/11/18 14:42
 */
@Slf4j
@Service
public class AgvHandler {
 
    @Resource
    private WrkMastMapper wrkMastMapper;
 
    @Resource
    private ApiLogService apiLogService;
 
    @Resource
    private TaskService taskService;
 
    @Resource
    private TaskLogService taskLogService;
 
    @Resource
    private BasStationMapper basStationMapper;
 
    @Value("${Agv.sendTask}")
    private boolean agvSendTask;
 
    /**
     * 呼叫agv搬运
     */
    public void callAgv(List<Task> taskList) {
 
        if (!agvSendTask) {
            return;
        }
 
        for (Task task : taskList) {
            // 呼叫agv
            String response = "";
            boolean success = false;
            String url = ApiInterfaceConstant.AGV_IP + ApiInterfaceConstant.AGV_CREATE_TASK_PATH;
            String namespace = "";
            switch (task.getIoType()) {
                case 1:
                case 10:
                case 53:
                case 57:
                    namespace = "入库";
                    break;
                case 3:
                    namespace = "转移";
                    break;
                case 101:
                case 110:
                case 103:
                case 107:
                    namespace = "出库";
                    break;
                default:
            }
            String body = getRequest(task,namespace);
            try {
                // 使用仙工M4接口
                response = new HttpHandler.Builder()
                        .setUri(ApiInterfaceConstant.AGV_IP)
                        .setPath(ApiInterfaceConstant.AGV_CREATE_TASK_PATH)
                        .setJson(body)
                        .build()
                        .doPost();
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.getInteger("code").equals(200)) {
                    success = true;
                    task.setWrkSts(8L);
                    taskService.updateById(task);
                    log.info(namespace + "呼叫agv搬运成功:{}", task.getId());
                } else {
                    log.error(namespace + "呼叫agv搬运失败!!!url:{};request:{};response:{}", url, body, response);
                }
            } catch (Exception e) {
                log.error(namespace + "呼叫agv搬运异常", e);
            } finally {
                try {
                    // 保存接口日志
                    apiLogService.save(
                            namespace + "呼叫agv搬运",
                            url,
                            null,
                            "127.0.0.1",
                            body,
                            response,
                            success
                    );
                } catch (Exception e) {
                    log.error(namespace + "呼叫agv保存接口日志异常:", e);
                }
            }
        }
    }
 
    /**
     * 构造请求内容(仙工M4格式)
     */
    private String getRequest(Task task, String nameSpace) {
        JSONObject object = new JSONObject();
        // taskId使用任务ID,格式:T + 任务ID
        object.put("taskId", "T" + task.getId());
        object.put("fromBin", task.getSourceStaNo());
        object.put("toBin", task.getStaNo());
        // robotGroup从invWh字段获取,如果没有则使用默认值
        String robotGroup = task.getInvWh();
        if (robotGroup == null || robotGroup.isEmpty()) {
            robotGroup = "Group-001"; // 默认机器人组
        }
        object.put("robotGroup", robotGroup);
        // kind根据任务类型映射
        String kind = "";
        switch (nameSpace) {
            case "入库":
                kind = "实托入库";
                break;
            case "出库":
                kind = "实托出库";
                break;
            case "转移":
                kind = "货物转运";
                break;
            default:
                kind = "货物转运";
        }
        object.put("kind", kind);
        return object.toJSONString();
    }
 
    /**
     * 任务完成转历史 释放暂存点
     */
    @Transactional(rollbackFor = Exception.class)
    public void moveTaskToHistory(List<Task> taskList) {
 
        // 写入历史表
        for (Task task : taskList) {
            TaskLog log = new TaskLog();
            BeanUtils.copyProperties(task, log);
            taskLogService.insert(log);
        }
 
        // 批量删除原任务
        List<Long> taskIds = taskList.stream().map(Task::getId).collect(Collectors.toList());
        taskService.delete(new EntityWrapper<Task>().in("id",taskIds));
 
        // 批量更新暂存点状态
        List<String> locOList = new ArrayList<>();
        List<String> locFList = new ArrayList<>();
        for (Task task : taskList) {
            String sourceStaNo = task.getSourceStaNo();
            String staNo = task.getStaNo();
            if (task.getIoType() == 3) {
                locOList.add(sourceStaNo);
                locFList.add(staNo);
            } else if (task.getIoType() < 100) {
                locOList.add(sourceStaNo);
            } else {
                locFList.add(staNo);
            }
        }
 
        if (!locOList.isEmpty()) {
            basStationMapper.updateLocStsBatch(locOList, "O");
        }
        if (!locFList.isEmpty()) {
            basStationMapper.updateLocStsBatch(locFList, "F");
        }
 
        log.info("agv任务档转历史成功:{}", taskIds);
    }
 
    /**
     * 货物到达出库口,生成agv任务
     */
    public void createAgvOutTasks(List<String> sites) {
 
        // 获取到可用出库站点的任务
        List<WrkMast> wrkMastList = wrkMastMapper.selectList(new EntityWrapper<WrkMast>().eq("call_agv", 1).in("sta_no",sites));
 
        for(WrkMast wrkMast:wrkMastList) {
            // todo 计算agv目标暂存位
            int endSite = 2004;
 
            // 插入agv任务
            Task task = new Task(wrkMast.getWrkNo(), 7L, wrkMast.getIoType(), String.valueOf(wrkMast.getStaNo()), String.valueOf(endSite), null, wrkMast.getBarcode());
            taskService.insert(task);
            // 更新任务档agv搬运标识
            wrkMast.setCallAgv(2);
            wrkMastMapper.updateById(wrkMast);
 
            // 更新暂存位状态 S.入库预约
            basStationMapper.updateLocStsBatch( Collections.singletonList(String.valueOf(endSite)), "S");
        }
    }
 
    /**
     * 取消AGV任务(仙工M4接口)
     * @param task 任务对象
     * @return 是否成功
     */
    public boolean cancelAgvTask(Task task) {
        if (!agvSendTask) {
            return false;
        }
 
        if (task == null || task.getId() == null) {
            log.error("取消AGV任务失败:任务或任务ID为空");
            return false;
        }
 
        String response = "";
        boolean success = false;
        String url = ApiInterfaceConstant.AGV_IP + ApiInterfaceConstant.AGV_CANCEL_TASK_PATH;
        String namespace = "";
        String kind = "";
        
        // 根据任务类型确定kind和namespace
        switch (task.getIoType()) {
            case 1:
            case 10:
            case 53:
            case 57:
                namespace = "入库";
                kind = "实托入库";
                break;
            case 3:
                namespace = "转移";
                kind = "货物转运";
                break;
            case 101:
            case 110:
            case 103:
            case 107:
                namespace = "出库";
                kind = "实托出库";
                break;
            default:
                kind = "货物转运";
        }
 
        // 构造取消任务请求
        JSONObject cancelRequest = new JSONObject();
        cancelRequest.put("taskId", "T" + task.getId());
        cancelRequest.put("kind", kind);
        String body = cancelRequest.toJSONString();
 
        try {
            response = new HttpHandler.Builder()
                    .setUri(ApiInterfaceConstant.AGV_IP)
                    .setPath(ApiInterfaceConstant.AGV_CANCEL_TASK_PATH)
                    .setJson(body)
                    .build()
                    .doPost();
            
            JSONObject jsonObject = JSON.parseObject(response);
            if (jsonObject.getInteger("code") != null && jsonObject.getInteger("code").equals(200)) {
                success = true;
                log.info(namespace + "取消AGV任务成功:{}", task.getId());
            } else {
                log.error(namespace + "取消AGV任务失败!!!url:{};request:{};response:{}", url, body, response);
            }
        } catch (Exception e) {
            log.error(namespace + "取消AGV任务异常", e);
        } finally {
            try {
                // 保存接口日志
                apiLogService.save(
                        namespace + "取消AGV任务",
                        url,
                        null,
                        "127.0.0.1",
                        body,
                        response,
                        success
                );
            } catch (Exception e) {
                log.error(namespace + "取消AGV任务保存接口日志异常:", e);
            }
        }
 
        return success;
    }
}