diff --git a/api/router.go b/api/router.go index 86dcd5e..b0b51e2 100644 --- a/api/router.go +++ b/api/router.go @@ -16,15 +16,6 @@ var ( ServerStartTime time.Time ) -func isValidInterface(ifName string) bool { - for _, validIface := range config.Config.AvailableInterfaces { - if ifName == validIface { - return true - } - } - return false -} - func SetupRoutes(r *gin.Engine) { r.StaticFile("/", "./static/index.html") r.Static("/static", "./static") @@ -43,7 +34,7 @@ func SetupRoutes(r *gin.Engine) { } // 验证接口 - if !isValidInterface(req.Interface) { + if !config.IsValidInterface(req.Interface) { c.JSON(http.StatusBadRequest, define.ApiResponse{ Status: "error", Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.Config.AvailableInterfaces), @@ -105,7 +96,7 @@ func SetupRoutes(r *gin.Engine) { } // 验证接口 - if !isValidInterface(req.Interface) { + if !config.IsValidInterface(req.Interface) { c.JSON(http.StatusBadRequest, define.ApiResponse{ Status: "error", Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.Config.AvailableInterfaces), @@ -158,7 +149,7 @@ func SetupRoutes(r *gin.Engine) { } // 验证接口 - if !isValidInterface(req.Interface) { + if !config.IsValidInterface(req.Interface) { c.JSON(http.StatusBadRequest, define.ApiResponse{ Status: "error", Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.Config.AvailableInterfaces), @@ -196,7 +187,7 @@ func SetupRoutes(r *gin.Engine) { } // 验证接口 - if !isValidInterface(ifName) { + if !config.IsValidInterface(ifName) { c.JSON(http.StatusBadRequest, define.ApiResponse{ Status: "error", Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", ifName, config.Config.AvailableInterfaces), @@ -299,7 +290,7 @@ func SetupRoutes(r *gin.Engine) { } // 验证接口 - if !isValidInterface(req.Interface) { + if !config.IsValidInterface(req.Interface) { c.JSON(http.StatusBadRequest, define.ApiResponse{ Status: "error", Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.Config.AvailableInterfaces), @@ -358,7 +349,7 @@ func SetupRoutes(r *gin.Engine) { if ifName != "" { // 验证接口 - if !isValidInterface(ifName) { + if !config.IsValidInterface(ifName) { c.JSON(http.StatusBadRequest, define.ApiResponse{ Status: "error", Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", ifName, config.Config.AvailableInterfaces), diff --git a/config/config.go b/config/config.go index a5bd41f..426ee2f 100644 --- a/config/config.go +++ b/config/config.go @@ -1,5 +1,12 @@ package config -import "hands/define" +import ( + "hands/define" + "slices" +) var Config *define.Config + +func IsValidInterface(ifName string) bool { + return slices.Contains(Config.AvailableInterfaces, ifName) +} diff --git a/hands/animation.go b/hands/animation.go index 43c270c..27500a4 100644 --- a/hands/animation.go +++ b/hands/animation.go @@ -35,7 +35,7 @@ func StartWaveAnimation(ifName string, speed int, handType string, handId uint32 } // 验证接口 - if !IsValidInterface(ifName) { + if !config.IsValidInterface(ifName) { log.Printf("❌ 无法启动波浪动画: 无效的接口 %s", ifName) return } @@ -150,7 +150,7 @@ func StartSwayAnimation(ifName string, speed int, handType string, handId uint32 } // 验证接口 - if !IsValidInterface(ifName) { + if !config.IsValidInterface(ifName) { log.Printf("❌ 无法启动摆动动画: 无效的接口 %s", ifName) return } @@ -238,7 +238,7 @@ func StopAllAnimations(ifName string) { } // 验证接口 - if !IsValidInterface(ifName) { + if !config.IsValidInterface(ifName) { log.Printf("⚠️ 尝试停止无效接口的动画: %s", ifName) return } diff --git a/hands/hands.go b/hands/hands.go index 5c247ad..fc1c1c3 100644 --- a/hands/hands.go +++ b/hands/hands.go @@ -6,7 +6,6 @@ import ( "hands/define" "log" "math/rand/v2" - "slices" "strings" "sync" "time" @@ -104,11 +103,6 @@ func ParseHandType(handType string, handId uint32, ifName string) uint32 { } } -// 验证接口是否可用 -func IsValidInterface(ifName string) bool { - return slices.Contains(config.Config.AvailableInterfaces, ifName) -} - // 发送手指姿态指令 - 支持手型参数 func SendFingerPose(ifName string, pose []byte, handType string, handId uint32) error { if len(pose) != 6 { @@ -121,7 +115,7 @@ func SendFingerPose(ifName string, pose []byte, handType string, handId uint32) } // 验证接口 - if !IsValidInterface(ifName) { + if !config.IsValidInterface(ifName) { return fmt.Errorf("无效的接口 %s,可用接口: %v", ifName, config.Config.AvailableInterfaces) } @@ -182,7 +176,7 @@ func SendPalmPose(ifName string, pose []byte, handType string, handId uint32) er } // 验证接口 - if !IsValidInterface(ifName) { + if !config.IsValidInterface(ifName) { return fmt.Errorf("无效的接口 %s,可用接口: %v", ifName, config.Config.AvailableInterfaces) } @@ -236,7 +230,7 @@ func resetToDefaultPose(ifName string) { } // 验证接口 - if !IsValidInterface(ifName) { + if !config.IsValidInterface(ifName) { log.Printf("⚠️ 尝试重置无效接口: %s", ifName) return }