| | |
| | | */ |
| | | public class NyHttpUtils { |
| | | |
| | | private static final boolean DEBUG = true;//调试模式 |
| | | |
| | | //获取HTTP请求标准结构体 |
| | | public static NyShuttleHttpCommand getHttpStandard(Integer shuttleNo, Integer wrkNo) { |
| | | NyShuttleHttpCommand httpStandard = new NyShuttleHttpCommand(); |
| | |
| | | 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", getTaskId());//TaskID需要随机 |
| | | body.put("start", navigateNodeToNyPointNode(start));//起点 |
| | | body.put("target", navigateNodeToNyPointNode(target));//终点 |
| | | body.put("start", nyStartPoint);//起点 |
| | | body.put("target", nyTargetPoint);//终点 |
| | | request.setBody(body); |
| | | |
| | | httpStandard.setRequest(request); |
| | |
| | | } |
| | | |
| | | //获取更新层坐标Z(楼层)命令 |
| | | public static NyShuttleHttpCommand getUpdateZCommand(Integer shuttleNo, Integer z) { |
| | | NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999); |
| | | 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<>(); |
| | |
| | | } |
| | | |
| | | //发出请求 |
| | | public static JSONObject requestCommand(Socket socket, NyShuttleHttpCommand httpCommand) { |
| | | try { |
| | | if (socket == null) { |
| | | return null; |
| | | } |
| | | // 获取输入流和输出流 |
| | | BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); |
| | | OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream()); |
| | | writer.write(JSON.toJSONString(httpCommand) + "\r\n"); |
| | | writer.flush(); |
| | | 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); |
| | | // 读取服务器的响应 |
| | | 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 (responseId != httpCommand.getRequest().getHeader().getRequestId()) { |
| | | return null;//响应ID与请求ID不一致 |
| | | } |
| | | |
| | | return filterBodyData(result);//返回Body结果集 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | JSONObject result = JSON.parseObject(sb.toString());//得到响应结果集 |
| | | if (!result.get("msgType").equals("responseMsg")) {//不是响应内容 |
| | | return null; |
| | | } |
| | | 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与请求ID不一致,不在调试模式下 |
| | | } |
| | | |
| | | return filterBodyData(result);//返回Body结果集 |
| | | } |
| | | |
| | | public static JSONObject filterBodyData(JSONObject data) { |