jiangping
2025-02-14 e3067f58cce422bbd26a7729bf59f5f4f34a530f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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.001));
            }catch (Exception e){
 
            }
        }
        if(value!=null && value.length>6){
            try {
                this.setChargeVoltage((float) (Byte2NumberUtils.bytesToInteger(new byte[]{value[4],value[4]}) * 0.001));
            }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);
        }
    }
}