package  org.ts.ddcs.kafka;

import com.alibaba.fastjson.JSONObject;

import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.util.BytesUtil;


/**
 * @author dylan
 */
@Component
public class KafkaMessageListener {

    private long count=0;

//    @KafkaListener(topics = "${run-config.mq.toRtuDatagram}", groupId = "${spring.data.kafka.group-id}", containerFactory = "kafkaListenerContainerFactory")
//    public void process(ConsumerRecord<String, String> record) {
//        try {
//            LogHelper.info("kafka " + record.topic() + " " + record.partition() + " " + record.value());
//            String body = record.value();
//            if (null != body) {
//                LogHelper.info("queue toRtuDatagram  body:" + body);
//                JSONObject data = JSONObject.parseObject(body);
//                String rtuCode = data.getString(GBConstants.BASE_KEY_RTU);
//                if (null != rtuCode) {
//                    DataProcessHolder.newInstance().dataProcess.submitDownloadDatagramTask(rtuCode);
//                }
//            }
//        } catch (Exception e) {
//            LogHelper.error(e);
//        }
//    }

    @KafkaListener(topics = "kafka-data-stream-datagram-logging", groupId = "${spring.data.kafka.group-id}", containerFactory = "kafkaListenerContainerFactory")
    public void processStream(ConsumerRecord<String, String> record) {
        try {
            LogHelper.info("logging kafka " + record.topic() + " " + record.partition() + " " + record.value());

         //   JSONObject body = JSONObject.parseObject(record.value());

//           byte[] buff = body.getBytes("datagram");
//           LogHelper.info(" block " + BytesUtil.byte2HexStr(buff));

            count +=1;

            LogHelper.info("kafka count "+count);

        } catch (Exception e) {
            LogHelper.error(e);
        }
    }
}
