package com.gaotao.common.utils; import com.alibaba.fastjson2.JSONObject; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.gaotao.modules.api.service.InterfaceCallLogService; import com.gaotao.modules.automatedWarehouse.mapper.WcsIntegrationMapper; import com.gaotao.modules.warehouse.entity.PalletData; import lombok.Getter; import lombok.Setter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; /** * AGV接口调用工具类 * 基于Lothar V4.13接口文档实现 */ @Setter @Getter @Component public class AgvClientUtil { //中山agv的交互site private static final String thisSite="55"; /** * -- GETTER -- * 获取AGV基础URL * -- SETTER -- * 设置AGV基础URL */ @Value("${custom.agv-url}") private String agvUrl; @Autowired private InterfaceCallLogService interfaceCallLogService; @Autowired private WcsIntegrationMapper wcsIntegrationMapper; /** *1. 发送任务 - 创建APR搬运任务 * @param taskId 任务唯一标识 * @param targets 任务目标点名称集合 * @param configId 任务配置名称(可选) * @param priority 任务优先级1-9(可选) * @return 响应结果 */ public void createTask(String taskId, List targets, String configId, Integer priority) { String url = agvUrl + "/rpc/createTask"; Map request = new HashMap<>(); request.put("taskId", taskId); request.put("targets", targets); if(targets.size()<2){ throw new RuntimeException("站点数据不足,必须是2个站点以上。"); } PalletData checkPallet= wcsIntegrationMapper.getPalletDataByStation(thisSite,targets.get(0)); if(checkPallet==null){ throw new RuntimeException("栈板数据不存在,请检查栈板数据。"); } //2点任务和3点任务是不一样的模板 switch (checkPallet.getPalletFamily()) { case "A01": configId="Steel_Pallet"+"_"+targets.size(); break; case "A02": configId="Boxed_Pallet"+"_"+targets.size(); break; case "A03": configId="Flat_Pallet"+"_"+targets.size(); break; } if (configId != null) { request.put("configId", configId); }else { request.put("configId", "Steel_Pallet"); } if (priority != null) { request.put("priority", priority); } Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "createTask", requestJson, "55", taskId, "AGV创建任务接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url,jsonBody,null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code!=200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } // 调用AGV接口 // ResponseData response = HttpClientUtil.doPostByRaw(url, request); // // // 更新接口日志结果 // if (logId != null) { // String responseJson = response != null ? JSONObject.toJSONString(response) : "null"; // interfaceCallLogService.updateCallResult(logId, responseJson, "SUCCESS", null, null); // } // // return response; } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } public void modifyPutPoint(String taskId, String target) { String url = agvUrl + "/rpc/modifyPutPoint"; Map request = new HashMap<>(); request.put("taskId", taskId); request.put("target", target); Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "modifyPutPoint", requestJson, "55", taskId, "修改目标点" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url,jsonBody,null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code!=200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } // 调用AGV接口 // ResponseData response = HttpClientUtil.doPostByRaw(url, request); // // // 更新接口日志结果 // if (logId != null) { // String responseJson = response != null ? JSONObject.toJSONString(response) : "null"; // interfaceCallLogService.updateCallResult(logId, responseJson, "SUCCESS", null, null); // } // // return response; } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** * 2 继续任务 - 继续执行等待中的任务 * @param taskId 任务唯一编号 */ public void continueTask(String taskId) { String url = agvUrl + "/rpc/continueTask"; Map request = new HashMap<>(); request.put("taskId", taskId); Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "continueTask", requestJson, "55", taskId, "AGV继续任务接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** *3. 取消任务 - 取消APR搬运任务 * @param taskId 任务唯一标识 */ public void cancelTask(String taskId) { String url = agvUrl + "/rpc/cancelTask"; Map request = new HashMap<>(); request.put("taskId", taskId); Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "cancelTask", requestJson, "55", taskId, "AGV取消任务接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** *4. 任务回调 - 任务执行到关键节点上报 * @param taskId 任务唯一标识 * @param deviceId APR编号 * @param targetPoint APR到达的目标点 */ public void feedbackTask(String taskId, String deviceId, String targetPoint) { String url = agvUrl + "/feedbackTask"; Map request = new HashMap<>(); request.put("taskId", taskId); request.put("deviceId", deviceId); request.put("targetPoint", targetPoint); Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "feedbackTask", requestJson, "55", taskId, "AGV任务回调接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** *5. APR查询 - 获取在线小车状态 * @return 在线小车数据 */ public Object getOnlineRobot() { String url = agvUrl + "/rpc/getOnlineRobot"; try { String ifsResponse = HttpUtils.doGet(url, null, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } // 返回数据部分 return jsonNode.get("data"); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } } /** * 6地图信息 * @param mapName 地图名称(可选) * @return 地图信息数据 */ public Object getMapInfo(String mapName) { String url = agvUrl + "/rpc/getMapInfo"; Map request = new HashMap<>(); if (mapName != null) { request.put("mapName", mapName); } Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "getMapInfo", requestJson, "55", mapName, "AGV获取地图信息接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } // 返回数据部分 JsonNode dataNode = jsonNode.get("data"); return dataNode != null ? dataNode : null; } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** *7. APR故障信息查询 * @return 故障信息数据 */ public Object getAlarms() { String url = agvUrl + "/rpc/getAlarms"; Map request = new HashMap<>(); Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "getAlarms", requestJson, "55", null, "AGV获取故障信息接口" ); String ifsResponse = HttpUtils.doGet(url, null, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } // 返回数据部分 return jsonNode.get("data"); } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** * 8 APR申请进出交管区 * @param areaName 交管区编号 * @param type 1进入申请,2-离开告知 * @param agvNo APR编号 */ public void applyTrafficCtlArea(String areaName, Integer type, String agvNo) { String url = agvUrl + "/applyTrafficCtlArea"; Map request = new HashMap<>(); request.put("areaName", areaName); request.put("type", type); if (agvNo != null) { request.put("agvNo", agvNo); } Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "applyTrafficCtlArea", requestJson, "55", areaName, "AGV申请进出交管区接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** * 9. APR申请进出设备区域 * @param deviceName 设备编号 * @param type 1进入申请,2-离开告知 * @param taskId APR正在执行的任务编号(可选) * @param action APR将要在此设备区域执行的动作,get-取货,put-放货(可选) * @param agvNo APR编号(可选) */ public void applyDeviceArea(String deviceName, Integer type, String taskId, String action, String agvNo) { String url = agvUrl + "/applyDeviceArea"; Map request = new HashMap<>(); request.put("deviceName", deviceName); request.put("type", type); if (taskId != null) { request.put("taskId", taskId); } if (action != null) { request.put("action", action); } if (agvNo != null) { request.put("agvNo", agvNo); } Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "applyDeviceArea", requestJson, "55", deviceName, "AGV申请进出设备区域接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } /** *10储位信息 * @param storageNames 目标储位名称列表(可选) * @param areaNames 目标区域名称列表(可选) * @param mapNames 目标地图名称列表(可选) */ public void getMapStorage(List storageNames, List areaNames, List mapNames) { String url = agvUrl + "/getMapStorage"; Map request = new HashMap<>(); if (storageNames != null) { request.put("storageNames", storageNames); } if (areaNames != null) { request.put("areaNames", areaNames); } if (mapNames != null) { request.put("mapNames", mapNames); } Long logId = null; try { // 记录接口调用日志 String requestJson = JSONObject.toJSONString(request); logId = interfaceCallLogService.logInterfaceCall( "AgvClientUtil", "getMapStorage", requestJson, "55", null, "AGV获取储位信息接口" ); ObjectMapper objectMapper = new ObjectMapper(); String jsonBody = objectMapper.writeValueAsString(request); String ifsResponse = HttpUtils.doPost(url, jsonBody, null); ObjectMapper mapper = new ObjectMapper(); JsonNode jsonNode = mapper.readTree(ifsResponse); int code = jsonNode.get("code").asInt(); String msg = jsonNode.get("msg").asText(); if(code != 200){ throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg); } } catch (Exception e) { // 更新接口日志错误信息 if (logId != null) { interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null); } throw new RuntimeException(e.getMessage()); } } }