| | |
| | | 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.*; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class NyHttpUtils { |
| | | |
| | | private static final String requestUrl = "localhost:9090/tzskwcs"; |
| | | 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(); |
| | |
| | | |
| | | //获取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<>(); |
| | |
| | | |
| | | //获取移动请求 |
| | | 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("taskId", getTaskId());//TaskID需要随机 |
| | | body.put("start", navigateNodeToNyPointNode(start));//起点 |
| | | body.put("target", navigateNodeToNyPointNode(target));//终点 |
| | | request.setBody(body); |
| | |
| | | |
| | | //获取进出提升机请求 |
| | | 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", navigateNodeToNyPointNode(start));//起点 |
| | | body.put("target", navigateNodeToNyPointNode(target));//终点 |
| | | body.put("taskId", getTaskId());//TaskID需要随机 |
| | | body.put("start", nyStartPoint);//起点 |
| | | body.put("target", nyTargetPoint);//终点 |
| | | request.setBody(body); |
| | | |
| | | httpStandard.setRequest(request); |
| | |
| | | |
| | | //获取顶升命令 |
| | | 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); |
| | |
| | | |
| | | //获取充电命令 |
| | | 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); |
| | |
| | | |
| | | //获取管制命令 |
| | | 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); |
| | |
| | | } |
| | | |
| | | //获取更新层坐标Z(楼层)命令 |
| | | 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<>(); |
| | |
| | | |
| | | //获取读FAS状态信息命令 |
| | | 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<>(); |
| | |
| | | |
| | | //获取更新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<>(); |
| | |
| | | |
| | | //获取更新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<>(); |
| | |
| | | |
| | | //获取更新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<>(); |
| | |
| | | |
| | | //获取更新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<>(); |
| | |
| | | |
| | | //获取更新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<>(); |
| | |
| | | |
| | | //获取单个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<>(); |
| | |
| | | |
| | | //获取自动录写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<>(); |
| | |
| | | return random.nextInt(9999999); |
| | | } |
| | | |
| | | //获取TaskId |
| | | public static Integer getTaskId() { |
| | | Random random = new Random(); |
| | | return random.nextInt(999999); |
| | | } |
| | | |
| | | //发出请求 |
| | | 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")) {//读取状态,不需要判断success |
| | | return body; |
| | | } |
| | | |
| | | if (body.get("result").equals("success")) { |
| | | return body; |
| | | } |
| | | } catch (Exception e) { |
| | | // e.printStackTrace(); |
| | | public static JSONObject requestCommand(Socket socket, NyShuttleHttpCommand httpCommand) throws IOException { |
| | | if (socket == null) { |
| | | return 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与请求ID不一致,不在调试模式下 |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | //地图节点转换牛眼节点 |