1
MrShi
2025-03-18 a836f03a5d1fbfa81e147d09ffdfa87ba3975c13
server/jtt808_parent/jtt808-protocol/src/main/java/org/yzh/protocol/commons/transform/attribute/Battery.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,119 @@
package org.yzh.protocol.commons.transform.attribute;
import io.github.yezhihao.protostar.Schema;
import io.github.yezhihao.protostar.util.ByteBufUtils;
import io.netty.buffer.ByteBuf;
import org.yzh.commons.util.Byte2NumberUtils;
import java.util.Arrays;
/**
 * èƒŽåŽ‹ 0x05
 * length 30
 */
public class Battery {
    public static final Integer key = 0Xe3;
    public static final Schema<Battery> SCHEMA = new TirePressureSchema();
    private byte[] value;
    private Integer capacity;//电池电量
    private Float voltage;//电池电压
    private Float chargeVoltage;//充电电压
    public Integer getCapacity() {
        return capacity;
    }
    public void setCapacity(Integer capacity) {
        this.capacity = capacity;
    }
    public Float getVoltage() {
        return voltage;
    }
    public void setVoltage(Float voltage) {
        this.voltage = voltage;
    }
    public Float getChargeVoltage() {
        return chargeVoltage;
    }
    public void setChargeVoltage(Float chargeVoltage) {
        this.chargeVoltage = chargeVoltage;
    }
    public Battery() {
    }
    /**
     * byte[] data={0x01,0x02,0x03,0x04,0x05,0x06}
     * ç”µæ± ç”µé‡æ˜¯ 0x0102,
     * ç”µæ± ç”µåŽ‹æ˜¯0x0304,
     * å……电电压是 0x0506,
     * ç”µåŽ‹å•ä½æ˜¯0.001v,
     * ä¸Šä¼  1000表示 1v。
     * @param value
     */
    public Battery(byte[] value) {
        this.value = value;
        if(value!=null && value.length>2){
            this.setCapacity(Byte2NumberUtils.bytesToInteger(new byte[]{value[0],value[1]}));
        }
        if(value!=null && value.length>4){
            try {
                this.setVoltage((float) (Byte2NumberUtils.bytesToInteger(new byte[]{value[2],value[3]}) * 0.01));
            }catch (Exception e){
            }
        }
        if(value!=null && value.length>6){
            try {
                this.setChargeVoltage((float) (Byte2NumberUtils.bytesToInteger(new byte[]{value[4],value[4]}) * 0.01));
            }catch (Exception e){
            }
        }
    }
    public byte[] getValue() {
        return value;
    }
    public void setValue(byte[] value) {
        this.value = value;
    }
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder(32);
        sb.append("Battery{value=").append(Arrays.toString(value));
        sb.append('}');
        return sb.toString();
    }
    private static class TirePressureSchema implements Schema<Battery> {
        private TirePressureSchema() {
        }
        @Override
        public Battery readFrom(ByteBuf input) {
            int len = input.readableBytes();
            if (len > 30)
                len = 30;
            byte[] value = new byte[len];
            input.readBytes(value);
            return new Battery(value);
        }
        @Override
        public void writeTo(ByteBuf output, Battery message) {
            ByteBufUtils.writeFixedLength(output, 30, message.value);
        }
    }
}