package  org.ts.ddcs.datatemplate.swDataTemplate;

import org.ts.ddcs.dataStream.DataStreamInterface;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.datatemplate.tool.DataItemTemplateHelp;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.util.BusinessConstant;
import org.ts.ddcs.util.BytesUtil;
import org.ts.ddcs.util.Commands;


/**
 * 规约报文数据模板  ， 专门实现链路维持报文
 *
 * @author dylan
 */
public class DataTemplate2014P2F extends DataTemplate2014 {
    @Override
    public void stream(DataStreamInterface steam) {
        LogHelper.info("SwDataTemplate2014P2F stream");
        if (steam.dataSize() == 0) {
            return;
        }
        if (steam.getByte(0) != 0x7E) {
            this.state = TestState.UNKNOWN;
            return;
        }
        if (steam.dataSize() < 2) {
            return;
        }
        if (steam.getByte(0) != 0x7E || steam.getByte(1) != 0x7E) {
            this.state = TestState.UNKNOWN;
            return;
        }
        if (steam.dataSize() < this.featureWeight) {
            return;
        }
        byte[] feture = steam.getBytes(this.featureWeight);
        if (!this.checkFeature(feture, 0, feture.length)) {
            this.state = TestState.UNKNOWN;
            return;
        }

        if (steam.dataSize() < 13) {
            return;
        }
        byte[] dataAreaLen = new byte[2];

        dataAreaLen[0] = steam.getByte(11);
        dataAreaLen[1] = steam.getByte(12);
       // int len = BytesUtil.byteslowbitToInt(dataAreaLen, 0);// 报文长度(int)
        int len =   DataItemTemplateHelp.getContentLen(dataAreaLen);
        if (len > BusinessConstant.DATAGRAM_BODY_MAX_LEN){
            this.state = TestState.UNKNOWN;
            return;
        }
        this.length = 11 + len + 6;
        if (steam.dataSize() < (11 + len + 6)) {
            return;
        }
        this.packet = steam.getBytes(this.length);
        int count = 0;
        this.packetHeader = new byte[2];
        System.arraycopy(this.packet, count, this.packetHeader, 0, 2);
        count += 2;
        //中心站地址
        this.centerStation = new byte[1];
        System.arraycopy(this.packet, count, this.centerStation, 0, 1);
        count += 1;
        //RTU地址
        this.rtuCodeByte = new byte[5];
        System.arraycopy(this.packet, count, this.rtuCodeByte, 0, 5);
        count += 5;

        //RTU密码
        this.rtuPw = new byte[2];
        System.arraycopy(this.packet, count, this.rtuPw, 0, 2);
        count += 2;

        //功能码
        this.funcCode = new byte[1];
        System.arraycopy(this.packet, count, this.funcCode, 0, 1);
        count += 1;

        this.command = this.funcCode[0];
        //报文上下行标识及长度  高四位用作上下行标识（0000 表示上行，1000，表示下行）；
        //其余12位表示报文正文长度，表示报文起始符之后，报文结束符之前的报文字节数，允许长度为0001-4095
        this.dataAreaLen =dataAreaLen;
        System.arraycopy(this.packet, count, this.dataAreaLen, 0, 2);
        count += 2;
      //  len = BytesUtil.bytes2Int(this.dataAreaLen, 0, this.dataAreaLen.length);// 报文长度(int)

        //报文起始符
        this.dataAreaStartFlag = new byte[1];
        System.arraycopy(this.packet, count, this.dataAreaStartFlag, 0, 1);
        count += 1;

        //报文正文
        this.dataArea = new byte[len];
        System.arraycopy(this.packet, count, this.dataArea, 0, len);
        count += len;

        //报文起始符
        this.dataAreaEndFlag = new byte[1];
        System.arraycopy(this.packet, count, this.dataAreaEndFlag, 0, 1);
        count += 1;

        //校验码
        this.packetCrc16 = new byte[2];
        System.arraycopy(this.packet, count, this.packetCrc16, 0, 2);
        count += 2;

        this.state = TestState.MATHCE;
    }
}
