From e04e311ff469ce0c40db3b17d6413aee60abde5c Mon Sep 17 00:00:00 2001 From: Eli Yip Date: Tue, 3 Jun 2025 09:26:49 +0800 Subject: [PATCH] feat: add legacy router and models --- api/legacy/models.go | 33 +++++++++++++++++++++++ api/legacy/router.go | 64 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 api/legacy/models.go create mode 100644 api/legacy/router.go diff --git a/api/legacy/models.go b/api/legacy/models.go new file mode 100644 index 0000000..3403823 --- /dev/null +++ b/api/legacy/models.go @@ -0,0 +1,33 @@ +package legacy + +// FingerPoseRequest 手指姿态设置请求 +type FingerPoseRequest struct { + Interface string `json:"interface,omitempty"` + Pose []byte `json:"pose" binding:"required,len=6"` + HandType string `json:"handType,omitempty"` // 新增:手型类型 + HandId uint32 `json:"handId,omitempty"` // 新增:CAN ID +} + +// PalmPoseRequest 掌部姿态设置请求 +type PalmPoseRequest struct { + Interface string `json:"interface,omitempty"` + Pose []byte `json:"pose" binding:"required,len=4"` + HandType string `json:"handType,omitempty"` // 新增:手型类型 + HandId uint32 `json:"handId,omitempty"` // 新增:CAN ID +} + +// AnimationRequest 动画控制请求 +type AnimationRequest struct { + Interface string `json:"interface,omitempty"` + Type string `json:"type" binding:"required,oneof=wave sway stop"` + Speed int `json:"speed" binding:"min=0,max=2000"` + HandType string `json:"handType,omitempty"` // 新增:手型类型 + HandId uint32 `json:"handId,omitempty"` // 新增:CAN ID +} + +// HandTypeRequest 手型设置请求 +type HandTypeRequest struct { + Interface string `json:"interface" binding:"required"` + HandType string `json:"handType" binding:"required,oneof=left right"` + HandId uint32 `json:"handId" binding:"required"` +} diff --git a/api/legacy/router.go b/api/legacy/router.go new file mode 100644 index 0000000..0fcc11c --- /dev/null +++ b/api/legacy/router.go @@ -0,0 +1,64 @@ +package legacy + +import ( + "time" + + "hands/device" + + "github.com/gin-gonic/gin" +) + +type LegacyServer struct { + mapper *InterfaceDeviceMapper + startTime time.Time +} + +// NewLegacyServer 创建新的兼容层 API 服务器实例 +func NewLegacyServer(deviceManager *device.DeviceManager) (*LegacyServer, error) { + mapper, err := NewInterfaceDeviceMapper(deviceManager) + if err != nil { + return nil, err + } + + return &LegacyServer{ + mapper: mapper, + startTime: time.Now(), + }, nil +} + +// SetupRoutes 设置兼容层 API 路由 +func (s *LegacyServer) SetupRoutes(r *gin.Engine) { + // 兼容层 API 路由组 + legacy := r.Group("/api/legacy") + { + // 手型设置 API + legacy.POST("/hand-type", s.handleHandType) + + // 手指姿态 API + legacy.POST("/fingers", s.handleFingers) + + // 掌部姿态 API + legacy.POST("/palm", s.handlePalm) + + // 预设姿势 API + legacy.POST("/preset/:pose", s.handlePreset) + + // 动画控制 API + legacy.POST("/animation", s.handleAnimation) + + // 获取传感器数据 API + legacy.GET("/sensors", s.handleSensors) + + // 系统状态 API + legacy.GET("/status", s.handleStatus) + + // 获取可用接口列表 API + legacy.GET("/interfaces", s.handleInterfaces) + + // 获取手型配置 API + legacy.GET("/hand-configs", s.handleHandConfigs) + + // 健康检查端点 + legacy.GET("/health", s.handleHealth) + } +}