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

import lombok.extern.slf4j.Slf4j;
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.dataStream.DataStreamProcessor;
import org.ts.ddcs.datatemplate.TestState;

import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

/***
 * Date:2023/9/4
 * Title:数据处理器模块
 * Description：DTU报文
 * @author dylan
 * @version 1.0
 * Remark：认为有必要的其他信息
 */
@Slf4j
public class DataStreamDtuLinkDatagramProcessor 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 UnsupportedEncodingException {

        this.dataStreamCache.setState(TestState.MATHCE);
        this.dataStreamCache.setMatchProcessor(this);
        int count = 0;
        byte[] addressHeader = new byte[1];
        addressHeader[0] = this.dataStreamCache.getByte(count);
        count += 1;
        String add = BytesHelp.byte2HexStr(addressHeader);
        if (add.equals("FA")) {
            byte[] command = new byte[1];
            command[0] = this.dataStreamCache.getByte(count);
            count += 1;
            int len = (int) this.dataStreamCache.getByte(count);
            count += 1;
            byte[] body = this.dataStreamCache.getBytes(count, len);
            byte[] content = this.dataStreamCache.getBytes(count, 20);
            byte[] uuid = content;
            count += 20;

            byte[] crc = this.dataStreamCache.getBytes(count, 2);
            count += 2;
            this.next(false);
        } else {
            this.next(true);
        }

    }

    @Override
    public void close(DataStreamAdapter dataStreamAdapter) {

    }

    @Override
    public void batchComplete(DataStreamAdapter dataStreamAdapter) {

    }

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