Merge pull request #5 from eli-yip/refactor

refactor: Refactor
This commit is contained in:
Su Yang 2025-05-28 00:39:26 +08:00 committed by GitHub
commit 44c73aea3e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
31 changed files with 2305 additions and 1222 deletions

6
.github/dependabot.yml vendored Normal file
View File

@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "weekly"

BIN
.github/logo.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 306 KiB

39
.github/workflows/CodeQL.yaml vendored Normal file
View File

@ -0,0 +1,39 @@
name: "CodeQL"
on:
push:
branches: ["main"]
pull_request:
branches: ["main"]
schedule:
- cron: "0 0 * * *"
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write
strategy:
fail-fast: false
matrix:
language: ["go"]
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
languages: ${{ matrix.language }}
- name: Autobuild
uses: github/codeql-action/autobuild@v3
- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v3
with:
category: "/language:${{matrix.language}}"

55
.github/workflows/release.yaml vendored Normal file
View File

@ -0,0 +1,55 @@
name: Release Dashboard Server
on:
workflow_dispatch:
push:
branches:
- "main"
tags:
- "*.*.*"
env:
GO_VERSION: "1.24"
GO111MODULE: on
permissions:
contents: write
id-token: write
packages: write
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v6
if: success() && startsWith(github.ref, 'refs/tags/')
with:
version: "~> v2"
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_USERNAME: ${{ github.repository_owner }}

115
.goreleaser.yaml Normal file
View File

@ -0,0 +1,115 @@
version: 2
project_name: dashboard-server
before:
hooks:
- go mod tidy
builds:
- id: dashboard-server
goos:
- linux
goarch:
- amd64
- arm64
- arm
goarm:
- "6"
- "7"
ldflags:
- -s -w
archives:
- formats: ["tar.gz"]
files:
- static
- README.md
- README_CN.md
- LICENSE
release:
draft: false
dockers:
- image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-amd64"
dockerfile: Dockerfile.goreleaser
use: buildx
build_flag_templates:
- "--platform=linux/amd64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
goos: linux
goarch: amd64
extra_files:
- static
- image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-arm64"
dockerfile: Dockerfile.goreleaser
use: buildx
build_flag_templates:
- "--platform=linux/arm64"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
goos: linux
goarch: arm64
extra_files:
- static
- image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv6"
dockerfile: Dockerfile.goreleaser
use: buildx
build_flag_templates:
- "--platform=linux/arm/v6"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
goos: linux
goarch: arm
goarm: "6"
extra_files:
- static
- image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv7"
dockerfile: Dockerfile.goreleaser
use: buildx
build_flag_templates:
- "--platform=linux/arm/v7"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
goos: linux
goarch: arm
goarm: "7"
extra_files:
- static
docker_manifests:
- name_template: "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}"
image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-amd64"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-arm64"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv6"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv7"
- name_template: "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:latest"
image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-amd64"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-arm64"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv6"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv7"
- name_template: "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:v{{ .Major }}"
image_templates:
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-amd64"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-arm64"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv6"
- "ghcr.io/{{ .Env.GITHUB_USERNAME }}/dashboard-server:{{ .Tag }}-armv7"

26
Dockerfile Normal file
View File

@ -0,0 +1,26 @@
# ---- Build Stage ----
FROM golang:1.24-alpine AS builder
WORKDIR /app
COPY --link go.mod go.sum ./
RUN go mod download
COPY --link . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o dashboard-server .
# ---- Runtime Stage ----
FROM alpine:3.21
WORKDIR /app
COPY --link static/ ./static/
COPY --link --from=builder /app/dashboard-server /usr/local/bin/dashboard-server
EXPOSE 9099
ENV SERVER_PORT="9099"
CMD ["dashboard-server"]

10
Dockerfile.goreleaser Normal file
View File

@ -0,0 +1,10 @@
FROM alpine:3.21
COPY --link dashboard-server /usr/local/bin/dashboard-server
COPY --link static/ ./static/
EXPOSE 9099
ENV SERVER_PORT="9099"
ENTRYPOINT ["dashboard-server"]

View File

@ -8,7 +8,7 @@
* **动态手型配置**:支持左手和右手手型的动态切换。
* **灵活接口配置**:支持多种 CAN 接口(如 `can0`, `can1`),可通过命令行参数或环境变量动态设置。
* **手指与掌部姿态控制**提供手指6字节和掌部4字节姿态数据发送功能。
* **手指与掌部姿态控制**提供手指6 字节和掌部4 字节)姿态数据发送功能。
* **预设动作执行**:内置丰富的手势动作,如握拳、张开、捏取、点赞、数字手势等。
* **实时动画控制**:支持波浪、横向摆动等动画效果,用户可动态启动和停止。
* **传感器数据实时监控**:提供接口压力数据的实时模拟和更新。

468
api/handler.go Normal file
View File

@ -0,0 +1,468 @@
package api
import (
"fmt"
"hands/config"
"hands/define"
"hands/hands"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// 手型设置处理函数
func HandleHandType(c *gin.Context) {
var req HandTypeRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "无效的手型设置请求:" + err.Error(),
})
return
}
// 验证接口
if !config.IsValidInterface(req.Interface) {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces),
})
return
}
// 验证手型 ID
if req.HandType == "left" && req.HandId != define.HAND_TYPE_LEFT {
req.HandId = define.HAND_TYPE_LEFT
} else if req.HandType == "right" && req.HandId != define.HAND_TYPE_RIGHT {
req.HandId = define.HAND_TYPE_RIGHT
}
// 设置手型配置
hands.SetHandConfig(req.Interface, req.HandType, req.HandId)
handTypeName := "右手"
if req.HandType == "left" {
handTypeName = "左手"
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: fmt.Sprintf("接口 %s 手型已设置为%s (0x%X)", req.Interface, handTypeName, req.HandId),
Data: map[string]any{
"interface": req.Interface,
"handType": req.HandType,
"handId": req.HandId,
},
})
}
// 手指姿态处理函数
func HandleFingers(c *gin.Context) {
var req FingerPoseRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "无效的手指姿态数据:" + err.Error(),
})
return
}
// 验证每个值是否在范围内
for _, v := range req.Pose {
if v < 0 || v > 255 {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "手指姿态值必须在 0-255 范围内",
})
return
}
}
// 如果未指定接口,使用默认接口
if req.Interface == "" {
req.Interface = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(req.Interface) {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces),
})
return
}
hands.StopAllAnimations(req.Interface)
if err := hands.SendFingerPose(req.Interface, req.Pose, req.HandType, req.HandId); err != nil {
c.JSON(http.StatusInternalServerError, define.ApiResponse{
Status: "error",
Error: "发送手指姿态失败:" + err.Error(),
})
return
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: "手指姿态指令发送成功",
Data: map[string]any{"interface": req.Interface, "pose": req.Pose},
})
}
// 掌部姿态处理函数
func HandlePalm(c *gin.Context) {
var req PalmPoseRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "无效的掌部姿态数据:" + err.Error(),
})
return
}
// 验证每个值是否在范围内
for _, v := range req.Pose {
if v < 0 || v > 255 {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "掌部姿态值必须在 0-255 范围内",
})
return
}
}
// 如果未指定接口,使用默认接口
if req.Interface == "" {
req.Interface = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(req.Interface) {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces),
})
return
}
hands.StopAllAnimations(req.Interface)
if err := hands.SendPalmPose(req.Interface, req.Pose, req.HandType, req.HandId); err != nil {
c.JSON(http.StatusInternalServerError, define.ApiResponse{
Status: "error",
Error: "发送掌部姿态失败:" + err.Error(),
})
return
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: "掌部姿态指令发送成功",
Data: map[string]any{"interface": req.Interface, "pose": req.Pose},
})
}
// 预设姿势处理函数
func HandlePreset(c *gin.Context) {
pose := c.Param("pose")
// 从查询参数获取接口名称和手型
ifName := c.Query("interface")
handType := c.Query("handType")
if ifName == "" {
ifName = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(ifName) {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces),
})
return
}
hands.StopAllAnimations(ifName)
var fingerPose []byte
var message string
switch pose {
case "fist":
fingerPose = []byte{64, 64, 64, 64, 64, 64}
message = "已设置握拳姿势"
case "open":
fingerPose = []byte{192, 192, 192, 192, 192, 192}
message = "已设置完全张开姿势"
case "pinch":
fingerPose = []byte{120, 120, 64, 64, 64, 64}
message = "已设置捏取姿势"
case "thumbsup":
fingerPose = []byte{64, 192, 192, 192, 192, 64}
message = "已设置竖起大拇指姿势"
case "point":
fingerPose = []byte{192, 64, 192, 192, 192, 64}
message = "已设置食指指点姿势"
// 数字手势
case "1":
fingerPose = []byte{192, 64, 192, 192, 192, 64}
message = "已设置数字 1 手势"
case "2":
fingerPose = []byte{192, 64, 64, 192, 192, 64}
message = "已设置数字 2 手势"
case "3":
fingerPose = []byte{192, 64, 64, 64, 192, 64}
message = "已设置数字 3 手势"
case "4":
fingerPose = []byte{192, 64, 64, 64, 64, 64}
message = "已设置数字 4 手势"
case "5":
fingerPose = []byte{192, 192, 192, 192, 192, 192}
message = "已设置数字 5 手势"
case "6":
fingerPose = []byte{64, 192, 192, 192, 192, 64}
message = "已设置数字 6 手势"
case "7":
fingerPose = []byte{64, 64, 192, 192, 192, 64}
message = "已设置数字 7 手势"
case "8":
fingerPose = []byte{64, 64, 64, 192, 192, 64}
message = "已设置数字 8 手势"
case "9":
fingerPose = []byte{64, 64, 64, 64, 192, 64}
message = "已设置数字 9 手势"
default:
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "无效的预设姿势",
})
return
}
// 解析手型 ID从查询参数或使用接口配置
handId := uint32(0)
if handType != "" {
handId = hands.ParseHandType(handType, 0, ifName)
}
if err := hands.SendFingerPose(ifName, fingerPose, handType, handId); err != nil {
c.JSON(http.StatusInternalServerError, define.ApiResponse{
Status: "error",
Error: "设置预设姿势失败:" + err.Error(),
})
return
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: message,
Data: map[string]any{"interface": ifName, "pose": fingerPose},
})
}
// 动画控制处理函数
func HandleAnimation(c *gin.Context) {
var req AnimationRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "无效的动画请求:" + err.Error(),
})
return
}
// 如果未指定接口,使用默认接口
if req.Interface == "" {
req.Interface = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(req.Interface) {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", req.Interface, config.Config.AvailableInterfaces),
})
return
}
// 停止当前动画
hands.StopAllAnimations(req.Interface)
// 如果是停止命令,直接返回
if req.Type == "stop" {
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: fmt.Sprintf("%s 动画已停止", req.Interface),
})
return
}
// 处理速度参数
if req.Speed <= 0 {
req.Speed = 500 // 默认速度
}
// 根据类型启动动画
switch req.Type {
case "wave":
hands.StartWaveAnimation(req.Interface, req.Speed, req.HandType, req.HandId)
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: fmt.Sprintf("%s 波浪动画已启动", req.Interface),
Data: map[string]any{"interface": req.Interface, "speed": req.Speed},
})
case "sway":
hands.StartSwayAnimation(req.Interface, req.Speed, req.HandType, req.HandId)
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: fmt.Sprintf("%s 横向摆动动画已启动", req.Interface),
Data: map[string]any{"interface": req.Interface, "speed": req.Speed},
})
default:
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: "无效的动画类型",
})
}
}
// 获取传感器数据处理函数
func HandleSensors(c *gin.Context) {
// 从查询参数获取接口名称
ifName := c.Query("interface")
hands.SensorMutex.RLock()
defer hands.SensorMutex.RUnlock()
if ifName != "" {
// 验证接口
if !config.IsValidInterface(ifName) {
c.JSON(http.StatusBadRequest, define.ApiResponse{
Status: "error",
Error: fmt.Sprintf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces),
})
return
}
// 请求特定接口的数据
if sensorData, ok := hands.SensorDataMap[ifName]; ok {
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Data: sensorData,
})
} else {
c.JSON(http.StatusInternalServerError, define.ApiResponse{
Status: "error",
Error: "传感器数据不存在",
})
}
} else {
// 返回所有接口的数据
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Data: hands.SensorDataMap,
})
}
}
// 系统状态处理函数
func HandleStatus(c *gin.Context) {
hands.AnimationMutex.Lock()
animationStatus := make(map[string]bool)
for _, ifName := range config.Config.AvailableInterfaces {
animationStatus[ifName] = hands.AnimationActive[ifName]
}
hands.AnimationMutex.Unlock()
// 检查 CAN 服务状态
canStatus := hands.CheckCanServiceStatus()
// 获取手型配置
hands.HandConfigMutex.RLock()
handConfigsData := make(map[string]any)
for ifName, handConfig := range hands.HandConfigs {
handConfigsData[ifName] = map[string]any{
"handType": handConfig.HandType,
"handId": handConfig.HandId,
}
}
hands.HandConfigMutex.RUnlock()
interfaceStatuses := make(map[string]any)
for _, ifName := range config.Config.AvailableInterfaces {
interfaceStatuses[ifName] = map[string]any{
"active": canStatus[ifName],
"animationActive": animationStatus[ifName],
"handConfig": handConfigsData[ifName],
}
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Data: map[string]any{
"interfaces": interfaceStatuses,
"uptime": time.Since(ServerStartTime).String(),
"canServiceURL": config.Config.CanServiceURL,
"defaultInterface": config.Config.DefaultInterface,
"availableInterfaces": config.Config.AvailableInterfaces,
"activeInterfaces": len(canStatus),
"handConfigs": handConfigsData,
},
})
}
// 获取可用接口列表处理函数
func HandleInterfaces(c *gin.Context) {
responseData := map[string]any{
"availableInterfaces": config.Config.AvailableInterfaces,
"defaultInterface": config.Config.DefaultInterface,
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Data: responseData,
})
}
// 获取手型配置处理函数
func HandleHandConfigs(c *gin.Context) {
hands.HandConfigMutex.RLock()
defer hands.HandConfigMutex.RUnlock()
result := make(map[string]any)
for _, ifName := range config.Config.AvailableInterfaces {
if handConfig, exists := hands.HandConfigs[ifName]; exists {
result[ifName] = map[string]any{
"handType": handConfig.HandType,
"handId": handConfig.HandId,
}
} else {
// 返回默认配置
result[ifName] = map[string]any{
"handType": "right",
"handId": define.HAND_TYPE_RIGHT,
}
}
}
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Data: result,
})
}
// 健康检查处理函数
func HandleHealth(c *gin.Context) {
c.JSON(http.StatusOK, define.ApiResponse{
Status: "success",
Message: "CAN Control Service is running",
Data: map[string]any{
"timestamp": time.Now(),
"availableInterfaces": config.Config.AvailableInterfaces,
"defaultInterface": config.Config.DefaultInterface,
"serviceVersion": "1.0.0-hand-type-support",
},
})
}

29
api/models.go Normal file
View File

@ -0,0 +1,29 @@
package api
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
}
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
}
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
}
type HandTypeRequest struct {
Interface string `json:"interface" binding:"required"`
HandType string `json:"handType" binding:"required,oneof=left right"`
HandId uint32 `json:"handId" binding:"required"`
}

50
api/router.go Normal file
View File

@ -0,0 +1,50 @@
package api
import (
"time"
"github.com/gin-gonic/gin"
)
// 全局变量
var (
ServerStartTime time.Time
)
func SetupRoutes(r *gin.Engine) {
r.StaticFile("/", "./static/index.html")
r.Static("/static", "./static")
api := r.Group("/api")
{
// 手型设置 API
api.POST("/hand-type", HandleHandType)
// 手指姿态 API
api.POST("/fingers", HandleFingers)
// 掌部姿态 API
api.POST("/palm", HandlePalm)
// 预设姿势 API
api.POST("/preset/:pose", HandlePreset)
// 动画控制 API
api.POST("/animation", HandleAnimation)
// 获取传感器数据 API
api.GET("/sensors", HandleSensors)
// 系统状态 API
api.GET("/status", HandleStatus)
// 获取可用接口列表 API
api.GET("/interfaces", HandleInterfaces)
// 获取手型配置 API
api.GET("/hand-configs", HandleHandConfigs)
// 健康检查端点
api.GET("/health", HandleHealth)
}
}

View File

@ -19,7 +19,7 @@ func ParseConfig() *define.Config {
flag.StringVar(&cfg.CanServiceURL, "can-url", "http://127.0.0.1:5260", "CAN 服务的 URL")
flag.StringVar(&cfg.WebPort, "port", "9099", "Web 服务的端口")
flag.StringVar(&cfg.DefaultInterface, "interface", "", "默认 CAN 接口")
flag.StringVar(&canInterfacesFlag, "can-interfaces", "", "支持的 CAN 接口列表,用逗号分隔 (例如: can0,can1,vcan0)")
flag.StringVar(&canInterfacesFlag, "can-interfaces", "", "支持的 CAN 接口列表,用逗号分隔 (例如can0,can1,vcan0)")
flag.Parse()
// 环境变量覆盖命令行参数
@ -45,7 +45,7 @@ func ParseConfig() *define.Config {
}
}
// 如果没有指定可用接口,从CAN服务获取
// 如果没有指定可用接口,从 CAN 服务获取
if len(cfg.AvailableInterfaces) == 0 {
log.Println("🔍 未指定可用接口,将从 CAN 服务获取...")
cfg.AvailableInterfaces = getAvailableInterfacesFromCanService(cfg.CanServiceURL)
@ -59,7 +59,7 @@ func ParseConfig() *define.Config {
return cfg
}
// 从CAN服务获取可用接口
// 从 CAN 服务获取可用接口
func getAvailableInterfacesFromCanService(canServiceURL string) []string {
resp, err := http.Get(canServiceURL + "/api/interfaces")
if err != nil {

12
config/config.go Normal file
View File

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

View File

@ -10,8 +10,8 @@ type Config struct {
// API 响应结构体
type ApiResponse struct {
Status string `json:"status"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Data interface{} `json:"data,omitempty"`
Status string `json:"status"`
Message string `json:"message,omitempty"`
Error string `json:"error,omitempty"`
Data any `json:"data,omitempty"`
}

8
define/hands.go Normal file
View File

@ -0,0 +1,8 @@
package define
type HandType int
const (
HAND_TYPE_LEFT HandType = 0x28
HAND_TYPE_RIGHT HandType = 0x27
)

268
hands/animation.go Normal file
View File

@ -0,0 +1,268 @@
package hands
import (
"hands/config"
"log"
"sync"
"time"
)
var (
AnimationActive map[string]bool // 每个接口的动画状态
AnimationMutex sync.Mutex
StopAnimationMap map[string]chan struct{} // 每个接口的停止动画通道
)
func initAnimation() {
// 初始化动画状态映射
AnimationActive = make(map[string]bool)
StopAnimationMap = make(map[string]chan struct{})
for _, ifName := range config.Config.AvailableInterfaces {
AnimationActive[ifName] = false
StopAnimationMap[ifName] = make(chan struct{}, 1)
}
}
// 执行波浪动画 - 支持手型参数
func StartWaveAnimation(ifName string, speed int, handType string, handId uint32) {
if speed <= 0 {
speed = 500 // 默认速度
}
// 如果未指定接口,使用默认接口
if ifName == "" {
ifName = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(ifName) {
log.Printf("❌ 无法启动波浪动画: 无效的接口 %s", ifName)
return
}
AnimationMutex.Lock()
// 如果已经有动画在运行,先停止它
if AnimationActive[ifName] {
select {
case StopAnimationMap[ifName] <- struct{}{}:
// 发送成功
default:
// 通道已满,无需发送
}
StopAnimationMap[ifName] = make(chan struct{}, 1)
}
AnimationActive[ifName] = true
AnimationMutex.Unlock()
currentStopChannel := StopAnimationMap[ifName]
go func() {
defer func() {
AnimationMutex.Lock()
AnimationActive[ifName] = false
AnimationMutex.Unlock()
log.Printf("👋 %s 波浪动画已完成", ifName)
}()
fingerOrder := []int{0, 1, 2, 3, 4, 5}
open := byte(64) // 0x40
close := byte(192) // 0xC0
log.Printf("🚀 开始 %s 波浪动画", ifName)
// 动画循环
for {
select {
case <-currentStopChannel:
log.Printf("🛑 %s 波浪动画被用户停止", ifName)
return
default:
// 波浪张开
for _, idx := range fingerOrder {
pose := make([]byte, 6)
for j := 0; j < 6; j++ {
if j == idx {
pose[j] = open
} else {
pose[j] = close
}
}
if err := SendFingerPose(ifName, pose, handType, handId); err != nil {
log.Printf("%s 动画发送失败: %v", ifName, err)
return
}
delay := time.Duration(speed) * time.Millisecond
select {
case <-currentStopChannel:
log.Printf("🛑 %s 波浪动画被用户停止", ifName)
return
case <-time.After(delay):
// 继续执行
}
}
// 波浪握拳
for _, idx := range fingerOrder {
pose := make([]byte, 6)
for j := 0; j < 6; j++ {
if j == idx {
pose[j] = close
} else {
pose[j] = open
}
}
if err := SendFingerPose(ifName, pose, handType, handId); err != nil {
log.Printf("%s 动画发送失败: %v", ifName, err)
return
}
delay := time.Duration(speed) * time.Millisecond
select {
case <-currentStopChannel:
log.Printf("🛑 %s 波浪动画被用户停止", ifName)
return
case <-time.After(delay):
// 继续执行
}
}
}
}
}()
}
// 执行横向摆动动画 - 支持手型参数
func StartSwayAnimation(ifName string, speed int, handType string, handId uint32) {
if speed <= 0 {
speed = 500 // 默认速度
}
// 如果未指定接口,使用默认接口
if ifName == "" {
ifName = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(ifName) {
log.Printf("❌ 无法启动摆动动画: 无效的接口 %s", ifName)
return
}
AnimationMutex.Lock()
if AnimationActive[ifName] {
select {
case StopAnimationMap[ifName] <- struct{}{}:
// 发送成功
default:
// 通道已满,无需发送
}
StopAnimationMap[ifName] = make(chan struct{}, 1)
}
AnimationActive[ifName] = true
AnimationMutex.Unlock()
currentStopChannel := StopAnimationMap[ifName]
go func() {
defer func() {
AnimationMutex.Lock()
AnimationActive[ifName] = false
AnimationMutex.Unlock()
log.Printf("🔄 %s 横向摆动动画已完成", ifName)
}()
leftPose := []byte{48, 48, 48, 48} // 0x30
rightPose := []byte{208, 208, 208, 208} // 0xD0
log.Printf("🚀 开始 %s 横向摆动动画", ifName)
// 动画循环
for {
select {
case <-currentStopChannel:
log.Printf("🛑 %s 横向摆动动画被用户停止", ifName)
return
default:
// 向左移动
if err := SendPalmPose(ifName, leftPose, handType, handId); err != nil {
log.Printf("%s 动画发送失败: %v", ifName, err)
return
}
delay := time.Duration(speed) * time.Millisecond
select {
case <-currentStopChannel:
log.Printf("🛑 %s 横向摆动动画被用户停止", ifName)
return
case <-time.After(delay):
// 继续执行
}
// 向右移动
if err := SendPalmPose(ifName, rightPose, handType, handId); err != nil {
log.Printf("%s 动画发送失败: %v", ifName, err)
return
}
select {
case <-currentStopChannel:
log.Printf("🛑 %s 横向摆动动画被用户停止", ifName)
return
case <-time.After(delay):
// 继续执行
}
}
}
}()
}
// 停止所有动画
func StopAllAnimations(ifName string) {
// 如果未指定接口,停止所有接口的动画
if ifName == "" {
for _, validIface := range config.Config.AvailableInterfaces {
StopAllAnimations(validIface)
}
return
}
// 验证接口
if !config.IsValidInterface(ifName) {
log.Printf("⚠️ 尝试停止无效接口的动画: %s", ifName)
return
}
AnimationMutex.Lock()
defer AnimationMutex.Unlock()
if AnimationActive[ifName] {
select {
case StopAnimationMap[ifName] <- struct{}{}:
log.Printf("✅ 已发送停止 %s 动画信号", ifName)
default:
StopAnimationMap[ifName] = make(chan struct{}, 1)
StopAnimationMap[ifName] <- struct{}{}
log.Printf("⚠️ %s 通道重置后发送了停止信号", ifName)
}
AnimationActive[ifName] = false
go func() {
time.Sleep(100 * time.Millisecond)
resetToDefaultPose(ifName)
}()
} else {
log.Printf(" %s 当前没有运行中的动画", ifName)
}
}

95
hands/can.go Normal file
View File

@ -0,0 +1,95 @@
package hands
import (
"bytes"
"encoding/json"
"fmt"
"hands/config"
"hands/define"
"log"
"net/http"
)
type CanMessage struct {
Interface string `json:"interface"`
ID uint32 `json:"id"`
Data []byte `json:"data"`
}
// 检查 CAN 服务状态
func CheckCanServiceStatus() map[string]bool {
resp, err := http.Get(config.Config.CanServiceURL + "/api/status")
if err != nil {
log.Printf("❌ CAN 服务状态检查失败: %v", err)
result := make(map[string]bool)
for _, ifName := range config.Config.AvailableInterfaces {
result[ifName] = false
}
return result
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Printf("❌ CAN 服务返回非正常状态:%d", resp.StatusCode)
result := make(map[string]bool)
for _, ifName := range config.Config.AvailableInterfaces {
result[ifName] = false
}
return result
}
var statusResp define.ApiResponse
if err := json.NewDecoder(resp.Body).Decode(&statusResp); err != nil {
log.Printf("❌ 解析 CAN 服务状态失败: %v", err)
result := make(map[string]bool)
for _, ifName := range config.Config.AvailableInterfaces {
result[ifName] = false
}
return result
}
// 检查状态数据
result := make(map[string]bool)
for _, ifName := range config.Config.AvailableInterfaces {
result[ifName] = false
}
// 从响应中获取各接口状态
if statusData, ok := statusResp.Data.(map[string]interface{}); ok {
if interfaces, ok := statusData["interfaces"].(map[string]interface{}); ok {
for ifName, ifStatus := range interfaces {
if status, ok := ifStatus.(map[string]interface{}); ok {
if active, ok := status["active"].(bool); ok {
result[ifName] = active
}
}
}
}
}
return result
}
// 发送请求到 CAN 服务
func sendToCanService(msg CanMessage) error {
jsonData, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("JSON 编码错误: %v", err)
}
resp, err := http.Post(config.Config.CanServiceURL+"/api/can", "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("CAN 服务请求失败: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var errResp define.ApiResponse
if err := json.NewDecoder(resp.Body).Decode(&errResp); err != nil {
return fmt.Errorf("CAN 服务返回错误HTTP %d", resp.StatusCode)
}
return fmt.Errorf("CAN 服务返回错误: %s", errResp.Error)
}
return nil
}

241
hands/hands.go Normal file
View File

@ -0,0 +1,241 @@
package hands
import (
"fmt"
"hands/config"
"hands/define"
"log"
"math/rand/v2"
"strings"
"sync"
"time"
)
// 手型配置结构体
type HandConfig struct {
HandType string `json:"handType"`
HandId uint32 `json:"handId"`
}
var (
HandConfigMutex sync.RWMutex
HandConfigs map[string]*HandConfig // 每个接口的手型配置
)
func Init() {
initSensorData()
initAnimation()
initHands()
}
func initHands() {
HandConfigs = make(map[string]*HandConfig)
}
func SetHandConfig(ifName, handType string, handId uint32) {
HandConfigMutex.Lock()
defer HandConfigMutex.Unlock()
HandConfigs[ifName] = &HandConfig{
HandType: handType,
HandId: handId,
}
log.Printf("🔧 接口 %s 手型配置已更新: %s (0x%X)", ifName, handType, handId)
}
func GetHandConfig(ifName string) *HandConfig {
HandConfigMutex.RLock()
if handConfig, exists := HandConfigs[ifName]; exists {
HandConfigMutex.RUnlock()
return handConfig
}
HandConfigMutex.RUnlock()
// 创建默认配置
HandConfigMutex.Lock()
defer HandConfigMutex.Unlock()
// 再次检查(双重检查锁定)
if handConfig, exists := HandConfigs[ifName]; exists {
return handConfig
}
// 创建默认配置(右手)
HandConfigs[ifName] = &HandConfig{
HandType: "right",
HandId: define.HAND_TYPE_RIGHT,
}
log.Printf("🆕 为接口 %s 创建默认手型配置: 右手 (0x%X)", ifName, define.HAND_TYPE_RIGHT)
return HandConfigs[ifName]
}
// 解析手型参数
func ParseHandType(handType string, handId uint32, ifName string) uint32 {
// 如果提供了有效的 handId直接使用
if handId != 0 {
return handId
}
// 根据 handType 字符串确定 ID
switch strings.ToLower(handType) {
case "left":
return define.HAND_TYPE_LEFT
case "right":
return define.HAND_TYPE_RIGHT
default:
// 使用接口的配置
handConfig := GetHandConfig(ifName)
return handConfig.HandId
}
}
// 发送手指姿态指令 - 支持手型参数
func SendFingerPose(ifName string, pose []byte, handType string, handId uint32) error {
if len(pose) != 6 {
return fmt.Errorf("无效的姿态数据长度,需要 6 个字节")
}
// 如果未指定接口,使用默认接口
if ifName == "" {
ifName = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(ifName) {
return fmt.Errorf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces)
}
// 解析手型 ID
canId := ParseHandType(handType, handId, ifName)
// 添加随机扰动
perturbedPose := make([]byte, len(pose))
for i, v := range pose {
perturbedPose[i] = perturb(v, 5)
}
// 构造 CAN 消息
msg := CanMessage{
Interface: ifName,
ID: canId, // 使用动态的手型 ID
Data: append([]byte{0x01}, perturbedPose...),
}
err := sendToCanService(msg)
if err == nil {
handTypeName := "右手"
if canId == define.HAND_TYPE_LEFT {
handTypeName = "左手"
}
log.Printf("✅ %s (%s, 0x%X) 手指动作已发送: [%X %X %X %X %X %X]",
ifName, handTypeName, canId, perturbedPose[0], perturbedPose[1], perturbedPose[2],
perturbedPose[3], perturbedPose[4], perturbedPose[5])
} else {
log.Printf("❌ %s 手指控制发送失败: %v", ifName, err)
}
return err
}
// 在 base 基础上进行 ±delta 的扰动,范围限制在 [0, 255]
func perturb(base byte, delta int) byte {
offset := rand.IntN(2*delta+1) - delta
v := int(base) + offset
if v < 0 {
v = 0
}
if v > 255 {
v = 255
}
return byte(v)
}
// 发送掌部姿态指令 - 支持手型参数
func SendPalmPose(ifName string, pose []byte, handType string, handId uint32) error {
if len(pose) != 4 {
return fmt.Errorf("无效的姿态数据长度,需要 4 个字节")
}
// 如果未指定接口,使用默认接口
if ifName == "" {
ifName = config.Config.DefaultInterface
}
// 验证接口
if !config.IsValidInterface(ifName) {
return fmt.Errorf("无效的接口 %s可用接口: %v", ifName, config.Config.AvailableInterfaces)
}
// 解析手型 ID
canId := ParseHandType(handType, handId, ifName)
// 添加随机扰动
perturbedPose := make([]byte, len(pose))
for i, v := range pose {
perturbedPose[i] = perturb(v, 8)
}
// 构造 CAN 消息
msg := CanMessage{
Interface: ifName,
ID: canId, // 使用动态的手型 ID
Data: append([]byte{0x04}, perturbedPose...),
}
err := sendToCanService(msg)
if err == nil {
handTypeName := "右手"
if canId == define.HAND_TYPE_LEFT {
handTypeName = "左手"
}
log.Printf("✅ %s (%s, 0x%X) 掌部姿态已发送: [%X %X %X %X]",
ifName, handTypeName, canId, perturbedPose[0], perturbedPose[1], perturbedPose[2], perturbedPose[3])
// 更新传感器数据中的掌部位置
SensorMutex.Lock()
if sensorData, exists := SensorDataMap[ifName]; exists {
copy(sensorData.PalmPosition, perturbedPose)
sensorData.LastUpdate = time.Now()
}
SensorMutex.Unlock()
} else {
log.Printf("❌ %s 掌部控制发送失败: %v", ifName, err)
}
return err
}
// 重置到默认姿势
func resetToDefaultPose(ifName string) {
// 如果未指定接口,重置所有接口
if ifName == "" {
for _, validIface := range config.Config.AvailableInterfaces {
resetToDefaultPose(validIface)
}
return
}
// 验证接口
if !config.IsValidInterface(ifName) {
log.Printf("⚠️ 尝试重置无效接口: %s", ifName)
return
}
defaultFingerPose := []byte{64, 64, 64, 64, 64, 64}
defaultPalmPose := []byte{128, 128, 128, 128}
// 获取当前接口的手型配置
handConfig := GetHandConfig(ifName)
if err := SendFingerPose(ifName, defaultFingerPose, handConfig.HandType, handConfig.HandId); err != nil {
log.Printf("%s 重置手指姿势失败: %v", ifName, err)
}
if err := SendPalmPose(ifName, defaultPalmPose, handConfig.HandType, handConfig.HandId); err != nil {
log.Printf("%s 重置掌部姿势失败: %v", ifName, err)
}
log.Printf("✅ 已重置 %s 到默认姿势", ifName)
}

65
hands/sensor.go Normal file
View File

@ -0,0 +1,65 @@
package hands
import (
"hands/config"
"math/rand/v2"
"sync"
"time"
)
// 传感器数据结构体
type SensorData struct {
Interface string `json:"interface"`
Thumb int `json:"thumb"`
Index int `json:"index"`
Middle int `json:"middle"`
Ring int `json:"ring"`
Pinky int `json:"pinky"`
PalmPosition []byte `json:"palmPosition"`
LastUpdate time.Time `json:"lastUpdate"`
}
var (
SensorDataMap map[string]*SensorData // 每个接口的传感器数据
SensorMutex sync.RWMutex
)
func initSensorData() {
// 初始化传感器数据映射
SensorDataMap = make(map[string]*SensorData)
for _, ifName := range config.Config.AvailableInterfaces {
SensorDataMap[ifName] = &SensorData{
Interface: ifName,
Thumb: 0,
Index: 0,
Middle: 0,
Ring: 0,
Pinky: 0,
PalmPosition: []byte{128, 128, 128, 128},
LastUpdate: time.Now(),
}
}
}
// 读取传感器数据 (模拟)
func ReadSensorData() {
go func() {
for {
SensorMutex.Lock()
// 为每个接口模拟压力数据 (0-100)
for _, ifName := range config.Config.AvailableInterfaces {
if sensorData, exists := SensorDataMap[ifName]; exists {
sensorData.Thumb = rand.IntN(101)
sensorData.Index = rand.IntN(101)
sensorData.Middle = rand.IntN(101)
sensorData.Ring = rand.IntN(101)
sensorData.Pinky = rand.IntN(101)
sensorData.LastUpdate = time.Now()
}
}
SensorMutex.Unlock()
time.Sleep(500 * time.Millisecond)
}
}()
}

1172
main.go

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,122 @@
package communication
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
// TODO: ID 的作用是什么
// RawMessage 代表发送给 can-bridge 服务或从其接收的原始消息结构
type RawMessage struct {
Interface string `json:"interface"` // 目标 CAN 接口名,例如 "can0", "vcan1"
ID uint32 `json:"id"` // CAN 帧的 ID
Data []byte `json:"data"` // CAN 帧的数据负载
}
// Communicator 定义了与 can-bridge Web 服务进行通信的接口
type Communicator interface {
// SendMessage 将 RawMessage 通过 HTTP POST 请求发送到 can-bridge 服务
SendMessage(msg RawMessage) error
// GetInterfaceStatus 获取指定 CAN 接口的状态
GetInterfaceStatus(ifName string) (isActive bool, err error)
// GetAllInterfaceStatuses 获取所有已知 CAN 接口的状态
GetAllInterfaceStatuses() (statuses map[string]bool, err error)
// SetServiceURL 设置 can-bridge 服务的 URL
SetServiceURL(url string)
// IsConnected 检查与 can-bridge 服务的连接状态
IsConnected() bool
}
// CanBridgeClient 实现与 can-bridge 服务的 HTTP 通信
type CanBridgeClient struct {
serviceURL string
client *http.Client
}
func NewCanBridgeClient(serviceURL string) Communicator {
return &CanBridgeClient{
serviceURL: serviceURL,
client: &http.Client{
Timeout: 5 * time.Second,
},
}
}
func (c *CanBridgeClient) SendMessage(msg RawMessage) error {
jsonData, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("序列化消息失败:%w", err)
}
url := fmt.Sprintf("%s/api/can", c.serviceURL)
resp, err := c.client.Post(url, "application/json", bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("发送 HTTP 请求失败:%w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("can-bridge服务返回错误: %d, %s", resp.StatusCode, string(body))
}
return nil
}
func (c *CanBridgeClient) GetInterfaceStatus(ifName string) (bool, error) {
url := fmt.Sprintf("%s/api/status/%s", c.serviceURL, ifName)
resp, err := c.client.Get(url)
if err != nil {
return false, fmt.Errorf("获取接口状态失败:%w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return false, fmt.Errorf("can-bridge 服务返回错误:%d", resp.StatusCode)
}
var status struct {
Active bool `json:"active"`
}
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
return false, fmt.Errorf("解析状态响应失败:%w", err)
}
return status.Active, nil
}
func (c *CanBridgeClient) GetAllInterfaceStatuses() (map[string]bool, error) {
url := fmt.Sprintf("%s/api/status", c.serviceURL)
resp, err := c.client.Get(url)
if err != nil {
return nil, fmt.Errorf("获取所有接口状态失败:%w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("can-bridge 服务返回错误:%d", resp.StatusCode)
}
var statuses map[string]bool
if err := json.NewDecoder(resp.Body).Decode(&statuses); err != nil {
return nil, fmt.Errorf("解析状态响应失败:%w", err)
}
return statuses, nil
}
func (c *CanBridgeClient) SetServiceURL(url string) { c.serviceURL = url }
func (c *CanBridgeClient) IsConnected() bool {
_, err := c.GetAllInterfaceStatuses()
return err == nil
}

View File

@ -0,0 +1,78 @@
package component
import (
"fmt"
"hands/pkg/device"
"math/rand/v2"
"time"
)
// PressureSensor 压力传感器实现
type PressureSensor struct {
id string
config map[string]any
isActive bool
samplingRate int
lastReading time.Time
}
func NewPressureSensor(id string, config map[string]any) *PressureSensor {
return &PressureSensor{
id: id,
config: config,
isActive: true,
samplingRate: 100,
lastReading: time.Now(),
}
}
func (p *PressureSensor) GetID() string {
return p.id
}
func (p *PressureSensor) GetType() device.ComponentType {
return device.SensorComponent
}
func (p *PressureSensor) GetConfiguration() map[string]any {
return p.config
}
func (p *PressureSensor) IsActive() bool {
return p.isActive
}
func (p *PressureSensor) ReadData() (device.SensorData, error) {
if !p.isActive {
return nil, fmt.Errorf("传感器 %s 未激活", p.id)
}
// 模拟压力数据读取
// 在实际实现中,这里应该从 can-bridge 或其他数据源读取真实数据
pressure := rand.Float64() * 100 // 0-100 的随机压力值
values := map[string]any{
"pressure": pressure,
"unit": "kPa",
"location": p.config["location"],
}
p.lastReading = time.Now()
return NewSensorData(p.id, values), nil
}
func (p *PressureSensor) GetDataType() string {
return "pressure"
}
func (p *PressureSensor) GetSamplingRate() int {
return p.samplingRate
}
func (p *PressureSensor) SetSamplingRate(rate int) error {
if rate <= 0 || rate > 1000 {
return fmt.Errorf("采样率必须在 1-1000Hz 之间")
}
p.samplingRate = rate
return nil
}

42
pkg/component/sensor.go Normal file
View File

@ -0,0 +1,42 @@
package component
import (
"hands/pkg/device"
"time"
)
// Sensor 传感器组件接口
type Sensor interface {
device.Component
ReadData() (device.SensorData, error)
GetDataType() string
GetSamplingRate() int
SetSamplingRate(rate int) error
}
// SensorDataImpl 传感器数据的具体实现
type SensorDataImpl struct {
timestamp time.Time
values map[string]any
sensorID string
}
func NewSensorData(sensorID string, values map[string]any) *SensorDataImpl {
return &SensorDataImpl{
timestamp: time.Now(),
values: values,
sensorID: sensorID,
}
}
func (s *SensorDataImpl) Timestamp() time.Time {
return s.timestamp
}
func (s *SensorDataImpl) Values() map[string]any {
return s.values
}
func (s *SensorDataImpl) SensorID() string {
return s.sensorID
}

80
pkg/device/commands.go Normal file
View File

@ -0,0 +1,80 @@
package device
// FingerPoseCommand 手指姿态指令
type FingerPoseCommand struct {
fingerID string
poseData []byte
targetComp string
}
func NewFingerPoseCommand(fingerID string, poseData []byte) *FingerPoseCommand {
return &FingerPoseCommand{
fingerID: fingerID,
poseData: poseData,
targetComp: "finger_" + fingerID,
}
}
func (c *FingerPoseCommand) Type() string {
return "SetFingerPose"
}
func (c *FingerPoseCommand) Payload() []byte {
return c.poseData
}
func (c *FingerPoseCommand) TargetComponent() string {
return c.targetComp
}
// PalmPoseCommand 手掌姿态指令
type PalmPoseCommand struct {
poseData []byte
targetComp string
}
func NewPalmPoseCommand(poseData []byte) *PalmPoseCommand {
return &PalmPoseCommand{
poseData: poseData,
targetComp: "palm",
}
}
func (c *PalmPoseCommand) Type() string {
return "SetPalmPose"
}
func (c *PalmPoseCommand) Payload() []byte {
return c.poseData
}
func (c *PalmPoseCommand) TargetComponent() string {
return c.targetComp
}
// GenericCommand 通用指令
type GenericCommand struct {
cmdType string
payload []byte
targetComp string
}
func NewGenericCommand(cmdType string, payload []byte, targetComp string) *GenericCommand {
return &GenericCommand{
cmdType: cmdType,
payload: payload,
targetComp: targetComp,
}
}
func (c *GenericCommand) Type() string {
return c.cmdType
}
func (c *GenericCommand) Payload() []byte {
return c.payload
}
func (c *GenericCommand) TargetComponent() string {
return c.targetComp
}

60
pkg/device/device.go Normal file
View File

@ -0,0 +1,60 @@
package device
import (
"hands/define"
"time"
)
// Device 代表一个可控制的设备单元
type Device interface {
GetID() string // 获取设备唯一标识
GetModel() string // 获取设备型号 (例如 "L10", "L20")
GetHandType() define.HandType // 获取设备手型
SetHandType(handType define.HandType) error // 设置设备手型
ExecuteCommand(cmd Command) error // 执行一个通用指令
ReadSensorData(sensorID string) (SensorData, error) // 读取特定传感器数据
GetComponents(componentType ComponentType) []Component // 获取指定类型的组件
GetStatus() (DeviceStatus, error) // 获取设备状态
Connect() error // 连接设备
Disconnect() error // 断开设备连接
}
// Command 代表一个发送给设备的指令
type Command interface {
Type() string // 指令类型,例如 "SetFingerPose", "SetPalmAngle"
Payload() []byte // 指令的实际数据
TargetComponent() string // 目标组件 ID
}
// SensorData 代表从传感器读取的数据
type SensorData interface {
Timestamp() time.Time
Values() map[string]any // 例如 {"pressure": 100, "angle": 30.5}
SensorID() string
}
// ComponentType 定义组件类型
type ComponentType string
const (
SensorComponent ComponentType = "sensor"
SkinComponent ComponentType = "skin"
ActuatorComponent ComponentType = "actuator"
)
// Component 代表设备的一个可插拔组件
type Component interface {
GetID() string
GetType() ComponentType
GetConfiguration() map[string]interface{} // 组件的特定配置
IsActive() bool
}
// DeviceStatus 代表设备状态
type DeviceStatus struct {
IsConnected bool
IsActive bool
LastUpdate time.Time
ErrorCount int
LastError string
}

35
pkg/device/factory.go Normal file
View File

@ -0,0 +1,35 @@
package device
import "fmt"
// DeviceFactory 设备工厂
type DeviceFactory struct {
constructors map[string]func(config map[string]any) (Device, error)
}
var defaultFactory = &DeviceFactory{
constructors: make(map[string]func(config map[string]any) (Device, error)),
}
// RegisterDeviceType 注册设备类型
func RegisterDeviceType(modelName string, constructor func(config map[string]any) (Device, error)) {
defaultFactory.constructors[modelName] = constructor
}
// CreateDevice 创建设备实例
func CreateDevice(modelName string, config map[string]any) (Device, error) {
constructor, ok := defaultFactory.constructors[modelName]
if !ok {
return nil, fmt.Errorf("未知的设备型号: %s", modelName)
}
return constructor(config)
}
// GetSupportedModels 获取支持的设备型号列表
func GetSupportedModels() []string {
models := make([]string, 0, len(defaultFactory.constructors))
for model := range defaultFactory.constructors {
models = append(models, model)
}
return models
}

63
pkg/device/manager.go Normal file
View File

@ -0,0 +1,63 @@
package device
import (
"fmt"
"sync"
)
// DeviceManager 管理设备实例
type DeviceManager struct {
devices map[string]Device
mutex sync.RWMutex
}
func NewDeviceManager() *DeviceManager { return &DeviceManager{devices: make(map[string]Device)} }
func (m *DeviceManager) RegisterDevice(dev Device) error {
m.mutex.Lock()
defer m.mutex.Unlock()
id := dev.GetID()
if _, exists := m.devices[id]; exists {
return fmt.Errorf("设备 %s 已存在", id)
}
m.devices[id] = dev
return nil
}
func (m *DeviceManager) GetDevice(id string) (Device, error) {
m.mutex.RLock()
defer m.mutex.RUnlock()
dev, exists := m.devices[id]
if !exists {
return nil, fmt.Errorf("设备 %s 不存在", id)
}
return dev, nil
}
func (m *DeviceManager) GetAllDevices() []Device {
m.mutex.RLock()
defer m.mutex.RUnlock()
devices := make([]Device, 0, len(m.devices))
for _, dev := range m.devices {
devices = append(devices, dev)
}
return devices
}
func (m *DeviceManager) RemoveDevice(id string) error {
m.mutex.Lock()
defer m.mutex.Unlock()
if _, exists := m.devices[id]; !exists {
return fmt.Errorf("设备 %s 不存在", id)
}
delete(m.devices, id)
return nil
}

View File

@ -0,0 +1,8 @@
package models
import "hands/pkg/device"
func init() {
// 注册 L10 设备类型
device.RegisterDeviceType("L10", NewL10Hand)
}

240
pkg/device/models/l10.go Normal file
View File

@ -0,0 +1,240 @@
package models
import (
"fmt"
"sync"
"time"
"hands/define"
"hands/pkg/communication"
"hands/pkg/component"
"hands/pkg/device"
)
// L10Hand L10 型号手部设备实现
type L10Hand struct {
id string
model string
handType define.HandType
communicator communication.Communicator
components map[device.ComponentType][]device.Component
status device.DeviceStatus
mutex sync.RWMutex
canInterface string // CAN 接口名称,如 "can0"
}
// NewL10Hand 创建 L10 手部设备实例
func NewL10Hand(config map[string]any) (device.Device, error) {
id, ok := config["id"].(string)
if !ok {
return nil, fmt.Errorf("缺少设备 ID 配置")
}
serviceURL, ok := config["can_service_url"].(string)
if !ok {
return nil, fmt.Errorf("缺少 can 服务 URL 配置")
}
canInterface, ok := config["can_interface"].(string)
if !ok {
canInterface = "can0" // 默认接口
}
handType, ok := config["hand_type"].(define.HandType)
if !ok {
handType = define.HAND_TYPE_LEFT
}
// 创建通信客户端
comm := communication.NewCanBridgeClient(serviceURL)
hand := &L10Hand{
id: id,
model: "L10",
handType: handType,
communicator: comm,
components: make(map[device.ComponentType][]device.Component),
canInterface: canInterface,
status: device.DeviceStatus{
IsConnected: false,
IsActive: false,
LastUpdate: time.Now(),
},
}
// 初始化组件
if err := hand.initializeComponents(config); err != nil {
return nil, fmt.Errorf("初始化组件失败:%w", err)
}
return hand, nil
}
func (h *L10Hand) GetHandType() define.HandType {
return h.handType
}
func (h *L10Hand) SetHandType(handType define.HandType) error {
h.handType = handType
return nil
}
func (h *L10Hand) initializeComponents(_ map[string]any) error {
// 初始化传感器组件
sensors := []device.Component{
component.NewPressureSensor("pressure_thumb", map[string]any{"location": "thumb"}),
component.NewPressureSensor("pressure_index", map[string]any{"location": "index"}),
component.NewPressureSensor("pressure_middle", map[string]any{"location": "middle"}),
component.NewPressureSensor("pressure_ring", map[string]any{"location": "ring"}),
component.NewPressureSensor("pressure_pinky", map[string]any{"location": "pinky"}),
}
h.components[device.SensorComponent] = sensors
return nil
}
func (h *L10Hand) GetID() string {
return h.id
}
func (h *L10Hand) GetModel() string {
return h.model
}
func (h *L10Hand) ExecuteCommand(cmd device.Command) error {
h.mutex.Lock()
defer h.mutex.Unlock()
// 将通用指令转换为 L10 特定的 CAN 消息
rawMsg, err := h.commandToRawMessage(cmd)
if err != nil {
return fmt.Errorf("转换指令失败:%w", err)
}
// 发送到 can-bridge 服务
if err := h.communicator.SendMessage(rawMsg); err != nil {
h.status.ErrorCount++
h.status.LastError = err.Error()
return fmt.Errorf("发送指令失败:%w", err)
}
h.status.LastUpdate = time.Now()
return nil
}
func (h *L10Hand) commandToRawMessage(cmd device.Command) (communication.RawMessage, error) {
var canID uint32
var data []byte
switch cmd.Type() {
case "SetFingerPose":
// 根据目标组件确定 CAN ID
canID = h.getFingerCanID(cmd.TargetComponent())
data = cmd.Payload()
case "SetPalmPose":
canID = h.getPalmCanID()
data = cmd.Payload()
default:
return communication.RawMessage{}, fmt.Errorf("不支持的指令类型: %s", cmd.Type())
}
return communication.RawMessage{
Interface: h.canInterface,
ID: canID,
Data: data,
}, nil
}
func (h *L10Hand) getFingerCanID(targetComponent string) uint32 {
// L10 设备的手指 CAN ID 映射
fingerIDs := map[string]uint32{
"finger_thumb": 0x100,
"finger_index": 0x101,
"finger_middle": 0x102,
"finger_ring": 0x103,
"finger_pinky": 0x104,
}
if id, exists := fingerIDs[targetComponent]; exists {
return id
}
return 0x100 // 默认拇指
}
func (h *L10Hand) getPalmCanID() uint32 {
return 0x200 // L10 设备的手掌 CAN ID
}
func (h *L10Hand) ReadSensorData(sensorID string) (device.SensorData, error) {
h.mutex.RLock()
defer h.mutex.RUnlock()
// 查找传感器组件
sensors := h.components[device.SensorComponent]
for _, comp := range sensors {
if comp.GetID() == sensorID {
if sensor, ok := comp.(component.Sensor); ok {
return sensor.ReadData()
}
}
}
return nil, fmt.Errorf("传感器 %s 不存在", sensorID)
}
func (h *L10Hand) GetComponents(componentType device.ComponentType) []device.Component {
h.mutex.RLock()
defer h.mutex.RUnlock()
if components, exists := h.components[componentType]; exists {
result := make([]device.Component, len(components))
copy(result, components)
return result
}
return []device.Component{}
}
func (h *L10Hand) GetStatus() (device.DeviceStatus, error) {
h.mutex.RLock()
defer h.mutex.RUnlock()
return h.status, nil
}
func (h *L10Hand) Connect() error {
h.mutex.Lock()
defer h.mutex.Unlock()
// 检查与 can-bridge 服务的连接
if !h.communicator.IsConnected() {
return fmt.Errorf("无法连接到 can-bridge 服务")
}
// 检查 CAN 接口状态
isActive, err := h.communicator.GetInterfaceStatus(h.canInterface)
if err != nil {
return fmt.Errorf("检查 CAN 接口状态失败:%w", err)
}
if !isActive {
return fmt.Errorf("CAN接口 %s 未激活", h.canInterface)
}
h.status.IsConnected = true
h.status.IsActive = true
h.status.LastUpdate = time.Now()
return nil
}
func (h *L10Hand) Disconnect() error {
h.mutex.Lock()
defer h.mutex.Unlock()
h.status.IsConnected = false
h.status.IsActive = false
h.status.LastUpdate = time.Now()
return nil
}

View File

@ -39,7 +39,7 @@
<div class="container">
<div class="control-panel">
<h2>手指控制 <span class="info-badge">指令0x01</span></h2>
<h2>手指控制 <span class="info-badge">指令 0x01</span></h2>
<div class="slider-group">
<h3>手指关节控制</h3>
@ -94,7 +94,7 @@
</div>
<div class="slider-group">
<h3>掌部控制 <span class="info-badge">指令0x04</span></h3>
<h3>掌部控制 <span class="info-badge">指令 0x04</span></h3>
<div class="slider-container">
<div class="slider-label">
<span>关节 7</span>

View File

@ -53,7 +53,7 @@ const LinkerHandController = {
PALM_BIG_OPEN: [128, 128, 128, 128], // 大张开掌部
YEAH: [0, 103, 255, 255, 0, 0], // Yeah!
PALM_YEAH: [255, 235, 128, 128], // Yeah!掌部
PALM_YEAH: [255, 235, 128, 128], // Yeah! 掌部
// 数字手势预设
ONE: [0, 57, 255, 0, 0, 0],
@ -199,7 +199,7 @@ const LinkerHandController = {
return;
}
logMessage('info', `发送掌部姿态到 ${enabledHands.length} 个启用的手部: [${pose.join(', ')}]`);
logMessage('info', `发送掌部姿态到 ${enabledHands.length} 个启用的手部[${pose.join(', ')}]`);
enabledHands.forEach(async (config) => {
await sendPalmPoseToHand(config, pose);
@ -214,7 +214,7 @@ const LinkerHandController = {
// 设置定时获取
setInterval(() => {
this.fetchSensorData();
}, 2000); // 每2秒更新一次
}, 2000); // 每 2 秒更新一次
},
// 获取传感器数据
@ -227,7 +227,7 @@ const LinkerHandController = {
}
})
.catch(error => {
console.error('获取传感器数据失败:', error);
console.error('获取传感器数据失败', error);
});
},
@ -250,7 +250,7 @@ const LinkerHandController = {
// 更新最后更新时间
const lastUpdate = new Date(data.lastUpdate).toLocaleTimeString();
html += `<div style="text-align:right;font-size:0.8em;margin-top:5px;">最后更新: ${lastUpdate}</div>`;
html += `<div style="text-align:right;font-size:0.8em;margin-top:5px;">最后更新${lastUpdate}</div>`;
sensorDisplay.innerHTML = html;
},
@ -281,7 +281,7 @@ async function initializeSystem() {
try {
logMessage('info', '开始初始化系统...');
// 步骤1: 加载可用接口
// 步骤 1: 加载可用接口
logMessage('info', '步骤 1/3: 加载可用接口');
await loadAvailableInterfaces();
@ -290,7 +290,7 @@ async function initializeSystem() {
throw new Error('未能获取到任何可用接口');
}
// 步骤2: 生成手部配置
// 步骤 2: 生成手部配置
logMessage('info', '步骤 2/3: 生成手部配置');
generateHandConfigs();
@ -299,14 +299,14 @@ async function initializeSystem() {
throw new Error('未能生成手部配置');
}
// 步骤3: 检查接口状态
// 步骤 3: 检查接口状态
logMessage('info', '步骤 3/3: 检查接口状态');
await checkAllInterfaceStatus();
logMessage('success', '系统初始化完成');
} catch (error) {
logMessage('error', `系统初始化失败: ${error.message}`);
logMessage('error', `系统初始化失败${error.message}`);
console.error('InitializeSystem Error:', error);
// 尝试使用默认配置恢复
@ -332,13 +332,13 @@ async function loadAvailableInterfaces() {
if (data.status === 'success') {
availableInterfaces = data.data.availableInterfaces || [];
logMessage('success', `获取到 ${availableInterfaces.length} 个可用接口: ${availableInterfaces.join(', ')}`);
logMessage('success', `获取到 ${availableInterfaces.length} 个可用接口${availableInterfaces.join(', ')}`);
hideConnectionWarning();
} else {
throw new Error(data.error || '获取接口失败');
}
} catch (error) {
logMessage('error', `获取接口失败: ${error.message}`);
logMessage('error', `获取接口失败${error.message}`);
showConnectionWarning();
// 设置默认值
availableInterfaces = ['can0', 'can1', 'vcan0', 'vcan1'];
@ -358,7 +358,7 @@ function generateHandConfigs() {
handsGrid.innerHTML = '';
if (!availableInterfaces || availableInterfaces.length === 0) {
handsGrid.innerHTML = '<div style="text-align: center; color: #666; padding: 20px;">没有可用的CAN接口</div>';
handsGrid.innerHTML = '<div style="text-align: center; color: #666; padding: 20px;">没有可用的 CAN 接口</div>';
logMessage('warning', '没有可用接口,无法生成手部配置');
return;
}
@ -402,7 +402,7 @@ function generateHandConfigs() {
}, 100);
}
// 添加一个安全的DOM检查函数
// 添加一个安全的 DOM 检查函数
function validateHandElement(handId) {
const element = document.getElementById(handId);
if (!element) {
@ -444,7 +444,7 @@ function safeUpdateHandElement(handId) {
}
} catch (error) {
console.error(`Error updating hand element ${handId}:`, error);
logMessage('error', `更新手部元素 ${handId} 时出错: ${error.message}`);
logMessage('error', `更新手部元素 ${handId} 时出错${error.message}`);
}
}
@ -458,7 +458,7 @@ function createHandElement(config) {
const handLabel = config.handType === 'left' ? '左手' : '右手';
const handId = handTypeIds[config.handType];
// 确保HTML结构完整且正确
// 确保 HTML 结构完整且正确
div.innerHTML = `
<div class="hand-header">
<input type="checkbox" class="hand-checkbox" id="${config.id}_checkbox" ${config.enabled ? 'checked' : ''}>
@ -487,7 +487,7 @@ function createHandElement(config) {
</div>
`;
// 使用 requestAnimationFrame 确保DOM完全渲染后再设置事件监听器
// 使用 requestAnimationFrame 确保 DOM 完全渲染后再设置事件监听器
requestAnimationFrame(() => {
setTimeout(() => {
setupHandEventListeners(config.id);
@ -506,17 +506,17 @@ function setupHandEventListeners(handId) {
// 检查所有必需的元素是否存在
if (!checkbox) {
console.error(`setupHandEventListeners: 找不到checkbox - ${handId}_checkbox`);
console.error(`setupHandEventListeners: 找不到 checkbox - ${handId}_checkbox`);
return;
}
if (!interfaceSelect) {
console.error(`setupHandEventListeners: 找不到interfaceSelect - ${handId}_interface`);
console.error(`setupHandEventListeners: 找不到 interfaceSelect - ${handId}_interface`);
return;
}
if (!handTypeSelect) {
console.error(`setupHandEventListeners: 找不到handTypeSelect - ${handId}_handtype`);
console.error(`setupHandEventListeners: 找不到 handTypeSelect - ${handId}_handtype`);
return;
}
@ -588,7 +588,7 @@ function updateHandElement(handId) {
// 安全地更新手型标签
const handTypeLabels = element.querySelectorAll('.control-label');
if (handTypeLabels.length >= 2) {
const handTypeLabel = handTypeLabels[1]; // 第二个label是手型的
const handTypeLabel = handTypeLabels[1]; // 第二个 label 是手型的
if (handTypeLabel) {
handTypeLabel.textContent = `手型 (CAN ID: 0x${handIdHex.toString(16).toUpperCase()})`;
}
@ -654,7 +654,7 @@ async function checkAllInterfaceStatus() {
hideConnectionWarning();
} catch (error) {
logMessage('error', `状态检查失败: ${error.message}`);
logMessage('error', `状态检查失败${error.message}`);
console.error('CheckAllInterfaceStatus Error:', error);
showConnectionWarning();
setAllHandStatusOffline();
@ -739,7 +739,7 @@ function setupEventListeners() {
document.getElementById('start-sway').addEventListener('click', () => startAnimationForAll('sway'));
document.getElementById('stop-animation').addEventListener('click', stopAllAnimations);
// 预设姿势按钮 - 使用LinkerHandController的预设
// 预设姿势按钮 - 使用 LinkerHandController 的预设
setupPresetButtons();
// 数字手势按钮事件
@ -797,7 +797,7 @@ function setupPresetButtons() {
function setupNumericPresets() {
const delayDefault = 30;
// 数字1-9的预设
// 数字 1-9 的预设
for (let i = 1; i <= 9; i++) {
const button = document.getElementById(`pose-${i}`);
if (button) {
@ -829,7 +829,7 @@ function getNumberName(num) {
return names[num] || '';
}
// 设置Refill Core功能
// 设置 Refill Core 功能
function setupRefillCore() {
document.getElementById("refill-core").addEventListener("click", () => {
event.preventDefault();
@ -844,32 +844,32 @@ function setupRefillCore() {
[[246, 155, 154, 25], [140, 62, 0, 15, 29, 143]], // 小指
];
const delayTime = 350; // 设定延迟时间为350ms
const delayTime = 350; // 设定延迟时间为 350ms
// 创建完整的序列:从第一个到最后一个,再从最后一个回到第二个
const forwardIndices = [...Array(rukaPoseList.length).keys()]; // [0,1,2,3]
const backwardIndices = [...forwardIndices].reverse().slice(1); // [3,2,1]
const sequenceIndices = [...forwardIndices, ...backwardIndices];
// 遍历序列索引为每个索引创建两个操作palm和finger
// 遍历序列索引为每个索引创建两个操作palm finger
sequenceIndices.forEach((index, step) => {
const targetPose = rukaPoseList[index];
// 应用palm预设
// 应用 palm 预设
setTimeout(() => {
console.log(`Step ${step+1}a: Applying palm preset for pose ${index+1}`);
LinkerHandController.applyPalmPreset(targetPose[0]);
const palmPose = LinkerHandController.getPalmPoseValues();
LinkerHandController.sendPalmPoseToAll(palmPose);
}, delayTime * (step * 2)); // 每个完整步骤有两个操作,所以是step*2
}, delayTime * (step * 2)); // 每个完整步骤有两个操作,所以是 step*2
// 应用finger预设
// 应用 finger 预设
setTimeout(() => {
console.log(`Step ${step+1}b: Applying finger preset for pose ${index+1}`);
LinkerHandController.applyFingerPreset(targetPose[1]);
const fingerPose = LinkerHandController.getFingerPoseValues();
LinkerHandController.sendFingerPoseToAll(fingerPose);
}, delayTime * (step * 2 + 1)); // 偏移一个delayTime
}, delayTime * (step * 2 + 1)); // 偏移一个 delayTime
});
});
}
@ -1196,7 +1196,7 @@ function logMessage(type, message) {
statusLog.appendChild(logEntry);
statusLog.scrollTop = statusLog.scrollHeight;
// 保持最多50条日志
// 保持最多 50 条日志
const entries = statusLog.querySelectorAll('.log-entry');
if (entries.length > 50) {
statusLog.removeChild(entries[0]);
@ -1205,12 +1205,12 @@ function logMessage(type, message) {
// 启动状态更新器
function startStatusUpdater() {
// 每5秒检查一次接口状态
// 每 5 秒检查一次接口状态
setInterval(async () => {
await checkAllInterfaceStatus();
}, 5000);
// 每30秒刷新一次接口列表
// 每 30 秒刷新一次接口列表
setInterval(async () => {
const oldInterfaces = [...availableInterfaces];
await loadAvailableInterfaces();
@ -1226,7 +1226,7 @@ function startStatusUpdater() {
async function debugSystemStatus() {
logMessage('info', '🔍 开始系统调试...');
// 检查HTML元素
// 检查 HTML 元素
const elements = {
'hands-grid': document.getElementById('hands-grid'),
'status-log': document.getElementById('status-log'),
@ -1243,11 +1243,11 @@ async function debugSystemStatus() {
});
// 检查全局变量
logMessage('info', `可用接口: [${availableInterfaces.join(', ')}]`);
logMessage('info', `手部配置数量: ${Object.keys(handConfigs).length}`);
logMessage('info', `启用手部数量: ${getEnabledHands().length}`);
logMessage('info', `可用接口[${availableInterfaces.join(', ')}]`);
logMessage('info', `手部配置数量${Object.keys(handConfigs).length}`);
logMessage('info', `启用手部数量${getEnabledHands().length}`);
// 测试API连通性
// 测试 API 连通性
try {
logMessage('info', '测试 /api/health 连接...');
const response = await fetch('/api/health');
@ -1256,40 +1256,40 @@ async function debugSystemStatus() {
logMessage('success', '✅ 健康检查通过');
console.log('Health Check Data:', data);
} else {
logMessage('error', `❌ 健康检查失败: HTTP ${response.status}`);
logMessage('error', `❌ 健康检查失败HTTP ${response.status}`);
}
} catch (error) {
logMessage('error', `❌ 健康检查异常: ${error.message}`);
logMessage('error', `❌ 健康检查异常${error.message}`);
}
// 测试接口API
// 测试接口 API
try {
logMessage('info', '测试 /api/interfaces 连接...');
const response = await fetch('/api/interfaces');
if (response.ok) {
const data = await response.json();
logMessage('success', '✅ 接口API通过');
logMessage('success', '✅ 接口 API 通过');
console.log('Interfaces API Data:', data);
} else {
logMessage('error', `❌ 接口API失败: HTTP ${response.status}`);
logMessage('error', `❌ 接口 API 失败:HTTP ${response.status}`);
}
} catch (error) {
logMessage('error', `❌ 接口API异常: ${error.message}`);
logMessage('error', `❌ 接口 API 异常:${error.message}`);
}
}
// 导出全局函数供HTML按钮使用
// 导出全局函数供 HTML 按钮使用
window.triggerButtonsSequentially = triggerButtonsSequentially;
window.debugSystemStatus = debugSystemStatus;
// 添加全局错误处理
window.addEventListener('error', function(event) {
logMessage('error', `全局错误: ${event.error?.message || event.message}`);
logMessage('error', `全局错误${event.error?.message || event.message}`);
console.error('Global Error:', event.error);
});
window.addEventListener('unhandledrejection', function(event) {
logMessage('error', `未处理的Promise拒绝: ${event.reason?.message || event.reason}`);
logMessage('error', `未处理的 Promise 拒绝:${event.reason?.message || event.reason}`);
console.error('Unhandled Promise Rejection:', event.reason);
});
@ -1333,7 +1333,7 @@ document.addEventListener('keydown', function(e) {
toggleAllHands();
}
// 数字键1-9快速设置预设姿势
// 数字键 1-9 快速设置预设姿势
if (e.key >= '1' && e.key <= '9' && !e.ctrlKey && !e.altKey) {
const activeElement = document.activeElement;
// 确保不在输入框中
@ -1373,7 +1373,7 @@ function addTooltips() {
'start-wave': '启动所有启用手部的手指波浪动画',
'start-sway': '启动所有启用手部的掌部摆动动画',
'stop-animation': '停止所有启用手部的动画',
'refill-core': '执行Refill Core动作序列'
'refill-core': '执行 Refill Core 动作序列'
};
Object.entries(tooltips).forEach(([id, text]) => {
@ -1411,8 +1411,8 @@ async function startSequentialHandAnimation(animationType = 'wave', interval = 5
return getInterfaceNumber(a.interface) - getInterfaceNumber(b.interface);
});
logMessage('info', `开始六手依次动画 - 类型: ${animationType}, 间隔: ${interval}ms, 循环: ${cycles}`);
logMessage('info', `动画顺序: ${sortedHands.map(h => h.interface).join(' → ')}`);
logMessage('info', `开始六手依次动画 - 类型${animationType}, 间隔:${interval}ms, 循环:${cycles}`);
logMessage('info', `动画顺序${sortedHands.map(h => h.interface).join(' → ')}`);
// 定义动画预设
const animationPresets = {
@ -1466,12 +1466,12 @@ async function startSequentialHandAnimation(animationType = 'wave', interval = 5
[64, 64, 64, 64, 64, 64], // 握拳 (0)
],
palmPoses: [
[255, 109, 255, 118], // 5对应的掌部
[255, 109, 255, 118], // 4对应的掌部
[255, 109, 255, 118], // 3对应的掌部
[255, 109, 255, 118], // 2对应的掌部
[255, 109, 255, 118], // 1对应的掌部
[128, 128, 128, 128], // 0对应的掌部
[255, 109, 255, 118], // 5 对应的掌部
[255, 109, 255, 118], // 4 对应的掌部
[255, 109, 255, 118], // 3 对应的掌部
[255, 109, 255, 118], // 2 对应的掌部
[255, 109, 255, 118], // 1 对应的掌部
[128, 128, 128, 128], // 0 对应的掌部
]
},
@ -1480,7 +1480,7 @@ async function startSequentialHandAnimation(animationType = 'wave', interval = 5
fingerPoses: [
[64, 64, 64, 64, 64, 64], // 起始握拳
[128, 64, 64, 64, 64, 64], // 拇指起
[255, 128, 64, 64, 64, 64], // 拇指+食指起
[255, 128, 64, 64, 64, 64], // 拇指 + 食指起
[255, 255, 128, 64, 64, 64], // 前三指起
[255, 255, 255, 128, 64, 64], // 前四指起
[255, 255, 255, 255, 128, 64], // 前五指起
@ -1582,10 +1582,10 @@ async function startCustomSequentialAnimation(config) {
sortedHands = sortedHands.reverse();
}
logMessage('info', `开始自定义六手动画 - 方向: ${direction}, 同时手数: ${simultaneousHands}`);
logMessage('info', `开始自定义六手动画 - 方向${direction}, 同时手数:${simultaneousHands}`);
// 执行动画逻辑...
// 这里可以根据simultaneousHands参数同时控制多只手
// 这里可以根据 simultaneousHands 参数同时控制多只手
// 实现类似的动画逻辑,但支持更多自定义选项
}