package org.ts.ddcs.DataItemTemplate;

import org.ts.ddcs.Entity.PptnEntity;
import org.ts.ddcs.Tool.BytesHelp;

import java.math.BigDecimal;

/**
 * funcode 0xF4
 * 1小时内每5分时段降雨量
 */
public class DataItemTemplateDRP extends DataItemTemplate {

    @Override
    public byte[] Stream(byte[] data) {
        byte[] remain = data;
        if (this.macth) {
            return remain;
        }
        String flag = BytesHelp.byte2HexStr(remain, 0, 1);
        if (flag.equals(this.feture)) {
            byte[] len = BytesHelp.subBytes(remain, 1, 1);
            byte[] drp = BytesHelp.subBytes(remain, 2, 12);
            double v = 0.0;
            for (int i = 0; i < 12; i++) {
                v += (double) drp[i] * 0.1;
            }
            this.bigDecimalValue = new BigDecimal(v);
            remain = BytesHelp.subBytes(remain, 14, remain.length - 14);
            this.macth = true;
        }
        return remain;
    }

    @Override
    public void SetEntity(Object entity) {
        PptnEntity pptnEntity = (PptnEntity) entity;
        pptnEntity.setDRP(this.bigDecimalValue);
        pptnEntity.setINTV(new BigDecimal(1));
    }
}
