jiangping
2025-02-14 c185de12314b8733f23ed7856e6d1e87ee95c1ca
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
package com.doumee.jtt808.web.model.vo;
 
import io.swagger.v3.oas.annotations.media.Schema;
import org.yzh.protocol.commons.Charsets;
 
import java.io.*;
import java.time.LocalDate;
 
import static io.github.yezhihao.protostar.util.DateTool.BCD;
 
/**
 * @author yezhihao
 * https://gitee.com/yezhihao/jt808-server
 */
public class DeviceInfo {
 
    @Schema(description = "签发日期")
    protected LocalDate issuedAt;
    @Schema(description = "预留字段")
    protected byte reserved;
    @Schema(description = "设备id")
    protected String deviceId;
 
    public DeviceInfo() {
    }
 
    public DeviceInfo(byte[] bytes) {
        formBytes(bytes);
    }
 
    public DeviceInfo(String deviceId, LocalDate issuedAt) {
        this.deviceId = deviceId;
        this.issuedAt = issuedAt;
    }
 
    public LocalDate getIssuedAt() {
        return issuedAt;
    }
 
    public void setIssuedAt(LocalDate issuedAt) {
        this.issuedAt = issuedAt;
    }
 
    public byte getReserved() {
        return reserved;
    }
 
    public void setReserved(byte reserved) {
        this.reserved = reserved;
    }
 
    public String getDeviceId() {
        return deviceId;
    }
 
    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }
 
    public DeviceInfo formBytes(byte[] bytes) {
        try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
             DataInputStream dis = new DataInputStream(bis)) {
 
            byte[] temp;
            dis.read(temp = new byte[3]);
            this.issuedAt = BCD.toDate(temp);
            this.reserved = dis.readByte();
            int len = dis.readUnsignedByte();
            dis.read(temp = new byte[len]);
            this.deviceId = new String(temp, Charsets.GBK);
 
            return this;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    public byte[] toBytes() {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(32);
             DataOutputStream dos = new DataOutputStream(bos)) {
 
            dos.write(BCD.from(issuedAt));
            dos.writeByte(reserved);
            byte[] bytes = deviceId.getBytes(Charsets.GBK);
            dos.writeByte(bytes.length);
            dos.write(bytes);
 
            return bos.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
 
    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("DeviceInfo{");
        sb.append("issuedAt=").append(issuedAt);
        sb.append(", reserved=").append(reserved);
        sb.append(", deviceId=").append(deviceId);
        sb.append('}');
        return sb.toString();
    }
}