fix(iot-gateway): 修复 JT808 注册应答消息编码失败的问题
Some checks failed
Java CI with Maven / build (11) (push) Has been cancelled
Java CI with Maven / build (17) (push) Has been cancelled
Java CI with Maven / build (8) (push) Has been cancelled

This commit is contained in:
lzh
2026-01-16 16:15:52 +08:00
parent 855f69ae6c
commit cd362e3f82

View File

@@ -49,8 +49,8 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
/**
* JT808 消息方法名
*/
private static final String METHOD_REGISTER = "jt808.terminal.register"; // 终端注册 (0x0100)
private static final String METHOD_AUTH = "jt808.terminal.auth"; // 终端鉴权 (0x0102)
private static final String METHOD_REGISTER = "jt808.terminal.register"; // 终端注册 (0x0100)
private static final String METHOD_AUTH = "jt808.terminal.auth"; // 终端鉴权 (0x0102)
/**
* 鉴权码缓存过期时间(分钟)
@@ -87,7 +87,7 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
@Override
public AuthResult handleAuthentication(String clientId, IotDeviceMessage message,
String codecType, NetSocket socket) {
String codecType, NetSocket socket) {
String method = message.getMethod();
// 路由到不同的认证处理方法
@@ -103,8 +103,8 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
@Override
public void handleBusinessMessage(String clientId, IotDeviceMessage message,
String codecType, NetSocket socket,
String productKey, String deviceName, String serverId) {
String codecType, NetSocket socket,
String productKey, String deviceName, String serverId) {
try {
// 发送消息到消息总线
deviceMessageService.sendDeviceMessage(message, productKey, deviceName, serverId);
@@ -119,7 +119,7 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
@Override
public void sendResponse(NetSocket socket, IotDeviceMessage originalMessage,
boolean success, String message, String codecType) {
boolean success, String message, String codecType) {
// JT808 协议的响应由具体的处理方法发送(注册应答、通用应答等)
// 此方法不需要实现,保留空实现
log.debug("[sendResponse][JT808 协议响应由具体方法发送,跳过通用响应]");
@@ -146,7 +146,7 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
* @return 认证结果PENDING 表示注册成功但需要继续鉴权)
*/
private AuthResult handleRegister(String clientId, IotDeviceMessage message,
String codecType, NetSocket socket) {
String codecType, NetSocket socket) {
try {
// 1. 提取终端手机号
String terminalPhone = extractTerminalPhone(message);
@@ -218,7 +218,7 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
* @return 认证结果SUCCESS 表示鉴权成功,可以发送业务消息)
*/
private AuthResult handleAuth(String clientId, IotDeviceMessage message,
String codecType, NetSocket socket) {
String codecType, NetSocket socket) {
try {
// 1. 提取终端手机号和鉴权码
String terminalPhone = extractTerminalPhone(message);
@@ -309,7 +309,8 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
private String generateAuthToken(String terminalPhone, IotDeviceRespDTO device) {
try {
// 构建认证参数username 格式productKey.deviceName
// String username = IotDeviceAuthUtils.buildUsername(device.getProductKey(), device.getDeviceName());
// String username = IotDeviceAuthUtils.buildUsername(device.getProductKey(),
// device.getDeviceName());
// 调用系统标准认证 API 获取认证信息
// 注意:这里只是为了获取密钥信息,不是真正的认证
@@ -350,7 +351,7 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
* @param requestId 请求ID
*/
private void sendRegisterResp(NetSocket socket, String phone, int replyFlowId, byte replyCode,
String authToken, String codecType, String requestId) {
String authToken, String codecType, String requestId) {
try {
// 生成平台流水号
int flowId = (int) (System.currentTimeMillis() % 65535) + 1;
@@ -364,14 +365,14 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
.put("flowId", flowId)
.build();
// 构建响应消息
IotDeviceMessage responseMessage = IotDeviceMessage.replyOf(
// 构建响应消息(使用 of 方法确保 params 正确传递给编码器)
IotDeviceMessage responseMessage = IotDeviceMessage.of(
requestId,
"jt808.platform.registerResp",
params,
params, // params 必须在第3个参数位置
null, // data
replyCode == 0 ? 0 : 401,
replyCode == 0 ? "注册成功" : "注册失败"
);
replyCode == 0 ? "注册成功" : "注册失败");
// 编码并发送
byte[] encodedData = deviceMessageService.encodeDeviceMessage(responseMessage, codecType);
@@ -401,7 +402,7 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
* @param requestId 请求ID
*/
private void sendCommonResp(NetSocket socket, String phone, int replyFlowId, int replyId,
byte replyCode, String codecType, String requestId) {
byte replyCode, String codecType, String requestId) {
try {
// 生成平台流水号
int flowId = (int) (System.currentTimeMillis() % 65535) + 1;
@@ -415,14 +416,14 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
.put("flowId", flowId)
.build();
// 构建响应消息
IotDeviceMessage responseMessage = IotDeviceMessage.replyOf(
// 构建响应消息(使用 of 方法确保 params 正确传递给编码器)
IotDeviceMessage responseMessage = IotDeviceMessage.of(
requestId,
"jt808.platform.commonResp",
params,
params, // params 必须在第3个参数位置
null, // data
replyCode == 0 ? 0 : 401,
replyCode == 0 ? "成功" : "失败"
);
replyCode == 0 ? "成功" : "失败");
// 编码并发送
byte[] encodedData = deviceMessageService.encodeDeviceMessage(responseMessage, codecType);
@@ -517,8 +518,8 @@ public class Jt808ProtocolHandler extends AbstractProtocolHandler {
private IotDeviceRespDTO findDeviceByDeviceName(String deviceName) {
try {
// 调用设备 API 查询(只传 deviceName
CommonResult<IotDeviceRespDTO> result =
deviceApi.getDevice(new IotDeviceGetReqDTO().setDeviceName(deviceName));
CommonResult<IotDeviceRespDTO> result = deviceApi
.getDevice(new IotDeviceGetReqDTO().setDeviceName(deviceName));
if (result.isSuccess() && result.getData() != null) {
return result.getData();