package org.ts.ddcs.DataTemplate;

import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import io.netty.channel.ChannelHandlerContext;

/**
 * created by swp on 2017/8/23
 */
public class UnknownDataTemplate extends DataTemplate  {
    @Override
    public boolean Macth(byte[] data) {

            int count = 0;
            startFlag = BytesHelp.subBytes(data,count,2);
            count +=2;
            centerStation = BytesHelp.subBytes(data,count,1);
            count += 1;
            rtuCode = BytesHelp.subBytes(data,count,5);
            count += 5;
            rtuPassword = BytesHelp.subBytes(data,count,2);
            count += 2;
            funcCode = BytesHelp.subBytes(data,count,1);
            count += 1;
            //报文上下行标识及长度  高四位用作上下行标识（0000 表示上行，1000，表示下行）；
            //其余12位表示报文正文长度，表示报文起始符之后，报文结束符之前的报文字节数，允许长度为0001-4095
            byte[] dataAreaLen2014 = BytesHelp.subBytes(data, count, 2);
            count += 2;
            dataLen = BytesHelp.bytes2Int(dataAreaLen2014, 0, dataAreaLen2014.length);// 报文长度(int)

            dataAreaStart= BytesHelp.subBytes(data,count,1);
            count += 1;

            if(dataLen>0){
                dataArea = BytesHelp.subBytes(data,count,dataLen);
            }
            count += dataLen;
            dataAreaEnd= BytesHelp.subBytes(data,count,1);
            count +=1;
            crc= BytesHelp.subBytes(data,count,2);
            count +=2;
            if(data.length == count){
                LogHelper.info("UNKNOWN:"+BytesHelp.byte2HexStr(funcCode));
                this.macth = true;
            }else{
                LogHelper.info("*************************   macth error *********************");
            }

        return this.macth;
    }
}
