自动化立体仓库 - WMS系统
zyx
2023-10-02 cda03cb23bc12f582029ac8d6df103d86e61fc8b
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
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.core.exception.CoolException;
import com.zy.asrs.entity.AgvWrkMast;
import com.zy.asrs.entity.param.AgvTaskCreateParam;
import com.zy.asrs.entity.param.AgvTaskParam;
import com.zy.asrs.entity.param.AgvTaskkDescribeParam;
import com.zy.asrs.mapper.AgvWrkMastMapper;
import com.zy.asrs.service.AgvLocMastService;
import com.zy.asrs.service.AgvWrkDetlService;
import com.zy.asrs.service.AgvWrkMastService;
import com.zy.asrs.service.ApiLogService;
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.stereotype.Service;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
@Service
@Slf4j
public class AgvWrkMastServiceImp extends ServiceImpl<AgvWrkMastMapper, AgvWrkMast> implements AgvWrkMastService {
 
    @Value("${agv.url}")
    private String url;
 
    @Value("${agv.taskCreatePath}")
    private String taskCreatePath;
 
    @Value("${agv.containerMoveInPath}")
    private String containerMoveInPath;
 
    @Value("${agv.containerMoveOutPath}")
    private String containerMoveOutPath;
 
    @Autowired
    AgvWrkMastMapper agvWrkMastMapper;
    @Autowired
    AgvWrkDetlService agvWrkDetlService;
    @Autowired
    AgvLocMastService agvLocMastService;
    @Autowired
    ApiLogService apiLogService;
 
    public void updateWrkStsByWrkNo(int wrkNo, long wrkSts) {
        AgvWrkMast agvWrkMast = this.selectById(wrkNo);
        //判断要修改的工作档状态是否合理,如果不合理则抛出异常
        checkWrkSts(agvWrkMast,wrkSts);
        agvWrkMast.setWrkSts(wrkSts);
        this.updateById(agvWrkMast);
    }
 
    // TODO 判断规则
    private boolean checkWrkSts(AgvWrkMast agvWrkMast,long wrkSts){
 
 
 
        return true;
    }
 
    public int startWrk(List<AgvWrkMast> agvWrkMastList, String taskType) throws IOException {
 
        AgvTaskCreateParam agvTaskCreateParam = new AgvTaskCreateParam();
        agvTaskCreateParam.setTaskType(taskType);
 
//        //调用容器入场时所需要参数
//        Map<String,List<Map<String,String>>> containerMoveParam = new HashMap<>();
//        List<Map<String,String>> positionCodeMapList = new ArrayList<>();
//        containerMoveParam.put("containerMoveIns",positionCodeMapList);
 
        getRequestParam(agvTaskCreateParam,agvWrkMastList);
 
        return doHttpRequest(agvTaskCreateParam,"上架任务下发",url, taskCreatePath,null,"127.0.0.1");
 
        //return containerMoveParam;
    }
 
    //容器入场
    public int containerMove(List<AgvWrkMast> agvWrkMastList) throws IOException {
        //调用容器入场时所需要参数
        Map<String,List<Map<String,String>>> containerMoveParam = new HashMap<>();
        List<Map<String,String>> positionCodeMapList = new ArrayList<>();
        containerMoveParam.put("containerMoveIns",positionCodeMapList);
 
        getContainerMoveParam(agvWrkMastList,positionCodeMapList);
        return doHttpRequest(containerMoveParam,"容器入场任务下发",url, containerMoveInPath,null,"127.0.0.1");
 
    }
 
    //容器离场
    public int containerMoveOut(List<AgvWrkMast> agvWrkMastList){
        Map<String,List<Map<String,String>>> containerMoveParam = new HashMap<>();
        List<Map<String,String>> positionCodeMapList = new ArrayList<>();
        containerMoveParam.put("containerMoveOuts",positionCodeMapList);
        for(AgvWrkMast agvWrkMast : agvWrkMastList){
            Map<String,String> positionCodeMap = new HashMap<>();
            positionCodeMap.put("positionCode",agvWrkMast.getLocNo());
            positionCodeMapList.add(positionCodeMap);
        }
        return doHttpRequest(containerMoveParam,"容器离场任务下发",url, containerMoveOutPath,null,"127.0.0.1");
    }
 
    public boolean insertByIncrease(AgvWrkMast agvWrkMast) {
        int index = this.baseMapper.insertByIncrease(agvWrkMast);
 
        return index > 0;
    }
 
    private void getContainerMoveParam(List<AgvWrkMast> agvWrkMastList,List<Map<String,String>> positionCodeMapList){
        //往容器入场参数中放入源站点位置
        for(AgvWrkMast agvWrkMast : agvWrkMastList){
            Map<String,String> positionCodeMap = new HashMap<>();
            positionCodeMap.put("positionCode",agvWrkMast.getSourceLocNo());
            positionCodeMapList.add(positionCodeMap);
        }
    }
 
    private void getRequestParam(AgvTaskCreateParam agvTaskCreateParam,List<AgvWrkMast> agvWrkMastList){
        List<AgvTaskParam> agvTaskParamList = agvWrkMastList.stream().map(agvWrkMast -> {
            AgvTaskParam agvTaskParam = new AgvTaskParam();
            AgvTaskkDescribeParam agvTaskkDescribeParam = new AgvTaskkDescribeParam();
            agvTaskParam.setTaskDescribe(agvTaskkDescribeParam);
            //AgvTaskParam
            agvTaskParam.setTaskCode(agvWrkMast.getWrkNo().toString());
            agvTaskParam.setTaskPriority(agvWrkMast.getIoPri().intValue());
 
            //AgvTaskkDescribeParam
            agvTaskkDescribeParam.setFromLocationCode(agvWrkMast.getSourceLocNo());
            agvTaskkDescribeParam.setToLocationCode(agvWrkMast.getLocNo());
            agvTaskkDescribeParam.setContainerCode(agvWrkMast.getBarcode());
            return agvTaskParam;
        }).collect(Collectors.toList());
 
        agvTaskCreateParam.setTasks(agvTaskParamList);
    }
 
    private int doHttpRequest(Object requestParam, String namespace, String url, String path, String appkey, String ip){
        String response = "";
        boolean success = false;
 
        try {
            response = new HttpHandler.Builder()
                    .setUri(url)
                    .setPath(path)
                    .setJson(JSONObject.toJSONString(requestParam))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
 
            int code = (int) jsonObject.get("code");
            if(code != 0){
                throw new CoolException("调用AGV响应错误");
            }
            success = true;
            return code;
        }catch (Exception e){
            log.error(e.getMessage());
            throw new CoolException("调用AGV响应错误");
        }finally {
            apiLogService.save(
                    namespace,
                    url + path,
                    appkey,
                    ip,
                    JSON.toJSONString(JSONObject.toJSONString(requestParam)),
                    response,
                    success
            );
        }
 
    }
 
}