refactor: move interface check to config package

This commit is contained in:
Eli Yip 2025-05-27 10:29:44 +08:00
parent e057ee1da0
commit 972f48f948
No known key found for this signature in database
GPG Key ID: C98B69D4CF7D7EC5
4 changed files with 20 additions and 28 deletions

View File

@ -16,15 +16,6 @@ var (
ServerStartTime time.Time 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) { func SetupRoutes(r *gin.Engine) {
r.StaticFile("/", "./static/index.html") r.StaticFile("/", "./static/index.html")
r.Static("/static", "./static") 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{ c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error", Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces), 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{ c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error", Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces), 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{ c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error", Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces), 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{ c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error", Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces), 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{ c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error", Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces), Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces),
@ -358,7 +349,7 @@ func SetupRoutes(r *gin.Engine) {
if ifName != "" { if ifName != "" {
// 验证接口 // 验证接口
if !isValidInterface(ifName) { if !config.IsValidInterface(ifName) {
c.JSON(http.StatusBadRequest, define.ApiResponse{ c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error", Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces), Error: fmt.Sprintf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces),

View File

@ -1,5 +1,12 @@
package config package config
import "hands/define" import (
"hands/define"
"slices"
)
var Config *define.Config var Config *define.Config
func IsValidInterface(ifName string) bool {
return slices.Contains(Config.AvailableInterfaces, ifName)
}

View File

@ -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) log.Printf("❌ 无法启动波浪动画: 无效的接口 %s", ifName)
return 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) log.Printf("❌ 无法启动摆动动画: 无效的接口 %s", ifName)
return return
} }
@ -238,7 +238,7 @@ func StopAllAnimations(ifName string) {
} }
// 验证接口 // 验证接口
if !IsValidInterface(ifName) { if !config.IsValidInterface(ifName) {
log.Printf("⚠️ 尝试停止无效接口的动画: %s", ifName) log.Printf("⚠️ 尝试停止无效接口的动画: %s", ifName)
return return
} }

View File

@ -6,7 +6,6 @@ import (
"hands/define" "hands/define"
"log" "log"
"math/rand/v2" "math/rand/v2"
"slices"
"strings" "strings"
"sync" "sync"
"time" "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 { func SendFingerPose(ifName string, pose []byte, handType string, handId uint32) error {
if len(pose) != 6 { 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) 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) 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) log.Printf("⚠️ 尝试重置无效接口: %s", ifName)
return return
} }