You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

583 lines
21 KiB

8 months ago
6 months ago
6 months ago
6 months ago
5 months ago
8 months ago
6 months ago
8 months ago
5 months ago
8 months ago
8 months ago
8 months ago
8 months ago
6 months ago
5 months ago
6 months ago
8 months ago
6 months ago
8 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
8 months ago
6 months ago
5 months ago
8 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
5 months ago
8 months ago
5 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
6 months ago
8 months ago
6 months ago
6 months ago
6 months ago
8 months ago
  1. package com.gaotao.common.utils;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.fasterxml.jackson.databind.JsonNode;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.gaotao.modules.api.service.InterfaceCallLogService;
  6. import com.gaotao.modules.automatedWarehouse.mapper.WcsIntegrationMapper;
  7. import com.gaotao.modules.warehouse.entity.PalletData;
  8. import lombok.Getter;
  9. import lombok.Setter;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Component;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. /**
  17. * AGV接口调用工具类
  18. * 基于Lothar V4.13接口文档实现
  19. */
  20. @Setter
  21. @Getter
  22. @Component
  23. public class AgvClientUtil {
  24. //中山agv的交互site
  25. private static final String thisSite="55";
  26. /**
  27. * -- GETTER --
  28. * 获取AGV基础URL
  29. * -- SETTER --
  30. * 设置AGV基础URL
  31. */
  32. @Value("${custom.agv-url}")
  33. private String agvUrl;
  34. @Autowired
  35. private InterfaceCallLogService interfaceCallLogService;
  36. @Autowired
  37. private WcsIntegrationMapper wcsIntegrationMapper;
  38. /**
  39. *1. 发送任务 - 创建APR搬运任务
  40. * @param taskId 任务唯一标识
  41. * @param targets 任务目标点名称集合
  42. * @param configId 任务配置名称可选
  43. * @param priority 任务优先级1-9可选
  44. * @return 响应结果
  45. */
  46. public void createTask(String taskId, List<String> targets, String configId, Integer priority) {
  47. String url = agvUrl + "/rpc/createTask";
  48. Map<String, Object> request = new HashMap<>();
  49. request.put("taskId", taskId);
  50. request.put("targets", targets);
  51. if(targets.size()<2){
  52. throw new RuntimeException("站点数据不足,必须是2个站点以上。");
  53. }
  54. PalletData checkPallet= wcsIntegrationMapper.getPalletDataByStation(thisSite,targets.get(0));
  55. if(checkPallet==null){
  56. throw new RuntimeException("栈板数据不存在,请检查栈板数据。");
  57. }
  58. //2点任务和3点任务是不一样的模板
  59. switch (checkPallet.getPalletFamily()) {
  60. case "A01":
  61. configId="Steel_Pallet"+"_"+targets.size();
  62. break;
  63. case "A02":
  64. configId="Boxed_Pallet"+"_"+targets.size();
  65. break;
  66. case "A03":
  67. configId="Flat_Pallet"+"_"+targets.size();
  68. break;
  69. }
  70. if (configId != null) {
  71. request.put("configId", configId);
  72. }else {
  73. request.put("configId", "Steel_Pallet");
  74. }
  75. if (priority != null) {
  76. request.put("priority", priority);
  77. }
  78. Long logId = null;
  79. try {
  80. // 记录接口调用日志
  81. String requestJson = JSONObject.toJSONString(request);
  82. logId = interfaceCallLogService.logInterfaceCall(
  83. "AgvClientUtil",
  84. "createTask",
  85. requestJson,
  86. "55",
  87. taskId,
  88. "AGV创建任务接口"
  89. );
  90. ObjectMapper objectMapper = new ObjectMapper();
  91. String jsonBody = objectMapper.writeValueAsString(request);
  92. String ifsResponse = HttpUtils.doPost(url,jsonBody,null);
  93. ObjectMapper mapper = new ObjectMapper();
  94. JsonNode jsonNode = mapper.readTree(ifsResponse);
  95. int code = jsonNode.get("code").asInt();
  96. String msg = jsonNode.get("msg").asText();
  97. if(code!=200){
  98. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  99. }
  100. // 调用AGV接口
  101. // ResponseData response = HttpClientUtil.doPostByRaw(url, request);
  102. //
  103. // // 更新接口日志结果
  104. // if (logId != null) {
  105. // String responseJson = response != null ? JSONObject.toJSONString(response) : "null";
  106. // interfaceCallLogService.updateCallResult(logId, responseJson, "SUCCESS", null, null);
  107. // }
  108. //
  109. // return response;
  110. } catch (Exception e) {
  111. // 更新接口日志错误信息
  112. if (logId != null) {
  113. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  114. }
  115. throw new RuntimeException(e.getMessage());
  116. }
  117. }
  118. public void modifyPutPoint(String taskId, String target) {
  119. String url = agvUrl + "/rpc/modifyPutPoint";
  120. Map<String, Object> request = new HashMap<>();
  121. request.put("taskId", taskId);
  122. request.put("target", target);
  123. Long logId = null;
  124. try {
  125. // 记录接口调用日志
  126. String requestJson = JSONObject.toJSONString(request);
  127. logId = interfaceCallLogService.logInterfaceCall(
  128. "AgvClientUtil",
  129. "modifyPutPoint",
  130. requestJson,
  131. "55",
  132. taskId,
  133. "修改目标点"
  134. );
  135. ObjectMapper objectMapper = new ObjectMapper();
  136. String jsonBody = objectMapper.writeValueAsString(request);
  137. String ifsResponse = HttpUtils.doPost(url,jsonBody,null);
  138. ObjectMapper mapper = new ObjectMapper();
  139. JsonNode jsonNode = mapper.readTree(ifsResponse);
  140. int code = jsonNode.get("code").asInt();
  141. String msg = jsonNode.get("msg").asText();
  142. if(code!=200){
  143. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  144. }
  145. // 调用AGV接口
  146. // ResponseData response = HttpClientUtil.doPostByRaw(url, request);
  147. //
  148. // // 更新接口日志结果
  149. // if (logId != null) {
  150. // String responseJson = response != null ? JSONObject.toJSONString(response) : "null";
  151. // interfaceCallLogService.updateCallResult(logId, responseJson, "SUCCESS", null, null);
  152. // }
  153. //
  154. // return response;
  155. } catch (Exception e) {
  156. // 更新接口日志错误信息
  157. if (logId != null) {
  158. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  159. }
  160. throw new RuntimeException(e.getMessage());
  161. }
  162. }
  163. /**
  164. * 2 继续任务 - 继续执行等待中的任务
  165. * @param taskId 任务唯一编号
  166. */
  167. public void continueTask(String taskId) {
  168. String url = agvUrl + "/rpc/continueTask";
  169. Map<String, Object> request = new HashMap<>();
  170. request.put("taskId", taskId);
  171. Long logId = null;
  172. try {
  173. // 记录接口调用日志
  174. String requestJson = JSONObject.toJSONString(request);
  175. logId = interfaceCallLogService.logInterfaceCall(
  176. "AgvClientUtil",
  177. "continueTask",
  178. requestJson,
  179. "55",
  180. taskId,
  181. "AGV继续任务接口"
  182. );
  183. ObjectMapper objectMapper = new ObjectMapper();
  184. String jsonBody = objectMapper.writeValueAsString(request);
  185. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  186. ObjectMapper mapper = new ObjectMapper();
  187. JsonNode jsonNode = mapper.readTree(ifsResponse);
  188. int code = jsonNode.get("code").asInt();
  189. String msg = jsonNode.get("msg").asText();
  190. if(code != 200){
  191. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  192. }
  193. } catch (Exception e) {
  194. // 更新接口日志错误信息
  195. if (logId != null) {
  196. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  197. }
  198. throw new RuntimeException(e.getMessage());
  199. }
  200. }
  201. /**
  202. *3. 取消任务 - 取消APR搬运任务
  203. * @param taskId 任务唯一标识
  204. */
  205. public void cancelTask(String taskId) {
  206. String url = agvUrl + "/rpc/cancelTask";
  207. Map<String, Object> request = new HashMap<>();
  208. request.put("taskId", taskId);
  209. Long logId = null;
  210. try {
  211. // 记录接口调用日志
  212. String requestJson = JSONObject.toJSONString(request);
  213. logId = interfaceCallLogService.logInterfaceCall(
  214. "AgvClientUtil",
  215. "cancelTask",
  216. requestJson,
  217. "55",
  218. taskId,
  219. "AGV取消任务接口"
  220. );
  221. ObjectMapper objectMapper = new ObjectMapper();
  222. String jsonBody = objectMapper.writeValueAsString(request);
  223. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  224. ObjectMapper mapper = new ObjectMapper();
  225. JsonNode jsonNode = mapper.readTree(ifsResponse);
  226. int code = jsonNode.get("code").asInt();
  227. String msg = jsonNode.get("msg").asText();
  228. if(code != 200){
  229. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  230. }
  231. } catch (Exception e) {
  232. // 更新接口日志错误信息
  233. if (logId != null) {
  234. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  235. }
  236. throw new RuntimeException(e.getMessage());
  237. }
  238. }
  239. /**
  240. *4. 任务回调 - 任务执行到关键节点上报
  241. * @param taskId 任务唯一标识
  242. * @param deviceId APR编号
  243. * @param targetPoint APR到达的目标点
  244. */
  245. public void feedbackTask(String taskId, String deviceId, String targetPoint) {
  246. String url = agvUrl + "/feedbackTask";
  247. Map<String, Object> request = new HashMap<>();
  248. request.put("taskId", taskId);
  249. request.put("deviceId", deviceId);
  250. request.put("targetPoint", targetPoint);
  251. Long logId = null;
  252. try {
  253. // 记录接口调用日志
  254. String requestJson = JSONObject.toJSONString(request);
  255. logId = interfaceCallLogService.logInterfaceCall(
  256. "AgvClientUtil",
  257. "feedbackTask",
  258. requestJson,
  259. "55",
  260. taskId,
  261. "AGV任务回调接口"
  262. );
  263. ObjectMapper objectMapper = new ObjectMapper();
  264. String jsonBody = objectMapper.writeValueAsString(request);
  265. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  266. ObjectMapper mapper = new ObjectMapper();
  267. JsonNode jsonNode = mapper.readTree(ifsResponse);
  268. int code = jsonNode.get("code").asInt();
  269. String msg = jsonNode.get("msg").asText();
  270. if(code != 200){
  271. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  272. }
  273. } catch (Exception e) {
  274. // 更新接口日志错误信息
  275. if (logId != null) {
  276. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  277. }
  278. throw new RuntimeException(e.getMessage());
  279. }
  280. }
  281. /**
  282. *5. APR查询 - 获取在线小车状态
  283. * @return 在线小车数据
  284. */
  285. public Object getOnlineRobot() {
  286. String url = agvUrl + "/rpc/getOnlineRobot";
  287. try {
  288. String ifsResponse = HttpUtils.doGet(url, null, null);
  289. ObjectMapper mapper = new ObjectMapper();
  290. JsonNode jsonNode = mapper.readTree(ifsResponse);
  291. int code = jsonNode.get("code").asInt();
  292. String msg = jsonNode.get("msg").asText();
  293. if(code != 200){
  294. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  295. }
  296. // 返回数据部分
  297. return jsonNode.get("data");
  298. } catch (Exception e) {
  299. throw new RuntimeException(e.getMessage());
  300. }
  301. }
  302. /**
  303. * 6地图信息
  304. * @param mapName 地图名称可选
  305. * @return 地图信息数据
  306. */
  307. public Object getMapInfo(String mapName) {
  308. String url = agvUrl + "/rpc/getMapInfo";
  309. Map<String, Object> request = new HashMap<>();
  310. if (mapName != null) {
  311. request.put("mapName", mapName);
  312. }
  313. Long logId = null;
  314. try {
  315. // 记录接口调用日志
  316. String requestJson = JSONObject.toJSONString(request);
  317. logId = interfaceCallLogService.logInterfaceCall(
  318. "AgvClientUtil",
  319. "getMapInfo",
  320. requestJson,
  321. "55",
  322. mapName,
  323. "AGV获取地图信息接口"
  324. );
  325. ObjectMapper objectMapper = new ObjectMapper();
  326. String jsonBody = objectMapper.writeValueAsString(request);
  327. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  328. ObjectMapper mapper = new ObjectMapper();
  329. JsonNode jsonNode = mapper.readTree(ifsResponse);
  330. int code = jsonNode.get("code").asInt();
  331. String msg = jsonNode.get("msg").asText();
  332. if(code != 200){
  333. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  334. }
  335. // 返回数据部分
  336. JsonNode dataNode = jsonNode.get("data");
  337. return dataNode != null ? dataNode : null;
  338. } catch (Exception e) {
  339. // 更新接口日志错误信息
  340. if (logId != null) {
  341. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  342. }
  343. throw new RuntimeException(e.getMessage());
  344. }
  345. }
  346. /**
  347. *7. APR故障信息查询
  348. * @return 故障信息数据
  349. */
  350. public Object getAlarms() {
  351. String url = agvUrl + "/rpc/getAlarms";
  352. Map<String, Object> request = new HashMap<>();
  353. Long logId = null;
  354. try {
  355. // 记录接口调用日志
  356. String requestJson = JSONObject.toJSONString(request);
  357. logId = interfaceCallLogService.logInterfaceCall(
  358. "AgvClientUtil",
  359. "getAlarms",
  360. requestJson,
  361. "55",
  362. null,
  363. "AGV获取故障信息接口"
  364. );
  365. String ifsResponse = HttpUtils.doGet(url, null, null);
  366. ObjectMapper mapper = new ObjectMapper();
  367. JsonNode jsonNode = mapper.readTree(ifsResponse);
  368. int code = jsonNode.get("code").asInt();
  369. String msg = jsonNode.get("msg").asText();
  370. if(code != 200){
  371. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  372. }
  373. // 返回数据部分
  374. return jsonNode.get("data");
  375. } catch (Exception e) {
  376. // 更新接口日志错误信息
  377. if (logId != null) {
  378. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  379. }
  380. throw new RuntimeException(e.getMessage());
  381. }
  382. }
  383. /**
  384. * 8 APR申请进出交管区
  385. * @param areaName 交管区编号
  386. * @param type 1进入申请2-离开告知
  387. * @param agvNo APR编号
  388. */
  389. public void applyTrafficCtlArea(String areaName, Integer type, String agvNo) {
  390. String url = agvUrl + "/applyTrafficCtlArea";
  391. Map<String, Object> request = new HashMap<>();
  392. request.put("areaName", areaName);
  393. request.put("type", type);
  394. if (agvNo != null) {
  395. request.put("agvNo", agvNo);
  396. }
  397. Long logId = null;
  398. try {
  399. // 记录接口调用日志
  400. String requestJson = JSONObject.toJSONString(request);
  401. logId = interfaceCallLogService.logInterfaceCall(
  402. "AgvClientUtil",
  403. "applyTrafficCtlArea",
  404. requestJson,
  405. "55",
  406. areaName,
  407. "AGV申请进出交管区接口"
  408. );
  409. ObjectMapper objectMapper = new ObjectMapper();
  410. String jsonBody = objectMapper.writeValueAsString(request);
  411. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  412. ObjectMapper mapper = new ObjectMapper();
  413. JsonNode jsonNode = mapper.readTree(ifsResponse);
  414. int code = jsonNode.get("code").asInt();
  415. String msg = jsonNode.get("msg").asText();
  416. if(code != 200){
  417. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  418. }
  419. } catch (Exception e) {
  420. // 更新接口日志错误信息
  421. if (logId != null) {
  422. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  423. }
  424. throw new RuntimeException(e.getMessage());
  425. }
  426. }
  427. /**
  428. * 9. APR申请进出设备区域
  429. * @param deviceName 设备编号
  430. * @param type 1进入申请2-离开告知
  431. * @param taskId APR正在执行的任务编号可选
  432. * @param action APR将要在此设备区域执行的动作get-取货put-放货可选
  433. * @param agvNo APR编号可选
  434. */
  435. public void applyDeviceArea(String deviceName, Integer type, String taskId, String action, String agvNo) {
  436. String url = agvUrl + "/applyDeviceArea";
  437. Map<String, Object> request = new HashMap<>();
  438. request.put("deviceName", deviceName);
  439. request.put("type", type);
  440. if (taskId != null) {
  441. request.put("taskId", taskId);
  442. }
  443. if (action != null) {
  444. request.put("action", action);
  445. }
  446. if (agvNo != null) {
  447. request.put("agvNo", agvNo);
  448. }
  449. Long logId = null;
  450. try {
  451. // 记录接口调用日志
  452. String requestJson = JSONObject.toJSONString(request);
  453. logId = interfaceCallLogService.logInterfaceCall(
  454. "AgvClientUtil",
  455. "applyDeviceArea",
  456. requestJson,
  457. "55",
  458. deviceName,
  459. "AGV申请进出设备区域接口"
  460. );
  461. ObjectMapper objectMapper = new ObjectMapper();
  462. String jsonBody = objectMapper.writeValueAsString(request);
  463. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  464. ObjectMapper mapper = new ObjectMapper();
  465. JsonNode jsonNode = mapper.readTree(ifsResponse);
  466. int code = jsonNode.get("code").asInt();
  467. String msg = jsonNode.get("msg").asText();
  468. if(code != 200){
  469. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  470. }
  471. } catch (Exception e) {
  472. // 更新接口日志错误信息
  473. if (logId != null) {
  474. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  475. }
  476. throw new RuntimeException(e.getMessage());
  477. }
  478. }
  479. /**
  480. *10储位信息
  481. * @param storageNames 目标储位名称列表可选
  482. * @param areaNames 目标区域名称列表可选
  483. * @param mapNames 目标地图名称列表可选
  484. */
  485. public void getMapStorage(List<String> storageNames, List<String> areaNames, List<String> mapNames) {
  486. String url = agvUrl + "/getMapStorage";
  487. Map<String, Object> request = new HashMap<>();
  488. if (storageNames != null) {
  489. request.put("storageNames", storageNames);
  490. }
  491. if (areaNames != null) {
  492. request.put("areaNames", areaNames);
  493. }
  494. if (mapNames != null) {
  495. request.put("mapNames", mapNames);
  496. }
  497. Long logId = null;
  498. try {
  499. // 记录接口调用日志
  500. String requestJson = JSONObject.toJSONString(request);
  501. logId = interfaceCallLogService.logInterfaceCall(
  502. "AgvClientUtil",
  503. "getMapStorage",
  504. requestJson,
  505. "55",
  506. null,
  507. "AGV获取储位信息接口"
  508. );
  509. ObjectMapper objectMapper = new ObjectMapper();
  510. String jsonBody = objectMapper.writeValueAsString(request);
  511. String ifsResponse = HttpUtils.doPost(url, jsonBody, null);
  512. ObjectMapper mapper = new ObjectMapper();
  513. JsonNode jsonNode = mapper.readTree(ifsResponse);
  514. int code = jsonNode.get("code").asInt();
  515. String msg = jsonNode.get("msg").asText();
  516. if(code != 200){
  517. throw new RuntimeException("调用AGV接口失败,错误码:"+code+",错误信息:"+msg);
  518. }
  519. } catch (Exception e) {
  520. // 更新接口日志错误信息
  521. if (logId != null) {
  522. interfaceCallLogService.updateCallResult(logId, null, "FAILED", e.getMessage(), null);
  523. }
  524. throw new RuntimeException(e.getMessage());
  525. }
  526. }
  527. }