Skip to content

Commit 60eb40b

Browse files
committed
chore: support filter message by id
1 parent fbd8b3d commit 60eb40b

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

api.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"net/http"
66
"strconv"
7+
"strings"
78

89
"github.com/gin-gonic/gin"
910
)
@@ -635,6 +636,31 @@ func (h *APIHandler) handleTeardownAllInterfaces(c *gin.Context) {
635636

636637
// ====== Message Listening Handlers (New) ======
637638

639+
// 判断用户传入的 hex string 是否匹配数据中的 id
640+
func MatchID(userHex string, id uint32) bool {
641+
var parsedID uint64
642+
var err error
643+
644+
// 如果用户传入的 hex 字符串以 "0x" 开头,先去掉前缀并转换为小写
645+
if strings.HasPrefix(userHex, "0x") {
646+
userHex = strings.TrimPrefix(strings.ToLower(userHex), "0x")
647+
parsedID, err = strconv.ParseUint(userHex, 16, 32)
648+
if err != nil {
649+
fmt.Println("❌ 无法解析 hex 参数:", err)
650+
return false
651+
}
652+
653+
} else {
654+
// 如果没有 "0x" 前缀,直接尝试解析为十进制
655+
parsedID, err = strconv.ParseUint(userHex, 10, 32)
656+
if err != nil {
657+
fmt.Println("❌ 无法解析十进制参数:", err)
658+
return false
659+
}
660+
}
661+
return uint32(parsedID) == id
662+
}
663+
638664
// handleGetMessages returns all messages for a specific interface
639665
func (h *APIHandler) handleGetMessages(c *gin.Context) {
640666
if h.messageListener == nil {
@@ -654,11 +680,11 @@ func (h *APIHandler) handleGetMessages(c *gin.Context) {
654680
return
655681
}
656682

657-
id := c.Query("id")
658-
if id != "" {
683+
userId := c.Query("id")
684+
if userId != "" {
659685
var filteredMessages []CanMessageLog
660686
for _, msg := range messages {
661-
if msg.HEX_ID == id {
687+
if MatchID(userId, msg.ID) {
662688
filteredMessages = append(filteredMessages, msg)
663689
}
664690
}

0 commit comments

Comments
 (0)