feat: add router and model definition
This commit is contained in:
parent
13ae6aaa0a
commit
bbbd4db6b1
111
api2/models.go
Normal file
111
api2/models.go
Normal file
@ -0,0 +1,111 @@
|
||||
package api2
|
||||
|
||||
import (
|
||||
"hands/device"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ===== 通用响应模型 =====
|
||||
|
||||
// ApiResponse 统一 API 响应格式(保持与原 API 兼容)
|
||||
type ApiResponse struct {
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Error string `json:"error,omitempty"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// ===== 设备管理相关模型 =====
|
||||
|
||||
// DeviceCreateRequest 创建设备请求
|
||||
type DeviceCreateRequest struct {
|
||||
ID string `json:"id" binding:"required"`
|
||||
Model string `json:"model" binding:"required"`
|
||||
Config map[string]any `json:"config"`
|
||||
HandType string `json:"handType,omitempty"` // "left" 或 "right"
|
||||
}
|
||||
|
||||
// DeviceInfo 设备信息响应
|
||||
type DeviceInfo struct {
|
||||
ID string `json:"id"`
|
||||
Model string `json:"model"`
|
||||
HandType string `json:"handType"`
|
||||
Status device.DeviceStatus `json:"status"`
|
||||
}
|
||||
|
||||
// DeviceListResponse 设备列表响应
|
||||
type DeviceListResponse struct {
|
||||
Devices []DeviceInfo `json:"devices"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
// HandTypeRequest 手型设置请求
|
||||
type HandTypeRequest struct {
|
||||
HandType string `json:"handType" binding:"required,oneof=left right"`
|
||||
}
|
||||
|
||||
// ===== 姿态控制相关模型 =====
|
||||
|
||||
// FingerPoseRequest 手指姿态设置请求
|
||||
type FingerPoseRequest struct {
|
||||
Pose []byte `json:"pose" binding:"required,len=6"`
|
||||
}
|
||||
|
||||
// PalmPoseRequest 手掌姿态设置请求
|
||||
type PalmPoseRequest struct {
|
||||
Pose []byte `json:"pose" binding:"required,len=4"`
|
||||
}
|
||||
|
||||
// ===== 动画控制相关模型 =====
|
||||
|
||||
// AnimationStartRequest 动画启动请求
|
||||
type AnimationStartRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
SpeedMs int `json:"speedMs,omitempty"`
|
||||
}
|
||||
|
||||
// AnimationStatusResponse 动画状态响应
|
||||
type AnimationStatusResponse struct {
|
||||
IsRunning bool `json:"isRunning"`
|
||||
CurrentName string `json:"currentName,omitempty"`
|
||||
AvailableList []string `json:"availableList"`
|
||||
}
|
||||
|
||||
// ===== 传感器相关模型 =====
|
||||
|
||||
// SensorDataResponse 传感器数据响应
|
||||
type SensorDataResponse struct {
|
||||
SensorID string `json:"sensorId"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Values map[string]any `json:"values"`
|
||||
}
|
||||
|
||||
// SensorListResponse 传感器列表响应
|
||||
type SensorListResponse struct {
|
||||
Sensors []SensorDataResponse `json:"sensors"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
// ===== 系统管理相关模型 =====
|
||||
|
||||
// SystemStatusResponse 系统状态响应
|
||||
type SystemStatusResponse struct {
|
||||
TotalDevices int `json:"totalDevices"`
|
||||
ActiveDevices int `json:"activeDevices"`
|
||||
SupportedModels []string `json:"supportedModels"`
|
||||
Devices map[string]DeviceInfo `json:"devices"`
|
||||
Uptime time.Duration `json:"uptime"`
|
||||
}
|
||||
|
||||
// SupportedModelsResponse 支持的设备型号响应
|
||||
type SupportedModelsResponse struct {
|
||||
Models []string `json:"models"`
|
||||
Total int `json:"total"`
|
||||
}
|
||||
|
||||
// HealthResponse 健康检查响应
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
Version string `json:"version,omitempty"`
|
||||
}
|
84
api2/router.go
Normal file
84
api2/router.go
Normal file
@ -0,0 +1,84 @@
|
||||
package api2
|
||||
|
||||
import (
|
||||
"hands/device"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Server API v2 服务器结构体
|
||||
type Server struct {
|
||||
deviceManager *device.DeviceManager
|
||||
startTime time.Time
|
||||
version string
|
||||
}
|
||||
|
||||
// NewServer 创建新的 API v2 服务器实例
|
||||
func NewServer(deviceManager *device.DeviceManager) *Server {
|
||||
return &Server{
|
||||
deviceManager: deviceManager,
|
||||
startTime: time.Now(),
|
||||
version: "2.0.0",
|
||||
}
|
||||
}
|
||||
|
||||
// SetupRoutes 设置 API v2 路由
|
||||
func (s *Server) SetupRoutes(r *gin.Engine) {
|
||||
r.StaticFile("/", "./static/index.html")
|
||||
r.Static("/static", "./static")
|
||||
|
||||
// API v2 路由组
|
||||
v2 := r.Group("/api/v2")
|
||||
{
|
||||
// 设备管理路由
|
||||
devices := v2.Group("/devices")
|
||||
{
|
||||
devices.GET("", s.handleGetDevices) // 获取所有设备列表
|
||||
devices.POST("", s.handleCreateDevice) // 创建新设备
|
||||
devices.GET("/:deviceId", s.handleGetDevice) // 获取设备详情
|
||||
devices.DELETE("/:deviceId", s.handleDeleteDevice) // 删除设备
|
||||
devices.PUT("/:deviceId/hand-type", s.handleSetHandType) // 设置手型
|
||||
|
||||
// 设备级别的功能路由
|
||||
deviceRoutes := devices.Group("/:deviceId")
|
||||
{
|
||||
// 姿态控制路由
|
||||
poses := deviceRoutes.Group("/poses")
|
||||
{
|
||||
poses.POST("/fingers", s.handleSetFingerPose) // 设置手指姿态
|
||||
poses.POST("/palm", s.handleSetPalmPose) // 设置手掌姿态
|
||||
poses.POST("/preset/:pose", s.handleSetPresetPose) // 设置预设姿势
|
||||
poses.POST("/reset", s.handleResetPose) // 重置姿态
|
||||
}
|
||||
|
||||
// 动画控制路由
|
||||
animations := deviceRoutes.Group("/animations")
|
||||
{
|
||||
animations.GET("", s.handleGetAnimations) // 获取可用动画列表
|
||||
animations.POST("/start", s.handleStartAnimation) // 启动动画
|
||||
animations.POST("/stop", s.handleStopAnimation) // 停止动画
|
||||
animations.GET("/status", s.handleAnimationStatus) // 获取动画状态
|
||||
}
|
||||
|
||||
// 传感器数据路由
|
||||
sensors := deviceRoutes.Group("/sensors")
|
||||
{
|
||||
sensors.GET("", s.handleGetSensors) // 获取所有传感器数据
|
||||
sensors.GET("/:sensorId", s.handleGetSensorData) // 获取特定传感器数据
|
||||
}
|
||||
|
||||
// 设备状态路由
|
||||
deviceRoutes.GET("/status", s.handleGetDeviceStatus) // 获取设备状态
|
||||
}
|
||||
}
|
||||
|
||||
// 系统管理路由
|
||||
system := v2.Group("/system")
|
||||
{
|
||||
system.GET("/models", s.handleGetSupportedModels) // 获取支持的设备型号
|
||||
system.GET("/status", s.handleGetSystemStatus) // 获取系统状态
|
||||
system.GET("/health", s.handleHealthCheck) // 健康检查
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user