package org.ts.ddcs.DataTemplate.DtuTemplate;


import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;

/**
 * created by swp on 2017/8/23
 */
public class DtuDataTemplate extends DtuTemplate {


    @Override
    public void Stream() {
        try {
            if (dataLen > 0) {
              int tmpValue = BytesHelp.bytes2short(this.dataArea);
              double value = (double)tmpValue/(double)1000;
              LogHelper.info("dtu value "+value);
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }

    @Override
    public boolean Macth(byte[] data) {
        byte[] fc = BytesHelp.subBytes(data, 0, 1);
        if (!this.fetureKey.equals(BytesHelp.byte2HexStr(fc))) {
            return this.macth;
        }
        int count = 0;
        startFlag = BytesHelp.subBytes(data, count, 1);
        count += 1;
        funcCode = BytesHelp.subBytes(data, count, 1);
        count += 1;
        byte[] len = BytesHelp.subBytes(data, count, 1);
        dataLen = (int) len[0];
        count+=1;
        if (dataLen > 0) {
            dataArea = BytesHelp.subBytes(data, count, dataLen);
        }
        count += dataLen;
        crc = BytesHelp.subBytes(data, count, 2);
        count += 2;
        if (data.length == count) {
            this.macth = true;
        } else {
            LogHelper.info("*************************   macth error *********************");
        }

        return this.macth;
    }

}
