From 2b118d22f058eab19bef175753517c2f65dd4418 Mon Sep 17 00:00:00 2001
From: Junjie <540245094@qq.com>
Date: 星期四, 21 十二月 2023 10:53:31 +0800
Subject: [PATCH] #检料盘点工作档更新

---
 src/main/java/com/zy/common/utils/NyHttpUtils.java |  174 +++++++++++++++++++++++++++++++++++++++------------------
 1 files changed, 118 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/zy/common/utils/NyHttpUtils.java b/src/main/java/com/zy/common/utils/NyHttpUtils.java
index fd22a84..40a7436 100644
--- a/src/main/java/com/zy/common/utils/NyHttpUtils.java
+++ b/src/main/java/com/zy/common/utils/NyHttpUtils.java
@@ -5,7 +5,10 @@
 import com.zy.common.model.NavigateNode;
 import com.zy.core.model.command.NyShuttleHttpCommand;
 import com.zy.core.model.protocol.NyShuttleProtocol;
+import springfox.documentation.spring.web.json.Json;
 
+import java.io.*;
+import java.net.Socket;
 import java.util.*;
 
 /**
@@ -13,13 +16,14 @@
  */
 public class NyHttpUtils {
 
-    private static final String requestUrl = "localhost:9090/skwcs";
+    private static final boolean DEBUG = true;//璋冭瘯妯″紡
 
     //鑾峰彇HTTP璇锋眰鏍囧噯缁撴瀯浣�
-    public static NyShuttleHttpCommand getHttpStandard(Integer shuttleNo) {
+    public static NyShuttleHttpCommand getHttpStandard(Integer shuttleNo, Integer wrkNo) {
         NyShuttleHttpCommand httpStandard = new NyShuttleHttpCommand();
         httpStandard.setMsgType("requestMsg");//璇锋眰娑堟伅
         httpStandard.setRobotId(shuttleNo);//杞﹁締缂栧彿
+        httpStandard.setWrkNo(wrkNo);//宸ヤ綔鍙�
 
         //璁剧疆璇锋眰娑堟伅
         NyShuttleHttpCommand.NyRequest request = new NyShuttleHttpCommand.NyRequest();
@@ -35,7 +39,7 @@
 
     //鑾峰彇FAS 鎵嬪姩/鑷姩鍒囨崲
     public static NyShuttleHttpCommand getFASSwitchCommand(Integer shuttleNo, boolean auto) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -48,14 +52,14 @@
 
     //鑾峰彇绉诲姩璇锋眰
     public static NyShuttleHttpCommand getMoveCommand(Integer shuttleNo, Integer wrkNo, NavigateNode start, NavigateNode target) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
         body.put("requestType", "move");//绉诲姩鍛戒护
-        body.put("taskId", wrkNo);//浠诲姟鍙�
-        body.put("start", navigateNodeToPointNode(start));//璧风偣
-        body.put("target", navigateNodeToPointNode(target));//缁堢偣
+        body.put("taskId", getTaskId());//TaskID闇�瑕侀殢鏈�
+        body.put("start", navigateNodeToNyPointNode(start));//璧风偣
+        body.put("target", navigateNodeToNyPointNode(target));//缁堢偣
         request.setBody(body);
 
         httpStandard.setRequest(request);
@@ -64,14 +68,30 @@
 
     //鑾峰彇杩涘嚭鎻愬崌鏈鸿姹�
     public static NyShuttleHttpCommand getInOutLiftCommand(Integer shuttleNo, Integer wrkNo, NavigateNode start, NavigateNode target, boolean in) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
+
+        NyShuttleProtocol.NyShuttlePointClass nyStartPoint = new NyShuttleProtocol.NyShuttlePointClass();
+        NyShuttleProtocol.NyShuttlePointClass nyTargetPoint = new NyShuttleProtocol.NyShuttlePointClass();
+        if (in) {
+            //杩涙彁鍗囨満
+            nyStartPoint = navigateNodeToNyPointNode(start);
+            nyTargetPoint.setX(target.getX());
+            nyTargetPoint.setY(target.getY());
+            nyTargetPoint.setZ(target.getZ());
+        }else {
+            //鍑烘彁鍗囨満
+            nyTargetPoint = navigateNodeToNyPointNode(target);
+            nyStartPoint.setX(start.getX());
+            nyStartPoint.setY(start.getY());
+            nyStartPoint.setZ(start.getZ());
+        }
 
         HashMap<String, Object> body = new HashMap<>();
         body.put("requestType", in ? "intoLift" : "outLift");//杩涘嚭鎻愬崌鏈�
-        body.put("taskId", wrkNo);//浠诲姟鍙�
-        body.put("start", navigateNodeToPointNode(start));//璧风偣
-        body.put("target", navigateNodeToPointNode(target));//缁堢偣
+        body.put("taskId", getTaskId());//TaskID闇�瑕侀殢鏈�
+        body.put("start", nyStartPoint);//璧风偣
+        body.put("target", nyTargetPoint);//缁堢偣
         request.setBody(body);
 
         httpStandard.setRequest(request);
@@ -80,12 +100,12 @@
 
     //鑾峰彇椤跺崌鍛戒护
     public static NyShuttleHttpCommand getPalletLiftCommand(Integer shuttleNo, Integer wrkNo, boolean lift) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
         body.put("requestType", lift ? "liftUp" : "liftDown");//椤跺崌鎴栦笅闄嶅懡浠�
-        body.put("taskId", wrkNo);//浠诲姟鍙�
+        body.put("taskId", getTaskId());//TaskID闇�瑕侀殢鏈�
         request.setBody(body);
 
         httpStandard.setRequest(request);
@@ -94,12 +114,12 @@
 
     //鑾峰彇鍏呯數鍛戒护
     public static NyShuttleHttpCommand getChargeCommand(Integer shuttleNo, Integer wrkNo, boolean charge) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
         body.put("requestType", charge ? "charge" : "stopCharge");//鍏呯數鎴栧仠姝㈠厖鐢�
-        body.put("taskId", wrkNo);//浠诲姟鍙�
+        body.put("taskId", getTaskId());//TaskID闇�瑕侀殢鏈�
         request.setBody(body);
 
         httpStandard.setRequest(request);
@@ -108,12 +128,12 @@
 
     //鑾峰彇绠″埗鍛戒护
     public static NyShuttleHttpCommand getSuspendCommand(Integer shuttleNo, Integer wrkNo, boolean suspend) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
         body.put("requestType", suspend ? "stop" : "resume");//绠″埗鎴栧彇娑堢鍒�
-        body.put("taskId", wrkNo);//浠诲姟鍙�
+        body.put("taskId", getTaskId());//TaskID闇�瑕侀殢鏈�
         request.setBody(body);
 
         httpStandard.setRequest(request);
@@ -121,8 +141,8 @@
     }
 
     //鑾峰彇鏇存柊灞傚潗鏍嘮(妤煎眰)鍛戒护
-    public static NyShuttleHttpCommand getUpdateZCommand(Integer shuttleNo, Integer z) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+    public static NyShuttleHttpCommand getUpdateZCommand(Integer shuttleNo, Integer z, Integer wrkNo) {
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -136,7 +156,7 @@
 
     //鑾峰彇璇籉AS鐘舵�佷俊鎭懡浠�
     public static NyShuttleHttpCommand getReadStatusCommand(Integer shuttleNo) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -149,7 +169,7 @@
 
     //鑾峰彇鏇存柊FAS鍩哄噯鍦板浘鍛戒护
     public static NyShuttleHttpCommand getUpdateFASBaseMapCommand(Integer shuttleNo, Integer xBase, Integer yBase, Integer zBase, Integer xEnd, Integer yEnd, Integer zEnd, Integer xBaseCoord, Integer yBaseCoord, Integer xDefaultSpace, Integer yDefaultSpace) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -175,7 +195,7 @@
 
     //鑾峰彇鏇存柊FAS鐗规畩X杞村湴鍥惧懡浠�
     public static NyShuttleHttpCommand getUpdateMapSpecialXAxisCommand(Integer shuttleNo, List<Map<String, Object>> list) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -189,7 +209,7 @@
 
     //鑾峰彇鏇存柊FAS鐗规畩Y杞村湴鍥惧懡浠�
     public static NyShuttleHttpCommand getUpdateMapSpecialYAxisCommand(Integer shuttleNo, List<Map<String, Object>> list) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -203,7 +223,7 @@
 
     //鑾峰彇鏇存柊FAS鐗规畩搴撲綅鐐瑰湴鍥惧懡浠�
     public static NyShuttleHttpCommand getUpdateMapSpecialPointCommand(Integer shuttleNo, List<Map<String, Object>> list) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -217,7 +237,7 @@
 
     //鑾峰彇鏇存柊FAS鎻愬崌鏈虹偣浣嶅湴鍥惧懡浠�
     public static NyShuttleHttpCommand getUpdateMapDevicePointCommand(Integer shuttleNo, List<Map<String, Object>> list) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -231,7 +251,7 @@
 
     //鑾峰彇鍗曚釜RFID褰曞啓鍛戒护
     public static NyShuttleHttpCommand getWriteSingleRFIDCommand(Integer shuttleNo, Integer xPoint, Integer yPoint, Integer zPoint) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -247,7 +267,7 @@
 
     //鑾峰彇鑷姩褰曞啓RFID鍛戒护
     public static NyShuttleHttpCommand getAutoWriteRFIDCommand(Integer shuttleNo, Integer dir, Integer xBase, Integer yBase, Integer zBase, Integer pointEnd, List<Integer> specialPoint) {
-        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo);
+        NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
         NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
 
         HashMap<String, Object> body = new HashMap<>();
@@ -270,38 +290,80 @@
         return random.nextInt(9999999);
     }
 
-    //鍙戝嚭璇锋眰
-    public static JSONObject requestCommand(NyShuttleHttpCommand httpCommand) {
-        try {
-            String response = new HttpHandler.Builder()
-                    .setUri(requestUrl)
-                    .setPath("/static/readStatus.json")
-                    .setJson(JSON.toJSONString(httpCommand))
-                    .build()
-                    .doGet();
-            JSONObject jsonObject = JSON.parseObject(response);
-            JSONObject response2 = jsonObject.getJSONObject("response");
-            JSONObject body = response2.getJSONObject("body");
-
-            if (body.get("responseType").equals("state")) {//璇诲彇鐘舵�侊紝涓嶉渶瑕佸垽鏂璼uccess
-                return body;
-            }
-
-            if (body.get("result").equals("success")) {
-                return body;
-            }
-        } catch (Exception e) {
-//            e.printStackTrace();
-        }
-        return null;
+    //鑾峰彇TaskId
+    public static Integer getTaskId() {
+        Random random = new Random();
+        return random.nextInt(999999);
     }
 
-    //鍦板浘鑺傜偣杞崲鏅�氳妭鐐箈yz
-    public static NyShuttleProtocol.NyShuttlePointClass navigateNodeToPointNode(NavigateNode node) {
+    //鍙戝嚭璇锋眰
+    public static JSONObject requestCommand(Socket socket, NyShuttleHttpCommand httpCommand) throws IOException {
+        if (socket == null) {
+            return null;
+        }
+
+        //鍘嬬缉鏁版嵁鍖�
+        JSONObject data = JSON.parseObject(JSON.toJSONString(httpCommand));
+        data.remove("nodes");
+
+        // 鑾峰彇杈撳叆娴佸拰杈撳嚭娴�
+        BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+        OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
+        writer.write(JSON.toJSONString(data) + "\r\n");
+        writer.flush();
+//            System.out.println("Sent message to server: " + JSON.toJSONString(httpCommand));
+
+        // 璇诲彇鏈嶅姟鍣ㄧ殑鍝嶅簲
+        StringBuffer sb = new StringBuffer();
+        String response = null;
+        char[] chars = new char[2048];//缂撳啿鍖�
+        do {
+            reader.read(chars);
+            String trim = new String(chars).trim();
+            sb.append(trim);
+            if (sb.lastIndexOf("\r\n") != -1) {
+                break;
+            }
+        } while (response != null);
+//            System.out.println("Received response from server: " + sb);
+
+        JSONObject result = JSON.parseObject(sb.toString());//寰楀埌鍝嶅簲缁撴灉闆�
+        if (!result.get("msgType").equals("responseMsg")) {//涓嶆槸鍝嶅簲鍐呭
+            return null;
+        }
+
+        JSONObject resultResponse = JSON.parseObject(result.get("response").toString());
+        JSONObject resultHeader = JSON.parseObject(resultResponse.get("header").toString());
+        int responseId = Integer.parseInt(resultHeader.get("responseId").toString());
+        if (!DEBUG && responseId != httpCommand.getRequest().getHeader().getRequestId()) {
+            return null;//鍝嶅簲ID涓庤姹侷D涓嶄竴鑷达紝涓嶅湪璋冭瘯妯″紡涓�
+        }
+
+        return filterBodyData(result);//杩斿洖Body缁撴灉闆�
+    }
+
+    public static JSONObject filterBodyData(JSONObject data) {
+        Object response = data.get("response");
+        if (response == null) {
+            return null;
+        }
+
+        JSONObject result = JSON.parseObject(response.toString());
+        Object body = result.get("body");
+        if (body == null) {
+            return null;
+        }
+        JSONObject jsonBody = JSON.parseObject(body.toString());
+        return jsonBody;
+    }
+
+    //鍦板浘鑺傜偣杞崲鐗涚溂鑺傜偣
+    public static NyShuttleProtocol.NyShuttlePointClass navigateNodeToNyPointNode(NavigateNode node) {
+        int[] NyPosition = NavigatePositionConvert.WCSXyzToNyXyz(node.getX(), node.getY(), node.getZ());//WCS绯荤粺鍧愭爣杞墰鐪煎潗鏍�
         NyShuttleProtocol.NyShuttlePointClass point = new NyShuttleProtocol.NyShuttlePointClass();
-        point.setX(node.getX());
-        point.setY(node.getY());
-        point.setZ(node.getZ());
+        point.setX(NyPosition[0]);
+        point.setY(NyPosition[1]);
+        point.setZ(NyPosition[2]);
         return point;
     }
 

--
Gitblit v1.9.1