package  org.ts.ddcs.datatemplate.swDataTemplateExt;

import org.ts.ddcs.dataStream.DataStreamInterface;
import org.ts.ddcs.datatemplate.TestState;
import org.ts.ddcs.util.BytesUtil;


/**
 * 规约报文数据模板扩展  ， 专门实现链路维持报文
 * 测站编码长度为16位
 * @author dylan
 */
public class DataTemplate2014F2fEx extends DataTemplate2014Ex {

    @Override
    public void stream(DataStreamInterface steam) {
        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() < 16) {
            return;
        }
        byte[] dataAreaLen = new byte[2];
        dataAreaLen[0] = steam.getByte(14);
        dataAreaLen[1] = steam.getByte(15);
        // 报文长度(int)
        int len = BytesUtil.byteslowbitToInt(dataAreaLen, 0);
        this.length = 14 + len + 6;
        if (steam.dataSize() < (14 + 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[8];
        System.arraycopy(this.packet, count, this.rtuCodeByte, 0, 8);
        count += 8;
        //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 = new byte[2];
        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;
    }
}
