/**
 * 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.util.BytesHelp;
import org.ts.ddcs.dataStream.*;
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 DataStreamSzyCrcCheckProcessor extends DataStreamProcessor {

    private DataStreamCache dataStreamCache;
    private boolean crcCehck = false;
    private byte[] packetCrc;
    private byte[] crc;

    @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.packetCrc = new byte[1];
        this.packetCrc[0] = BytesHelp.GetCRCByte(BytesHelp.subBytes(datagramBuff, 3, datagramBuff.length - 5));
        this.crc = this.dataStreamCache.getBytesValue(DataStreamCacheMetadata.METADATA_CRC.getName());
        if (this.packetCrc[0] == this.crc[0]) {
            this.crcCehck = true;
            byte[] funcCodeByte = (byte[]) dataStreamCache.getValue(DataStreamCacheMetadata.METADATA_SZY_AFN.getName());
            dataStreamCache.setDatagramCode(BytesHelp.byte2HexStr(funcCodeByte));
            this.dataStreamCache.putValue(DataStreamCacheMetadata.METADATA_PICK_TIME.getName(), now.format(timeFormat));
            byte[] rtuCodeByte = (byte[]) dataStreamCache.getValue(DataStreamCacheMetadata.METADATA_RTU_CODE.getName());
            dataStreamCache.setRtuCode(BytesHelp.byte2HexStr(rtuCodeByte));
            if (null == dataStreamAdapter.getRtuCode()) {
                dataStreamAdapter.setRtuCode(dataStreamCache.getRtuCode());
            }
            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.packetCrc), BytesHelp.byte2HexStr(this.crc));
        }
    }

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

        return null;
    }

}
