|
@@ -0,0 +1,79 @@
|
|
|
+package com.gyee.wisdom;
|
|
|
+
|
|
|
+import com.gyee.wisdom.model.TagPoint;
|
|
|
+import com.gyee.wisdom.service.CacheService;
|
|
|
+import com.gyee.ygys.protocol.BitMapGroup;
|
|
|
+import com.gyee.ygys.protocol.BitMapMessage;
|
|
|
+import com.gyee.ygys.protocol.BitMapMessageParser;
|
|
|
+import com.gyee.ygys.utils.BytesUtil;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.kafka.annotation.KafkaListener;
|
|
|
+import org.springframework.kafka.annotation.PartitionOffset;
|
|
|
+import org.springframework.kafka.annotation.TopicPartition;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.BitSet;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author xiesj
|
|
|
+ * @version 1.0
|
|
|
+ * @date 1
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class KafkaConsumer {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CacheService cacheService;
|
|
|
+
|
|
|
+
|
|
|
+ private static final Logger LOG = LoggerFactory.getLogger(KafkaConsumer.class);
|
|
|
+
|
|
|
+ @KafkaListener(groupId = "group1",topicPartitions = {@TopicPartition(topic="NSSFJ",partitionOffsets = @PartitionOffset(partition = "0",initialOffset = "1"))})
|
|
|
+ public void receive(byte[] message){
|
|
|
+ LOG.info("KafkaMessageConsumer 接收到消息:"+message);
|
|
|
+ Map<TagPoint, Double> tagPointDoubleMap = parseByteArrayFromKafka(message);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Map<TagPoint,Double> parseByteArrayFromKafka(byte[] message){
|
|
|
+ Map<TagPoint,Double> valueMap = new HashMap<>();
|
|
|
+ BitMapMessage bitMapMessage = BitMapMessageParser.parserBitMapMessage(message);
|
|
|
+ BitSet bitMapL1 = bitMapMessage.getBitMapL1();
|
|
|
+ for(int i=0;i<bitMapL1.size();i++){
|
|
|
+ if(bitMapL1.get(i)){
|
|
|
+ int groupIndex = i;
|
|
|
+ List<BitMapGroup> filterGroups = bitMapMessage.getGroups().stream().filter(bitMapGroup -> bitMapGroup.getGroupIndex() == groupIndex).collect(Collectors.toList());
|
|
|
+ for (int j=0;j<filterGroups.size();j++){
|
|
|
+ BitMapGroup bitMapGroup = filterGroups.get(j);
|
|
|
+ BitSet bitMapL2 = bitMapGroup.getBitMapL2();
|
|
|
+ int dataOffset = 0;
|
|
|
+ byte[] data = bitMapGroup.getData();
|
|
|
+ for(int x=0;x<bitMapL2.size();x++){
|
|
|
+ if (bitMapL2.get(x)){
|
|
|
+ TagPoint tp = cacheService.getPoint(bitMapGroup.getGroupIndex(), x);//x代表点的索引
|
|
|
+ byte[] valueArray = new byte[8];
|
|
|
+ if (tp.getDataType().equals("BOOL")){
|
|
|
+ System.arraycopy(data,dataOffset,valueArray,0,1);
|
|
|
+ dataOffset+=1;
|
|
|
+ }else{
|
|
|
+ System.arraycopy(data,dataOffset,valueArray,0,8);
|
|
|
+ dataOffset+=8;
|
|
|
+ }
|
|
|
+ double value = BytesUtil.byte2Double(valueArray);
|
|
|
+ valueMap.put(tp,value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return valueMap;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|