/**
 * Copyright 2019 DH
 * All right reserved.
 * 项目名称: 大恒泰山系统
 * 创建日期:2021/12/16
 */
package org.ts.ddcs.dataStream.processor.szy;


import lombok.extern.slf4j.Slf4j;
import org.ts.ddcs.common.metadata.DatagramMetadataEnum;
import org.ts.ddcs.common.util.ToolUtil;
import org.ts.ddcs.enums.SzyDatagramTypeCodeEnum;
import org.ts.ddcs.enums.SzyElementCodeEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.dataStream.*;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;

import java.io.UnsupportedEncodingException;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/***
 * Date:2021/12/16
 * Title:水资源规约数据处理器模块
 * Description：定时报 0xC0
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSzyFixedTimeDatagramProcessor extends DataStreamProcessor {

    private DataStreamCache dataStreamCache;

    private Map<String, List<Map<String, Object>>> keyStore = new HashMap<>();

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

    @Override
    public void process(DataStreamAdapter dataStreamAdapter) throws UnsupportedEncodingException {
        LocalDateTime now = LocalDateTime.now().withSecond(0).withNano(0);
        this.keyStore.clear();
        this.dataStreamCache.setState(TestState.MATHCE);
        this.dataStreamCache.setMatchProcessor(this);
        String datagramCode = this.dataStreamCache.getDatagramCode();
        if (SzyDatagramTypeCodeEnum.DATAGRAM_CODE_C0.getName().equals(datagramCode)) {
            String agreement = dataStreamAdapter.getAgreement();
            Map<String, DataItemTemplate> itemTemplateMap = this.context.getItemTemplate(agreement);
            byte[] dataArea = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_DATA_AREA.getName());
            int dataAreaLen = dataArea.length;
            int count = 0;
            int controlCode = this.dataStreamCache.getIntValue(DataStreamCacheMetadata.METADATA_SZY_CA_CODE.getName());
            if (controlCode == ToolUtil.hexStrToInt(SzyElementCodeEnum.CODE_WATER_QUALITY.getCode())) {
                //水质
                DataItemTemplate itemTemplate = itemTemplateMap.get(SzyElementCodeEnum.CODE_WATER_QUALITY.getCode());
                if (null != itemTemplate) {
                    int readSize = itemTemplate.stream(dataArea, count, dataAreaLen - 9);
                    if (itemTemplate.isMacth()) {
                        itemTemplate.saveValue(this.keyStore);
                    }
                    count += readSize;
                } else {
                    count += dataAreaLen - 9;
                }
                //报警状态
                DataItemTemplate itemTemplateWarn = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode());
                int readSize = itemTemplateWarn.stream(dataArea, count);
                if (itemTemplateWarn.isMacth()) {
                    itemTemplateWarn.saveValue(this.keyStore);
                }
                count += readSize;
                //测站状态
                DataItemTemplate itemTemplateStatus = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode());
                readSize = itemTemplateStatus.stream(dataArea, count);
                if (itemTemplateStatus.isMacth()) {
                    itemTemplateStatus.saveValue(this.keyStore);
                }
                count += readSize;
                //发报时间
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                String st = now.format(timeFormat);
                //采集时间
                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName(), st);
                if (dataArea.length - count == 7) {
                    //SL 427 规约
                    //采集时间为秒分时日月年
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT427.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String collectTime = "20" + itemTemplateTt.getStringValue();
                        this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), collectTime);
                    }
                } else if (dataArea.length - count == 5) {
                    //采集时间为秒分时日
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String ct = itemTemplateTt.getStringValue();
                        int day =Integer.valueOf(ct.substring(0, 2));
                        LocalDate localDate= LocalDate.now();
                        int month = localDate.getMonthValue();
                        int maxDay= localDate.lengthOfMonth();
                        if (day>maxDay){
                            LocalDate preDate= LocalDate.now().minusMonths(1);
                            int preMaxDay= preDate.lengthOfMonth();
                            if (day<=preMaxDay){
                                month = preDate.getMonthValue();
                            }
                        }
                        LocalDateTime tm = now.withMonth(month).withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);

                      //  LocalDateTime tm = now.withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);
                        if (tm.isBefore(now)) {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        } else if (tm.isAfter(now)) {
                            LocalDateTime pretm = tm.minusMonths(1);
                            Duration durationFirst = Duration.between(pretm, now);
                            Duration durationSecend = Duration.between(now, tm);
                            long tm1 = durationFirst.toMillis();
                            long tm2 = durationSecend.toMillis();
                            if (tm1 > tm2) {
                                long hours = durationSecend.toHours();
                                if (hours > 24) {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                                } else {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                                }
                            } else if (tm1 < tm2) {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                            } else {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                            }
                        } else {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        }
                    }
                }
                count += readSize;
            } else if (controlCode == ToolUtil.hexStrToInt(SzyElementCodeEnum.CODE_FLOW_SPEED.getCode())) {
                //流速
                boolean sl427 = false;
                int dataLen = dataAreaLen - 5 - 4;
                if (dataLen % 3 > 0) {
                    //SL 427 规约
                    dataLen = dataAreaLen - 7 - 4;
                    sl427 = true;
                }
                DataItemTemplate itemTemplate = itemTemplateMap.get(SzyElementCodeEnum.CODE_FLOW_SPEED.getCode());
                if (null != itemTemplate) {
                    int readSize = itemTemplate.stream(dataArea, count, dataLen);
                    if (itemTemplate.isMacth()) {
                        itemTemplate.saveValue(this.keyStore);
                    }
                    count += readSize;
                } else {
                    count += dataLen;
                }
                //报警状态
                DataItemTemplate itemTemplateWarn = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode());
                int readSize = itemTemplateWarn.stream(dataArea, count);
                if (itemTemplateWarn.isMacth()) {
                    itemTemplateWarn.saveValue(this.keyStore);
                }
                count += readSize;
                //测站状态
                DataItemTemplate itemTemplateStatus = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode());
                readSize = itemTemplateStatus.stream(dataArea, count);
                if (itemTemplateStatus.isMacth()) {
                    itemTemplateStatus.saveValue(this.keyStore);
                }
                count += readSize;
                //发报时间
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                String st = now.format(timeFormat);
                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName(), st);
                //采集时间
                if (sl427) {
                    //SL 427 规约
                    //采集时间为秒分时日月年
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT427.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String collectTime = "20" + itemTemplateTt.getStringValue();
                        this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), collectTime);
                    }
                } else {
                    //采集时间为秒分时日
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String ct = itemTemplateTt.getStringValue();
                        int day =Integer.valueOf(ct.substring(0, 2));
                        LocalDate localDate= LocalDate.now();
                        int month = localDate.getMonthValue();
                        int maxDay= localDate.lengthOfMonth();
                        if (day>maxDay){
                            LocalDate preDate= LocalDate.now().minusMonths(1);
                            int preMaxDay= preDate.lengthOfMonth();
                            if (day<=preMaxDay){
                                month = preDate.getMonthValue();
                            }
                        }
                        LocalDateTime tm = now.withMonth(month).withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);

                        // LocalDateTime tm = now.withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);
                        if (tm.isBefore(now)) {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        } else if (tm.isAfter(now)) {
                            LocalDateTime pretm = tm.minusMonths(1);
                            Duration durationFirst = Duration.between(pretm, now);
                            Duration durationSecend = Duration.between(now, tm);
                            long tm1 = durationFirst.toMillis();
                            long tm2 = durationSecend.toMillis();
                            if (tm1 > tm2) {
                                long hours = durationSecend.toHours();
                                if (hours > 24) {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                                } else {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                                }
                            } else if (tm1 < tm2) {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                            } else {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                            }
                        } else {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        }
                    }
                }
                count += readSize;
            } else if (controlCode == ToolUtil.hexStrToInt(SzyElementCodeEnum.CODE_WATER_LEVEL.getCode())) {
                //水位
                DataItemTemplate itemTemplate = itemTemplateMap.get(SzyElementCodeEnum.CODE_WATER_LEVEL.getCode());
                if (null != itemTemplate) {
                    int readSize = itemTemplate.stream(dataArea, count);
                    if (itemTemplate.isMacth()) {
                        itemTemplate.saveValue(this.keyStore);
                    }
                    count += readSize;
                } else {
                    count += dataAreaLen - 9;
                }
                //报警状态
                DataItemTemplate itemTemplateWarn = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode());
                int readSize = itemTemplateWarn.stream(dataArea, count);
                if (itemTemplateWarn.isMacth()) {
                    itemTemplateWarn.saveValue(this.keyStore);
                }
                count += readSize;
                //测站状态
                DataItemTemplate itemTemplateStatus = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode());
                readSize = itemTemplateStatus.stream(dataArea, count);
                if (itemTemplateStatus.isMacth()) {
                    itemTemplateStatus.saveValue(this.keyStore);
                }
                count += readSize;
                //发报时间
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                String st = now.format(timeFormat);
                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName(), st);
                //采集时间
                if (dataArea.length - count == 7) {
                    //SL 427 规约
                    //采集时间为秒分时日月年
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT427.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String collectTime = "20" + itemTemplateTt.getStringValue();
                        this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), collectTime);
                    }
                } else if (dataArea.length - count == 5) {
                    //采集时间为秒分时日
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String ct = itemTemplateTt.getStringValue();
                        int day =Integer.valueOf(ct.substring(0, 2));
                        LocalDate localDate= LocalDate.now();
                        int month = localDate.getMonthValue();
                        int maxDay= localDate.lengthOfMonth();
                        if (day>maxDay){
                            LocalDate preDate= LocalDate.now().minusMonths(1);
                            int preMaxDay= preDate.lengthOfMonth();
                            if (day<=preMaxDay){
                                month = preDate.getMonthValue();
                            }
                        }
                        LocalDateTime tm = now.withMonth(month).withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);

                        //  LocalDateTime tm = now.withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);
                        if (tm.isBefore(now)) {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        } else if (tm.isAfter(now)) {
                            LocalDateTime pretm = tm.minusMonths(1);
                            Duration durationFirst = Duration.between(pretm, now);
                            Duration durationSecend = Duration.between(now, tm);
                            long tm1 = durationFirst.toMillis();
                            long tm2 = durationSecend.toMillis();
                            if (tm1 > tm2) {
                                long hours = durationSecend.toHours();
                                if (hours > 24) {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                                } else {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                                }
                            } else if (tm1 < tm2) {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                            } else {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                            }
                        } else {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        }
                    }
                }
                count += readSize;
            } else if (controlCode == ToolUtil.hexStrToInt(SzyElementCodeEnum.CODE_FLOW_RATE.getCode())) {
                //流量
                boolean sl427 = false;
                int dataLen = dataAreaLen - 5 - 4;
                if (dataLen % 5 > 0) {
                    //  //SL 427 规约
                    dataLen = dataAreaLen - 7 - 4;
                    sl427 = true;
                }
                DataItemTemplate itemTemplate = itemTemplateMap.get(SzyElementCodeEnum.CODE_FLOW_RATE.getCode());
                if (null != itemTemplate) {
                    int readSize = itemTemplate.stream(dataArea, count, dataLen);
                    if (itemTemplate.isMacth()) {
                        itemTemplate.saveValue(this.keyStore);
                    }
                    count += readSize;
                } else {
                    count += dataLen;
                }
                //报警状态
                DataItemTemplate itemTemplateWarn = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_WARN.getCode());
                int readSize = itemTemplateWarn.stream(dataArea, count);
                if (itemTemplateWarn.isMacth()) {
                    itemTemplateWarn.saveValue(this.keyStore);
                }
                count += readSize;
                //测站状态
                DataItemTemplate itemTemplateStatus = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_RTU_STATUS.getCode());
                readSize = itemTemplateStatus.stream(dataArea, count);
                if (itemTemplateStatus.isMacth()) {
                    itemTemplateStatus.saveValue(this.keyStore);
                }
                count += readSize;
                //发报时间
                DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
                String st = now.format(timeFormat);
                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName(), st);
                //采集时间
                if (sl427) {
                    //SL 427 规约
                    //采集时间为秒分时日月年
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT427.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String collectTime = "20" + itemTemplateTt.getStringValue();
                        this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), collectTime);
                    }
                } else {
                    //采集时间为秒分时日
                    DataItemTemplate itemTemplateTt = itemTemplateMap.get(SzyElementCodeEnum.CODE_EXT_TT.getCode());
                    readSize = itemTemplateTt.stream(dataArea, count);
                    if (itemTemplateTt.isMacth()) {
                        String ct = itemTemplateTt.getStringValue();
                        int day =Integer.valueOf(ct.substring(0, 2));
                        LocalDate localDate= LocalDate.now();
                        int month = localDate.getMonthValue();
                        int maxDay= localDate.lengthOfMonth();
                        if (day>maxDay){
                            LocalDate preDate= LocalDate.now().minusMonths(1);
                            int preMaxDay= preDate.lengthOfMonth();
                            if (day<=preMaxDay){
                                month = preDate.getMonthValue();
                            }
                        }
                        LocalDateTime tm = now.withMonth(month).withDayOfMonth(Integer.valueOf(ct.substring(0, 2))).withHour(Integer.valueOf(ct.substring(2, 4))).withMinute(Integer.valueOf(ct.substring(4, 6))).withSecond(0).withNano(0);
                        if (tm.isBefore(now)) {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        } else if (tm.isAfter(now)) {
                            LocalDateTime pretm = tm.minusMonths(1);
                            Duration durationFirst = Duration.between(pretm, now);
                            Duration durationSecend = Duration.between(now, tm);
                            long tm1 = durationFirst.toMillis();
                            long tm2 = durationSecend.toMillis();
                            if (tm1 > tm2) {
                                long hours = durationSecend.toHours();
                                if (hours > 24) {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                                } else {
                                    this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                                }
                            } else if (tm1 < tm2) {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), pretm.format(timeFormat));
                            } else {
                                this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                            }
                        } else {
                            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_COLLECT_TIME.getName(), tm.format(timeFormat));
                        }
                    }
                }
                count += readSize;
            }
            if (count != dataAreaLen) {
                log.error("CO报文", "数据解析发生异常！");
            }
            this.next(false);
        } else {
            this.next(true);
        }
    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {
        log.info("接收到水资源 C0 定时报,回复报文");
        byte[] rtuCodeByte = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_RTU_CODE.getName());
        byte[] ca = new byte[1];
        byte[] byteBuff = this.dataStreamCache.getDatagramBuff();
        ca[0] = byteBuff[3];
        ca[0] = (byte) (ca[0] & 0x7F);
        String content = BytesHelp.byte2HexStr(ca) + BytesHelp.byte2HexStr(rtuCodeByte) + "C0";
        content += "00";
        byte[] contenbuff = BytesHelp.hexStr2Bytes(content);
        byte[] crc = new byte[1];
        crc[0] = BytesHelp.GetCRCByte(contenbuff);
        int datagramLen = 1 + 1 + 1 + contenbuff.length + 1 + 1;
        byte[] datagram = new byte[datagramLen];
        int count = 0;
        datagram[count] = 0x68;
        count += 1;
        datagram[count] = (byte) contenbuff.length;
        count += 1;
        datagram[count] = 0x68;
        count += 1;
        System.arraycopy(contenbuff, 0, datagram, count, contenbuff.length);
        count += contenbuff.length;
        datagram[count] = crc[0];
        count += 1;
        datagram[count] = 0x16;
        dataStreamAdapter.output(datagram);

    }

    @Override
    public List<Map<String, Object>> sink(DataStreamAdapter dataStreamAdapter) {
        List<Map<String, Object>> elementDataList = new ArrayList<>();
        Set<String> keys = this.keyStore.keySet();
        for (String key : keys) {
            Map<String, Object> data = new HashMap<>();
            // data.put(JsonPackMetadataEnum.PACK_METADATA_DATAGRAM.getName(), BytesHelp.byte2HexStr(this.dataStreamCache.getDatagramBuff()));
            //data.put(JsonPackMetadataEnum.PACK_METADATA_FROM_TIME.getName(), this.dataStreamCache.getValue(DatagramConstant.pickPacketTime));
            // data.put(JsonPackMetadataEnum.PACK_METADATA_RTU_CODE.getName(), this.dataStreamCache.getRtuCode());
            // data.put(JsonPackMetadataEnum.PACK_METADATA_COLLECT_TIME.getName(), this.dataStreamCache.getStringValue(DatagramConstant.collectTime));
            // data.put(JsonPackMetadataEnum.PACK_METADATA_UP_TIME.getName(), this.dataStreamCache.getStringValue(DatagramConstant.sendPacketTime));
            data.put(DatagramMetadataEnum.CACHE_METADATA_ELEMENT_CODE.getName(), key);
            List<Map<String, Object>> elements = this.keyStore.get(key);
            data.put(DatagramMetadataEnum.CACHE_METADATA_DATA.getName(), elements);
            elementDataList.add(data);
        }
        return elementDataList;
    }
}
