From dd217ef315321d55548ecb9d0be1b5e8bcb00c1d Mon Sep 17 00:00:00 2001 From: htjcAdmin Date: Sat, 15 Mar 2025 09:33:51 +0800 Subject: [PATCH] =?UTF-8?q?/*1.=20=E5=8D=8E=E8=BD=AF=E6=97=A0=E4=BA=BA?= =?UTF-8?q?=E6=9C=BA=E6=8E=A7=E5=88=B6=E6=8C=87=E4=BB=A4=E5=AF=B9=E6=8E=A5?= =?UTF-8?q?=EF=BC=9B2.=20jar=E5=8C=85=E6=B7=B7=E6=B7=86=E3=80=82*/?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 44 ++++++++++-- proguard.cfg | 65 +++++++++++++++++ .../tcpserver/TcpserverApplication.java | 27 ++++++- .../inspect/tcpserver/tcp/BaseControl.java | 12 ++-- .../inspect/tcpserver/tcp/DownXml2Json.java | 9 ++- .../inspect/tcpserver/tcp/NettyClient.java | 6 +- .../inspect/tcpserver/tcp/NettyServer.java | 72 ++++++++++++++----- .../com/inspect/tcpserver/tcp/SystemType.java | 8 +++ .../com/inspect/tcpserver/tcp/UpJson2Xml.java | 9 ++- src/main/resources/logback.xml | 2 +- 10 files changed, 211 insertions(+), 43 deletions(-) create mode 100644 proguard.cfg diff --git a/pom.xml b/pom.xml index 3468200..1824105 100644 --- a/pom.xml +++ b/pom.xml @@ -105,15 +105,47 @@ ${project.artifactId} + + + + + + + + + + - org.apache.maven.plugins - maven-compiler-plugin - 3.8.1 + com.github.wvengen + proguard-maven-plugin + 2.3.1 + + + package + + proguard + + + - 1.8 - 1.8 - UTF-8 + 6.0.3 + ${project.build.finalName}.jar + ${project.build.finalName}.jar + true + ${project.basedir}/proguard.cfg + + ${java.home}/lib/rt.jar + ${java.home}/lib/jce.jar + ${java.home}/lib/jsse.jar + + + + net.sf.proguard + proguard-base + 6.0.3 + + org.springframework.boot diff --git a/proguard.cfg b/proguard.cfg new file mode 100644 index 0000000..b4545d7 --- /dev/null +++ b/proguard.cfg @@ -0,0 +1,65 @@ +-target 1.8 ##Specify the java version number +-dontshrink ##Default is enabled, here the shrink is turned off, that is, the unused classes/members are not deleted. +-dontoptimize ##Default is enabled, here to turn off bytecode level optimization +-useuniqueclassmembernames ## Take a unique strategy for confusing the naming of class members +-adaptclassstrings ## After confusing the class name, replace it with a place like Class.forName('className') +-dontnote +-ignorewarnings ## warnings are ignored +-dontwarn +-keep public class * extends org.springframework.boot.web.support.SpringBootServletInitializer +-keepdirectories ## Keep the package structure +-keepclasseswithmembers public class * { public static void main(java.lang.String[]);} ##Maintain the class of the main method and its method name +-keepclassmembers enum * { *; } ##Reserving enumeration members and methods +-keepclassmembers class * { + @org.springframework.beans.factory.annotation.Autowired *; + @org.springframework.beans.factory.annotation.Qualifier *; + @org.springframework.beans.factory.annotation.Value *; + @org.springframework.beans.factory.annotation.Required *; + @org.springframework.context.annotation.Bean *; + @org.springframework.context.annotation.Primary *; + @org.springframework.boot.context.properties.ConfigurationProperties *; + @org.springframework.boot.context.properties.EnableConfigurationProperties *; + @javax.inject.Inject *; + @javax.annotation.PostConstruct *; + @javax.annotation.PreDestroy *; +} +-keep @org.springframework.cache.annotation.EnableCaching class * +-keep @org.springframework.context.annotation.Configuration class * +-keep @org.springframework.boot.context.properties.ConfigurationProperties class * +-keep @org.springframework.boot.autoconfigure.SpringBootApplication class * +-allowaccessmodification +-keepattributes *Annotation* +-keepdirectories com.jayk.springboot.proguard.obfuscationdemo +-keepdirectories org.springframework.boot.autoconfigure +## Do not change names of the getters and setter, if you remove this ##thymeleaf unable to find the getter and setter i.e: ##${greetingDTO.message} +-keepclassmembers class * { + *** get*(); + void set*(***); +} +-keepclassmembernames class * { + java.lang.Class class$(java.lang.String); + java.lang.Class class$(java.lang.String, boolean); +} +-keepclassmembers enum * { + public static **[] values(); + public static ** valueOf(java.lang.String); + public static ** fromValue(java.lang.String); +} +-keepnames class * implements java.io.Serializable +-keepclassmembernames public class com.test.blah.config.liquibase.AsyncSpringLiquibase +-keepclassmembers class * implements java.io.Serializable { + static final long serialVersionUID; + private static final java.io.ObjectStreamField[] serialPersistentFields; + !static !transient ; + !private ; + !private ; + private void writeObject(java.io.ObjectOutputStream); + private void readObject(java.io.ObjectInputStream); + java.lang.Object writeReplace(); + java.lang.Object readResolve(); +} +-keepclassmembers class * { + @org.springframework.beans.factory.annotation.Autowired ; + @org.springframework.beans.factory.annotation.Autowired ; + @org.springframework.security.access.prepost.PreAuthorize ; +} \ No newline at end of file diff --git a/src/main/java/com/inspect/tcpserver/TcpserverApplication.java b/src/main/java/com/inspect/tcpserver/TcpserverApplication.java index 8a6fdb8..caa22c0 100644 --- a/src/main/java/com/inspect/tcpserver/TcpserverApplication.java +++ b/src/main/java/com/inspect/tcpserver/TcpserverApplication.java @@ -1,15 +1,40 @@ package com.inspect.tcpserver; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.AnnotationBeanNameGenerator; import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication @EnableAsync public class TcpserverApplication { + public static class CustomGenerator implements BeanNameGenerator { + + @Override + public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { + return definition.getBeanClassName(); + } + } + public static void main(String[] args) { - SpringApplication.run(TcpserverApplication.class, args); +// SpringApplication.run(TcpserverApplication.class, args); +// new SpringApplicationBuilder(TcpserverApplication.class).beanNameGenerator(new AnnotationBeanNameGenerator() { +// @Override +// protected String buildDefaultBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { +// return definition.getBeanClassName(); +// } +// }).run(args); + + new SpringApplicationBuilder(TcpserverApplication.class) + .beanNameGenerator(new CustomGenerator()) + .run(args); + + } } diff --git a/src/main/java/com/inspect/tcpserver/tcp/BaseControl.java b/src/main/java/com/inspect/tcpserver/tcp/BaseControl.java index 6342710..7799b9a 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/BaseControl.java +++ b/src/main/java/com/inspect/tcpserver/tcp/BaseControl.java @@ -220,13 +220,13 @@ class PatrolDeviceStateModel { public String unit; //单位 } -class PatrolDeviceRuningControl extends BaseControl { - public List Items; +class PatrolDeviceRunningControl extends BaseControl { + public List Items; } //巡视设备运行数据 @XStreamAlias("Item") -class PatrolDeviceRuningModel { +class PatrolDeviceRunningModel { @XStreamAsAttribute public String patroldevice_name;//巡视设备名称 @XStreamAsAttribute @@ -268,13 +268,13 @@ class NestStateModel { } -class NestRuningControl extends BaseControl { - public List Items; +class NestRunningControl extends BaseControl { + public List Items; } //无人机机巢运行数据 @XStreamAlias("Item") -class NestRuningModel { +class NestRunningModel { @XStreamAsAttribute public String nest_name; //机巢名称 @XStreamAsAttribute diff --git a/src/main/java/com/inspect/tcpserver/tcp/DownXml2Json.java b/src/main/java/com/inspect/tcpserver/tcp/DownXml2Json.java index 0d7cceb..8175e42 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/DownXml2Json.java +++ b/src/main/java/com/inspect/tcpserver/tcp/DownXml2Json.java @@ -5,7 +5,6 @@ import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.naming.NoNameCoder; import com.thoughtworks.xstream.io.xml.Xpp3Driver; import com.thoughtworks.xstream.security.AnyTypePermission; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -164,11 +163,11 @@ public class DownXml2Json { //巡检设备运行数据 public String PatrolDeviceRunningControlXml2Json(String xml) { XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); - xStream.alias(alias, PatrolDeviceRuningControl.class); + xStream.alias(alias, PatrolDeviceRunningControl.class); xStream.autodetectAnnotations(true); xStream.ignoreUnknownElements(); xStream.addPermission(AnyTypePermission.ANY); - PatrolDeviceRuningControl obj = (PatrolDeviceRuningControl) xStream.fromXML(xml); + PatrolDeviceRunningControl obj = (PatrolDeviceRunningControl) xStream.fromXML(xml); String resultJson = JSON.toJSONString(obj); return resultJson; } @@ -188,11 +187,11 @@ public class DownXml2Json { //机巢运行数据 public String NestRunningControlXml2Json(String xml) { XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); - xStream.alias(alias, NestRuningControl.class); + xStream.alias(alias, NestRunningControl.class); xStream.autodetectAnnotations(true); xStream.ignoreUnknownElements(); xStream.addPermission(AnyTypePermission.ANY); - NestRuningControl obj = (NestRuningControl) xStream.fromXML(xml); + NestRunningControl obj = (NestRunningControl) xStream.fromXML(xml); String resultJson = JSON.toJSONString(obj); return resultJson; } diff --git a/src/main/java/com/inspect/tcpserver/tcp/NettyClient.java b/src/main/java/com/inspect/tcpserver/tcp/NettyClient.java index 92ba5d1..52cd324 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/NettyClient.java +++ b/src/main/java/com/inspect/tcpserver/tcp/NettyClient.java @@ -65,7 +65,7 @@ public class NettyClient { @Resource private RestTemplate restTemplate; - @Resource + @Resource(name = "stringRedisTemplate") private RedisTemplate redisTemplate; @Value("${iip_server.send.url}") @@ -517,7 +517,7 @@ public class NettyClient { break; case PushType.nestRunning: // xml = upJson2Xml.NestRunningJson2Xml(json); - xml = upJson2Xml.UpStreamJson2Xml(json, NestRuningControl.class); + xml = upJson2Xml.UpStreamJson2Xml(json, NestRunningControl.class); logger.info("######## 会话: {}, 向上级系统发送无人机机巢运行数据 ########\n{}", uuid, xml); break; case PushType.nestState: @@ -532,7 +532,7 @@ public class NettyClient { break; case PushType.patrolDeviceRunning: // xml = upJson2Xml.PatrolDeviceRunningControlJson2Xml(json); - xml = upJson2Xml.UpStreamJson2Xml(json, PatrolDeviceRuningControl.class); + xml = upJson2Xml.UpStreamJson2Xml(json, PatrolDeviceRunningControl.class); logger.info("######## 会话: {}, 向上级系统发送巡视设备运行数据 ########\n{}", uuid, xml); break; case PushType.result: diff --git a/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java b/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java index 2693644..35bef0c 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java +++ b/src/main/java/com/inspect/tcpserver/tcp/NettyServer.java @@ -38,6 +38,7 @@ import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.Map; import java.util.concurrent.*; @@ -67,7 +68,7 @@ public class NettyServer { @Resource DeviceServerProperties deviceServerProperties; - @Resource + @Resource(name = "stringRedisTemplate") private RedisTemplate redisTemplate; @Resource @@ -162,7 +163,32 @@ public class NettyServer { nettyServerHandler.sendMsg(uuid, clientKey, tcpClientMap.get(clientKey), allBuf); sendIndex++; } else { - logger.warn(Color.RED + "######## 会话: {}, 设备端机器人: [{}] 离线!!! ########" + Color.END, uuid, tcpClientMap.get(clientKey)); + logger.warn(Color.RED + "######## 会话: {}, 客户: [{}/{}] 离线!!! ########" + Color.END, uuid, tcpClientMap.get(clientKey), clientKey); + } + } + + public void flushMsgToDeviceBroadcast(String uuid, String clientKey, boolean request, String xml) { + for (Map.Entry entry : tcpClientMap.entrySet()) { + logger.info("######## flushMsgToDeviceBroadcast key: {}, value: {} ########", entry.getKey(), entry.getValue()); + ByteBuf byteBuf = Unpooled.copiedBuffer(xml, CharsetUtil.UTF_8); + int length = byteBuf.readableBytes(); + ByteBuf allBuf = Unpooled.buffer(length + ConfigType.dataLength); + allBuf.writeByte(0xEB); + allBuf.writeByte(0x90); + allBuf.writeLongLE(sendIndex); + allBuf.writeLongLE(receiveIndex); + allBuf.writeByte(request ? 0x00 : 0x01); + allBuf.writeIntLE(length); + allBuf.writeBytes(byteBuf); + allBuf.writeByte(0xEB); + allBuf.writeByte(0x90); + redisTemplate.opsForValue().set(String.valueOf(sendIndex), allBuf.toString(CharsetUtil.US_ASCII), 60L, TimeUnit.SECONDS); + logger.info(Color.MAGENTA + "######## => 会话:{}, 客户:{}, 向设备回送序列:{}, 接收端序列:{}, 消息########\n{}" + Color.END, uuid, tcpClientMap.get(clientKey), sendIndex, receiveIndex, xml); + nettyServerHandler.sendMsg(uuid, entry.getKey(), tcpClientMap.get(entry.getKey()), allBuf); + sendIndex++; + try { + Thread.sleep(1); + } catch (InterruptedException e) {} } } @@ -195,6 +221,7 @@ public class NettyServer { Document document = saxReader.read(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))); Element root = document.getRootElement(); String sendCode = root.element("SendCode").getText(); + String receiveCode = root.element("ReceiveCode").getText(); tcpClientMap.put(sendCode, binaryModel.id); int type = 0; if (null != root.element("Type") && !StringUtil.isNullOrEmpty(root.element("Type").getText())) { @@ -205,7 +232,7 @@ public class NettyServer { command = Integer.parseInt(root.element("Command").getText()); } -// logger.info(Color.MAGENTA + "######## <= 会话: {}, 客户: {}, 消息类型: {}, 命令:{}, 消息体: ########\n{}" + Color.END, binaryModel.uuid, binaryModel.id, type, command, xml); + logger.info(Color.MAGENTA + "######## <= 会话: {}, 客户: [{}/{}], 消息类型: {}, 命令:{}, 消息体: ########\n{}" + Color.END, binaryModel.uuid, sendCode, binaryModel.id, type, command, xml); //判断是否重发 if (type == SystemType.system) { if (command == SystemType.has_response || command == SystemType.no_response) { @@ -279,7 +306,7 @@ public class NettyServer { break; case PushType.patrolDeviceRunning:// insert into basedata_mont_patdevrundata // json = downXml2Json.PatrolDeviceRunningControlXml2Json(xml); - json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, PatrolDeviceRuningControl.class); + json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, PatrolDeviceRunningControl.class); logger.info("######## 会话: {}, 客户: {}, 客户端设备运行数据 ########", binaryModel.uuid, binaryModel.id); break; case PushType.nestState:// insert into basedata_mont_neststadata @@ -289,7 +316,7 @@ public class NettyServer { break; case PushType.nestRunning:// insert into basedata_mont_nestrundata // json = downXml2Json.NestRunningControlXml2Json(xml); - json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, NestRuningControl.class); + json = downXml2Json.DownStreamJson2Xml(binaryModel.uuid, binaryModel.id, xml, NestRunningControl.class); logger.info("######## 会话: {}, 客户: {}, 客户端机巢运行数据 ########", binaryModel.uuid, binaryModel.id); break; case PushType.location:// insert into basedata_mont_patdevcoord @@ -324,20 +351,33 @@ public class NettyServer { break; default: // server-handle-接收到的type:{},不在处理范围内,不予处理 - logger.info("######## 会话: {}, 客户: {}, 非法的消息不予处理, 类型{}, 消息体 {} ########", binaryModel.uuid, binaryModel.id, type, xml); + logger.info("######## 会话: {}, 客户: {}, 非法的消息不予处理, 类型{}, 消息体 ########\n{}", binaryModel.uuid, binaryModel.id, type, xml); } if (type != SystemType.system && !StringUtil.isNullOrEmpty(json)) { - //rabbitmq推送到消息队列中基于springboot_xggd - JSONObject jsonObject = JSONObject.parseObject(json); - jsonObject.put("uuid", binaryModel.uuid); - json = jsonObject.toJSONString(); - // send to BasedataMontDataMqAcceptHandle - rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, json); - String receiveCode = root.element("ReceiveCode").getText(); - boolean isHost = json.contains(aliasHost); - logger.info("######## 会话: {}, 客户: {}, isHost: {}", binaryModel.uuid, binaryModel.id, isHost); - sendResponseToDevice(binaryModel.uuid, receiveCode, sendCode, isHost); + if ((type == NestCtlType.courseReversal && command == 3)) { // 处理用SSCOM模拟的数据, 向无人机发送控制指令 + logger.info("######## 会话: {}, 客户: {}, 向设备透传200001控制指令", binaryModel.uuid, binaryModel.id); + flushMsgToDeviceBroadcast(binaryModel.uuid, receiveCode, false, xml); + } else { + //rabbitmq推送到消息队列中基于springboot_xggd + JSONObject jsonObject = JSONObject.parseObject(json); + jsonObject.put("uuid", binaryModel.uuid); + json = jsonObject.toJSONString(); + // send to BasedataMontDataMqAcceptHandle + rabbitTemplate.convertAndSend(Constant.EX_CHANGE_NAME, Constant.ROUTING_KEY_NAME, json); + boolean isHost = json.contains(aliasHost); + logger.info("######## 会话: {}, 客户: {}, isHost: {}", binaryModel.uuid, binaryModel.id, isHost); + sendResponseToDevice(binaryModel.uuid, receiveCode, sendCode, isHost); + } + } else { + if ((type == NestCtlType.suddenStop && command == 7) + || (type == NestCtlType.ptzPitch && command == 6) + || (type == NestCtlType.picModelSet && command == 1) + || (type == NestCtlType.nestSuddenStop && command == 2)) {// 处理用SSCOM模拟的数据, 向无人机发送控制指令 + logger.info("######## 会话: {}, 客户: {}, 向设备透传200002~20005控制指令", binaryModel.uuid, binaryModel.id); + flushMsgToDeviceBroadcast(binaryModel.uuid, receiveCode, false, xml); + } } + } public void sendResponseToDevice(String uuid, String sendCode, String receiveCode, boolean isHost) { diff --git a/src/main/java/com/inspect/tcpserver/tcp/SystemType.java b/src/main/java/com/inspect/tcpserver/tcp/SystemType.java index 62a16c2..8b7e347 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/SystemType.java +++ b/src/main/java/com/inspect/tcpserver/tcp/SystemType.java @@ -25,6 +25,14 @@ class UAVType { public static final int nest = 20005; } +class NestCtlType { + public static final int courseReversal = 20001; // + public static final int suddenStop = 20002; // + public static final int ptzPitch = 20003; // + public static final int picModelSet = 20004; // + public static final int nestSuddenStop = 20005; // +} + class TaskType { public static final int taskControl = 41; public static final int taskSend = 101; diff --git a/src/main/java/com/inspect/tcpserver/tcp/UpJson2Xml.java b/src/main/java/com/inspect/tcpserver/tcp/UpJson2Xml.java index 878268a..05518f7 100644 --- a/src/main/java/com/inspect/tcpserver/tcp/UpJson2Xml.java +++ b/src/main/java/com/inspect/tcpserver/tcp/UpJson2Xml.java @@ -4,7 +4,6 @@ import com.alibaba.fastjson.JSON; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.naming.NoNameCoder; import com.thoughtworks.xstream.io.xml.Xpp3Driver; -import org.apache.commons.lang3.exception.ExceptionUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -148,10 +147,10 @@ public class UpJson2Xml { //巡视设备运行数据 public String PatrolDeviceRunningControlJson2Xml(String json) { - PatrolDeviceRuningControl obj = JSON.parseObject(json, PatrolDeviceRuningControl.class); + PatrolDeviceRunningControl obj = JSON.parseObject(json, PatrolDeviceRunningControl.class); XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); xStream.autodetectAnnotations(true); - xStream.alias(alias, PatrolDeviceRuningControl.class); + xStream.alias(alias, PatrolDeviceRunningControl.class); String resultXML = xStream.toXML(obj); return resultXML; } @@ -168,10 +167,10 @@ public class UpJson2Xml { //无人机机巢运行数据 public String NestRunningJson2Xml(String json) { - NestRuningControl obj = JSON.parseObject(json, NestRuningControl.class); + NestRunningControl obj = JSON.parseObject(json, NestRunningControl.class); XStream xStream = new XStream(new Xpp3Driver(new NoNameCoder())); xStream.autodetectAnnotations(true); - xStream.alias(alias, NestRuningControl.class); + xStream.alias(alias, NestRunningControl.class); String resultXML = xStream.toXML(obj); return resultXML; } diff --git a/src/main/resources/logback.xml b/src/main/resources/logback.xml index b2745a0..29435eb 100644 --- a/src/main/resources/logback.xml +++ b/src/main/resources/logback.xml @@ -62,7 +62,7 @@ - +