自动化立体仓库 - WCS系统
#
zjj
2023-12-11 c8b2322c5c43b6e627ff724405281bc53c6c98f5
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
package com.zy.asrs.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.core.common.Cools;
import com.core.common.R;
import com.core.exception.CoolException;
import com.zy.asrs.domain.enums.TaskStatusType;
import com.zy.asrs.entity.StaDesc;
import com.zy.asrs.entity.TaskWrk;
import com.zy.asrs.entity.ToWmsDTO;
import com.zy.asrs.entity.param.TaskOverParam;
import com.zy.asrs.entity.param.WMSAndAGVInterfaceParam;
import com.zy.asrs.entity.param.taskCreateParam;
import com.zy.asrs.service.ApiLogService;
import com.zy.asrs.service.OpenService;
import com.zy.asrs.service.StaDescService;
import com.zy.asrs.service.TaskWrkService;
import com.zy.asrs.utils.Utils;
import com.zy.common.utils.HttpHandler;
import com.zy.core.DevpThread;
import com.zy.core.cache.MessageQueue;
import com.zy.core.cache.SlaveConnection;
import com.zy.core.enums.SlaveType;
import com.zy.core.model.DevpSlave;
import com.zy.core.model.Task;
import com.zy.core.model.protocol.StaProtocol;
import com.zy.core.properties.SlaveProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;
 
@Service
public class OpenServiceImpl implements OpenService {
 
    @Autowired
    private TaskWrkService taskWrkService;
    @Autowired
    private SlaveProperties slaveProperties;
    @Autowired
    private ApiLogService apiLogService;
 
    @Autowired
    private StaDescService staDescService;
 
 
 
    @Value("${wms.url}")
    private String wmsUrl;
    public ArrayList<String> wharfCode1 = new ArrayList<String>(){{
        add("J-1101"); add("J-1103"); add("J-1105"); add("J-1107"); add("J-1109"); add("J-1111");
    }};
 
 
 
    @Override
    public R taskCreate(taskCreateParam param) {
        TaskWrk taskWrk = taskWrkService.selectByTaskNo(param.getTaskNo());
        if (taskWrk != null) {
            throw new CoolException(param.getTaskNo() + "任务已存在,请勿重复提交");
        }
        if (param.getIoType().equals(0)){
            throw new CoolException("生成任务失败,任务类型不存在!");
        }
 
        Date now = new Date();
        taskWrk = new TaskWrk();
        taskWrk.setTaskNo(param.getTaskNo());//任务号
        taskWrk.setStatus(TaskStatusType.RECEIVE.id);//任务状态:接收
        taskWrk.setCreateTime(now);
        taskWrk.setIoType(param.getIoType());//任务类型
        taskWrk.setIoPri(param.getTaskPriority());//优先级
        taskWrk.setBarcode(param.getBarcode());//条码
        taskWrk.setCrnNo(param.getCrn());
        if(param.getIoType() == 1){
            taskWrk.setWrkSts(1);
            if (!Cools.isEmpty(param.getTargetPoint())) {
                taskWrk.setTargetPoint(Utils.getWcsLocNo(param.getTargetPoint()));//终点
                taskWrk.setOriginTargetPoint(param.getTargetPoint());
            }
 
        } else if (param.getIoType() == 2) {
            taskWrk.setWrkSts(11);
            if (!Cools.isEmpty(param.getStartPoint())) {
                taskWrk.setStartPoint(Utils.getWcsLocNo(param.getStartPoint()));//起点
                taskWrk.setOriginStartPoint(param.getStartPoint());
            }
            taskWrk.setTargetPoint(param.getTargetPoint());
        }
 
 
 
        if (!Cools.isEmpty(param.getMemo())) {
            taskWrk.setMemo(param.getMemo());//备注
        }
 
        if (!taskWrkService.insert(taskWrk)) {
            return R.error("创建任务失败");
        }
        return R.ok();
    }
 
    @Override
    public R AgvToWCSToWms(WMSAndAGVInterfaceParam param) throws IOException {
        ToWmsDTO toWmsDTO = new ToWmsDTO();
        Map<String, Object> map = new HashMap<>();
        map.put("x-api-key","7a15b5db-29b6-552c-8cff-0cfec3756da2");
        List<Integer> list = new ArrayList<>();
        if (param.getWharfSource().contains("J")){
            list = getInEnableRoadway();
        }else {
            list = getInEnableRoadway2();
        }
 
 
        //TaskWrk taskWrk = taskWrkService.selectByTaskNo(param.getTaskNo());
        toWmsDTO.setWarehouseId("1688469798893297665");
        toWmsDTO.setContainerCode(param.getContainerCode());
        toWmsDTO.setApplyType("TUNNEL");
        toWmsDTO.setWharfSource(null);
        toWmsDTO.setCanInboundTunnels(list);
        String response = null;
        try {
            response = new HttpHandler.Builder()
                    .setHeaders(map)
                    .setUri(wmsUrl)
                    .setPath("wcsManager/wcsInterface/inboundTaskApply")
                    .setJson(JSON.toJSONString(toWmsDTO))
                    .build()
                    .doPost();
        }catch (Exception e){
            return R.error("wms连接失败"+e.getMessage());
        }
 
        JSONObject jsonObject = JSON.parseObject(response);
 
        apiLogService.save("wms请求入库码头接口"
                ,wmsUrl+"wcsManager/wcsInterface/inboundTaskApply"
                ,null
                ,"127.0.0.1"
                ,JSON.toJSONString(param)
                ,response
                ,true
        );
 
        if (jsonObject.getInteger("code").equals(200)) {
            //解析
            List<WMSAndAGVInterfaceParam> params = new ArrayList<>();
            params.add(param);
            WMSAndAGVInterfaceParam data = JSONObject.parseObject(jsonObject.get("data").toString(), WMSAndAGVInterfaceParam.class);
            String msg = null;
            try {
                taskCreate(new taskCreateParam(data));
            }catch (Exception e){
                msg = e.getMessage();
            }
            if (msg != null){
                return R.error(msg);
            }
            Map<String,String> map1 = new HashMap<>();
            StaDesc staDesc = new StaDesc();
            if (param.getWharfSource().contains("J")){
                staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                        .eq("crn_no",data.getTaskTunnel())
                        .eq("type_no",1)
                        .lt("crn_stn",200));
            }else {
                staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                        .eq("crn_no",data.getTaskTunnel())
                        .eq("type_no",1)
                        .ge("crn_stn",200));
            }
            //map1.put("taskTunnel", staDesc.getStnDesc());
            map1.put("taskTunnel", "J-1104");
            return R.ok(map1);
        }
        return R.error((String) jsonObject.get("msg"));
    }
 
    @Override
    public void getAgvTaskOver(TaskOverParam param) {
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            StaDesc staDesc = staDescService.selectOne(new EntityWrapper<StaDesc>()
                    .eq("stn_desc", param.getWharfCode()));
            if (Cools.isEmpty(staDesc)){
                throw new RuntimeException("未查询到站点信息");
            }
            // 获取入库站信息
            DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
            StaProtocol staProtocol = devpThread.getStation().get(staDesc.getStnNo());
            if (staProtocol == null) {
                continue;
            } else {
                staProtocol = staProtocol.clone();
            }
            Short workNo = staProtocol.getWorkNo();
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                if (inSta.getBackSta().equals(staDesc.getStnNo())){
                    if (wharfCode1.contains(param.getWharfCode())){
                        if (!staProtocol.isLoading() && !staProtocol.isCar()){
                            //AGV出库完成以后
                        }
 
                    }else {
                        if (staProtocol.isLoading() && !staProtocol.isCar() && workNo == 0){
                            //AGV入库完成以后
                            staProtocol.setWorkNo((short) 9999);
                            staProtocol.setStaNo(inSta.getStaNo().shortValue());
                            boolean result = MessageQueue.offer(SlaveType.Devp, devp.getId(), new Task(3, staProtocol));
                            if (result) {
 
                            } else {
                                throw new RuntimeException("agv返回完成后,命令下发失败");
                            }
                        }
                    }
                }
            }
 
 
 
        }
 
    }
 
    public List<Integer> getInEnableRoadway(){
        int[] roadway = null;
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new HashMap<>();
        map.put(103,1);
        map.put(107,2);
        map.put(111,3);
        map.put(115,4);
        map.put(119,5);
        map.put(123,6);
        //put(301,7);
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getBackSta());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                if (workNo == 0 && !staProtocol.isLoading()){
                    if (Cools.isEmpty( map.get(inSta.getStaNo()))){
                        continue;
                    }
                    list.add( map.get(inSta.getStaNo()));
                }
            }
        }
        //roadway = list.stream().mapToInt(Integer::intValue).toArray();
        return list;
    }
 
    public List<Integer> getInEnableRoadway2(){
        int[] roadway = null;
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> map = new HashMap<>();
        map.put(205,1);
        map.put(211,2);
        map.put(217,3);
        map.put(223,4);
        map.put(229,5);
        map.put(235,6);
        //put(301,7);
        // 根据输送线plc遍历
        for (DevpSlave devp : slaveProperties.getDevp()) {
            // 遍历入库口
            for (DevpSlave.Sta inSta : devp.getInSta()) {
                // 获取入库站信息
                DevpThread devpThread = (DevpThread) SlaveConnection.get(SlaveType.Devp, devp.getId());
                StaProtocol staProtocol = devpThread.getStation().get(inSta.getBackSta());
                if (staProtocol == null) {
                    continue;
                } else {
                    staProtocol = staProtocol.clone();
                }
                Short workNo = staProtocol.getWorkNo();
                if (workNo == 0 && !staProtocol.isLoading()){
                    if (Cools.isEmpty( map.get(inSta.getStaNo()))){
                        continue;
                    }
                    list.add( map.get(inSta.getStaNo()));
                }
            }
        }
        //roadway = list.stream().mapToInt(Integer::intValue).toArray();
        return list;
    }
 
 
}