自动化立体仓库 - WMS系统
zhou zhou
2025-12-25 418a73bcee019e078307a1c2c255235de33d20ca
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
package com.zy.asrs.task.handler;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.R;
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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
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_CALL_CARRY_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 {
                response = new HttpHandler.Builder()
                        .setUri(ApiInterfaceConstant.AGV_IP)
                        .setPath(ApiInterfaceConstant.AGV_CALL_CARRY_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);
                }
            }
        }
    }
 
    /**
     * 构造请求内容
     */
    private String getRequest(Task task, String nameSpace) {
        JSONObject object = new JSONObject();
        object.put("entityName", "ContainerTransportOrder");
        JSONObject entityValue = new JSONObject();
        entityValue.put("id", task.getId());
        String kind = "";
        switch (nameSpace) {
            case "入库":
                kind = "inBound";
                break;
            case "出库":
                kind = "outBound";
                break;
            case "转移":
                kind = "moveBound";
                break;
            default:
        }
        entityValue.put("kind", kind);
        entityValue.put("status", "Created");
        entityValue.put("priority", 0);
        entityValue.put("container", task.getBarcode());
        entityValue.put("fromBin", task.getSourceStaNo());
        entityValue.put("toBin", task.getStaNo());
        entityValue.put("emptyFlag", task.getIoType() == 10 ? 1 : 2); // 空托1,满托2 agv带着告诉输送线plc
        object.put("entityValue", entityValue.toJSONString());
        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");
        }
    }
}