#
Junjie
2025-05-12 e0fbb31fcf45f56b7b57c741a6b11f03cf205512
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
package com.zy.asrs.task;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.zy.asrs.domain.enums.TaskStatusType;
import com.zy.asrs.entity.LocMast;
import com.zy.asrs.entity.TaskWrk;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.LocMastService;
import com.zy.asrs.service.TaskWrkService;
import com.zy.common.utils.HttpHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
 
@Slf4j
@Component
public class AssignAgvTaskScheduler {
 
    @Autowired
    private TaskWrkService taskWrkService;
    @Autowired
    private ApiLogService apiLogService;
    @Autowired
    private LocMastService locMastService;
    @Value("${agv.url}")
    private String agvUrl;
    @Value("${agv.applyTask}")
    private String agvApplyTask;
 
    /**
     * 自动派发AGV搬运任务
     */
    @Scheduled(cron = "0/3 * * * * ? ")
    public void execute() {
        List<TaskWrk> taskWrks = taskWrkService.selectList(new EntityWrapper<TaskWrk>()
                .eq("status", TaskStatusType.RECEIVE.id).eq("io_type", 4));
 
        for (TaskWrk taskWrk : taskWrks) {
            String startPoint = taskWrk.getStartPoint();
            String targetPoint = taskWrk.getTargetPoint();
            LocMast startLocMast = locMastService.selectByLocNo(startPoint);
            if(startLocMast == null){
                continue;
            }
 
            if (!startLocMast.getLocSts().equals("R")) {
                continue;
            }
 
            LocMast targetLocMast = locMastService.selectByLocNo(targetPoint);
            if(targetLocMast == null){
                continue;
            }
 
            if (!targetLocMast.getLocSts().equals("S")) {
                continue;
            }
 
            HashMap<String, Object> requestParam = new HashMap<>();
            requestParam.put("reqCode", taskWrk.getTaskNo());
            requestParam.put("taskCode", taskWrk.getTaskNo());
            requestParam.put("taskTyp", "YK");
 
            ArrayList<HashMap<String, Object>> positionCodePathList = new ArrayList<>();
            HashMap<String, Object> startPointParam = new HashMap<>();
            startPointParam.put("positionCode", taskWrk.getStartPoint());
            startPointParam.put("type", "00");
 
            HashMap<String, Object> targetPointParam = new HashMap<>();
            targetPointParam.put("positionCode", taskWrk.getTargetPoint());
            targetPointParam.put("type", "00");
 
            positionCodePathList.add(startPointParam);
            positionCodePathList.add(targetPointParam);
            requestParam.put("positionCodePath", positionCodePathList);
 
            String response = null;
            boolean requestStatus = false;
            try {
                log.info("WCS派发任务给AGV={}", taskWrk);
                response = new HttpHandler.Builder()
                        .setUri(agvUrl)
                        .setPath(agvApplyTask)
                        .setJson(JSON.toJSONString(requestParam))
                        .build()
                        .doPost();
                JSONObject jsonObject = JSON.parseObject(response);
                if (jsonObject.get("code").equals("0")) {
                    taskWrk.setStatus(TaskStatusType.DISTRIBUTE.id);
                    taskWrkService.updateById(taskWrk);
                    requestStatus = true;
                }
            } catch (Exception e) {
                log.error("WCS派发任务给AGV失败{},返回值={}", taskWrk, response);
            } finally {
                apiLogService.save("WCS派发任务给AGV"
                        , agvUrl + agvApplyTask
                        , null
                        , "127.0.0.1"
                        , JSON.toJSONString(requestParam)
                        , response
                        , requestStatus
                );
            }
        }
    }
}