18045010223
2025-07-07 0d3a683a0c97154b1f2e6657398664537e4e3e82
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package org.yzh.protocol.t808.T8900Lock;
 
import io.github.yezhihao.protostar.annotation.Field;
import io.github.yezhihao.protostar.annotation.Message;
import org.yzh.commons.util.BytesUtils;
import org.yzh.protocol.basics.JTMessage;
import org.yzh.protocol.commons.JT808;
 
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
 
/**
 * 消息透传应答 - 蝶阀锁状态响应
 */
public class T8900Response {
 
    @Field(index = 0, length = 2, desc = "锁ID")
    private byte[] lockId;
 
    @Field(index = 2, length = 1, desc = "命令类型")
    private byte commandType;
 
    @Field(index = 3, length = 1, desc = "锁芯状态")
    private byte lockCoreStatus;
 
    @Field(index = 4, length = 1, desc = "锁体安装位置状态")
    private byte installationStatus;
 
    @Field(index = 5, length = 1, desc = "外壳状态")
    private byte shellStatus;
 
    @Field(index = 6, length = 1, desc = "初始上电锁芯状态")
    private byte initialPowerStatus;
 
    @Field(index = 7, length = 2, desc = "电池电压")
    private short batteryVoltage;
 
    @Field(index = 9, length = 2, desc = "电源电压")
    private short powerVoltage;
 
    @Field(index = 11, length = 1, desc = "年")
    private byte year;
 
    @Field(index = 12, length = 1, desc = "月")
    private byte month;
 
    @Field(index = 13, length = 1, desc = "日")
    private byte day;
 
    @Field(index = 14, length = 1, desc = "时")
    private byte hour;
 
    @Field(index = 15, length = 1, desc = "分")
    private byte minute;
 
    @Field(index = 16, length = 1, desc = "秒")
    private byte second;
 
    @Field(index = 17, length = 1, desc = "校验码")
    private byte checksum;
 
    // Getters with parsed values
    public String getLockIdHex() {
        return BytesUtils.bytes2hex(lockId);
    }
 
    public String getLockCoreStatusDesc() {
        switch (lockCoreStatus) {
            case 0x01: return "开锁状态";
            case 0x02: return "关锁状态";
            case 0x03: return "位置异常状态";
            case 0x04: return "锁芯正在旋转进行中";
            default: return "未知状态:" + lockCoreStatus;
        }
    }
 
    // 解析状态为可读字符串
    public String parseStatus() {
        return String.format(
                "锁ID: 0x%04X\n" +
                        "锁芯状态: %s\n" +
                        "锁体状态: %s\n" +
                        "电池电压: %.1fV\n" +
                        "时间: %s",
                lockId,
                getStatusDesc(lockCoreStatus, "开锁", "关锁", "位置异常", "旋转中"),
                getStatusDesc(installationStatus, "正常", "异常"),
                batteryVoltage * 0.1,
                getDeviceTimeString()
        );
    }
 
    private String getStatusDesc(byte code, String... options) {
        return code - 1 >= 0 && code - 1 < options.length ? options[code - 1] : "未知";
    }
 
    private String parseTime(byte[] timeBytes) {
        // 解析yy-MM-dd hh:mm:ss(示例:假设timeBytes[0]=年低8位,需根据实际协议调整)
        return String.format("%02d-%02d-%02d %02d:%02d:%02d",
                timeBytes[0] & 0xFF,
                timeBytes[1] & 0xFF,
                timeBytes[2] & 0xFF,
                timeBytes[3] & 0xFF,
                timeBytes[4] & 0xFF,
                timeBytes[5] & 0xFF
        );
    }
 
    public String getInstallationStatusDesc() {
        return installationStatus == 0x01 ? "正常" : "异常";
    }
 
    public String getShellStatusDesc() {
        return shellStatus == 0x01 ? "正常" : "异常";
    }
 
    public String getInitialPowerStatusDesc() {
        return initialPowerStatus == 0x01 ? "开锁状态" : "关锁状态";
    }
 
    public double getBatteryVoltage() {
        return (batteryVoltage & 0xFFFF) * 0.1;
    }
 
    public double getPowerVoltage() {
        return (powerVoltage & 0xFFFF) * 0.1;
    }
 
    public LocalDateTime getDeviceTime() {
        int yearValue = 2000 + (year & 0xFF);
        return LocalDateTime.of(
            yearValue,
            month & 0xFF,
            day & 0xFF,
            hour & 0xFF,
            minute & 0xFF,
            second & 0xFF
        );
    }
 
    public String getDeviceTimeString() {
        return getDeviceTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
 
    // 校验方法
    public boolean isValidChecksum() {
        byte sum = 0;
        for (int i = 0; i < 17; i++) {
            sum ^= getByteAt(i);
        }
        return sum == checksum;
    }
 
    private byte getByteAt(int index) {
        switch (index) {
            case 0: case 1: return lockId[index];
            case 2: return commandType;
            case 3: return lockCoreStatus;
            case 4: return installationStatus;
            case 5: return shellStatus;
            case 6: return initialPowerStatus;
            case 7: return (byte) (batteryVoltage & 0xFF);
            case 8: return (byte) ((batteryVoltage >> 8) & 0xFF);
            case 9: return (byte) (powerVoltage & 0xFF);
            case 10: return (byte) ((powerVoltage >> 8) & 0xFF);
            case 11: return year;
            case 12: return month;
            case 13: return day;
            case 14: return hour;
            case 15: return minute;
            case 16: return second;
            default: throw new IndexOutOfBoundsException();
        }
    }
 
    // Getters and Setters
    public byte[] getLockId() {
        return lockId;
    }
 
    public void setLockId(byte[] lockId) {
        this.lockId = lockId;
    }
 
    public byte getCommandType() {
        return commandType;
    }
 
    public void setCommandType(byte commandType) {
        this.commandType = commandType;
    }
 
    public byte getLockCoreStatus() {
        return lockCoreStatus;
    }
 
    public void setLockCoreStatus(byte lockCoreStatus) {
        this.lockCoreStatus = lockCoreStatus;
    }
 
    public byte getInstallationStatus() {
        return installationStatus;
    }
 
    public void setInstallationStatus(byte installationStatus) {
        this.installationStatus = installationStatus;
    }
 
    public byte getShellStatus() {
        return shellStatus;
    }
 
    public void setShellStatus(byte shellStatus) {
        this.shellStatus = shellStatus;
    }
 
    public byte getInitialPowerStatus() {
        return initialPowerStatus;
    }
 
    public void setInitialPowerStatus(byte initialPowerStatus) {
        this.initialPowerStatus = initialPowerStatus;
    }
 
    public short getBatteryVoltageRaw() {
        return batteryVoltage;
    }
 
    public void setBatteryVoltage(short batteryVoltage) {
        this.batteryVoltage = batteryVoltage;
    }
 
    public short getPowerVoltageRaw() {
        return powerVoltage;
    }
 
    public void setPowerVoltage(short powerVoltage) {
        this.powerVoltage = powerVoltage;
    }
 
    public byte getYear() {
        return year;
    }
 
    public void setYear(byte year) {
        this.year = year;
    }
 
    public byte getMonth() {
        return month;
    }
 
    public void setMonth(byte month) {
        this.month = month;
    }
 
    public byte getDay() {
        return day;
    }
 
    public void setDay(byte day) {
        this.day = day;
    }
 
    public byte getHour() {
        return hour;
    }
 
    public void setHour(byte hour) {
        this.hour = hour;
    }
 
    public byte getMinute() {
        return minute;
    }
 
    public void setMinute(byte minute) {
        this.minute = minute;
    }
 
    public byte getSecond() {
        return second;
    }
 
    public void setSecond(byte second) {
        this.second = second;
    }
 
    public byte getChecksum() {
        return checksum;
    }
 
    public void setChecksum(byte checksum) {
        this.checksum = checksum;
    }
}