package org.ts.ddcs.DataItemTemplate;


import org.ts.ddcs.Entity.RtuStatusEntity;
import org.ts.ddcs.Log.LogHelper;
import org.ts.ddcs.Tool.BytesHelp;
import org.ts.ddcs.Tool.StringUtil;

import java.io.UnsupportedEncodingException;


/**
 * funcode ff53
 * 唯一识别码
 */
@SuppressWarnings("all")
public class DataItemTemplateFF53 extends DataItemTemplate {


    @Override
    public byte[] Stream(byte[] data) {
        try {
            byte[] remain = data;
            if (this.macth) {
                return remain;
            }
            int count = 0;
            String flag = BytesHelp.byte2HexStr(remain, count, 2);
            count += 2;
            if (flag.equals(this.feture)) {
                byte[] len = BytesHelp.subBytes(remain, count, 1);
                count += 1;
                int dataTotalLen = len[0] >> 3;
                int decimalLen = len[0] & 7;
                byte[] buff = BytesHelp.subBytes(remain, count, dataTotalLen);
                count += dataTotalLen;
                String value = new String(buff, "utf-8");
                if (value != null && value.length() > 0) {
                    if (value.startsWith("no find")) {
                        value = null;
                    }
                }
                this.varcharValue = value;
                remain = BytesHelp.subBytes(remain, count, remain.length - count);
                this.macth = true;
            }
            return remain;
        } catch (Exception e) {
            LogHelper.error(e);
        }
        return data;
    }

    @Override
    public void SetEntity(Object entity) {
        String uuid = null;
        RtuStatusEntity statusEntity = (RtuStatusEntity) entity;
        statusEntity.setUuid(this.varcharValue);
    }
}
