自动化立体仓库 - WMS系统
1
ZY
2024-10-31 fb4cfaec4b05618ef49f483cd80b770a286bb9f8
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
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.core.common.Cools;
import com.core.exception.CoolException;
import com.zy.asrs.entity.AgvBasDevp;
import com.zy.asrs.entity.AgvLocMast;
import com.zy.asrs.entity.AgvWrkMast;
import com.zy.asrs.entity.param.AgvTaskCreateParam;
import com.zy.asrs.entity.param.AgvTaskParam;
import com.zy.asrs.mapper.AgvWrkMastMapper;
import com.zy.asrs.service.*;
import com.zy.common.service.AgvCommonService;
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.*;
 
@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.taskCancelPath}")
    private String taskCancelPath;
 
    @Value("${agv.containerMoveInPath}")
    private String containerMoveInPath;
 
    @Value("${agv.containerMoveOutPath}")
    private String containerMoveOutPath;
 
    @Value("${agv.containerArrivedPath}")
    private String containerArrivedPath;
 
    @Autowired
    AgvWrkMastMapper agvWrkMastMapper;
    @Autowired
    AgvWrkDetlService agvWrkDetlService;
    @Autowired
    AgvLocMastService agvLocMastService;
    @Autowired
    ApiLogService apiLogService;
    @Autowired
    AgvBasDevpService agvBasDevpService;
    @Autowired
    private AgvCommonService agvCommonService;
    @Autowired
    private AgvWrkMastService agvWrkMastService;
 
 
    public void updateWrkStsByWrkNo(int wrkNo, long wrkSts) {
        AgvWrkMast agvWrkMast = this.selectById(wrkNo);
        //判断要修改的工作档状态是否合理,如果不合理则抛出异常
        checkWrkSts(agvWrkMast,wrkSts);
        agvWrkMast.setWrkSts(wrkSts);
        agvWrkMast.setModiTime(new Date());
        this.updateById(agvWrkMast);
    }
 
    public void updateWrkStsByWrkNo(int wrkNo, long wrkSts,String manuType,long userId) {
        Date now = new Date();
        AgvWrkMast agvWrkMast = this.selectById(wrkNo);
        //判断要修改的工作档状态是否合理,如果不合理则抛出异常
        checkWrkSts(agvWrkMast,wrkSts);
        agvWrkMast.setWrkSts(wrkSts);
        agvWrkMast.setManuType(manuType);
        agvWrkMast.setModiTime(now);
        agvWrkMast.setModiUser(userId);
        this.updateById(agvWrkMast);
    }
 
    // TODO 判断规则
    private boolean checkWrkSts(AgvWrkMast agvWrkMast,long wrkSts){
 
 
 
        return true;
    }
 
    public int startWrk(AgvWrkMast agvWrkMast) throws IOException {
 
        AgvTaskCreateParam agvTaskCreateParam = new AgvTaskCreateParam();
        //agvTaskCreateParam.setTaskType(taskType);
 
        getRequestParam(agvTaskCreateParam,agvWrkMast);
 
        return doHttpRequest(agvTaskCreateParam,"搬运任务下发",url, taskCreatePath,null,"127.0.0.1");
 
        //return containerMoveParam;
    }
 
    public int startAllcationIn(AgvWrkMast agvWrkMast) throws IOException {
        AgvBasDevp basDevp = agvBasDevpService.selectByDevNo(agvWrkMast.getSourceLocNo());
        //检索库位,选择合适的库位
        AgvLocMast locMast = agvCommonService.getLocNo(basDevp.getLocType1(),basDevp.getFloor(),false,true);
        agvWrkMast.setLocNo(locMast.getLocNo());
        agvWrkMast.setWrkSts(201L);
        agvWrkMast.setLogErrMemo("startAllcationIn");
        agvWrkMast.setModiTime(new Date());
        agvWrkMastService.updateById(agvWrkMast);
        // 更目标库位
        locMast.setLocSts("S");
        agvLocMastService.updateById(locMast);
        return 0;
 
    }
 
    //货架入场
    public int containerMoveIn(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);
 
        if(Cools.isEmpty(positionCodeMapList)){
            return 0;
        }
        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;
    }
 
    @Override
    public boolean deleteByWrkNo(int wrkNo) {
        return this.delete(new EntityWrapper<AgvWrkMast>().eq("wrk_no", wrkNo));
    }
 
    //货架到达通知
    public int containerArrived(AgvWrkMast agvWrkMast) {
 
        Map<String,String> containerArrivedParam = new HashMap<>();
        containerArrivedParam.put("slotCode",agvWrkMast.getSourceLocNo());
        containerArrivedParam.put("containerCode",agvWrkMast.getBarcode());
 
        return doHttpRequest(containerArrivedParam,"货架达到通知",url, containerArrivedPath,null,"127.0.0.1");
    }
 
    @Override
    public List<AgvWrkMast> selectReadyAgvWrkMast() {
        List<String> stationCodes = agvBasDevpService.selectAllStationCode();
        Wrapper<AgvWrkMast> wrapper = new EntityWrapper<AgvWrkMast>().eq("wrk_sts", 21L).like(false, "loc_no", "@");
        wrapper.in("loc_no",stationCodes);
        return this.selectList(wrapper);
        //return this.baseMapper.selectReadyAgvWrkMast();
    }
 
    @Override
    public AgvWrkMast selectByContainerCode(String containerCode) {
        return this.selectOne(new EntityWrapper<AgvWrkMast>().eq("barcode",containerCode));
    }
 
    @Override
    public int callCancelTask(int wrkNo) {
 
        AgvTaskCreateParam agvTaskCreateParam = new AgvTaskCreateParam();
        agvTaskCreateParam.setTaskCode(wrkNo + "");
 
        return doHttpRequest(agvTaskCreateParam,"取消任务下发",url, taskCancelPath,null,"127.0.0.1");
 
    }
 
 
    private void getContainerMoveParam(List<AgvWrkMast> agvWrkMastList,List<Map<String,String>> positionCodeMapList){
        //往货架入场参数中放入源站点位置
        for(AgvWrkMast agvWrkMast : agvWrkMastList){
            //判断是否为输送线入库,是则不需要货架进场请求
            if("Y".equals(agvWrkMast.getMk())){
                continue;
            }
            Map<String,String> positionCodeMap = new HashMap<>();
            positionCodeMap.put("positionCode",agvWrkMast.getSourceLocNo());
            positionCodeMapList.add(positionCodeMap);
        }
    }
 
    private void getRequestParam(AgvTaskCreateParam agvTaskCreateParam,AgvWrkMast agvWrkMast){
 
        List<AgvTaskParam> agvTaskParamList = Arrays.asList(
                //起始位
                new AgvTaskParam(agvWrkMast.getSourceLocNo()),
                //目标位
                new AgvTaskParam(agvWrkMast.getLocNo()));
 
//        //起始位
//        agvTaskParamList.add(new AgvTaskParam(agvWrkMast.getSourceLocNo()));
//        //目标位
//        agvTaskParamList.add(new AgvTaskParam(agvWrkMast.getLocNo()));
 
        if(agvWrkMast.getIoType() == 53 || agvWrkMast.getIoType() == 57 ){
            agvWrkMast.setWrkNo(-agvWrkMast.getWrkNo());
        }
        agvTaskCreateParam.setTaskCode(agvWrkMast.getWrkNo().toString());
        switch (agvWrkMast.getIoType()) {
            case 108:
            case 12:
                agvTaskCreateParam.setTaskTyp("F06");
                break;
            case 109:
                if (agvWrkMast.getWrkSts() == 201) {
                    agvTaskCreateParam.setTaskTyp("F06");
                } else {
                    agvTaskCreateParam.setTaskTyp("F01");
                }
                break;
            case 1:
            case 57:
                // 入库任务是只有 接驳位 -- > 库位
                AgvLocMast loc = agvLocMastService.selectOne(new EntityWrapper<AgvLocMast>().eq("loc_no", agvWrkMast.getLocNo()));
                String locSub = loc.getLocNo().substring(loc.getLocNo().length() - 4).substring(0, 2).substring(0, 2);
                AgvBasDevp agvBasDevp = agvBasDevpService.selectByDevNo(agvWrkMast.getSourceLocNo());
                if ("02".equals(locSub)) {
                    if (agvBasDevp.getFloor() == 4) {
                        agvTaskCreateParam.setTaskTyp("F01");
                    } else {
                        agvTaskCreateParam.setTaskTyp("F06");
                    }
 
                } else {
                    // 目标楼层位1楼 但源库位(接驳位)在吸塑二楼
                    if (agvBasDevp.getFloor() == 4) {
                        agvTaskCreateParam.setTaskTyp("F06");
                    } else {
                        agvTaskCreateParam.setTaskTyp("F01");
                    }
 
 
                }
                break;
            default:
                agvTaskCreateParam.setTaskTyp("F01");
                break;
        }
 
        agvTaskCreateParam.setPositionCodePath(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 = Integer.parseInt(jsonObject.get("code").toString());
            if(code != 0){
                //TODO 张超
                //throw new CoolException("调用AGV响应错误");
                return code;
            }
            success = true;
            return code;
        }catch (Exception e){
            log.error(e.getMessage());
            //TODO 张超
            //throw new CoolException("调用AGV响应错误");
            return 1;
        }finally {
            apiLogService.save(
                    namespace,
                    url + path,
                    appkey,
                    ip,
                    JSON.toJSONString(JSONObject.toJSONString(requestParam)),
                    response,
                    success
            );
        }
 
    }
 
}