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;
|
}
|
|
}
|