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 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 { 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); } } }