/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2023/8/28
 */
package org.ts.ddcs.dataStream.sink;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.rocksdb.RocksDBException;
import org.ts.ddcs.common.constant.KafkaConstants;
import org.ts.ddcs.common.metadata.TaishanV1JsonPackMetadataEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamCache;
import org.ts.ddcs.dataStream.DataStreamLineManager;
import org.ts.ddcs.kafka.KafkaClient;
import org.ts.ddcs.main.MainProcessorHolder;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * Date:2023/8/28
 * Title: 报文转发，不重新封装数据
 * Description：
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamKafkaSink extends DataStreamSink {
    private DataStreamCache dataStreamCache;

    @Override
    public void init(DataStreamLineManager context) {
        this.setContext(context);
        this.dataStreamCache = context.getCache();
    }

    @Override
    public void sink(DataStreamAdapter dataStreamAdapter, List<Map<String, Object>> elementDatas) {
        log.info("sink {} 接收时间 {} ", dataStreamAdapter.getRtuCode(), this.dataStreamCache.getStringValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName()));
        JSONObject body = new JSONObject();
        body.put(TaishanV1JsonPackMetadataEnum.PACK_METADATA_DATAGRAM.getName(), BytesHelp.byte2HexStr(this.dataStreamCache.getDatagramBuff()));
        body.put(TaishanV1JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
        body.put(TaishanV1JsonPackMetadataEnum.PACK_METADATA_AGREEMENT_TYPE.getName(), dataStreamAdapter.getAgreement());
        SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        Date dt = Date.from(this.dataStreamCache.getPickPacketTime().atZone(ZoneId.systemDefault()).toInstant());
        body.put(TaishanV1JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName(), timeFormat.format(dt));
        body.put(TaishanV1JsonPackMetadataEnum.PACK_METADATA_DATAGRAM_TYPE.getName(), this.dataStreamCache.getDatagramCode());

        log.info("data item :{}", body.toJSONString());
        String key = "topic." + KafkaConstants.TOPIC_TS_V1_DATAGRAM;
        try {
            byte[] topicBuff = MainProcessorHolder.newInstance().dataProcess.getStoreCache(key);
            String topic = new String(topicBuff, "utf-8");
            log.info("topic {}", topic);
            KafkaClient.messageSubmit(KafkaConstants.TOPIC_TS_V1_DATAGRAM, body.toJSONString());

            body.put("collectTime", this.dataStreamCache.getValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName()));
            KafkaClient.messageSubmit(KafkaConstants.TOPIC_TS_HBASE, body.toJSONString());

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (RocksDBException e) {
            e.printStackTrace();
        }
    }
}
