package  org.ts.ddcs.datatemplate.dtuDataTemplate;



import org.ts.ddcs.common.constant.GBConstants;
import org.ts.ddcs.dataStream.DataStreamAdapter;
import org.ts.ddcs.dataStream.DataStreamInterface;
import org.ts.ddcs.dataprocesser.DataProcesserInterface;
import org.ts.ddcs.datatemplate.DataTemplate;
import org.ts.ddcs.datatemplate.DataTemplateInterface;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.filter.DataFilter;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.util.BytesUtil;


/**
 * created by swp on 2017/8/23
 *
 * @author dylan
 */

public class DtuLinkDataTemplate extends DataTemplate implements DataTemplateInterface {
    /**
     * 地址
     */
    protected byte[] addressHeader;
    /**
     * 数据长度
     */
    protected int len = 0;
    /**
     * 功能码
     */
    protected byte[] command;
    /**
     * 数据包
     */
    protected byte[] packet;
    /**
     * 检验码
     */
    protected byte[] crc;
    /**
     * 唯一码
     */
    protected byte[] uuid;

    @Override
    public void stream(DataStreamInterface stream) {
        if (stream.dataSize() < 1) {
            return;
        }
        int count = 0;
        addressHeader = new byte[1];
        addressHeader[0] = stream.getByte(0);
        count += 1;
        String add = BytesUtil.byte2HexStr(addressHeader);
        if (!add.equals("00")) {
            return;
        }
        if (stream.dataSize() < 25) {
            return;
        }
        command = new byte[1];
        command[0] = stream.getByte(count);
        count += 1;
        len = (int) stream.getByte(count);
        count += 1;
        byte[] content = stream.getBytes(count, 20);
        this.uuid = content;
        count += 20;
        crc = stream.getBytes(count, 2);
        count += 2;
        this.state = TestState.MATHCE;
        this.length = 25;

        this.packet = stream.getBytes(this.length);

        LogHelper.info("============ " + BytesUtil.byte2HexStr(stream.getBytes(0, this.length)));
    }


    @Override
    public boolean checkFeature(byte[] check, int pos, int length) {
        return true;
    }

    @Override
    public void ready() {
        state = TestState.LOOK;
    }

    @Override
    public int length() {
        return this.length;
    }

    @Override
    public TestState state() {
        return state;
    }

    @Override
    public int featureWeight() {
        return this.featureWeight;
    }

    @Override
    public void setFeatureWeight(int weight) {
        this.featureWeight = weight;
    }

    @Override
    public String getName() {
        return this.templateName;
    }

    @Override
    public void setName(String name) {
        this.templateName = name;
    }

    @Override
    public void bridgeDataProcessor(DataProcesserInterface processer, DataStreamAdapter dataStreamAdapter) {
        processer.packetReadly(this.packet, 0, this.packet.length, dataStreamAdapter, this);
    }

    @Override
    public boolean bridgeDataFilter(DataFilter filter, DataStreamAdapter dataStreamAdapter) {
        return filter.filter(this.packet, 0, this.packet.length, dataStreamAdapter, this);
    }

    @Override
    public byte[] funcCode() {
        return this.command;
    }

    @Override
    public byte[] rtuCode() {
        return this.uuid;
    }

    @Override
    public byte[] datagramContext() {
        return this.packet;
    }

    @Override
    public String agreement() {
        return "";
    }

    @Override
    public boolean hasNext() {
        return false;
    }

    @Override
    public int packageTotal() {
        return 0;
    }

    @Override
    public int packageIndex() {
        return 0;
    }

    @Override
    public byte[] body() {
        return new byte[0];
    }
}
