|
|
@@ -25,35 +25,42 @@ public class JT809DecoderAdapter extends ByteToMessageDecoder {
|
|
|
private static Logger log = LoggerFactory.getLogger(JT809DecoderAdapter.class);
|
|
|
@Override
|
|
|
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
|
|
|
- //判断是否有可读的字节
|
|
|
- if (in.readableBytes() <= 0) {
|
|
|
- return;
|
|
|
- }
|
|
|
- // 1、进行转义
|
|
|
- byte[] bytes = PacketDecoderUtils.decoderEscape(in);
|
|
|
- // 2、校验crc
|
|
|
- if (!CrcUtil.checkCRC(bytes)){
|
|
|
- return;
|
|
|
- }
|
|
|
- // 3、判断是那种类型的数据,交给具体的解码器类完成。
|
|
|
- ByteBuf byteBuf = CommonUtils.getByteBuf(bytes);
|
|
|
- byteBuf.skipBytes(9);
|
|
|
- // 获取业务标志
|
|
|
- short msgId = byteBuf.readShort();
|
|
|
-
|
|
|
- // 交给具体的解码器
|
|
|
- JT809BasePacket packet = null;
|
|
|
try {
|
|
|
- packet = DecoderFactory.getDecoder(msgId).decoder(bytes);
|
|
|
- } catch (Exception e) {
|
|
|
- if (e instanceof NullPointerException) {
|
|
|
- // log.info("没有可用的解析器,忽略这条信息!此信息不在业务范围内。");
|
|
|
- // 没有可用的解析器,忽略这条信息!此信息不在业务范围内。
|
|
|
- } else {
|
|
|
- log.error("报文解析出错!错误信息:{};报文信息:{};",e.getMessage(),PACKET_CACHE.get(Thread.currentThread().getName()));
|
|
|
+ //判断是否有可读的字节
|
|
|
+ if (in.readableBytes() <= 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 1、进行转义
|
|
|
+ byte[] bytes = PacketDecoderUtils.decoderEscape(in);
|
|
|
+ // 2、校验crc
|
|
|
+ if (!CrcUtil.checkCRC(bytes)){
|
|
|
+ return;
|
|
|
}
|
|
|
- return;
|
|
|
+ // 3、判断是那种类型的数据,交给具体的解码器类完成。
|
|
|
+ ByteBuf byteBuf = CommonUtils.getByteBuf(bytes);
|
|
|
+ byteBuf.skipBytes(9);
|
|
|
+ // 获取业务标志
|
|
|
+ short msgId = byteBuf.readShort();
|
|
|
+
|
|
|
+ // 交给具体的解码器
|
|
|
+ JT809BasePacket packet = null;
|
|
|
+ try {
|
|
|
+ packet = DecoderFactory.getDecoder(msgId).decoder(bytes);
|
|
|
+ } catch (Exception e) {
|
|
|
+ if (e instanceof NullPointerException) {
|
|
|
+ // log.info("没有可用的解析器,忽略这条信息!此信息不在业务范围内。");
|
|
|
+ // 没有可用的解析器,忽略这条信息!此信息不在业务范围内。
|
|
|
+ } else {
|
|
|
+ log.error("报文解析出错!错误信息:{};报文信息:{};",e.getMessage(),PACKET_CACHE.get(Thread.currentThread().getName()));
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ out.add(packet);
|
|
|
+ }catch (Exception e){
|
|
|
+
|
|
|
+ }finally {
|
|
|
+ ctx.flush();
|
|
|
}
|
|
|
- out.add(packet);
|
|
|
+
|
|
|
}
|
|
|
}
|