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


import lombok.extern.slf4j.Slf4j;

import org.ts.ddcs.enums.SwDatagramTypeCodeEnum;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.dataStream.*;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;

import java.text.ParseException;

import java.util.*;

/***
 * Date:2021/12/16
 * Title:数据处理器模块
 * Description：链路报 0x2F
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamSwLinkHeartProcessor extends DataStreamProcessor {

    private DataStreamCache dataStreamCache;

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

    @Override
    public void process(DataStreamAdapter dataStreamAdapter) throws ParseException {
        this.dataStreamCache.setState(TestState.MATHCE);
        this.dataStreamCache.setMatchProcessor(this);
        String datagramCode = this.dataStreamCache.getDatagramCode();
        if (SwDatagramTypeCodeEnum.DATAGRAM_CODE_2F.getName().equals(datagramCode)) {
            //解析报文
            byte[] dataArea = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_DATA_AREA.getName());
            int count = 0;
            //流水号
            byte[] serialNo = BytesHelp.subBytes(dataArea, count, 2);
            dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_SERIAL_NO.getName(), serialNo);
            count += 2;
            // 发报时间
            byte[] sendPacketTimeBuff = BytesHelp.subBytes(dataArea, count, 6);
            String sendPacketTime = "20" + BytesHelp.byte2HexStr(sendPacketTimeBuff);
            dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_UP_TIME.getName(), sendPacketTime);
            count += 6;
            this.next(false);
        } else {
            this.next(true);
        }
    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {
        //  SimpleDateFormat timeFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        // String sendPacketTime = (String) this.dataStreamCache.getValue(DatagramConstant.sendPacketTime);
        // String pickPacketTime = (String) this.dataStreamCache.getValue(DatagramConstant.pickPacketTime);
        //  Date st = timeFormat.parse(sendPacketTime);
        // Date pt = timeFormat.parse(pickPacketTime);
        // SimpleDateFormat timeFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // log.info("接收到2F报文 发报时间：{} 接收时间：{}", timeFormat2.format(st), timeFormat2.format(pt));
    }

    @Override
    public List<Map<String, Object>> sink(DataStreamAdapter dataStreamAdapter) {
        List<Map<String, Object>> elementDataList = new ArrayList<>(0);
        return elementDataList;
    }


}
