GatherRespParser.java 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package com.hb.proj.gather.protocol;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import com.hb.proj.gather.business.DataAssembler;
  9. import com.hb.proj.gather.business.DataTransRepSingleTask;
  10. import com.hb.proj.gather.business.DataTransRepTask;
  11. import com.hb.proj.gather.business.GatherTaskExecutor;
  12. import com.hb.proj.gather.model.DiagramPO;
  13. import com.hb.proj.gather.model.SingleCombPO;
  14. import io.netty.buffer.ByteBuf;
  15. public class GatherRespParser {
  16. private final static Logger logger = LoggerFactory.getLogger(GatherRespParser.class);
  17. /**
  18. * 数据解析入口
  19. * @param byteBuf 接收的消息
  20. * @param startIndex 解析开始索引
  21. * @param dataLen 数据长度字节数
  22. * @param cmd
  23. * @param serial 设备编号
  24. */
  25. public static void parse(ByteBuf byteBuf ,int startIndex,int dataLen,String cmd,String serial) {
  26. ZLOpdProtCMDEnum cmdEum=ZLOpdProtCMDEnum.valueOf(cmd);
  27. if(cmdEum.getItemBytCount()==4) {
  28. Map<String,Float> dataMap=parseFloat(byteBuf,startIndex,dataLen,cmdEum.getParamCodes());
  29. SingleCombPO po=DataAssembler.putPieceData(serial, dataMap);
  30. if(po!=null) {
  31. GatherTaskExecutor.execute(new DataTransRepSingleTask(po));
  32. }
  33. }
  34. else if(cmdEum.getItemBytCount()==2) { //默认为功图数据解析
  35. List<Float> datas=parseShort2Float(byteBuf,startIndex,dataLen);
  36. DiagramPO po=DataAssembler.putPieceData(serial,(cmdEum.getParamCodes())[0], datas);
  37. if(po!=null) {
  38. GatherTaskExecutor.execute(new DataTransRepTask(po));
  39. }
  40. }
  41. }
  42. /**
  43. * 解析消息中的数据部分 每个数据项 4字节
  44. * @param byteBuf
  45. * @param startIndex 数据区开始索引
  46. * @param dataLen 数据区长度
  47. * @param paramCodes 数据项编码
  48. */
  49. public static Map<String,Float> parseFloat(ByteBuf byteBuf ,int startIndex,int dataLen,String[] paramCodes) {
  50. byteBuf.readerIndex(startIndex);
  51. Map<String,Float> rtnData=new HashMap<String,Float>(paramCodes.length);
  52. int i=0;
  53. while(true) {
  54. rtnData.put(paramCodes[i++],byteBuf.readFloat() ); //顺序读取,readIndex 自动后移
  55. if(i>=paramCodes.length || byteBuf.readerIndex()>=(startIndex+dataLen)) {
  56. break;
  57. }
  58. }
  59. logger.info("数据解析完:{}",rtnData);
  60. return rtnData;
  61. }
  62. /**
  63. * 解析消息中的数据部分 每个数据项 2字节
  64. * @param byteBuf
  65. * @param startIndex
  66. * @param dataLen
  67. * @return
  68. */
  69. public static List<Float> parseShort2Float(ByteBuf byteBuf ,int startIndex,int dataLen) {
  70. byteBuf.readerIndex(startIndex);
  71. List<Float> rtns=new ArrayList<Float>();
  72. while(true) {
  73. rtns.add(byteBuf.readShort()+0.0f); //顺序读取,readIndex 自动后移
  74. if(byteBuf.readerIndex()>=(startIndex+dataLen)) {
  75. break;
  76. }
  77. }
  78. logger.info("数据解析完:{}",rtns);
  79. return rtns;
  80. }
  81. /**
  82. * 专为功图点数检测解析
  83. * @param byteBuf
  84. * @param startIndex
  85. * @param dataLen
  86. * @return
  87. */
  88. public static short parseDiagramPoint(ByteBuf byteBuf ,int startIndex) {
  89. byteBuf.readerIndex(startIndex);
  90. return byteBuf.readShort();
  91. }
  92. }