diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java index dc69c33..55c8156 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java @@ -7,9 +7,9 @@ */ public class Constant { - public static final int BITS_OF_HEAD = 0x2A; - public static final int BITS_OF_END = 0x0A; - public static final int SHELTER_ID = 0xFF; + public static final byte BITS_OF_HEAD = (byte) 0x2A; + public static final byte BITS_OF_END = (byte) 0x0A; + public static final byte SHELTER_ID = (byte) 0xFF; /** * 洋流相关常量 diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java index dc69c33..55c8156 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java @@ -7,9 +7,9 @@ */ public class Constant { - public static final int BITS_OF_HEAD = 0x2A; - public static final int BITS_OF_END = 0x0A; - public static final int SHELTER_ID = 0xFF; + public static final byte BITS_OF_HEAD = (byte) 0x2A; + public static final byte BITS_OF_END = (byte) 0x0A; + public static final byte SHELTER_ID = (byte) 0xFF; /** * 洋流相关常量 diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java index b18e3ad..ed3cabe 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java @@ -16,16 +16,16 @@ /** * 水下机器人信息 */ - public static RobotInfo decodeRobotInfo(int[] bytes) { + public static RobotInfo decodeRobotInfo(byte[] bytes) { RobotInfo robot = new RobotInfo(); - robot.setRobotId(bytes[4]); + robot.setRobotId((int) bytes[4]); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 5, lngBytes, 0, 4); robot.setLng(covertLngLat(lngBytes)); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 9, latBytes, 0, 4); robot.setLat(covertLngLat(latBytes)); @@ -35,23 +35,23 @@ robot.setInstrumentE((float) (bytes[16])); robot.setPowerE((float) (bytes[17])); - int[] pitchAngleBytes = new int[2]; + byte[] pitchAngleBytes = new byte[2]; System.arraycopy(bytes, 18, pitchAngleBytes, 0, 2); robot.setPitchAngle(covertPitchAngle(pitchAngleBytes)); - int[] rollAngleBytes = new int[2]; + byte[] rollAngleBytes = new byte[2]; System.arraycopy(bytes, 20, rollAngleBytes, 0, 2); robot.setRollAngle(covertRollAngle(rollAngleBytes)); - int[] headingAngleBytes = new int[2]; + byte[] headingAngleBytes = new byte[2]; System.arraycopy(bytes, 22, headingAngleBytes, 0, 2); robot.setHeadingAngle(covertHeadingAngle(headingAngleBytes)); - int[] buoyancyBytes = new int[2]; + byte[] buoyancyBytes = new byte[2]; System.arraycopy(bytes, 24, buoyancyBytes, 0, 2); robot.setBuoyancy(covertBuoyancy(buoyancyBytes)); - int[] positionBytes = new int[2]; + byte[] positionBytes = new byte[2]; System.arraycopy(bytes, 26, positionBytes, 0, 2); //滑块位置算法和浮力算法一样 robot.setPosition(covertBuoyancy(positionBytes)); @@ -63,16 +63,16 @@ robot.setDetectStatus("发现目标"); } - int[] targetDisBytes = new int[2]; + byte[] targetDisBytes = new byte[2]; System.arraycopy(bytes, 29, targetDisBytes, 0, 2); robot.setTargetDistance(covertTargetDistance(targetDisBytes)); - int[] targetDirBytes = new int[2]; + byte[] targetDirBytes = new byte[2]; System.arraycopy(bytes, 31, targetDirBytes, 0, 2); //目标方位算法和航向角算法一样 robot.setTargetDirection(covertHeadingAngle(targetDirBytes)); - int[] targetHzBytes = new int[2]; + byte[] targetHzBytes = new byte[2]; System.arraycopy(bytes, 33, targetHzBytes, 0, 2); robot.setTargetHertz(covertTargetHertz(targetHzBytes)); return robot; @@ -81,12 +81,12 @@ /** * 区域覆盖 */ - public static String decodeRobotRegion(int[] bytes) { + public static String decodeRobotRegion(byte[] bytes) { Map regionMap = new HashMap<>(); int count = bytes[4]; regionMap.put("count", String.valueOf(count)); - int[] idsBytes = new int[count]; + byte[] idsBytes = new byte[count]; System.arraycopy(bytes, 5, idsBytes, 0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < idsBytes.length; i++) { @@ -100,14 +100,14 @@ //经度和纬度,所以需要*2 int targetSize = count * 2 * 4; - int[] targetBytes = new int[targetSize]; + byte[] targetBytes = new byte[targetSize]; //起始4个字节,机器人个数1个字节 System.arraycopy(bytes, 5 + idsBytes.length, targetBytes, 0, targetSize); regionMap.put("robotTargets", formatLanLat(targetBytes)); regionMap.put("pointCount", String.valueOf(bytes[5 + idsBytes.length + targetBytes.length])); - int[] radiusBytes = new int[2]; + byte[] radiusBytes = new byte[2]; System.arraycopy(bytes, 5 + idsBytes.length + targetBytes.length + 1, radiusBytes, 0, 2); regionMap.put("regionRadius", covertTargetDistance(radiusBytes)); @@ -120,7 +120,7 @@ //经度和纬度,所以需要*2 int pointSize = count * 2 * 4; - int[] pointBytes = new int[pointSize]; + byte[] pointBytes = new byte[pointSize]; System.arraycopy(bytes, 5 + idsBytes.length + targetBytes.length + 1 + radiusBytes.length + 1, pointBytes, 0, pointSize); @@ -131,14 +131,14 @@ /** * 路径规划 */ - public static String decodeRobotRoute(int[] bytes) { + public static String decodeRobotRoute(byte[] bytes) { Map routeMap = new HashMap<>(2); int count = bytes[4]; routeMap.put("routeCount", String.valueOf(count)); int routeSize = count * 2 * 4; - int[] routeBytes = new int[routeSize]; + byte[] routeBytes = new byte[routeSize]; System.arraycopy(bytes, 5, routeBytes, 0, routeSize); routeMap.put("robotRoutes", formatLanLat(routeBytes)); return JSON.toJSONString(routeMap); @@ -147,13 +147,13 @@ /** * 定向任务 */ - public static String decodeDirectedTask(int[] bytes) { + public static String decodeDirectedTask(byte[] bytes) { Map taskMap = new HashMap<>(2); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 6, angleBytes, 0, 2); taskMap.put("headingAngle", covertHeadingAngle(angleBytes) + "°"); return JSON.toJSONString(taskMap); @@ -162,10 +162,10 @@ /** * 航路点任务 */ - public static String decodeTaskRoute(int[] bytes) { + public static String decodeTaskRoute(byte[] bytes) { Map taskMap = new HashMap<>(3); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); @@ -173,7 +173,7 @@ taskMap.put("routeCount", String.valueOf(count)); int routeSize = count * 2 * 4; - int[] routeBytes = new int[routeSize]; + byte[] routeBytes = new byte[routeSize]; System.arraycopy(bytes, 7, routeBytes, 0, routeSize); taskMap.put("robotRoutes", formatLanLat(routeBytes)); return JSON.toJSONString(taskMap); @@ -182,17 +182,17 @@ /** * 定深直航任务 */ - public static String decodeDirectFlightTask(int[] bytes) { + public static String decodeDirectFlightTask(byte[] bytes) { Map taskMap = new HashMap<>(3); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 6, angleBytes, 0, 2); taskMap.put("headingAngle", covertHeadingAngle(angleBytes) + "°"); - int[] timesBytes = new int[2]; + byte[] timesBytes = new byte[2]; System.arraycopy(bytes, 8, timesBytes, 0, 2); taskMap.put("time", covertToDec(timesBytes) + "s"); return JSON.toJSONString(taskMap); @@ -201,21 +201,21 @@ /** * 驻留任务 */ - public static String decodeResideTask(int[] bytes) { + public static String decodeResideTask(byte[] bytes) { Map taskMap = new HashMap<>(4); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 4, lngBytes, 0, 4); taskMap.put("lng", String.valueOf(covertLngLat(lngBytes))); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 8, latBytes, 0, 4); taskMap.put("lat", String.valueOf(covertLngLat(latBytes))); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 12, angleBytes, 0, 2); taskMap.put("depth", covertDepth(angleBytes) + "m"); - int[] timesBytes = new int[2]; + byte[] timesBytes = new byte[2]; System.arraycopy(bytes, 14, timesBytes, 0, 2); taskMap.put("time", covertToDec(timesBytes) + "min"); return JSON.toJSONString(taskMap); @@ -224,20 +224,20 @@ /** * 投放任务 */ - public static String decodePutTask(int[] bytes) { + public static String decodePutTask(byte[] bytes) { Map taskMap = new HashMap<>(4); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 4, lngBytes, 0, 4); taskMap.put("lng", String.valueOf(covertLngLat(lngBytes))); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 8, latBytes, 0, 4); taskMap.put("lat", String.valueOf(covertLngLat(latBytes))); int count = bytes[12]; taskMap.put("count", String.valueOf(count)); - int[] idsBytes = new int[count]; + byte[] idsBytes = new byte[count]; System.arraycopy(bytes, 13, idsBytes, 0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < idsBytes.length; i++) { @@ -254,11 +254,11 @@ /** * 短信内容 */ - public static String decodeSMS(int[] bytes) { + public static String decodeSMS(byte[] bytes) { //帧头、帧长度、帧类型、子类型、机器人ID,CRC16校验码、帧尾总长度=8 int dataLen = bytes.length - 8; - int[] dataBytes = new int[dataLen]; + byte[] dataBytes = new byte[dataLen]; System.arraycopy(bytes, 5, dataBytes, 0, dataLen); StringBuilder builder = new StringBuilder(); for (int dataByte : dataBytes) { @@ -270,9 +270,9 @@ /** * AIS数据请求 */ - public static String decodeAISRequest(int[] bytes) { + public static String decodeAISRequest(byte[] bytes) { Map taskMap = new HashMap<>(2); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("radius", covertToDec(depthBytes) + "km"); @@ -285,13 +285,13 @@ * 编码encode * 解码decode */ - public static ShelterPosition decodeShelterPosition(int[] bytes) { + public static ShelterPosition decodeShelterPosition(byte[] bytes) { ShelterPosition shelter = new ShelterPosition(); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 5, lngBytes, 0, 4); shelter.setLng(covertLngLat(lngBytes)); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 9, latBytes, 0, 4); shelter.setLat(covertLngLat(latBytes)); @@ -301,10 +301,10 @@ return shelter; } - public static String formatLanLat(int[] targetBytes) { + public static String formatLanLat(byte[] targetBytes) { List lnglat = new ArrayList<>(); for (int i = 0; i < targetBytes.length; i += 4) { - int[] target = new int[4]; + byte[] target = new byte[4]; System.arraycopy(targetBytes, i, target, 0, 4); //每4个字节转换一次经纬度 @@ -326,7 +326,7 @@ /** * 十进制转换算法 */ - private static double covertToDec(int[] bytes) { + private static double covertToDec(byte[] bytes) { return ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); } @@ -334,7 +334,7 @@ /** * 任务深度转换算法 */ - private static double covertDepth(int[] bytes) { + private static double covertDepth(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (a * 0.1); @@ -343,7 +343,7 @@ /** * 目标频率转换算法 */ - private static String covertTargetHertz(int[] bytes) { + private static String covertTargetHertz(byte[] bytes) { return ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF) + "hz"; } @@ -351,7 +351,7 @@ /** * 目标距离转换算法 */ - private static String covertTargetDistance(int[] bytes) { + private static String covertTargetDistance(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01) + "km"; @@ -360,7 +360,7 @@ /** * 横滚角转换算法 */ - private static float covertBuoyancy(int[] bytes) { + private static float covertBuoyancy(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 100); @@ -369,7 +369,7 @@ /** * 横滚角转换算法 */ - private static float covertHeadingAngle(int[] bytes) { + private static float covertHeadingAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01); @@ -378,7 +378,7 @@ /** * 横滚角转换算法 */ - private static float covertRollAngle(int[] bytes) { + private static float covertRollAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 180); @@ -387,7 +387,7 @@ /** * 俯仰角转换算法 */ - private static float covertPitchAngle(int[] bytes) { + private static float covertPitchAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 90); @@ -420,7 +420,7 @@ /** * 经纬度转换算法 */ - private static double covertLngLat(int[] bytes) { + private static double covertLngLat(byte[] bytes) { double per = ( ((bytes[0] & 0xFF) << 24) + ((bytes[1] & 0xFF) << 16) + diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java index dc69c33..55c8156 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java @@ -7,9 +7,9 @@ */ public class Constant { - public static final int BITS_OF_HEAD = 0x2A; - public static final int BITS_OF_END = 0x0A; - public static final int SHELTER_ID = 0xFF; + public static final byte BITS_OF_HEAD = (byte) 0x2A; + public static final byte BITS_OF_END = (byte) 0x0A; + public static final byte SHELTER_ID = (byte) 0xFF; /** * 洋流相关常量 diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java index b18e3ad..ed3cabe 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java @@ -16,16 +16,16 @@ /** * 水下机器人信息 */ - public static RobotInfo decodeRobotInfo(int[] bytes) { + public static RobotInfo decodeRobotInfo(byte[] bytes) { RobotInfo robot = new RobotInfo(); - robot.setRobotId(bytes[4]); + robot.setRobotId((int) bytes[4]); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 5, lngBytes, 0, 4); robot.setLng(covertLngLat(lngBytes)); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 9, latBytes, 0, 4); robot.setLat(covertLngLat(latBytes)); @@ -35,23 +35,23 @@ robot.setInstrumentE((float) (bytes[16])); robot.setPowerE((float) (bytes[17])); - int[] pitchAngleBytes = new int[2]; + byte[] pitchAngleBytes = new byte[2]; System.arraycopy(bytes, 18, pitchAngleBytes, 0, 2); robot.setPitchAngle(covertPitchAngle(pitchAngleBytes)); - int[] rollAngleBytes = new int[2]; + byte[] rollAngleBytes = new byte[2]; System.arraycopy(bytes, 20, rollAngleBytes, 0, 2); robot.setRollAngle(covertRollAngle(rollAngleBytes)); - int[] headingAngleBytes = new int[2]; + byte[] headingAngleBytes = new byte[2]; System.arraycopy(bytes, 22, headingAngleBytes, 0, 2); robot.setHeadingAngle(covertHeadingAngle(headingAngleBytes)); - int[] buoyancyBytes = new int[2]; + byte[] buoyancyBytes = new byte[2]; System.arraycopy(bytes, 24, buoyancyBytes, 0, 2); robot.setBuoyancy(covertBuoyancy(buoyancyBytes)); - int[] positionBytes = new int[2]; + byte[] positionBytes = new byte[2]; System.arraycopy(bytes, 26, positionBytes, 0, 2); //滑块位置算法和浮力算法一样 robot.setPosition(covertBuoyancy(positionBytes)); @@ -63,16 +63,16 @@ robot.setDetectStatus("发现目标"); } - int[] targetDisBytes = new int[2]; + byte[] targetDisBytes = new byte[2]; System.arraycopy(bytes, 29, targetDisBytes, 0, 2); robot.setTargetDistance(covertTargetDistance(targetDisBytes)); - int[] targetDirBytes = new int[2]; + byte[] targetDirBytes = new byte[2]; System.arraycopy(bytes, 31, targetDirBytes, 0, 2); //目标方位算法和航向角算法一样 robot.setTargetDirection(covertHeadingAngle(targetDirBytes)); - int[] targetHzBytes = new int[2]; + byte[] targetHzBytes = new byte[2]; System.arraycopy(bytes, 33, targetHzBytes, 0, 2); robot.setTargetHertz(covertTargetHertz(targetHzBytes)); return robot; @@ -81,12 +81,12 @@ /** * 区域覆盖 */ - public static String decodeRobotRegion(int[] bytes) { + public static String decodeRobotRegion(byte[] bytes) { Map regionMap = new HashMap<>(); int count = bytes[4]; regionMap.put("count", String.valueOf(count)); - int[] idsBytes = new int[count]; + byte[] idsBytes = new byte[count]; System.arraycopy(bytes, 5, idsBytes, 0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < idsBytes.length; i++) { @@ -100,14 +100,14 @@ //经度和纬度,所以需要*2 int targetSize = count * 2 * 4; - int[] targetBytes = new int[targetSize]; + byte[] targetBytes = new byte[targetSize]; //起始4个字节,机器人个数1个字节 System.arraycopy(bytes, 5 + idsBytes.length, targetBytes, 0, targetSize); regionMap.put("robotTargets", formatLanLat(targetBytes)); regionMap.put("pointCount", String.valueOf(bytes[5 + idsBytes.length + targetBytes.length])); - int[] radiusBytes = new int[2]; + byte[] radiusBytes = new byte[2]; System.arraycopy(bytes, 5 + idsBytes.length + targetBytes.length + 1, radiusBytes, 0, 2); regionMap.put("regionRadius", covertTargetDistance(radiusBytes)); @@ -120,7 +120,7 @@ //经度和纬度,所以需要*2 int pointSize = count * 2 * 4; - int[] pointBytes = new int[pointSize]; + byte[] pointBytes = new byte[pointSize]; System.arraycopy(bytes, 5 + idsBytes.length + targetBytes.length + 1 + radiusBytes.length + 1, pointBytes, 0, pointSize); @@ -131,14 +131,14 @@ /** * 路径规划 */ - public static String decodeRobotRoute(int[] bytes) { + public static String decodeRobotRoute(byte[] bytes) { Map routeMap = new HashMap<>(2); int count = bytes[4]; routeMap.put("routeCount", String.valueOf(count)); int routeSize = count * 2 * 4; - int[] routeBytes = new int[routeSize]; + byte[] routeBytes = new byte[routeSize]; System.arraycopy(bytes, 5, routeBytes, 0, routeSize); routeMap.put("robotRoutes", formatLanLat(routeBytes)); return JSON.toJSONString(routeMap); @@ -147,13 +147,13 @@ /** * 定向任务 */ - public static String decodeDirectedTask(int[] bytes) { + public static String decodeDirectedTask(byte[] bytes) { Map taskMap = new HashMap<>(2); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 6, angleBytes, 0, 2); taskMap.put("headingAngle", covertHeadingAngle(angleBytes) + "°"); return JSON.toJSONString(taskMap); @@ -162,10 +162,10 @@ /** * 航路点任务 */ - public static String decodeTaskRoute(int[] bytes) { + public static String decodeTaskRoute(byte[] bytes) { Map taskMap = new HashMap<>(3); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); @@ -173,7 +173,7 @@ taskMap.put("routeCount", String.valueOf(count)); int routeSize = count * 2 * 4; - int[] routeBytes = new int[routeSize]; + byte[] routeBytes = new byte[routeSize]; System.arraycopy(bytes, 7, routeBytes, 0, routeSize); taskMap.put("robotRoutes", formatLanLat(routeBytes)); return JSON.toJSONString(taskMap); @@ -182,17 +182,17 @@ /** * 定深直航任务 */ - public static String decodeDirectFlightTask(int[] bytes) { + public static String decodeDirectFlightTask(byte[] bytes) { Map taskMap = new HashMap<>(3); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 6, angleBytes, 0, 2); taskMap.put("headingAngle", covertHeadingAngle(angleBytes) + "°"); - int[] timesBytes = new int[2]; + byte[] timesBytes = new byte[2]; System.arraycopy(bytes, 8, timesBytes, 0, 2); taskMap.put("time", covertToDec(timesBytes) + "s"); return JSON.toJSONString(taskMap); @@ -201,21 +201,21 @@ /** * 驻留任务 */ - public static String decodeResideTask(int[] bytes) { + public static String decodeResideTask(byte[] bytes) { Map taskMap = new HashMap<>(4); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 4, lngBytes, 0, 4); taskMap.put("lng", String.valueOf(covertLngLat(lngBytes))); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 8, latBytes, 0, 4); taskMap.put("lat", String.valueOf(covertLngLat(latBytes))); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 12, angleBytes, 0, 2); taskMap.put("depth", covertDepth(angleBytes) + "m"); - int[] timesBytes = new int[2]; + byte[] timesBytes = new byte[2]; System.arraycopy(bytes, 14, timesBytes, 0, 2); taskMap.put("time", covertToDec(timesBytes) + "min"); return JSON.toJSONString(taskMap); @@ -224,20 +224,20 @@ /** * 投放任务 */ - public static String decodePutTask(int[] bytes) { + public static String decodePutTask(byte[] bytes) { Map taskMap = new HashMap<>(4); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 4, lngBytes, 0, 4); taskMap.put("lng", String.valueOf(covertLngLat(lngBytes))); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 8, latBytes, 0, 4); taskMap.put("lat", String.valueOf(covertLngLat(latBytes))); int count = bytes[12]; taskMap.put("count", String.valueOf(count)); - int[] idsBytes = new int[count]; + byte[] idsBytes = new byte[count]; System.arraycopy(bytes, 13, idsBytes, 0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < idsBytes.length; i++) { @@ -254,11 +254,11 @@ /** * 短信内容 */ - public static String decodeSMS(int[] bytes) { + public static String decodeSMS(byte[] bytes) { //帧头、帧长度、帧类型、子类型、机器人ID,CRC16校验码、帧尾总长度=8 int dataLen = bytes.length - 8; - int[] dataBytes = new int[dataLen]; + byte[] dataBytes = new byte[dataLen]; System.arraycopy(bytes, 5, dataBytes, 0, dataLen); StringBuilder builder = new StringBuilder(); for (int dataByte : dataBytes) { @@ -270,9 +270,9 @@ /** * AIS数据请求 */ - public static String decodeAISRequest(int[] bytes) { + public static String decodeAISRequest(byte[] bytes) { Map taskMap = new HashMap<>(2); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("radius", covertToDec(depthBytes) + "km"); @@ -285,13 +285,13 @@ * 编码encode * 解码decode */ - public static ShelterPosition decodeShelterPosition(int[] bytes) { + public static ShelterPosition decodeShelterPosition(byte[] bytes) { ShelterPosition shelter = new ShelterPosition(); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 5, lngBytes, 0, 4); shelter.setLng(covertLngLat(lngBytes)); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 9, latBytes, 0, 4); shelter.setLat(covertLngLat(latBytes)); @@ -301,10 +301,10 @@ return shelter; } - public static String formatLanLat(int[] targetBytes) { + public static String formatLanLat(byte[] targetBytes) { List lnglat = new ArrayList<>(); for (int i = 0; i < targetBytes.length; i += 4) { - int[] target = new int[4]; + byte[] target = new byte[4]; System.arraycopy(targetBytes, i, target, 0, 4); //每4个字节转换一次经纬度 @@ -326,7 +326,7 @@ /** * 十进制转换算法 */ - private static double covertToDec(int[] bytes) { + private static double covertToDec(byte[] bytes) { return ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); } @@ -334,7 +334,7 @@ /** * 任务深度转换算法 */ - private static double covertDepth(int[] bytes) { + private static double covertDepth(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (a * 0.1); @@ -343,7 +343,7 @@ /** * 目标频率转换算法 */ - private static String covertTargetHertz(int[] bytes) { + private static String covertTargetHertz(byte[] bytes) { return ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF) + "hz"; } @@ -351,7 +351,7 @@ /** * 目标距离转换算法 */ - private static String covertTargetDistance(int[] bytes) { + private static String covertTargetDistance(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01) + "km"; @@ -360,7 +360,7 @@ /** * 横滚角转换算法 */ - private static float covertBuoyancy(int[] bytes) { + private static float covertBuoyancy(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 100); @@ -369,7 +369,7 @@ /** * 横滚角转换算法 */ - private static float covertHeadingAngle(int[] bytes) { + private static float covertHeadingAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01); @@ -378,7 +378,7 @@ /** * 横滚角转换算法 */ - private static float covertRollAngle(int[] bytes) { + private static float covertRollAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 180); @@ -387,7 +387,7 @@ /** * 俯仰角转换算法 */ - private static float covertPitchAngle(int[] bytes) { + private static float covertPitchAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 90); @@ -420,7 +420,7 @@ /** * 经纬度转换算法 */ - private static double covertLngLat(int[] bytes) { + private static double covertLngLat(byte[] bytes) { double per = ( ((bytes[0] & 0xFF) << 24) + ((bytes[1] & 0xFF) << 16) + diff --git a/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java b/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java index bc8a81e..d7bb43b 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java +++ b/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java @@ -6,7 +6,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.bytes.ByteArrayDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; @@ -50,9 +50,10 @@ @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); - + //接收消息格式-byte[] + pipeline.addLast("decoder", new ByteArrayDecoder()); + //发送消息格式-String pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); - pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); /** * 自定义ChannelInboundHandlerAdapter */ diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java index dc69c33..55c8156 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/Constant.java @@ -7,9 +7,9 @@ */ public class Constant { - public static final int BITS_OF_HEAD = 0x2A; - public static final int BITS_OF_END = 0x0A; - public static final int SHELTER_ID = 0xFF; + public static final byte BITS_OF_HEAD = (byte) 0x2A; + public static final byte BITS_OF_END = (byte) 0x0A; + public static final byte SHELTER_ID = (byte) 0xFF; /** * 洋流相关常量 diff --git a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java index b18e3ad..ed3cabe 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java +++ b/casic-shelter/src/main/java/com/casic/missiles/modular/system/utils/DecodeData.java @@ -16,16 +16,16 @@ /** * 水下机器人信息 */ - public static RobotInfo decodeRobotInfo(int[] bytes) { + public static RobotInfo decodeRobotInfo(byte[] bytes) { RobotInfo robot = new RobotInfo(); - robot.setRobotId(bytes[4]); + robot.setRobotId((int) bytes[4]); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 5, lngBytes, 0, 4); robot.setLng(covertLngLat(lngBytes)); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 9, latBytes, 0, 4); robot.setLat(covertLngLat(latBytes)); @@ -35,23 +35,23 @@ robot.setInstrumentE((float) (bytes[16])); robot.setPowerE((float) (bytes[17])); - int[] pitchAngleBytes = new int[2]; + byte[] pitchAngleBytes = new byte[2]; System.arraycopy(bytes, 18, pitchAngleBytes, 0, 2); robot.setPitchAngle(covertPitchAngle(pitchAngleBytes)); - int[] rollAngleBytes = new int[2]; + byte[] rollAngleBytes = new byte[2]; System.arraycopy(bytes, 20, rollAngleBytes, 0, 2); robot.setRollAngle(covertRollAngle(rollAngleBytes)); - int[] headingAngleBytes = new int[2]; + byte[] headingAngleBytes = new byte[2]; System.arraycopy(bytes, 22, headingAngleBytes, 0, 2); robot.setHeadingAngle(covertHeadingAngle(headingAngleBytes)); - int[] buoyancyBytes = new int[2]; + byte[] buoyancyBytes = new byte[2]; System.arraycopy(bytes, 24, buoyancyBytes, 0, 2); robot.setBuoyancy(covertBuoyancy(buoyancyBytes)); - int[] positionBytes = new int[2]; + byte[] positionBytes = new byte[2]; System.arraycopy(bytes, 26, positionBytes, 0, 2); //滑块位置算法和浮力算法一样 robot.setPosition(covertBuoyancy(positionBytes)); @@ -63,16 +63,16 @@ robot.setDetectStatus("发现目标"); } - int[] targetDisBytes = new int[2]; + byte[] targetDisBytes = new byte[2]; System.arraycopy(bytes, 29, targetDisBytes, 0, 2); robot.setTargetDistance(covertTargetDistance(targetDisBytes)); - int[] targetDirBytes = new int[2]; + byte[] targetDirBytes = new byte[2]; System.arraycopy(bytes, 31, targetDirBytes, 0, 2); //目标方位算法和航向角算法一样 robot.setTargetDirection(covertHeadingAngle(targetDirBytes)); - int[] targetHzBytes = new int[2]; + byte[] targetHzBytes = new byte[2]; System.arraycopy(bytes, 33, targetHzBytes, 0, 2); robot.setTargetHertz(covertTargetHertz(targetHzBytes)); return robot; @@ -81,12 +81,12 @@ /** * 区域覆盖 */ - public static String decodeRobotRegion(int[] bytes) { + public static String decodeRobotRegion(byte[] bytes) { Map regionMap = new HashMap<>(); int count = bytes[4]; regionMap.put("count", String.valueOf(count)); - int[] idsBytes = new int[count]; + byte[] idsBytes = new byte[count]; System.arraycopy(bytes, 5, idsBytes, 0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < idsBytes.length; i++) { @@ -100,14 +100,14 @@ //经度和纬度,所以需要*2 int targetSize = count * 2 * 4; - int[] targetBytes = new int[targetSize]; + byte[] targetBytes = new byte[targetSize]; //起始4个字节,机器人个数1个字节 System.arraycopy(bytes, 5 + idsBytes.length, targetBytes, 0, targetSize); regionMap.put("robotTargets", formatLanLat(targetBytes)); regionMap.put("pointCount", String.valueOf(bytes[5 + idsBytes.length + targetBytes.length])); - int[] radiusBytes = new int[2]; + byte[] radiusBytes = new byte[2]; System.arraycopy(bytes, 5 + idsBytes.length + targetBytes.length + 1, radiusBytes, 0, 2); regionMap.put("regionRadius", covertTargetDistance(radiusBytes)); @@ -120,7 +120,7 @@ //经度和纬度,所以需要*2 int pointSize = count * 2 * 4; - int[] pointBytes = new int[pointSize]; + byte[] pointBytes = new byte[pointSize]; System.arraycopy(bytes, 5 + idsBytes.length + targetBytes.length + 1 + radiusBytes.length + 1, pointBytes, 0, pointSize); @@ -131,14 +131,14 @@ /** * 路径规划 */ - public static String decodeRobotRoute(int[] bytes) { + public static String decodeRobotRoute(byte[] bytes) { Map routeMap = new HashMap<>(2); int count = bytes[4]; routeMap.put("routeCount", String.valueOf(count)); int routeSize = count * 2 * 4; - int[] routeBytes = new int[routeSize]; + byte[] routeBytes = new byte[routeSize]; System.arraycopy(bytes, 5, routeBytes, 0, routeSize); routeMap.put("robotRoutes", formatLanLat(routeBytes)); return JSON.toJSONString(routeMap); @@ -147,13 +147,13 @@ /** * 定向任务 */ - public static String decodeDirectedTask(int[] bytes) { + public static String decodeDirectedTask(byte[] bytes) { Map taskMap = new HashMap<>(2); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 6, angleBytes, 0, 2); taskMap.put("headingAngle", covertHeadingAngle(angleBytes) + "°"); return JSON.toJSONString(taskMap); @@ -162,10 +162,10 @@ /** * 航路点任务 */ - public static String decodeTaskRoute(int[] bytes) { + public static String decodeTaskRoute(byte[] bytes) { Map taskMap = new HashMap<>(3); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); @@ -173,7 +173,7 @@ taskMap.put("routeCount", String.valueOf(count)); int routeSize = count * 2 * 4; - int[] routeBytes = new int[routeSize]; + byte[] routeBytes = new byte[routeSize]; System.arraycopy(bytes, 7, routeBytes, 0, routeSize); taskMap.put("robotRoutes", formatLanLat(routeBytes)); return JSON.toJSONString(taskMap); @@ -182,17 +182,17 @@ /** * 定深直航任务 */ - public static String decodeDirectFlightTask(int[] bytes) { + public static String decodeDirectFlightTask(byte[] bytes) { Map taskMap = new HashMap<>(3); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("taskDepth", covertDepth(depthBytes) + "m"); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 6, angleBytes, 0, 2); taskMap.put("headingAngle", covertHeadingAngle(angleBytes) + "°"); - int[] timesBytes = new int[2]; + byte[] timesBytes = new byte[2]; System.arraycopy(bytes, 8, timesBytes, 0, 2); taskMap.put("time", covertToDec(timesBytes) + "s"); return JSON.toJSONString(taskMap); @@ -201,21 +201,21 @@ /** * 驻留任务 */ - public static String decodeResideTask(int[] bytes) { + public static String decodeResideTask(byte[] bytes) { Map taskMap = new HashMap<>(4); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 4, lngBytes, 0, 4); taskMap.put("lng", String.valueOf(covertLngLat(lngBytes))); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 8, latBytes, 0, 4); taskMap.put("lat", String.valueOf(covertLngLat(latBytes))); - int[] angleBytes = new int[2]; + byte[] angleBytes = new byte[2]; System.arraycopy(bytes, 12, angleBytes, 0, 2); taskMap.put("depth", covertDepth(angleBytes) + "m"); - int[] timesBytes = new int[2]; + byte[] timesBytes = new byte[2]; System.arraycopy(bytes, 14, timesBytes, 0, 2); taskMap.put("time", covertToDec(timesBytes) + "min"); return JSON.toJSONString(taskMap); @@ -224,20 +224,20 @@ /** * 投放任务 */ - public static String decodePutTask(int[] bytes) { + public static String decodePutTask(byte[] bytes) { Map taskMap = new HashMap<>(4); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 4, lngBytes, 0, 4); taskMap.put("lng", String.valueOf(covertLngLat(lngBytes))); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 8, latBytes, 0, 4); taskMap.put("lat", String.valueOf(covertLngLat(latBytes))); int count = bytes[12]; taskMap.put("count", String.valueOf(count)); - int[] idsBytes = new int[count]; + byte[] idsBytes = new byte[count]; System.arraycopy(bytes, 13, idsBytes, 0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < idsBytes.length; i++) { @@ -254,11 +254,11 @@ /** * 短信内容 */ - public static String decodeSMS(int[] bytes) { + public static String decodeSMS(byte[] bytes) { //帧头、帧长度、帧类型、子类型、机器人ID,CRC16校验码、帧尾总长度=8 int dataLen = bytes.length - 8; - int[] dataBytes = new int[dataLen]; + byte[] dataBytes = new byte[dataLen]; System.arraycopy(bytes, 5, dataBytes, 0, dataLen); StringBuilder builder = new StringBuilder(); for (int dataByte : dataBytes) { @@ -270,9 +270,9 @@ /** * AIS数据请求 */ - public static String decodeAISRequest(int[] bytes) { + public static String decodeAISRequest(byte[] bytes) { Map taskMap = new HashMap<>(2); - int[] depthBytes = new int[2]; + byte[] depthBytes = new byte[2]; System.arraycopy(bytes, 4, depthBytes, 0, 2); taskMap.put("radius", covertToDec(depthBytes) + "km"); @@ -285,13 +285,13 @@ * 编码encode * 解码decode */ - public static ShelterPosition decodeShelterPosition(int[] bytes) { + public static ShelterPosition decodeShelterPosition(byte[] bytes) { ShelterPosition shelter = new ShelterPosition(); - int[] lngBytes = new int[4]; + byte[] lngBytes = new byte[4]; System.arraycopy(bytes, 5, lngBytes, 0, 4); shelter.setLng(covertLngLat(lngBytes)); - int[] latBytes = new int[4]; + byte[] latBytes = new byte[4]; System.arraycopy(bytes, 9, latBytes, 0, 4); shelter.setLat(covertLngLat(latBytes)); @@ -301,10 +301,10 @@ return shelter; } - public static String formatLanLat(int[] targetBytes) { + public static String formatLanLat(byte[] targetBytes) { List lnglat = new ArrayList<>(); for (int i = 0; i < targetBytes.length; i += 4) { - int[] target = new int[4]; + byte[] target = new byte[4]; System.arraycopy(targetBytes, i, target, 0, 4); //每4个字节转换一次经纬度 @@ -326,7 +326,7 @@ /** * 十进制转换算法 */ - private static double covertToDec(int[] bytes) { + private static double covertToDec(byte[] bytes) { return ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); } @@ -334,7 +334,7 @@ /** * 任务深度转换算法 */ - private static double covertDepth(int[] bytes) { + private static double covertDepth(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (a * 0.1); @@ -343,7 +343,7 @@ /** * 目标频率转换算法 */ - private static String covertTargetHertz(int[] bytes) { + private static String covertTargetHertz(byte[] bytes) { return ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF) + "hz"; } @@ -351,7 +351,7 @@ /** * 目标距离转换算法 */ - private static String covertTargetDistance(int[] bytes) { + private static String covertTargetDistance(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01) + "km"; @@ -360,7 +360,7 @@ /** * 横滚角转换算法 */ - private static float covertBuoyancy(int[] bytes) { + private static float covertBuoyancy(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 100); @@ -369,7 +369,7 @@ /** * 横滚角转换算法 */ - private static float covertHeadingAngle(int[] bytes) { + private static float covertHeadingAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01); @@ -378,7 +378,7 @@ /** * 横滚角转换算法 */ - private static float covertRollAngle(int[] bytes) { + private static float covertRollAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 180); @@ -387,7 +387,7 @@ /** * 俯仰角转换算法 */ - private static float covertPitchAngle(int[] bytes) { + private static float covertPitchAngle(byte[] bytes) { float a = ((bytes[0] & 0xFF) << 8) + (bytes[1] & 0xFF); return (float) (a * 0.01 - 90); @@ -420,7 +420,7 @@ /** * 经纬度转换算法 */ - private static double covertLngLat(int[] bytes) { + private static double covertLngLat(byte[] bytes) { double per = ( ((bytes[0] & 0xFF) << 24) + ((bytes[1] & 0xFF) << 16) + diff --git a/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java b/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java index bc8a81e..d7bb43b 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java +++ b/casic-shelter/src/main/java/com/casic/missiles/socket/BootNettyServer.java @@ -6,7 +6,7 @@ import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; -import io.netty.handler.codec.string.StringDecoder; +import io.netty.handler.codec.bytes.ByteArrayDecoder; import io.netty.handler.codec.string.StringEncoder; import io.netty.util.CharsetUtil; import lombok.extern.slf4j.Slf4j; @@ -50,9 +50,10 @@ @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); - + //接收消息格式-byte[] + pipeline.addLast("decoder", new ByteArrayDecoder()); + //发送消息格式-String pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); - pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); /** * 自定义ChannelInboundHandlerAdapter */ diff --git a/casic-shelter/src/main/java/com/casic/missiles/socket/service/impl/SocketServiceImpl.java b/casic-shelter/src/main/java/com/casic/missiles/socket/service/impl/SocketServiceImpl.java index 5680b05..38f2903 100644 --- a/casic-shelter/src/main/java/com/casic/missiles/socket/service/impl/SocketServiceImpl.java +++ b/casic-shelter/src/main/java/com/casic/missiles/socket/service/impl/SocketServiceImpl.java @@ -3,6 +3,7 @@ import com.alibaba.fastjson.JSON; import com.casic.missiles.modular.system.dto.ShipDTO; import com.casic.missiles.modular.system.dto.TyphoonDTO; +import com.casic.missiles.modular.system.model.RobotInfo; import com.casic.missiles.modular.system.model.ShelterPosition; import com.casic.missiles.modular.system.service.IShelterPositionService; import com.casic.missiles.modular.system.service.IShipService; @@ -15,6 +16,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import java.util.Arrays; import java.util.List; import java.util.Map; @@ -37,14 +39,14 @@ this.shelterPositionService = shelterPositionService; } - private boolean isCorrectData(int[] bytes) { + private boolean isCorrectData(byte[] bytes) { if (bytes[0] != Constant.BITS_OF_HEAD) { return false; } return bytes[bytes.length - 1] == Constant.BITS_OF_END; } - private int decodeRobotId(int[] bytes) { + private int decodeRobotId(byte[] bytes) { int robotId; if (Constant.SHELTER_ID == bytes[4]) { //返回方舱的周围的AIS数据 @@ -64,16 +66,11 @@ */ @Override public void communicate(ChannelHandlerContext ctx, Object msg) { - String temp = msg.toString(); - log.info("channelRead message ===> " + temp); + byte[] data = (byte[]) msg; + log.info("channelRead message ===> " + Arrays.toString(data)); /** * 回应客户端 * */ - String[] dataArray = temp.split(" "); - int[] data = new int[dataArray.length]; - for (int i = 0; i < dataArray.length; i++) { - data[i] = Integer.parseInt(dataArray[i], 16); - } if (isCorrectData(data)) { int dataType = data[2]; int robotId = decodeRobotId(data); @@ -83,7 +80,7 @@ switch (dataType) { case 0x01: //水下机器人信息 - DecodeData.decodeRobotInfo(data); + RobotInfo robotInfo = DecodeData.decodeRobotInfo(data); break; case 0x02: //算法结果 @@ -131,10 +128,10 @@ case 0x07: //方舱位置 ShelterPosition shelterPosition = DecodeData.decodeShelterPosition( - new int[]{0x2A, 0x23, 0x07, 0x00, 0x00, - 0x4D, 0xBD, 0x80, 0xCF, - 0x0C, 0x5E, 0x28, 0xC2, - 0xC7, 0x16, 0x0A} + new byte[]{(byte) 0x2A, (byte) 0x23, (byte) 0x07, (byte) 0x00, (byte) 0x00, + (byte) 0x4D, (byte) 0xBD, (byte) 0x80, (byte) 0xCF, + (byte) 0x0C, (byte) 0x5E, (byte) 0x28, (byte) 0xC2, + (byte) 0xC7, (byte) 0x16, (byte) 0x0A} ); shelterPositionService.save(shelterPosition); break;