package com.zy.common.utils;
|
|
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSONObject;
|
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.*;
|
|
/**
|
* 牛眼四向穿梭车HTTP请求工具类
|
*/
|
public class NyHttpUtils {
|
|
private static final boolean DEBUG = true;//调试模式
|
|
//获取HTTP请求标准结构体
|
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();
|
NyShuttleHttpCommand.NyRequest.NyHeader header = new NyShuttleHttpCommand.NyRequest.NyHeader();
|
header.setVersion("1.0");//版本号
|
header.setRequestId(getRequestId());//消息编号
|
|
//设置请求头
|
request.setHeader(header);
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取FAS 手动/自动切换
|
public static NyShuttleHttpCommand getFASSwitchCommand(Integer shuttleNo, boolean auto) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", auto ? "switchAuto" : "switchManual");
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取移动请求
|
public static NyShuttleHttpCommand getMoveCommand(Integer shuttleNo, Integer wrkNo, NavigateNode start, NavigateNode target) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, wrkNo);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "move");//移动命令
|
body.put("taskId", getTaskId());//TaskID需要随机
|
body.put("start", navigateNodeToNyPointNode(start));//起点
|
body.put("target", navigateNodeToNyPointNode(target));//终点
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取进出提升机请求
|
public static NyShuttleHttpCommand getInOutLiftCommand(Integer shuttleNo, Integer wrkNo, NavigateNode start, NavigateNode target, boolean in) {
|
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", nyStartPoint);//起点
|
body.put("target", nyTargetPoint);//终点
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取顶升命令
|
public static NyShuttleHttpCommand getPalletLiftCommand(Integer shuttleNo, Integer wrkNo, boolean lift) {
|
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", getTaskId());//TaskID需要随机
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取充电命令
|
public static NyShuttleHttpCommand getChargeCommand(Integer shuttleNo, Integer wrkNo, boolean charge) {
|
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", getTaskId());//TaskID需要随机
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取管制命令
|
public static NyShuttleHttpCommand getSuspendCommand(Integer shuttleNo, Integer wrkNo, boolean suspend) {
|
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", getTaskId());//TaskID需要随机
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取更新层坐标Z(楼层)命令
|
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<>();
|
body.put("requestType", "updateFloor");//更新层Z
|
body.put("z", z);//坐标Z
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取读FAS状态信息命令
|
public static NyShuttleHttpCommand getReadStatusCommand(Integer shuttleNo) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "readState");//读FAS状态信息
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取更新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, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "updateMapStandard");//更新FAS基准地图
|
|
HashMap<String, Object> standard = new HashMap<>();
|
standard.put("xBase", xBase);//库位X向基点
|
standard.put("yBase", yBase);//库位Y向基点
|
standard.put("zBase", zBase);//库位Z向基点
|
standard.put("xEnd", xEnd);//库位X向终点
|
standard.put("yEnd", yEnd);//库位Y向终点
|
standard.put("zEnd", zEnd);//库位Z向终点
|
standard.put("xBaseCoord", xBaseCoord);//库位X向基点坐标 单位mm
|
standard.put("yBaseCoord", yBaseCoord);//库位Y向基点坐标 单位mm
|
standard.put("xDefaultSpace", xDefaultSpace);//库位X向标准间距 单位mm
|
standard.put("yDefaultSpace", yDefaultSpace);//库位Y向标准间距 单位mm
|
body.put("standard", standard);
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取更新FAS特殊X轴地图命令
|
public static NyShuttleHttpCommand getUpdateMapSpecialXAxisCommand(Integer shuttleNo, List<Map<String, Object>> list) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "updateMapSpecialXAxis");//更新FAS特殊X轴地图
|
body.put("specialXAxis", list);
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取更新FAS特殊Y轴地图命令
|
public static NyShuttleHttpCommand getUpdateMapSpecialYAxisCommand(Integer shuttleNo, List<Map<String, Object>> list) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "updateMapSpecialYAxis");//更新FAS特殊Y轴地图
|
body.put("specialYAxis", list);
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取更新FAS特殊库位点地图命令
|
public static NyShuttleHttpCommand getUpdateMapSpecialPointCommand(Integer shuttleNo, List<Map<String, Object>> list) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "updateMapSpecialPoint");//更新FAS特殊库位点地图
|
body.put("specialPoint", list);
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取更新FAS提升机点位地图命令
|
public static NyShuttleHttpCommand getUpdateMapDevicePointCommand(Integer shuttleNo, List<Map<String, Object>> list) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "updateMapDevicePoint");//更新FAS提升机点位地图
|
body.put("devicePoint", list);
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取单个RFID录写命令
|
public static NyShuttleHttpCommand getWriteSingleRFIDCommand(Integer shuttleNo, Integer xPoint, Integer yPoint, Integer zPoint) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "WriteSingleRFID");//单个RFID录写
|
body.put("xPoint", xPoint);//X 点位
|
body.put("yPoint", yPoint);//Y 点位
|
body.put("zPoint", zPoint);//Z 点位
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取自动录写RFID命令
|
public static NyShuttleHttpCommand getAutoWriteRFIDCommand(Integer shuttleNo, Integer dir, Integer xBase, Integer yBase, Integer zBase, Integer pointEnd, List<Integer> specialPoint) {
|
NyShuttleHttpCommand httpStandard = getHttpStandard(shuttleNo, 9999);
|
NyShuttleHttpCommand.NyRequest request = httpStandard.getRequest();
|
|
HashMap<String, Object> body = new HashMap<>();
|
body.put("requestType", "AutoWriteRFID");//自动录写RFID
|
body.put("requestCode", dir);//方向xDir/yDir
|
body.put("xBase", xBase);//X 起始点位
|
body.put("yBase", yBase);//Y 起始点位
|
body.put("zBase", zBase);//Z 起始点位
|
body.put("pointEnd", pointEnd);//终点坐标 根据requestCode来决定终点坐标的方向
|
body.put("specialPoint", specialPoint);//特殊点位过程中不需要录制的RFID点位
|
request.setBody(body);
|
|
httpStandard.setRequest(request);
|
return httpStandard;
|
}
|
|
//获取请求编号
|
public static Integer getRequestId() {
|
Random random = new Random();
|
return random.nextInt(9999999);
|
}
|
|
//获取TaskId
|
public static Integer getTaskId() {
|
Random random = new Random();
|
return random.nextInt(999999);
|
}
|
|
//发出请求
|
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();
|
char[] chars = new char[2048];//缓冲区
|
while (true) {
|
reader.read(chars);
|
String trim = new String(chars);
|
sb.append(trim);
|
if (trim.lastIndexOf("\r\n") != -1) {
|
break;
|
}
|
}
|
|
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;
|
}
|
|
//地图节点转换牛眼节点
|
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(NyPosition[0]);
|
point.setY(NyPosition[1]);
|
point.setZ(NyPosition[2]);
|
return point;
|
}
|
|
}
|