| | |
| | | package com.genersoft.iot.vmp.netty.handle; |
| | | |
| | | import com.genersoft.iot.vmp.netty.business.entity.TalkBackEquipment; |
| | | import com.genersoft.iot.vmp.netty.business.service.TalkBackEquipmentService; |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.channel.SimpleChannelInboundHandler; |
| | |
| | | import io.netty.util.CharsetUtil; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * updHandler udp 服务端数据接收处理 |
| | |
| | | public class UdpServerHandler extends SimpleChannelInboundHandler<DatagramPacket> { |
| | | private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| | | |
| | | private static UdpServerHandler udpServerHandler; |
| | | |
| | | @Autowired |
| | | private TalkBackEquipmentService talkBackEquipmentService; |
| | | |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | udpServerHandler = this; |
| | | udpServerHandler.talkBackEquipmentService = this.talkBackEquipmentService; |
| | | } |
| | | |
| | | @Override |
| | | protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception { |
| | | // 读取收到的数据 |
| | |
| | | buf.readBytes(req); |
| | | String body = new String(req, CharsetUtil.UTF_8); |
| | | System.out.println("【UDP】>>>>>> 收到客户端的数据:"+body); |
| | | //获取字符串的长度 |
| | | int length = body.length(); |
| | | System.out.println("数据长度>>>>>> = " + length); |
| | | //保存数据 |
| | | saveTalkBackEquipmentInfo(body); |
| | | |
| | | } |
| | | |
| | | //捕获异常 |
| | | /** |
| | | * 保存设备数据 |
| | | * @param body |
| | | */ |
| | | @Async |
| | | public void saveTalkBackEquipmentInfo(String body) { |
| | | TalkBackEquipment equipmentRecord = new TalkBackEquipment(); |
| | | equipmentRecord.setName("ces"); |
| | | equipmentRecord.setCode("cs123456"); |
| | | equipmentRecord.setStatus(1); |
| | | equipmentRecord.setUnitName("CSDW"); |
| | | equipmentRecord.setUnitCode("CSDW123456"); |
| | | equipmentRecord.setPoliceName("JINGYUAN"); |
| | | equipmentRecord.setPoliceCode("JINGYUAN123456"); |
| | | equipmentRecord.setTerminalNumber("788888888888"); |
| | | equipmentRecord.setType(1); |
| | | equipmentRecord.setLongitude("128.124124412"); |
| | | equipmentRecord.setLatitude("27.1224521421"); |
| | | equipmentRecord.setSpeed("10"); |
| | | equipmentRecord.setDirection("10"); |
| | | equipmentRecord.setElevation("10"); |
| | | equipmentRecord.setPrecisions("10"); |
| | | equipmentRecord.setReceiveTime(new Date()); |
| | | equipmentRecord.setCreateTime(new Date()); |
| | | equipmentRecord.setUpdateTime(new Date()); |
| | | //保存数据 |
| | | udpServerHandler.talkBackEquipmentService.save(equipmentRecord); |
| | | } |
| | | |
| | | /** |
| | | * 捕获异常 |
| | | * @param ctx |
| | | * @param cause |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)throws Exception { |
| | | logger.error("UdpServerHandler exceptionCaught"+cause.getMessage()); |
| | |
| | | ctx.close(); |
| | | } |
| | | |
| | | //消息没有结束的时候触发 |
| | | /** |
| | | * 消息没有结束的时候触发 |
| | | * @param ctx |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { |
| | | ctx.flush(); |