代码更新

This commit is contained in:
ldj_willow
2022-10-25 09:55:30 +08:00
parent 913cf0d696
commit d0b41eca0e
50 changed files with 836 additions and 1483 deletions

View File

@@ -0,0 +1,551 @@
package org.nl.acs.agv.server.impl;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.nl.acs.agv.server.ZheDaAgvService;
import org.nl.acs.config.AcsConfig;
import org.nl.acs.config.server.AcsConfigService;
import org.nl.acs.instruction.service.InstructionService;
import org.nl.acs.instruction.service.dto.Instruction;
import org.nl.acs.opc.Device;
import org.nl.acs.opc.DeviceAppService;
import org.nl.acs.opc.DeviceAppServiceImpl;
import org.nl.utils.SpringContextHolder;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
@Slf4j
@Service
@RequiredArgsConstructor
public class ZheDaAgvServiceImpl implements ZheDaAgvService {
private final AcsConfigService acsConfigService;
@Override
//ZDAGV
public HttpResponse sendAgvInstToZDAgv(Instruction inst) throws Exception {
JSONObject jo = new JSONObject();
String start_point_code = inst.getStart_point_code();
String next_point_code = inst.getNext_point_code();
//1、叠盘架追加任务2、养生区需要追加任务在缓存点等待3、二楼普通任务4、一楼普通任务
String task_type = inst.getInstruction_type();
jo.put("deadline", getNextDay(1));
//判断是否追加任务
if (task_type.equals("2") || task_type.equals("1")) {
jo.put("complete", "false");
} else {
jo.put("complete", "true");
}
jo.put("task_code", inst.getInstruction_code());
//根据任务,下发指令类型
JSONArray destinations = new JSONArray();
if (task_type.equals("1")) {
destinations.add(destination(start_point_code, "Wait", "5", "1"));
} else {
destinations.add(destination(start_point_code, "Load", "1", "1"));
if (task_type.equals("2")) {
destinations.add(destination(next_point_code, "Wait", "5", "1"));
} else {
destinations.add(destination(next_point_code, "Unload", "5", "1"));
}
}
jo.put("destinations", destinations);
if (StrUtil.equals(acsConfigService.findConfigFromCache().get(AcsConfig.FORKAGV).toString(), "1")) {
String url = "";
String agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL);
String agvurl2 = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL2);
String agvport = acsConfigService.findConfigFromCache().get(AcsConfig.AGVPORT);
//不同楼层下发不同的agv系统
if (task_type.equals("8")) {
url = agvurl2;
} else {
url = agvurl;
}
url = url + ":" + agvport + "v1/transportOrders/" + inst.getInstruction_code();
log.info("下发agv任务请求:{}", url);
HttpResponse result = HttpRequest.post(agvurl)
.body(String.valueOf(jo))//表单内容
.timeout(20000)//超时,毫秒
.execute();
log.info("下发agv任务请求反馈:{}", result);
return result;
} else {
return null;
}
}
@Override
//ZDAGV
public HttpResponse queryZDAgvInstStatus(String type) {
if (StrUtil.equals(acsConfigService.findConfigFromCache().get(AcsConfig.FORKAGV).toString(), "1")) {
String agvurl = "";
if (type.equals("1")) {
agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL);
}
if (type.equals("2")) {
agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL2);
}
String agvport = acsConfigService.findConfigFromCache().get(AcsConfig.AGVPORT);
agvurl = agvurl + ":" + agvport + "/transportOrders";
HttpResponse result = HttpRequest.get(agvurl)
.timeout(20000)//超时,毫秒
.execute();
System.out.println("查询agv指令数据:" + result.body());
return result;
} else {
return null;
}
}
@Override
//ZDAGV
public synchronized String process(String jobno, String type, String address, String action, String processingVehicle) {
log.info("查询到AGV请求参数,jobno:{},address:{}", jobno + ",address:" + address + ",type:" + type + ",action:" + action);
//释放AGV资源继续后续动作
boolean is_feedback = false;
String str = "";
String backaddress = address;
if (address.indexOf(".") > 0) {
str = address.substring(address.indexOf(".") + 1, address.length());
address = address.substring(0, address.indexOf("."));
} else if (address.indexOf("-") > 0) {
address = address.substring(0, address.indexOf("-"));
}
InstructionService instructionService = SpringContextHolder.getBean("instructionServiceImpl");
Instruction inst = instructionService.findByCodeFromCache(jobno);
DeviceAppService appService = SpringContextHolder.getBean(DeviceAppServiceImpl.class);
Device addressdevice = appService.findDeviceByCode(address);
CargoLiftConveyorDeviceDriver cargoLiftConveyorDeviceDriver;
EmptyVehicleStackingPositionDeviceDriver emptyVehicleStackingPositionDeviceDriver;
HailiangSmartplcTestDeviceDriver hailiangSmartplcTestDeviceDriver;
HaoKaiAutoConveyorDeviceDriver haoKaiAutoConveyorDeviceDriver;
PaintConveyorDeviceDriver paintConveyorDeviceDriver;
//取货的进入前等待和离开等待
if (action.equals("Load")) {
if ("EntryRequired".equals(type)) {
//共挤线三工位
if (addressdevice.getDeviceDriver() instanceof HailiangSmartplcTestDeviceDriver) {
hailiangSmartplcTestDeviceDriver = (HailiangSmartplcTestDeviceDriver) addressdevice.getDeviceDriver();
if ((hailiangSmartplcTestDeviceDriver.getAction() == 1 || hailiangSmartplcTestDeviceDriver.getAction() == 3) && hailiangSmartplcTestDeviceDriver.getMove() == 1) {
inst.setExecute_status("1");
is_feedback = true;
}
}
//叠盘位
if (addressdevice.getDeviceDriver() instanceof EmptyVehicleStackingPositionDeviceDriver) {
emptyVehicleStackingPositionDeviceDriver = (EmptyVehicleStackingPositionDeviceDriver) addressdevice.getDeviceDriver();
int number = emptyVehicleStackingPositionDeviceDriver.getNumber();
if (number < Integer.valueOf(str)) {
log.info("叠盘位:" + jobno + "当前层高为:" + number + ",不存在第" + str + "的托盘!");
return null;
}
inst.setExecute_status("1");
is_feedback = true;
}
//货梯对接线
if (addressdevice.getDeviceDriver() instanceof CargoLiftConveyorDeviceDriver) {
cargoLiftConveyorDeviceDriver = (CargoLiftConveyorDeviceDriver) addressdevice.getDeviceDriver();
if ((cargoLiftConveyorDeviceDriver.getAction() == 1 || cargoLiftConveyorDeviceDriver.getAction() == 3) && cargoLiftConveyorDeviceDriver.getMove() == 1) {
inst.setExecute_status("1");
is_feedback = true;
}
}
//豪凯自动线对接位
if (addressdevice.getDeviceDriver() instanceof HaoKaiAutoConveyorDeviceDriver) {
haoKaiAutoConveyorDeviceDriver = (HaoKaiAutoConveyorDeviceDriver) addressdevice.getDeviceDriver();
if ((haoKaiAutoConveyorDeviceDriver.getAction() == 1 || haoKaiAutoConveyorDeviceDriver.getAction() == 3) && haoKaiAutoConveyorDeviceDriver.getMove() == 1) {
inst.setExecute_status("1");
is_feedback = true;
}
}
//油漆线
if (addressdevice.getDeviceDriver() instanceof PaintConveyorDeviceDriver) {
paintConveyorDeviceDriver = (PaintConveyorDeviceDriver) addressdevice.getDeviceDriver();
if ((paintConveyorDeviceDriver.getAction() == 1 || paintConveyorDeviceDriver.getAction() == 3) && paintConveyorDeviceDriver.getMove() == 1) {
inst.setExecute_status("1");
is_feedback = true;
}
}
}
if ("PauseOnStation".equals(type)) {
if (addressdevice.getDeviceDriver() instanceof HailiangSmartplcTestDeviceDriver) {
hailiangSmartplcTestDeviceDriver = (HailiangSmartplcTestDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("2");
hailiangSmartplcTestDeviceDriver.writing(2);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof EmptyVehicleStackingPositionDeviceDriver) {
emptyVehicleStackingPositionDeviceDriver = (EmptyVehicleStackingPositionDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("2");
emptyVehicleStackingPositionDeviceDriver.writing(2);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof CargoLiftConveyorDeviceDriver) {
cargoLiftConveyorDeviceDriver = (CargoLiftConveyorDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("2");
cargoLiftConveyorDeviceDriver.writing(2);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof HaoKaiAutoConveyorDeviceDriver) {
haoKaiAutoConveyorDeviceDriver = (HaoKaiAutoConveyorDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("2");
haoKaiAutoConveyorDeviceDriver.writing(2);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof PaintConveyorDeviceDriver) {
paintConveyorDeviceDriver = (PaintConveyorDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("2");
paintConveyorDeviceDriver.writing(2);
is_feedback = true;
}
}
}
//等待点等待
if (action.equals("Wait")) {
if ("Wait".equals(type)) {
JSONObject jo = new JSONObject();
jo.put("task_code", inst.getInstruction_code());
JSONArray destinations = new JSONArray();
String inst_type = inst.getInstruction_type();
//如果任务类型为1在点位进行等待则查询当前叠盘位的数量取当前数量的层数进行追加任务
if ("1".equals(inst_type)) {
emptyVehicleStackingPositionDeviceDriver = (EmptyVehicleStackingPositionDeviceDriver) addressdevice.getDeviceDriver();
int current_num = emptyVehicleStackingPositionDeviceDriver.getNumber();
if (current_num > 12) {
log.info("当前叠盘架:" + jobno + "已放满!");
return null;
}
String start_point_code = inst.getStart_point_code();
String next_point_code = inst.getNext_point_code();
start_point_code = start_point_code + "." + (current_num + 1);
destinations.add(destination(start_point_code, "Load", "1", "1"));
destinations.add(destination(next_point_code, "Unload", "1", "1"));
}
//如果任务类型为2在点位进行等待则调用LMS的货位申请接口
if ("2".equals(inst_type)) {
String next_point_code = "";
//调用LMS接口
destinations.add(destination(next_point_code, "Unload", "5", "1"));
}
jo.put("destinations", destinations);
String agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL);
String agvport = acsConfigService.findConfigFromCache().get(AcsConfig.AGVPORT);
String url = agvurl + ":" + agvport + "addDestinations";
log.info("下发agv任务请求:{}", url);
HttpResponse result = HttpRequest.post(url)
.body(String.valueOf(jo))//表单内容
.timeout(20000)//超时,毫秒
.execute();
log.info("下发agv任务请求反馈:{}", result);
//对任务进行封口
JSONObject complete = new JSONObject();
complete.put("task_code", inst.getInstruction_code());
String url2 = agvurl + ":" + agvport + "markComplete";
log.info("下发agv任务请求:{}", url2);
HttpResponse result2 = HttpRequest.post(url2)
.body(String.valueOf(complete))//表单内容
.timeout(20000)//超时,毫秒
.execute();
log.info("下发agv任务请求反馈:{}", result2);
}
}
//放货的进入前等待和离开等待
if (action.equals("Unload")) {
if ("EntryRequired".equals(type)) {
if (addressdevice.getDeviceDriver() instanceof HailiangSmartplcTestDeviceDriver) {
hailiangSmartplcTestDeviceDriver = (HailiangSmartplcTestDeviceDriver) addressdevice.getDeviceDriver();
if ((hailiangSmartplcTestDeviceDriver.getAction() == 2 || hailiangSmartplcTestDeviceDriver.getAction() == 3) && hailiangSmartplcTestDeviceDriver.getMove() == 0) {
inst.setExecute_status("3");
is_feedback = true;
}
}
if (addressdevice.getDeviceDriver() instanceof EmptyVehicleStackingPositionDeviceDriver) {
emptyVehicleStackingPositionDeviceDriver = (EmptyVehicleStackingPositionDeviceDriver) addressdevice.getDeviceDriver();
int number = emptyVehicleStackingPositionDeviceDriver.getNumber();
if (number >= Integer.valueOf(str)) {
log.info("叠盘位:" + jobno + "" + str + "上有货!");
return null;
}
inst.setExecute_status("3");
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof CargoLiftConveyorDeviceDriver) {
cargoLiftConveyorDeviceDriver = (CargoLiftConveyorDeviceDriver) addressdevice.getDeviceDriver();
if ((cargoLiftConveyorDeviceDriver.getAction() == 2 || cargoLiftConveyorDeviceDriver.getAction() == 3) && cargoLiftConveyorDeviceDriver.getMove() == 0) {
inst.setExecute_status("3");
is_feedback = true;
}
}
if (addressdevice.getDeviceDriver() instanceof HaoKaiAutoConveyorDeviceDriver) {
haoKaiAutoConveyorDeviceDriver = (HaoKaiAutoConveyorDeviceDriver) addressdevice.getDeviceDriver();
if ((haoKaiAutoConveyorDeviceDriver.getAction() == 2 || haoKaiAutoConveyorDeviceDriver.getAction() == 3) && haoKaiAutoConveyorDeviceDriver.getMove() == 0) {
inst.setExecute_status("3");
is_feedback = true;
}
}
if (addressdevice.getDeviceDriver() instanceof PaintConveyorDeviceDriver) {
paintConveyorDeviceDriver = (PaintConveyorDeviceDriver) addressdevice.getDeviceDriver();
if ((paintConveyorDeviceDriver.getAction() == 2 || paintConveyorDeviceDriver.getAction() == 3) && paintConveyorDeviceDriver.getMove() == 0) {
inst.setExecute_status("3");
is_feedback = true;
}
}
}
if ("PauseOnStation".equals(type)) {
if (addressdevice.getDeviceDriver() instanceof HailiangSmartplcTestDeviceDriver) {
hailiangSmartplcTestDeviceDriver = (HailiangSmartplcTestDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("4");
hailiangSmartplcTestDeviceDriver.writing(3);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof EmptyVehicleStackingPositionDeviceDriver) {
emptyVehicleStackingPositionDeviceDriver = (EmptyVehicleStackingPositionDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("4");
emptyVehicleStackingPositionDeviceDriver.writing(3);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof CargoLiftConveyorDeviceDriver) {
cargoLiftConveyorDeviceDriver = (CargoLiftConveyorDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("4");
cargoLiftConveyorDeviceDriver.writing(3);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof HaoKaiAutoConveyorDeviceDriver) {
haoKaiAutoConveyorDeviceDriver = (HaoKaiAutoConveyorDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("4");
haoKaiAutoConveyorDeviceDriver.writing(3);
is_feedback = true;
}
if (addressdevice.getDeviceDriver() instanceof PaintConveyorDeviceDriver) {
paintConveyorDeviceDriver = (PaintConveyorDeviceDriver) addressdevice.getDeviceDriver();
inst.setExecute_status("4");
paintConveyorDeviceDriver.writing(3);
is_feedback = true;
}
}
}
JSONObject requestjo = new JSONObject();
if (is_feedback) {
requestjo.put("task_code",jobno);
requestjo.put("operation",action);
if (type.equals("entryRequired") || type.equals("EntryRequired")){
requestjo.put("entryRequired","true");
}else {
requestjo.put("pauseOnStation","true");
}
log.info("反馈AGV请求数据:{}", requestjo);
System.out.println("back agv:" + requestjo);
String agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL);
String agvport = acsConfigService.findConfigFromCache().get(AcsConfig.AGVPORT);
if (inst.getInstruction_type().equals("4")){
agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL2);
}
agvurl = agvurl + ":" + agvport + "/v1/transportOrders/" + jobno + "/interact";
HttpResponse result = HttpRequest.post(agvurl)
.body(String.valueOf(requestjo))
.timeout(20000)//超时,毫秒
.execute();
}
return requestjo.toString();
}
@Override
//ZDAGV
public HttpResponse markComplete(String code) throws Exception {
if (StrUtil.equals(acsConfigService.findConfigFromCache().get(AcsConfig.FORKAGV).toString(), "1")) {
String agvurl = acsConfigService.findConfigFromCache().get(AcsConfig.AGVURL);
String agvport = acsConfigService.findConfigFromCache().get(AcsConfig.AGVPORT);
agvurl = agvurl + ":" + agvport + "/v1/" + code + "/markComplete";
log.info("关闭agv运单序列请求:{}", agvurl);
HttpResponse result = HttpRequest.post(agvurl)
//.body(String.valueOf(orderjo))//表单内容
.timeout(20000)//超时,毫秒
.execute();
log.info("关闭agv运单序列请求反馈:{}", result);
return result;
} else {
return null;
}
}
/**
* 返回一个点位操作子任务
*
* @param locationName 点位
* @param operation 点位操作
* @param propertiesType 子任务类型
* @param pro 子任务参数
* 调用demo:destination("sh15p", "Spin", "2", "3.14")
* demo:destination("cz14", "JackUnload", "3", "")
* @return
*/
//ZDAGV
public static JSONObject destination(String locationName, String operation, String propertiesType, String pro) {
//新增业务订单
JSONObject destinationOrder = new JSONObject();
//目标工作站
destinationOrder.put("locationName", locationName);
//机器人在工作站要执行的操作
destinationOrder.put("operation", operation);
if (propertiesType.equals("1")) {//取货前等待、取货后等待
//pro 1 进入离开等待
if ("1".equals(pro)) {
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "EntryRequired");
pro1.put("value", "True");
properties.add(pro1);
JSONObject pro2 = new JSONObject();
pro2.put("key", "PauseOnStation");
pro2.put("value", "True");
properties.add(pro2);
destinationOrder.put("properties", properties);
//进入等待 离开不等待
} else if ("2".equals(pro)) {
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "EntryRequired");
pro1.put("value", "True");
properties.add(pro1);
JSONObject pro2 = new JSONObject();
pro2.put("key", "PauseOnStation");
pro2.put("value", "False");
properties.add(pro2);
destinationOrder.put("properties", properties);
//进入不等待 离开等待
} else if ("3".equals(pro)) {
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "EntryRequired");
pro1.put("value", "False");
properties.add(pro1);
JSONObject pro2 = new JSONObject();
pro2.put("key", "PauseOnStation");
pro2.put("value", "True");
properties.add(pro2);
destinationOrder.put("properties", properties);
//不等待
} else {
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "EntryRequired");
pro1.put("value", "False");
properties.add(pro1);
JSONObject pro2 = new JSONObject();
pro2.put("key", "PauseOnStation");
pro2.put("value", "False");
properties.add(pro2);
destinationOrder.put("properties", properties);
}
} else if (propertiesType.equals("2")) {//Spin转动
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "global_spin_angle");//坐标系类型global_spin_angle为全局坐标系
pro1.put("value", pro);//弧度值如3.14
properties.add(pro1);
JSONObject pro2 = new JSONObject();
pro2.put("key", "spin_direction");//固定值
pro2.put("value", "0");//弧度值如0
properties.add(pro2);
destinationOrder.put("properties", properties);
} else if (propertiesType.equals("3")) {//JackUnload,Jackload不操作
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "recognize");//固定值
pro1.put("value", "false");//固定值
properties.add(pro1);
destinationOrder.put("properties", properties);
} else if (propertiesType.equals("4")) {
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "robot_spin_angle");//坐标系类型robot_spin_angle为机器人坐标系
pro1.put("value", pro);//弧度值如3.14
properties.add(pro1);
JSONObject pro2 = new JSONObject();
pro2.put("key", "spin_direction");//固定值
pro2.put("value", "0");//弧度值如0
properties.add(pro2);
destinationOrder.put("properties", properties);
} else if (propertiesType.equals("5")) {//在该点进行等待
JSONArray properties = new JSONArray();
JSONObject pro1 = new JSONObject();
pro1.put("key", "Wait");
pro1.put("value", "True");
properties.add(pro1);
destinationOrder.put("properties", properties);
}
return destinationOrder;
}
/**
* 获得之后num个天的时间
*
* @param num
* @return
*/
public static String getNextDay(int num) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.DAY_OF_MONTH, num);
Date date = calendar.getTime();
TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
df.setTimeZone(tz);
String nowAsISO = df.format(date);
return nowAsISO;
}
}