Skip to content

Commit

Permalink
fix(codec): 解决 ExtendedJt808FieldDeserializerLocationExtraItem 没有考虑…
Browse files Browse the repository at this point in the history
… `length` 属性的问题
  • Loading branch information
hylexus committed Mar 24, 2024
1 parent 6496d72 commit 47dc992
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.1.4-rc.3(2024-03-24)

### 🐞 Bug Fixes

- 解决 `ExtendedJt808FieldDeserializerLocationExtraItem` 没有考虑 `length` 属性的问题

## 2.1.4-rc.2(2024-01-28)

### ⭐ New Features
Expand Down
18 changes: 9 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,15 @@ def modulesToPublishToMavenRepo = [
"jt-808-server-spring-boot-autoconfigure",
"jt-808-server-spring-boot-starter-boot2",
"jt-808-server-spring-boot-starter",
"jt-1078-server-support",
"jt-1078-server-spring-boot-autoconfigure",
"jt-1078-server-spring-boot-starter",
"jt-1078-server-spring-boot-starter-boot2",
"jt-dashboard-common",
"jt-dashboard-client",
"jt-dashboard-client-spring-boot-starter",
"jt-dashboard-server",
"jt-dashboard-server-spring-boot-starter",
// "jt-1078-server-support",
// "jt-1078-server-spring-boot-autoconfigure",
// "jt-1078-server-spring-boot-starter",
// "jt-1078-server-spring-boot-starter-boot2",
// "jt-dashboard-common",
// "jt-dashboard-client",
// "jt-dashboard-client-spring-boot-starter",
// "jt-dashboard-server",
// "jt-dashboard-server-spring-boot-starter",
]
configure(subprojects.findAll {
modulesToPublishToMavenRepo.contains(it.name)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
projectGroup=io.github.hylexus.jt
projectVersion=2.2.0-beta3
projectVersion=2.1.4-rc.3
# scm
projectScmUrl=https://github.com/hylexus/jt-framework
projectScmConnection=scm:git:[email protected]:hylexus/jt-framework.git
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public Object deserialize(ByteBuf byteBuf, MsgDataType msgDataType, int start, i
throw new JtIllegalStateException("Jt808AnnotationBasedDecoder is null");
}

if (length > 0) {
byteBuf = byteBuf.readSlice(length);
}

final Map<Integer, Object> result = new LinkedHashMap<>();
while (byteBuf.isReadable()) {
final int key = byteBuf.readUnsignedByte();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package io.github.hylexus.jt.jt808.support.annotation.codec;

import io.github.hylexus.jt.jt808.support.annotation.msg.req.RequestFieldAlias;
import io.github.hylexus.jt.jt808.support.data.deserialize.DefaultJt808FieldDeserializerRegistry;
import io.github.hylexus.jt.utils.HexStringUtils;
import lombok.Data;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class SimpleJt808MsgDecoderTest {

SimpleJt808MsgDecoder decoder;

@BeforeEach
void setUp() {
this.decoder = new SimpleJt808MsgDecoder(new Jt808AnnotationBasedDecoder(new DefaultJt808FieldDeserializerRegistry(true)));
}

@Test
void test() {
final String hexString = "010040560100000000013912344329007B000B00026964393837363534333231"
+ "747970653030313233343536373831323334353637383837363534333231494430303030313233"
+ "34353637383132333435363738383736353433323101B8CA4A2D31323334353925";
System.out.println(hexString);

final TestEntityForDecode entity = this.decoder.decode(HexStringUtils.hexString2Bytes(hexString), TestEntityForDecode.class);

Assertions.assertEquals(0x0100, entity.getMsgId());
Assertions.assertEquals(1, entity.getVersion());
Assertions.assertEquals("00000000013912344329", entity.getTerminalPhoneNumber());
Assertions.assertEquals(123, entity.getFlowId());
Assertions.assertEquals(11, entity.getProvinceId());
Assertions.assertEquals(2, entity.getCityId());
Assertions.assertEquals("id987654321", entity.getManufacturerId());
Assertions.assertEquals("type00123456781234567887654321", entity.getTerminalType());
Assertions.assertEquals("ID0000123456781234567887654321", entity.getTerminalId());
Assertions.assertEquals(1, entity.getColor());
Assertions.assertEquals("甘J-123459", entity.getCarIdentifier());
Assertions.assertEquals(37, entity.getCheckSum());
}

@Data
public static class TestEntityForDecode {

// region 消息头
// byte[0,2) 消息ID WORD
@RequestFieldAlias.Word(order = -100)
private int msgId;

// byte[2,4) 消息体属性 WORD
@RequestFieldAlias.Word(order = -90)
private int msgBodyProperty;

// byte[4,5) 协议版本号 BYTE
@RequestFieldAlias.Byte(order = -80)
private byte version;

// byte[5,15) 终端手机号 BCD[10]
@RequestFieldAlias.Bcd(order = -70, length = 10)
private String terminalPhoneNumber;

// byte[15,17) 消息流水号 WORD
@RequestFieldAlias.Word(order = -60)
private int flowId;

// 消息包封装项: 消息体属性中第 13 位为 1 时才有该属性
// byte[17,19) 消息总包数 WORD
@RequestFieldAlias.Word(order = -50, conditionalOn = "hasSubPackage()")
private Integer totalPackage;

// 消息包封装项: 消息体属性中第 13 位为 1 时才有该属性
// byte[19,21) 包序号 WORD
@RequestFieldAlias.Word(order = -40, conditionalOn = "hasSubPackage()")
private Integer currentPackageNo;

// bit[13] 0010,0000,0000,0000(2000)(是否有子包)
public boolean hasSubPackage() {
// return ((msgBodyProperty & 0x2000) >> 13) == 1;
return (msgBodyProperty & 0x2000) > 0;
}
// endregion 消息头

// region 消息体
// fixme 目前的消息体和消息头只能写在同一个类里(读取类属性的时候没有考虑父类的情况)
// 1. [0-2) WORD 省域ID
// WORD 类型固定长度就是2字节 所以无需指定length
@RequestFieldAlias.Word(order = 1)
private int provinceId;

// 2. [2-4) WORD 市县域ID
@RequestFieldAlias.Word(order = 2)
private int cityId;

// 3. [4-15) BYTE[11] 制造商ID
@RequestFieldAlias.Bytes(order = 3, length = 11)
private String manufacturerId;

// 4. [15-45) BYTE[30] 终端型号
@RequestFieldAlias.Bytes(order = 4, length = 30)
private String terminalType;

// 5. [45-75) BYTE[30] 终端ID
@RequestFieldAlias.Bytes(order = 5, length = 30)
private String terminalId;

// 6. [75] BYTE 车牌颜色
@RequestFieldAlias.Byte(order = 6)
private byte color;

// 7. [76,n) String 车辆标识
// 使用 SpEL 计算消息长度(上下文中的消息体总长度减去前面消费掉的字节数)
@RequestFieldAlias.String(order = 7, lengthExpression = "msgBodyLength() - 76")
private String carIdentifier;
// endregion 消息体

// region 检验码
@RequestFieldAlias.Byte(order = 300)
private short checkSum;
// endregion 检验码

// region 辅助类和工具方法
public int msgBodyStartIndex(boolean hasSubPackage) {
// 2019
return hasSubPackage ? 21 : 17;
}

// bit[0-9] 0000,0011,1111,1111(3FF)(消息体长度)
public int msgBodyLength() {
return msgBodyProperty & 0x3ff;
}
// endregion 辅助类和工具方法
}

}

0 comments on commit 47dc992

Please sign in to comment.