From 9f8d7e3f8eb1e81e6eab411a415030085ff64da5 Mon Sep 17 00:00:00 2001
From: zhongrj <646384940@qq.com>
Date: Tue, 07 Mar 2023 14:09:09 +0800
Subject: [PATCH] 对讲设备读取数据修改
---
src/main/java/com/genersoft/iot/vmp/netty/handle/UdpServerHandler.java | 102 +++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 94 insertions(+), 8 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/netty/handle/UdpServerHandler.java b/src/main/java/com/genersoft/iot/vmp/netty/handle/UdpServerHandler.java
index 5f22349..6ecfac8 100644
--- a/src/main/java/com/genersoft/iot/vmp/netty/handle/UdpServerHandler.java
+++ b/src/main/java/com/genersoft/iot/vmp/netty/handle/UdpServerHandler.java
@@ -2,11 +2,13 @@
import com.genersoft.iot.vmp.netty.business.entity.TalkBackEquipment;
import com.genersoft.iot.vmp.netty.business.service.TalkBackEquipmentService;
+import com.genersoft.iot.vmp.netty.config.MyDecoder;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.util.CharsetUtil;
+import okio.Utf8;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -14,7 +16,15 @@
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
+import java.nio.charset.StandardCharsets;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
import java.util.Date;
+
+import static com.genersoft.iot.vmp.netty.config.MyDecoder.hexToByte;
+import static com.genersoft.iot.vmp.netty.config.MyDecoder.hexTohort;
+import static com.genersoft.iot.vmp.netty.util.Hex.getDouble;
+import static com.genersoft.iot.vmp.netty.util.Hex.hexStr2Str;
/**
* updHandler udp 服务端数据接收处理
@@ -43,14 +53,92 @@
ByteBuf buf = (ByteBuf) datagramPacket.copy().content();
byte[] req = new byte[buf.readableBytes()];
buf.readBytes(req);
- String body = new String(req, CharsetUtil.UTF_8);
- System.out.println("【UDP】>>>>>> 收到客户端的数据:"+body);
+ String s = bytesToHexString(req);
+ System.out.println("s111111 = " + s);
//获取字符串的长度
- int length = body.length();
+ int length = s.length();
System.out.println("数据长度>>>>>> = " + length);
- //保存数据
- saveTalkBackEquipmentInfo(body);
+ //数据解析
+ dataHandler(s);
+ }
+ /**
+ * 数据解析
+ * @param s 16位字符串
+ */
+ private void dataHandler(String s) {
+// s = "AAAACCCC220000000033373933343732393100000000000000000000000052D50451F77D5D406C04E275FD723C400000015F0000000507E70306103B0A";
+ //创建设备对象
+ TalkBackEquipment backEquipment = new TalkBackEquipment();
+ if (s.substring(4,8).equals("CCCC")) {
+ backEquipment.setStatus(1);
+ }
+ //断开连接
+ if (s.substring(4,8).equals("FFFF")) {
+ backEquipment.setStatus(0);
+ }
+ //截取字符串
+ backEquipment.setTerminalNumber(hexStr2Str(s.substring(20, 60)));
+
+ backEquipment.setLongitude(Double.toString(getDouble(s.substring(60, 76))));
+
+ backEquipment.setLatitude(Double.toString(getDouble(s.substring(76, 92))));
+
+ backEquipment.setSpeed(Short.toString(hexTohort(s.substring(92, 96))));
+ backEquipment.setDirection(Short.toString(hexTohort(s.substring(96, 100))));
+ backEquipment.setElevation(Short.toString(hexTohort(s.substring(100, 104))));
+ backEquipment.setPrecisions(Short.toString(hexTohort(s.substring(104, 108))));
+
+ String year = Short.toString(hexTohort(s.substring(108, 112)));
+
+ String month = Short.toString(hexToByte(s.substring(112, 114)));
+ if (Integer.parseInt(month)<10){
+ month = "0" + month;
+ }
+ String day = Short.toString(hexToByte(s.substring(114, 116)));
+ if (Integer.parseInt(day)<10){
+ day = "0" + day;
+ }
+ String hour = Short.toString(hexToByte(s.substring(116, 118)));
+ if (Integer.parseInt(hour)<10){
+ hour = "0" + hour;
+ }
+ String minute = Short.toString(hexToByte(s.substring(118, 120)));
+ if (Integer.parseInt(minute)<10){
+ minute = "0" + minute;
+ }
+ String second = Short.toString(hexToByte(s.substring(120, 122)));
+ if (Integer.parseInt(second)<10){
+ second = "0" + second;
+ }
+ String time = year + "-" + month + "-" + day +" " + hour+ ":"+minute + ":" + second;
+ System.out.println("接收时间: time = " + time);
+ //时间拼接
+ try {
+ backEquipment.setReceiveTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));
+ } catch (ParseException e) {
+ e.printStackTrace();
+ }
+ backEquipment.setUpdateTime(new Date());
+ // 新增
+ udpServerHandler.talkBackEquipmentService.save(backEquipment);
+ }
+
+ /**
+ * 转换
+ * @param bArray
+ * @return
+ */
+ public String bytesToHexString(byte[] bArray) {
+ StringBuffer sb = new StringBuffer(bArray.length);
+ String sTemp;
+ for (int i = 0; i < bArray.length; i++) {
+ sTemp = Integer.toHexString(0xFF & bArray[i]);
+ if (sTemp.length() < 2)
+ sb.append(0);
+ sb.append(sTemp.toUpperCase());
+ }
+ return sb.toString();
}
/**
@@ -68,7 +156,6 @@
equipmentRecord.setPoliceName("JINGYUAN");
equipmentRecord.setPoliceCode("JINGYUAN123456");
equipmentRecord.setTerminalNumber("788888888888");
- equipmentRecord.setType(1);
equipmentRecord.setLongitude("128.124124412");
equipmentRecord.setLatitude("27.1224521421");
equipmentRecord.setSpeed("10");
@@ -78,8 +165,7 @@
equipmentRecord.setReceiveTime(new Date());
equipmentRecord.setCreateTime(new Date());
equipmentRecord.setUpdateTime(new Date());
- //保存数据
- udpServerHandler.talkBackEquipmentService.save(equipmentRecord);
+
}
/**
--
Gitblit v1.9.3