| | |
| | | package com.ard.utils.tcp; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.ard.alarm.radar.domain.ArdEquipRadar; |
| | | import com.ard.alarm.radar.domain.RadarAlarmData; |
| | | import com.ard.alarm.radar.domain.ArdAlarmRadar; |
| | | import com.ard.utils.ByteUtils; |
| | | import com.ard.utils.GisUtils; |
| | | import com.ard.utils.mqtt.MqttConsumer; |
| | | import io.netty.buffer.ByteBuf; |
| | | import io.netty.channel.ChannelHandlerContext; |
| | | import io.netty.channel.SimpleChannelInboundHandler; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import javax.xml.bind.DatatypeConverter; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | import java.util.concurrent.ScheduledFuture; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | |
| | | /** |
| | | * @Description: tcp客户端处理 |
| | | * @Description: tcp客户端处理-弃用 |
| | | * @ClassName: NettyTcpClientHandler |
| | | * @Author: 刘苏义 |
| | | * @Date: 2023年06月25日17:02 |
| | | * @Version: 1.0 |
| | | **/ |
| | | @Slf4j |
| | | @Slf4j(topic = "netty") |
| | | public class NettyTcpClientHandler extends SimpleChannelInboundHandler<ByteBuf> { |
| | | |
| | | private String host; |
| | | private Integer port; |
| | | private Double longitude; |
| | | private Double lagitude; |
| | | private Double altitude; |
| | | private String name; |
| | | private String id; |
| | | |
| | | public NettyTcpClientHandler(ArdEquipRadar ardEquipRadar) { |
| | | this.host = ardEquipRadar.getIp(); |
| | | this.port = ardEquipRadar.getPort(); |
| | | this.longitude = ardEquipRadar.getLongitude(); |
| | | this.lagitude = ardEquipRadar.getLatitude(); |
| | | this.name = ardEquipRadar.getName(); |
| | | this.id = ardEquipRadar.getId(); |
| | | this.altitude = ardEquipRadar.getAltitude(); |
| | | } |
| | | |
| | | private ChannelHandlerContext context; |
| | | private ScheduledFuture<?> heartbeatTask; |
| | | |
| | | @Override |
| | | protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg){ |
| | | protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) { |
| | | // 处理接收到的消息 |
| | | byte[] byteArray = new byte[msg.readableBytes()]; |
| | | msg.getBytes(msg.readerIndex(), byteArray); |
| | | String hexString = DatatypeConverter.printHexBinary(byteArray); |
| | | |
| | | log.info("Received: " + hexString); |
| | | byte[] bytes = MessageParsing.receiveCompletePacket(byteArray); |
| | | if (bytes != null) { |
| | | processData(bytes); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void channelActive(ChannelHandlerContext ctx) throws Exception { |
| | | // 当客户端连接成功后,发送消息给服务器 |
| | | ByteBuf message = ctx.alloc().buffer(); |
| | | message.writeBytes("Hello, Server!".getBytes()); |
| | | ctx.writeAndFlush(message); |
| | | public void channelActive(ChannelHandlerContext ctx) { |
| | | context = ctx; |
| | | startHeartbeatTask();//开始发送心跳 |
| | | } |
| | | |
| | | /** |
| | | * 开始心跳任务 |
| | | */ |
| | | private void startHeartbeatTask() { |
| | | heartbeatTask = context.executor().scheduleAtFixedRate(() -> { |
| | | // 发送心跳消息 |
| | | ByteBuf message = context.alloc().buffer(); |
| | | byte[] header = {0x01, 0x02, 0x01}; |
| | | byte[] payload = {0x10, 0x00, 0x00, 0x00}; |
| | | byte[] payloadCrc32 = ByteUtils.parseCrc32(payload); |
| | | byte[] footer = {0x01, 0x02, 0x00}; |
| | | byte[] heart = ByteUtils.appendArrays(header, payload, payloadCrc32, footer); |
| | | // byte[] heart = {0x01, 0x02, 0x01, 0x10, 0x00, 0x00, 0x00, (byte) 0x83, (byte) 0x88, 0x5d, 0x71, 0x01, 0x02, 0x00}; |
| | | String hexString = DatatypeConverter.printHexBinary(heart); |
| | | log.info("发送心跳:" + hexString); |
| | | message.writeBytes(heart); |
| | | context.writeAndFlush(message); |
| | | |
| | | }, 0, 5, TimeUnit.SECONDS); |
| | | } |
| | | |
| | | /** |
| | | * 停止心跳任务 |
| | | */ |
| | | private void stopHeartbeatTask() { |
| | | if (heartbeatTask != null) { |
| | | heartbeatTask.cancel(false); |
| | | heartbeatTask = null; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause){ |
| | | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| | | log.info("tcp客户端连接异常"); |
| | | // 发生异常时的处理 |
| | | cause.printStackTrace(); |
| | | ctx.close(); |
| | | stopHeartbeatTask();//停止心跳发送 |
| | | } |
| | | |
| | | /** |
| | | * 解析报警数据 |
| | | */ |
| | | public void processData(byte[] data) { |
| | | try { |
| | | //region crc校验-目前仅用于显示校验结果 |
| | | Boolean crc32Check = MessageParsing.CRC32Check(data); |
| | | if (!crc32Check) { |
| | | log.info("CRC32校验不通过"); |
| | | } else { |
| | | log.info("CRC32校验通过"); |
| | | } |
| | | //endregion |
| | | //log.info("原始数据:" + DatatypeConverter.printHexBinary(data)); |
| | | //log.info("雷达信息:" + host + "【port】" + port + "【X】" + longitude + "【Y】" + lagitude + "【Z】" + altitude); |
| | | data = MessageParsing.transferData(data);//去掉包头和包尾、校验及转义 |
| | | //region 负载头解析 |
| | | byte[] type = Arrays.copyOfRange(data, 0, 1);//命令类型 |
| | | // log.info("命令类型:" + DatatypeConverter.printHexBinary(type)); |
| | | byte[] cmdId = Arrays.copyOfRange(data, 1, 2);//命令ID |
| | | // log.info("命令ID:" + DatatypeConverter.printHexBinary(cmdId)); |
| | | byte[] payloadSize = Arrays.copyOfRange(data, 2, 4);//有效负载大小 |
| | | payloadSize = ByteUtils.toLittleEndian(payloadSize); |
| | | int payloadSizeToDecimal = ByteUtils.bytesToDecimal(payloadSize); |
| | | // log.info("有效负载大小(转整型):" + payloadSizeToDecimal); |
| | | //endregion |
| | | List<ArdAlarmRadar> radarAlarmInfos = new ArrayList<>(); |
| | | String alarmTime = ""; |
| | | Integer targetNum = 0; |
| | | if (Arrays.equals(cmdId, new byte[]{0x01})) { |
| | | //region 告警信息反馈 |
| | | byte[] dwTim = Arrays.copyOfRange(data, 4, 8); |
| | | dwTim = ByteUtils.toLittleEndian(dwTim); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | long l = ByteUtils.bytesToDecimal(dwTim); |
| | | alarmTime = sdf.format(l * 1000); |
| | | // log.info("周视图像的出现时间(转date):" + alarmTime); |
| | | |
| | | byte[] wTargetNum = Arrays.copyOfRange(data, 8, 10); |
| | | wTargetNum = ByteUtils.toLittleEndian(wTargetNum); |
| | | targetNum = ByteUtils.bytesToDecimal(wTargetNum); |
| | | if (targetNum == 0) { |
| | | return; |
| | | } |
| | | log.info("目标总点数(转整型):" + targetNum); |
| | | |
| | | //解析NET_TARGET_UNIT(64是NET_TARGET_HEAD的字节数) |
| | | int uintSize = (payloadSizeToDecimal - 64) / targetNum; |
| | | // log.info("单条报警大小:" + uintSize); |
| | | |
| | | for (int i = 0; i < targetNum; i++) { |
| | | |
| | | Integer index = 68 + uintSize * i; |
| | | byte[] dwID = Arrays.copyOfRange(data, index, index + 4); |
| | | // log.info("目标ID:" + DatatypeConverter.printHexBinary(cmdId)); |
| | | dwID = ByteUtils.toLittleEndian(dwID); |
| | | int id = ByteUtils.bytesToDecimal(dwID); |
| | | |
| | | // log.info("目标ID号:" + id); |
| | | |
| | | byte[] iDistance = Arrays.copyOfRange(data, index + 8, index + 12); |
| | | iDistance = ByteUtils.toLittleEndian(iDistance); |
| | | Double Distance = ByteUtils.bytesToDouble(iDistance); |
| | | // log.info("目标当前距离(m):" + Distance); |
| | | //region 不需要的字段 |
| | | // byte[] dwGSum = Arrays.copyOfRange(data, index + 4, index + 8); |
| | | // dwGSum = toLittleEndian(dwGSum); |
| | | // int GSum = byteArrayToDecimal(dwGSum); |
| | | // log.info("目标当前像素灰度和:" + GSum); |
| | | // byte[] iTw = Arrays.copyOfRange(data, index + 12, index + 16); |
| | | // iTw = toLittleEndian(iTw); |
| | | // int Tw = byteArrayToDecimal(iTw); |
| | | // log.info("目标当前的像素宽度:" + Tw); |
| | | // |
| | | // byte[] iTh = Arrays.copyOfRange(data, index + 16, index + 20); |
| | | // iTh = toLittleEndian(iTh); |
| | | // int Th = byteArrayToDecimal(iTh); |
| | | // log.info("目标当前的像素高度:" + Th); |
| | | // |
| | | // byte[] wPxlArea = Arrays.copyOfRange(data, index + 20, index + 22); |
| | | // wPxlArea = toLittleEndian(wPxlArea); |
| | | // int PxlArea = byteArrayToDecimal(wPxlArea); |
| | | // log.info("目标当前像素面积:" + PxlArea); |
| | | // |
| | | // byte[] cTrkNum = Arrays.copyOfRange(data, index + 22, index + 23); |
| | | // cTrkNum = toLittleEndian(cTrkNum); |
| | | // int TrkNum = byteArrayToDecimal(cTrkNum); |
| | | // log.info("轨迹点数:" + TrkNum); |
| | | |
| | | // byte[] sVx = Arrays.copyOfRange(data, index + 24, index + 26); |
| | | // sVx = toLittleEndian(sVx); |
| | | // int Vx = byteArrayToDecimal(sVx); |
| | | // log.info("目标当前速度矢量(像素距离)X:" + Vx); |
| | | // |
| | | // byte[] sVy = Arrays.copyOfRange(data, index + 26, index + 28); |
| | | // sVy = toLittleEndian(sVy); |
| | | // int Vy = byteArrayToDecimal(sVy); |
| | | // log.info("目标当前速度矢量(像素距离)Y:" + Vy); |
| | | // |
| | | // byte[] sAreaNo = Arrays.copyOfRange(data, index + 28, index + 30); |
| | | // sAreaNo = toLittleEndian(sAreaNo); |
| | | // int AreaNo = byteArrayToDecimal(sAreaNo); |
| | | // log.info("目标归属的告警区域号:" + AreaNo); |
| | | // |
| | | // byte[] cGrp = Arrays.copyOfRange(data, index + 30, index + 31); |
| | | // cGrp = toLittleEndian(cGrp); |
| | | // int Grp = byteArrayToDecimal(cGrp); |
| | | // log.info("所属组:" + Grp); |
| | | //endregion |
| | | String alarmType = ""; |
| | | byte[] cStat = Arrays.copyOfRange(data, index + 23, index + 24); |
| | | cStat = ByteUtils.toLittleEndian(cStat); |
| | | String binaryString = String.format("%8s", Integer.toBinaryString(cStat[0] & 0xFF)).replace(' ', '0'); |
| | | // log.info("目标当前状态:" + binaryString); |
| | | // 提取第4位至第6位的值 |
| | | int extractedValue = (cStat[0] >> 4) & 0b00001111; |
| | | // 判断提取的值 |
| | | if (extractedValue == 0b0000) { |
| | | alarmType = "运动目标检测"; |
| | | } else if (extractedValue == 0b0001) { |
| | | alarmType = "热源检测"; |
| | | } |
| | | // log.info("报警类型:" + alarmType); |
| | | |
| | | byte[] szName = Arrays.copyOfRange(data, index + 64, index + 96); |
| | | String alarmPointName = ByteUtils.bytesToStringZh(szName); |
| | | // log.info("所属告警区域名称:" + alarmPointName); |
| | | byte[] afTx = Arrays.copyOfRange(data, index + 96, index + 100); |
| | | afTx = ByteUtils.toLittleEndian(afTx); |
| | | float fTx = ByteUtils.bytesToFloat(afTx); |
| | | // log.info("水平角度:" + fTx); |
| | | byte[] afTy = Arrays.copyOfRange(data, index + 112, index + 116); |
| | | afTy = ByteUtils.toLittleEndian(afTy); |
| | | float fTy = ByteUtils.bytesToFloat(afTy); |
| | | // log.info("垂直角度:" + fTy); |
| | | Double[] radarXY = {longitude, lagitude}; |
| | | Double[] alarmXY = GisUtils.CalculateCoordinates(radarXY, Distance, (double) fTx); |
| | | // log.info("报警信息:" + "【id】" + id + "【name】" + alarmPointName + "【alarmType】" + alarmType + "【alarmTime】" + alarmTime + "【distance】" + Distance + "【P】" + fTx + "【T】" + fTy + "【X】" + alarmXY[0] + "【Y】" + alarmXY[1]); |
| | | ArdAlarmRadar ardAlarmRadar = new ArdAlarmRadar(); |
| | | ardAlarmRadar.setTargetId(id); |
| | | ardAlarmRadar.setName(alarmPointName); |
| | | ardAlarmRadar.setLongitude(alarmXY[0]); |
| | | ardAlarmRadar.setLatitude(alarmXY[1]); |
| | | ardAlarmRadar.setAlarmType(alarmType); |
| | | radarAlarmInfos.add(ardAlarmRadar); |
| | | } |
| | | //endregion |
| | | } |
| | | if (Arrays.equals(cmdId, new byte[]{0x04})) { |
| | | //region抽油机AI状态反馈 |
| | | String hexString = DatatypeConverter.printHexBinary(data); |
| | | //log.info(hexString); |
| | | |
| | | byte[] dwTim = Arrays.copyOfRange(data, 4, 8); |
| | | dwTim = ByteUtils.toLittleEndian(dwTim); |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | long l = ByteUtils.bytesToDecimal(dwTim); |
| | | alarmTime = sdf.format(l * 1000); |
| | | //log.info("周视图像的出现时间(转date):" + alarmTime); |
| | | |
| | | byte[] wTargetNum = Arrays.copyOfRange(data, 8, 10); |
| | | wTargetNum = ByteUtils.toLittleEndian(wTargetNum); |
| | | targetNum = ByteUtils.bytesToDecimal(wTargetNum); |
| | | log.info("目标总点数(转整型):" + targetNum); |
| | | if (targetNum == 0) { |
| | | return; |
| | | } |
| | | //解析NET_TARGET_UNIT(64是NET_TARGET_HEAD的字节数) |
| | | int uintSize = (payloadSizeToDecimal - 64) / targetNum; |
| | | //log.info("单条报警大小:" + uintSize); |
| | | for (int i = 0; i < targetNum; i++) { |
| | | Integer index = 68 + uintSize * i; |
| | | byte[] dwID = Arrays.copyOfRange(data, index, index + 4); |
| | | //log.info("目标ID:" + DatatypeConverter.printHexBinary(dwID)); |
| | | dwID = ByteUtils.toLittleEndian(dwID); |
| | | int id = ByteUtils.bytesToDecimal(dwID); |
| | | //log.info("目标ID号:" + id); |
| | | //region 不需要的字段 |
| | | byte[] iTw = Arrays.copyOfRange(data, index + 4, index + 8); |
| | | iTw = ByteUtils.toLittleEndian(iTw); |
| | | int Tw = ByteUtils.bytesToDecimal(iTw); |
| | | // log.info("目标当前的像素宽度:" + Tw); |
| | | |
| | | byte[] iTh = Arrays.copyOfRange(data, index + 8, index + 12); |
| | | iTh = ByteUtils.toLittleEndian(iTh); |
| | | int Th = ByteUtils.bytesToDecimal(iTh); |
| | | //log.info("目标当前的像素高度:" + Th); |
| | | |
| | | byte[] fTx = Arrays.copyOfRange(data, index + 12, index + 16); |
| | | fTx = ByteUtils.toLittleEndian(fTx); |
| | | float fTxAngle = ByteUtils.bytesToFloat(fTx); |
| | | //log.info("p角度:" + fTxAngle); |
| | | byte[] fTy = Arrays.copyOfRange(data, index + 16, index + 20); |
| | | fTy = ByteUtils.toLittleEndian(fTy); |
| | | float fTyAngle = ByteUtils.bytesToFloat(fTy); |
| | | //log.info("t角度:" + fTyAngle); |
| | | |
| | | byte[] sAreaNo = Arrays.copyOfRange(data, index + 20, index + 22); |
| | | sAreaNo = ByteUtils.toLittleEndian(sAreaNo); |
| | | int AreaNo = ByteUtils.bytesToDecimal(sAreaNo); |
| | | //log.info("目标归属的告警区域号:" + AreaNo); |
| | | |
| | | byte[] cGrp = Arrays.copyOfRange(data, index + 22, index + 23); |
| | | cGrp = ByteUtils.toLittleEndian(cGrp); |
| | | int Grp = ByteUtils.bytesToDecimal(cGrp); |
| | | //log.info("所属组:" + Grp); |
| | | //endregion |
| | | String alarmType; |
| | | byte[] cStat = Arrays.copyOfRange(data, index + 23, index + 24); |
| | | cStat = ByteUtils.toLittleEndian(cStat); |
| | | //String binaryString = String.format("%8s", Integer.toBinaryString(cStat[0] & 0xFF)).replace(' ', '0'); |
| | | //log.info("目标当前状态:" + binaryString); |
| | | // 提取第0位值 |
| | | // 使用位运算操作判断第0位是否为1 |
| | | boolean isB0 = (cStat[0] & 0x01) == 0x00; |
| | | // 判断提取的值 |
| | | if (isB0) { |
| | | alarmType = "雷达抽油机停机"; |
| | | } else { |
| | | continue; |
| | | } |
| | | //log.info("报警类型:" + alarmType); |
| | | |
| | | byte[] szName = Arrays.copyOfRange(data, index + 32, index + 64); |
| | | //log.info("所属告警区域名称:" + DatatypeConverter.printHexBinary(szName)); |
| | | String alarmPointName = ByteUtils.bytesToStringZh(szName); |
| | | // log.info("所属告警区域名称:" + alarmPointName); |
| | | //log.info("报警信息:" + "【id】" + id + "【name】" + alarmPointName + "【alarmType】" + alarmType + "【alarmTime】" + alarmTime); |
| | | ArdAlarmRadar ardAlarmRadar = new ArdAlarmRadar(); |
| | | ardAlarmRadar.setTargetId(id); |
| | | ardAlarmRadar.setName(alarmPointName); |
| | | ardAlarmRadar.setAlarmType(alarmType); |
| | | radarAlarmInfos.add(ardAlarmRadar); |
| | | } |
| | | //endregion |
| | | } |
| | | if (StringUtils.isEmpty(alarmTime)) { |
| | | return; |
| | | } |
| | | if (targetNum == 0) { |
| | | return; |
| | | } |
| | | RadarAlarmData radarAlarmData = new RadarAlarmData(); |
| | | radarAlarmData.setRadarId(id); |
| | | radarAlarmData.setRadarName(name); |
| | | radarAlarmData.setAlarmTime(alarmTime); |
| | | radarAlarmData.setArdAlarmRadars(radarAlarmInfos); |
| | | MqttConsumer.publish(2, false, "radar", JSON.toJSONString(radarAlarmData)); |
| | | } catch (Exception ex) { |
| | | log.error("雷达报文解析异常:" + ex.getMessage()); |
| | | } |
| | | } |
| | | |
| | | } |