/**
 * 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.common.util.BytesHelp;
import org.ts.ddcs.common.util.Sw2014DatagramTemplateHelp;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamCache;
import org.ts.ddcs.dataStream.DataStreamLineManager;
import org.ts.ddcs.dataStream.DataStreamProcessor;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.metadata.DataStreamCacheMetadata;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

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

    private DataStreamCache dataStreamCache;
    private boolean crcCehck = false;
    private byte[] packetCrc16;
    private byte[] crc16;

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

    @Override
    public void process(DataStreamAdapter dataStreamAdapter) {
        LocalDateTime now = LocalDateTime.now().withNano(0);
        DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        this.dataStreamCache.setPickPacketTime(now);
        this.dataStreamCache.setMatchProcessor(this);
        byte[] datagramBuff = this.dataStreamCache.getDatagramBuff();
        //校验码，取报文最后两个字节
        this.packetCrc16 = new byte[2];
        this.packetCrc16[0] = datagramBuff[datagramBuff.length - 2];
        this.packetCrc16[1] = datagramBuff[datagramBuff.length - 1];
        //生成校验码,包体去掉报文最后两个字节
        this.crc16 = Sw2014DatagramTemplateHelp.crcFun2014(datagramBuff, 0, datagramBuff.length - 2);
        if (BytesHelp.byte2HexStr(this.packetCrc16).equals(BytesHelp.byte2HexStr(this.crc16))) {
            this.crcCehck = true;
            byte[] funcCodeByte = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_SW_AFN.getName());
            dataStreamCache.setDatagramCode(BytesHelp.byte2HexStr(funcCodeByte));
            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName(), now.format(timeFormat));
            byte[] rtuCodeByte = dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_RTU_CODE.getName());
            dataStreamCache.setRtuCode(BytesHelp.byte2HexStr(rtuCodeByte));
            if (null == dataStreamAdapter.getRtuCode()) {
                dataStreamAdapter.setRtuCode(BytesHelp.byte2HexStr(rtuCodeByte));
            }
            this.next(true);
        } else {
            this.dataStreamCache.setState(TestState.ERROR);
            this.dataStreamCache.setMatchProcessor(this);
            this.crcCehck = false;
            this.next(false);
        }
    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {
        if (!this.crcCehck) {
            byte[] datagram = this.dataStreamCache.getDatagramBuff();
            log.info("原始报文 {}", BytesHelp.byte2HexStr(datagram));
            log.info("报文CRC校验错误 {} {}", BytesHelp.byte2HexStr(this.packetCrc16), BytesHelp.byte2HexStr(this.crc16));
            dataStreamCache.setReadSize(this.dataStreamCache.getDatagramSize());
        }
    }

    @Override
    public List<Map<String, Object>> sink(DataStreamAdapter dataStreamAdapter) {
        return null;
    }

}
