| ¶Ô±ÈÐÂÎļþ |
| | |
| | | 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); |
| | | } |
| | | } |
| | | } |