package org.springblade.modules.netty.handle; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springblade.modules.netty.business.entity.DeviceMobilePositionPoliceCamera; import org.springblade.modules.netty.business.service.DeviceChannelPoliceCameraService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; import static org.springblade.modules.netty.config.MyDecoder.hexToByte; import static org.springblade.modules.netty.config.MyDecoder.hexTohort; import static org.springblade.modules.netty.util.Hex.getDouble; import static org.springblade.modules.netty.util.Hex.hexStr2Str; /** * updHandler udp 服务端数据接收处理 * @author zhongrj * @date 2023-02-21 */ @Component public class UdpServerHandler extends SimpleChannelInboundHandler { private Logger logger = LoggerFactory.getLogger(this.getClass()); private static UdpServerHandler udpServerHandler; @Autowired private DeviceChannelPoliceCameraService deviceChannelPoliceCameraService; @PostConstruct public void init() { udpServerHandler = this; udpServerHandler.deviceChannelPoliceCameraService = this.deviceChannelPoliceCameraService; } @Override protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception { // 读取收到的数据 ByteBuf buf = (ByteBuf) datagramPacket.copy().content(); byte[] req = new byte[buf.readableBytes()]; buf.readBytes(req); String body = new String(req, CharsetUtil.UTF_8); // 字节转16进制字符串 String s = bytesToHexString(req); //获取字符串的长度 int length = s.length(); // System.out.println("数据长度>>>>>> = " + length); //数据解析 dataHandler(s); } /** * 数据解析 * @param s 16位字符串 */ private void dataHandler(String s) { System.out.println("s = " + s); s = "AAAACCCC220000000033373933343732393100000000000000000000000052D50451F77D5D406C04E275FD723C400000015F0000000507E70306103B0A"; System.out.println("s = " + s); //创建设备对象 DeviceMobilePositionPoliceCamera backEquipment = new DeviceMobilePositionPoliceCamera(); // if (s.substring(4,8).equals("CCCC")) { // backEquipment.setStatus(1); // } // //断开连接 // if (s.substring(4,8).equals("FFFF")) { // backEquipment.setStatus(0); // } //截取字符串,并去除后面多余的空格(如果有) backEquipment.setChannelId(hexStr2Str(s.substring(20, 60)).trim()); //设置经纬度 backEquipment.setLongitude(getDouble(s.substring(60, 76))); backEquipment.setLatitude(getDouble(s.substring(76, 92))); // 设置速度、方向等 backEquipment.setSpeed(hexTohort(s.substring(92, 96))); backEquipment.setDirection(hexTohort(s.substring(96, 100))); backEquipment.setAltitude(hexTohort(s.substring(100, 104))); backEquipment.setReportSource(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; backEquipment.setTime(time); // 该处设置成和通道一样 backEquipment.setDeviceId(backEquipment.getChannelId()); // 新增 udpServerHandler.deviceChannelPoliceCameraService.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(); } /** * 捕获异常 * @param ctx * @param cause * @throws Exception */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception { logger.error("UdpServerHandler exceptionCaught"+cause.getMessage()); System.out.println("UdpServerHandler exceptionCaught"+cause.getMessage()); cause.printStackTrace(); ctx.close(); } /** * 消息没有结束的时候触发 * @param ctx * @throws Exception */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } }