#
zjj
2024-07-19 ec269d75b05661235513e252d048c82ccfb26c9b
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package com.zy.asrs.wcs.core.timer;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zy.asrs.common.utils.HttpHandler;
import com.zy.asrs.framework.common.Cools;
import com.zy.asrs.wcs.core.domain.param.AgvTaskCreateParam;
import com.zy.asrs.wcs.core.domain.param.CreateWcsTaskParam;
import com.zy.asrs.wcs.core.domain.param.ReturnWcsTaskStatisParam;
import com.zy.asrs.wcs.core.entity.*;
import com.zy.asrs.wcs.core.model.enums.LocStsType;
import com.zy.asrs.wcs.core.model.enums.TaskStsType;
import com.zy.asrs.wcs.core.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.*;
 
@Slf4j
@Component
public class TaskTimer {
 
    @Autowired
    private TaskService taskService;
    @Autowired
    private TaskLogService taskLogService;
    @Autowired
    private MotionService motionService;
    @Autowired
    private MotionLogService motionLogService;
    @Autowired
    private LocService locService;
    @Autowired
    private BasConveyorStaService basConveyorStaService;
 
    //出库转发agv
    @Scheduled(cron = "0/1 * * * * ? ")
    @Transactional
    public synchronized void sendOutTaskToAgv() {
        ArrayList<Long> taskSts = new ArrayList<>();
        taskSts.add(TaskStsType.COMPLETE_OUTBOUND.sts);
 
        BasConveyorSta basConveyorSta = basConveyorStaService.getOne(new LambdaQueryWrapper<BasConveyorSta>().eq(BasConveyorSta::getSiteNo, 100));
        if (basConveyorSta == null) {
            return;
        }
        if (basConveyorSta.getWorkMode() != 2){
            return;
        }
        if (basConveyorSta.getTaskNo() == 0){
            return;
        }
        if (!basConveyorSta.getAutoing().equals("Y") || !basConveyorSta.getLoading().equals("Y")) {
            return;
        }
 
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getStatus, 1)
                .in(Task::getTaskSts, taskSts));
        for (Task task : tasks) {
            if (Cools.isEmpty(task.getMemo())){
                task.setTaskSts(999L);
                taskService.updateById(task);
                continue;
            }else if (task.getMemo().equals("06YZ0001")){
                HashMap<String, String> mesMap = new HashMap<>();
                mesMap.put("taskNo", task.getWmsTaskNo());
                mesMap.put("contNo",task.getZpallet());
                boolean returnTask4 = toMesHttpRequest(mesMap, "172.18.231.126", "/api/wcs/wcsTaskInfo", "4",task.getZpallet());
                if (returnTask4){
                    task.setTaskSts(1001L);
                    taskService.updateById(task);
                    log.info("出库任务完成推送mes"+task.getWmsTaskNo());
                }else {
                    log.error("出库任务完成推送mes失败"+task.getWmsTaskNo());
                }
                continue;
            }
            if (task.getTaskNo().equals(basConveyorSta.getTaskNo().toString())) {
                //绑定托盘码
                boolean httpRequest1 = agvBindBarcodeHttpRequest("", "", task);
                AgvTaskCreateParam agvTaskCreateParam = new AgvTaskCreateParam();
                getOutRequestParam(agvTaskCreateParam,task);
                //发送出库任务
                boolean httpRequest = doHttpRequest(agvTaskCreateParam, "172.18.16.248:443", "/rcs/rtas/api/robot/controller/task/submit");
                if (httpRequest){
                    task.setTaskSts(1000L);
                    taskService.updateById(task);
                    log.info("出库任务推送rgv"+task.getWmsTaskNo());
                }else {
                    log.error("出库任务推送rgv失败"+task.getWmsTaskNo());
                }
            }
        }
    }
 
    private void getOutRequestParam(AgvTaskCreateParam agvTaskCreateParam, Task param){
        List<AgvTaskCreateParam.AgvTaskParam> agvTaskParamList = Arrays.asList(
                //起始位
                new AgvTaskCreateParam.AgvTaskParam("SITE","06YZ0001",0,1),
                //目标位
                new AgvTaskCreateParam.AgvTaskParam("SITE",param.getMemo(),1,1)
        );
        List<AgvTaskCreateParam.carrierInfoParam> carrierInfoParam = Arrays.asList(
                //起始位
                new AgvTaskCreateParam.carrierInfoParam()
 
        );
        agvTaskCreateParam.setTaskType("F01");
        agvTaskCreateParam.setInitPriority(param.getPriority().toString());
        agvTaskCreateParam.setRobotTaskCode(param.getWmsTaskNo());
        agvTaskCreateParam.setTargetRoute(agvTaskParamList);
        agvTaskCreateParam.setCarrierInfo(carrierInfoParam);
 
    }
    private boolean agvBindBarcodeHttpRequest(String url, String path,Task task){
        String response = "";
        boolean success = false;
        Map<String, Object> headers = new HashMap<>();
        headers.put("Content-Type", "application/json;charset=UTF-8");
        headers.put("X-lr-request-id", task.getWmsTaskNo());
        headers.put("X-lr-version", 4.1);
        headers.put("X-lr-trace-id", "{{$guid}}");
 
        Map<String, Object> map = new HashMap<>();
        map.put("carrierCode",task.getZpallet());
        map.put("siteCode",task.getMemo());
        map.put("extra",null);
        try {
            response = new HttpHandler.Builder()
                    .setHeaders(headers)
                    .setUri("172.18.16.248:443")
                    .setHttps(true)
                    .setPath("/rcs/rtas/api/robot/controller/carrier/bind")
                    .setJson(JSONObject.toJSONString(map))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            String code = jsonObject.get("code").toString();
            log.info("agv绑定箱号,请求体:"+JSONObject.toJSONString(map)+",返回:"+response);
            if(code.equals("SUCCESS")){
                success = true;
            }
        }catch (Exception e){
            log.info("agv绑定箱号请求报错"+e.getMessage());
        }
        return success;
    }
 
    private boolean doHttpRequest(AgvTaskCreateParam requestParam, String url, String path){
        String response = "";
        boolean success = false;
        Map<String, Object> map = new HashMap<>();
        map.put("Content-Type", "application/json;charset=UTF-8");
        map.put("X-lr-request-id", requestParam.getRobotTaskCode());
        map.put("X-lr-version", 4.1);
        map.put("X-lr-trace-id", "{{$guid}}");
        log.info("转发agv请求报文:"+JSONObject.toJSONString(requestParam));
        try {
            response = new HttpHandler.Builder()
                    .setHeaders(map)
                    .setUri(url)
                    .setHttps(true)
                    .setPath(path)
                    .setJson(JSONObject.toJSONString(requestParam))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
            log.info("转发agv请求返回:"+response);
            String code = jsonObject.get("code").toString();
            if(code.equals("SUCCESS")){
                success = true;
            }
 
        }catch (Exception e){
            log.info("转发agv请求报错"+e.getMessage());
        }
        return success;
    }
 
    @Scheduled(cron = "0/1 * * * * ? ")
    @Transactional
    public synchronized void clearCompletedTask() {
        ArrayList<Long> taskSts = new ArrayList<>();
        taskSts.add(TaskStsType.COMPLETE_INBOUND.sts);
 
 
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getStatus, 1)
                .in(Task::getTaskSts, taskSts));
        for (Task task : tasks) {
 
            if (Cools.isEmpty(task.getMemo())){
                task.setTaskSts(998L);
                taskService.updateById(task);
                continue;
            }
 
            HashMap<String, String> mesMap = new HashMap<>();
            mesMap.put("taskNo", task.getWmsTaskNo());
            mesMap.put("contNo",task.getZpallet());
            boolean returnTask4 = toMesHttpRequest(mesMap, "172.18.231.126", "/api/wcs/wcsTaskInfo", "4",task.getMemo());
            if (returnTask4){
                log.info("任务完成后上报mes成功,任务号:"+task.getWmsTaskNo());
                //记录库存信息
                updateRecordLoc(task);
                //任务转历史档
                saveTaskLog(task);
 
            }else {
                log.error("任务完成后返回mes失败");
            }
 
        }
    }
 
    @Scheduled(cron = "0/1 * * * * ? ")
    @Transactional
    public synchronized void clearCompletedTask999() {
        ArrayList<Long> taskSts = new ArrayList<>();
        taskSts.add(TaskStsType.OUT_TASK_OVER_HAND.sts);
        taskSts.add(TaskStsType.OUT_TASK_OVER.sts);
        taskSts.add(TaskStsType.COMPLETE_CHARGE.sts);
        taskSts.add(TaskStsType.COMPLETE_MOVE.sts);
        taskSts.add(TaskStsType.COMPLETE_MANUAL.sts);
        taskSts.add(TaskStsType.COMPLETE_LADEN_MOVE.sts);
        taskSts.add(TaskStsType.IN_TASK_OVER_HAND.sts);
 
 
        List<Task> tasks = taskService.list(new LambdaQueryWrapper<Task>()
                .eq(Task::getStatus, 1)
                .in(Task::getTaskSts, taskSts));
        for (Task task : tasks) {
                //记录库存信息
                updateRecordLoc(task);
                //任务转历史档
                saveTaskLog(task);
 
        }
    }
 
    //更新库存信息
    @Transactional
    public void updateRecordLoc(Task task) {
        if (task.getRecordLoc() == null) {
            return;
        }
 
        if (task.getRecordLoc().equals("Y")) {//记录库存信息
            //源库位 => 空库
            //目标库位 => 在库
            Loc originLoc = locService.selectByLocNo(task.getOriginLoc());
            if (originLoc != null) {
                originLoc.setLocSts(LocStsType.O.val());
                locService.updateById(originLoc);
            }
 
            Loc destLoc = locService.selectByLocNo(task.getDestLoc());
            if (destLoc != null) {
                destLoc.setLocSts(LocStsType.F.val());
                locService.updateById(destLoc);
            }
        } else if (task.getRecordLoc().equals("record-dest")) {//只记录目标库位信息
            //目标库位 => 在库
            Loc destLoc = locService.selectByLocNo(task.getDestLoc());
            if (destLoc != null) {
                destLoc.setLocSts(LocStsType.F.val());
                locService.updateById(destLoc);
            }
        }
    }
 
    //更新历史档
    @Transactional
    public void saveTaskLog(Task task) {
        //创建历史档
        TaskLog taskLog = new TaskLog();
        taskLog.sync(task);
        taskLog.setUpdateTime(new Date());
        taskLogService.save(taskLog);
 
        List<Motion> motions = motionService.list(new LambdaQueryWrapper<Motion>().eq(Motion::getTaskNo, task.getTaskNo()).eq(Motion::getHostId, task.getHostId()));
        for (Motion motion : motions) {
            //创建动作历史档
            MotionLog motionLog = new MotionLog();
            motionLog.sync(motion);
            motionLog.setUpdateTime(new Date());
            motionLogService.save(motionLog);
        }
 
        //删除源任务
        taskService.removeById(task.getId());
        //删除动作
        motionService.remove(new LambdaQueryWrapper<Motion>().eq(Motion::getTaskNo, task.getTaskNo()).eq(Motion::getHostId, task.getHostId()));
    }
 
    private boolean toMesHttpRequest(Map<String,String> map, String url, String path, String code,String localtion){
        Date now = new Date();
        long time = now.getTime()/1000;
        ReturnWcsTaskStatisParam returnWcsTaskStatisParam = new ReturnWcsTaskStatisParam();
        returnWcsTaskStatisParam.setRequestPK(map.get("taskNo"));
        returnWcsTaskStatisParam.setTrkId(map.get("taskNo"));
        returnWcsTaskStatisParam.setContNo(map.get("contNo"));
        returnWcsTaskStatisParam.setCode(code);
        returnWcsTaskStatisParam.setOperator("wcs");
        returnWcsTaskStatisParam.setOperationTime(time);
        returnWcsTaskStatisParam.setTrkType("01");
        returnWcsTaskStatisParam.setCurPos(localtion);
        String response = "";
        boolean success = false;
        try {
            response = new HttpHandler.Builder()
                    .setUri(url)
                    .setPath(path)
                    .setJson(JSONObject.toJSONString(returnWcsTaskStatisParam))
                    .build()
                    .doPost();
            JSONObject jsonObject = JSON.parseObject(response);
 
            String code1 = jsonObject.get("success").toString();
            if(code1.equals("1")){
                success = true;
            }
        }catch (Exception e){
            return success;
        }
        return success;
    }
 
}