package org.ts.ddcs.itemdatatemplate.szy;


import com.alibaba.fastjson.JSONObject;
import org.ts.ddcs.common.util.BytesHelp;
import org.ts.ddcs.common.util.Sw2014DatagramTemplateHelp;
import org.ts.ddcs.common.util.ValueLen;
import org.ts.ddcs.itemdatatemplate.DataItemTemplate;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 0xFF55
 * 设备状态,1字节HEX	0 - SD卡正常   1 - SD卡损坏
 */
public class DataItemTemplateSzyFF55 extends DataItemTemplate {


    @Override
    public int stream(byte[] data, int index) throws UnsupportedEncodingException {
        this.macth = false;
        this.intValues = new ArrayList<>();
        int count = 0;
        String flag = BytesHelp.byte2HexStr(data, index + count, 2);
        if (flag.toUpperCase().equals(this.feture)) {
            count += 2;
            ValueLen valueLen = Sw2014DatagramTemplateHelp.getValueLen(data, index + count);
            count += 1;
            byte[] buff = BytesHelp.subBytes(data, index + count, valueLen.getByteLen());
            this.intValues.add((int) buff[0]);
            count += valueLen.getByteLen();
            this.valueLen = valueLen;
            this.macth = true;
        }
        return count;
    }

    @Override
    public int stream(byte[] data, int index, int batchSize) {
        return 0;
    }

    @Override
    public String getStringValue() {
        return Integer.toString(intValues.get(0));
    }

    @Override
    public void saveValue(Map<String, List<Map<String,Object>>> keyStore) {
        this.saveIntValues(keyStore);
    }
}
