Files
scrq-hd/.svn/pristine/11/110edb0e4c2f82da039b32a3f606c0da2296df25.svn-base
2025-07-03 10:34:04 +08:00

143 lines
5.1 KiB
Plaintext

package com.cmeim.stock.service;
import com.alibaba.fastjson.JSONObject;
import com.cmeim.common.core.exception.ServiceException;
import com.cmeim.common.core.utils.DateUtil;
import com.cmeim.common.core.web.domain.RespondEnum;
import com.cmeim.stock.po.AgvTask;
import com.cmeim.stock.repository.AgvTaskRepository;
import com.cmeim.stock.util.HttpRequest;
import com.cmeim.stock.vo.AgvRequestVo;
import com.sun.org.apache.regexp.internal.RE;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/**
* @Verasion:1.0
* @Author:DZY
* @Date:2023/6/26
**/
@Service
public class AGVService {
@Autowired
private AgvTaskRepository agvTaskRepository;
/**
* 生成AGV搬运请求参数
* @param requestVo
*/
public String taskParamCreate(AgvRequestVo requestVo) {
UUID uuid = UUID.randomUUID();
String taskCode = uuid.toString();
JSONObject data = new JSONObject(true);
//搬运方式
if (requestVo.getType() == 1) {
//货架方式
data.put("taskType", "putaway");
} else {
//搬运方式
data.put("taskType", "carry");
}
//任务组优先级
data.put("groupPriority", 0);
List<JSONObject> taskList = new ArrayList<>();
JSONObject data1 = new JSONObject(true);
//任务单号
data1.put("taskCode", taskCode);
//任务优先级
data1.put("taskPriority", 0);
JSONObject data2 = new JSONObject(true);
//容器编码
data2.put("containerCode", requestVo.getContainerCode());
//容器类型
data2.put("containerType", "CT_KUBOT_STANDARD");
//工作为标签
data2.put("storageTag", "");
//起始工作位
data2.put("fromLocationCode", requestVo.getFromLocationCode());
//目标工作位
data2.put("toLocationCode", requestVo.getToLocationCode());
//目标工作站
data2.put("toStationCode", "");
data1.put("taskDescribe", data2);
taskList.add(data1);
data.put("tasks", taskList);
final String result = taskCreate(JSONObject.toJSONString(data), requestVo.getRequestType());
return result;
}
/**
* 发送AGV搬运请求
* @param param 请求参数
* @param requestType 请求类型
*/
public String taskCreate(String param, Integer requestType) {
final String result = HttpRequest.doPost("http://{IP:PORT}/task/create", JSONObject.toJSONString(param));
if (JSONObject.parseObject(result).getInteger("code") == 0) {
//请求单号
String taskCode = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(param)
.getJSONArray("tasks").get(0))).getString("taskCode");
updateAgvTask(taskCode, requestType, param, 2,"");
} else {
String taskCode = JSONObject.parseObject(JSONObject.toJSONString(JSONObject.parseObject(param)
.getJSONArray("tasks").get(0))).getString("taskCode");
updateAgvTask(taskCode, requestType, param, 4,JSONObject.parseObject(result).getString("msg"));
}
return result;
}
/**
* 更新agv任务表
* @param taskCode 请求单号
* @param requestType 请求类型
* @param param 请求参数
* @param status 请求状态
*/
public void updateAgvTask(String taskCode, Integer requestType, String param, Integer status,String message) {
AgvTask agvTask = new AgvTask();
agvTask = agvTaskRepository.findByRequestCode(taskCode);
if (null == agvTask) {
agvTask.setCreatedBy("admin");
agvTask.setCreatedBy(DateUtil.getCurrentDate());
agvTask.setDictType(requestType);
} else {
agvTask.setUpdatedBy("admin");
agvTask.setUpdatedDt(DateUtil.getCurrentDate());
}
agvTask.setJson(param);
agvTask.setRequestCode(taskCode);
agvTask.setStatus(status);
agvTaskRepository.save(agvTask);
}
/**
* 取消AGV任务
* @param taskCode
*/
public void taskCancel(String taskCode){
List<String> list=new ArrayList<>();
list.add(taskCode);
JSONObject data=new JSONObject();
data.put("taskCodes",list);
final AgvTask agvTask = agvTaskRepository.findByRequestCodeAndStatus(taskCode,2);
if(null==agvTask)
{
throw new ServiceException(RespondEnum.FAILURE,"没有该待执行任务");
}
final String result = HttpRequest.doPost("http://{IP:PORT}/task/cancel", JSONObject.toJSONString(data));
if (JSONObject.parseObject(result).getInteger("code") == 0) {
updateAgvTask(taskCode, agvTask.getDictType(), agvTask.getJson(), 5,"");
} else {
updateAgvTask(taskCode, agvTask.getDictType(), agvTask.getJson(), 4,JSONObject.parseObject(result).getString("msg"));
}
}
}