diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..b2dfe19 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "weekly" \ No newline at end of file diff --git a/.github/logo.png b/.github/logo.png new file mode 100644 index 0000000..136cb4c Binary files /dev/null and b/.github/logo.png differ diff --git a/.github/workflows/CodeQL.yaml b/.github/workflows/CodeQL.yaml new file mode 100644 index 0000000..3bd3ea4 --- /dev/null +++ b/.github/workflows/CodeQL.yaml @@ -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}}" diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 0000000..f4f4f0e --- /dev/null +++ b/.github/workflows/release.yaml @@ -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 }} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..47c6b2c --- /dev/null +++ b/.gitignore @@ -0,0 +1,362 @@ +# Custom +/temp.md +/hands + +#################### Go.gitignore #################### + +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work + +#################### Archives.gitignore #################### + +# It's better to unpack these files and commit the raw source because +# git has its own built in compression methods. +*.7z +*.jar +*.rar +*.zip +*.gz +*.gzip +*.tgz +*.bzip +*.bzip2 +*.bz2 +*.xz +*.lzma +*.cab +*.xar + +# Packing-only formats +*.iso +*.tar + +# Package management formats +*.dmg +*.xpi +*.gem +*.egg +*.deb +*.rpm +*.msi +*.msm +*.msp +*.txz + +#################### Backup.gitignore #################### + +*.bak +*.gho +*.ori +*.orig +*.tmp + +#################### Emacs.gitignore #################### + +# -*- mode: gitignore; -*- +*~ +\#*\# +/.emacs.desktop +/.emacs.desktop.lock +*.elc +auto-save-list +tramp +.\#* + +# Org-mode +.org-id-locations +*_archive + +# flymake-mode +*_flymake.* + +# eshell files +/eshell/history +/eshell/lastdir + +# elpa packages +/elpa/ + +# reftex files +*.rel + +# AUCTeX auto folder +/auto/ + +# cask packages +.cask/ +dist/ + +# Flycheck +flycheck_*.el + +# server auth directory +/server/ + +# projectiles files +.projectile + +# directory configuration +.dir-locals.el + +# network security +/network-security.data + + +#################### JetBrains.gitignore #################### + +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 + +# User-specific stuff +.idea/**/workspace.xml +.idea/**/tasks.xml +.idea/**/usage.statistics.xml +.idea/**/dictionaries +.idea/**/shelf + +# AWS User-specific +.idea/**/aws.xml + +# Generated files +.idea/**/contentModel.xml + +# Sensitive or high-churn files +.idea/**/dataSources/ +.idea/**/dataSources.ids +.idea/**/dataSources.local.xml +.idea/**/sqlDataSources.xml +.idea/**/dynamic.xml +.idea/**/uiDesigner.xml +.idea/**/dbnavigator.xml + +# Gradle +.idea/**/gradle.xml +.idea/**/libraries + +# Gradle and Maven with auto-import +# When using Gradle or Maven with auto-import, you should exclude module files, +# since they will be recreated, and may cause churn. Uncomment if using +# auto-import. +# .idea/artifacts +# .idea/compiler.xml +# .idea/jarRepositories.xml +# .idea/modules.xml +# .idea/*.iml +# .idea/modules +# *.iml +# *.ipr + +# CMake +cmake-build-*/ + +# Mongo Explorer plugin +.idea/**/mongoSettings.xml + +# File-based project format +*.iws + +# IntelliJ +out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Cursive Clojure plugin +.idea/replstate.xml + +# SonarLint plugin +.idea/sonarlint/ + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties + +# Editor-based Rest Client +.idea/httpRequests + +# Android studio 3.1+ serialized cache file +.idea/caches/build_file_checksums.ser + +#################### Linux.gitignore #################### + +*~ + +# temporary files which can be created if a process still has a handle open of a deleted file +.fuse_hidden* + +# KDE directory preferences +.directory + +# Linux trash folder which might appear on any partition or disk +.Trash-* + +# .nfs files are created when an open file is removed but is still being accessed +.nfs* + +#################### NotepadPP.gitignore #################### + +# Notepad++ backups # +*.bak + +#################### PuTTY.gitignore #################### + +# Private key +*.ppk + +#################### SublimeText.gitignore #################### + +# Cache files for Sublime Text +*.tmlanguage.cache +*.tmPreferences.cache +*.stTheme.cache + +# Workspace files are user-specific +*.sublime-workspace + +# Project files should be checked into the repository, unless a significant +# proportion of contributors will probably not be using Sublime Text +# *.sublime-project + +# SFTP configuration file +sftp-config.json +sftp-config-alt*.json + +# Package control specific files +Package Control.last-run +Package Control.ca-list +Package Control.ca-bundle +Package Control.system-ca-bundle +Package Control.cache/ +Package Control.ca-certs/ +Package Control.merged-ca-bundle +Package Control.user-ca-bundle +oscrypto-ca-bundle.crt +bh_unicode_properties.cache + +# Sublime-github package stores a github token in this file +# https://packagecontrol.io/packages/sublime-github +GitHub.sublime-settings + +#################### Vim.gitignore #################### + +# Swap +[._]*.s[a-v][a-z] +!*.svg # comment out if you don't need vector files +[._]*.sw[a-p] +[._]s[a-rt-v][a-z] +[._]ss[a-gi-z] +[._]sw[a-p] + +# Session +Session.vim +Sessionx.vim + +# Temporary +.netrwhist +*~ +# Auto-generated tag files +tags +# Persistent undo +[._]*.un~ + +#################### VisualStudioCode.gitignore #################### + +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +!.vscode/*.code-snippets + +# Local History for Visual Studio Code +.history/ + +# Built Visual Studio Code Extensions +*.vsix + +#################### Windows.gitignore #################### + +# Windows thumbnail cache files +Thumbs.db +Thumbs.db:encryptable +ehthumbs.db +ehthumbs_vista.db + +# Dump file +*.stackdump + +# Folder config file +[Dd]esktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msix +*.msm +*.msp + +# Windows shortcuts +*.lnk + +#################### macOS.gitignore #################### + +# General +.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +#################### Custom.gitignore #################### + +# add your custom gitignore here: +!.gitignore +!.gitsubmodules \ No newline at end of file diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 0000000..d7e2972 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,117 @@ +version: 2 + +project_name: dashboard-server + +before: + hooks: + - go mod tidy + +builds: + - id: dashboard-server + env: + - CGO_ENABLED=0 + 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" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6dfa4f3 --- /dev/null +++ b/Dockerfile @@ -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 /app/dashboard-server + +EXPOSE 9099 + +ENV SERVER_PORT="9099" + +CMD ["/app/dashboard-server"] \ No newline at end of file diff --git a/Dockerfile.goreleaser b/Dockerfile.goreleaser new file mode 100644 index 0000000..6a8f670 --- /dev/null +++ b/Dockerfile.goreleaser @@ -0,0 +1,10 @@ +FROM alpine:3.21 + +WORKDIR /app + +COPY --link . . + +EXPOSE 9099 +ENV SERVER_PORT="9099" + +CMD ["/app/dashboard-server"] diff --git a/README.md b/README.md index 6e754bb..3b61a47 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ Dexterous hand-operated dashboard for LinkerHand 👋! +[中文文档](./docs/README_CN.md) [中文贡献指南](./docs/contribute_CN.md) + ## Project Overview **Dexterous Hand Dashboard** is a control dashboard service specifically developed for the LinkerHand dexterous hand device. Built with Golang, it provides a flexible RESTful API interface, enabling finger and palm pose control, execution of preset gestures, real-time sensor data monitoring, and dynamic configuration of hand type (left or right) and CAN interfaces. diff --git a/api/animation_handlers.go b/api/animation_handlers.go new file mode 100644 index 0000000..c842c10 --- /dev/null +++ b/api/animation_handlers.go @@ -0,0 +1,197 @@ +package api + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +// handleGetAnimations 获取可用动画列表 +func (s *Server) handleGetAnimations(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 获取动画引擎 + animEngine := dev.GetAnimationEngine() + + // 获取已注册的动画列表 + availableAnimations := animEngine.GetRegisteredAnimations() + + // 获取当前动画状态 + isRunning := animEngine.IsRunning() + currentName := animEngine.GetCurrentAnimation() + + response := AnimationStatusResponse{ + IsRunning: isRunning, + CurrentName: currentName, + AvailableList: availableAnimations, + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} + +// handleStartAnimation 启动动画 +func (s *Server) handleStartAnimation(c *gin.Context) { + deviceId := c.Param("deviceId") + + var req AnimationStartRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "无效的动画请求:" + err.Error(), + }) + return + } + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 获取动画引擎 + animEngine := dev.GetAnimationEngine() + + // 验证动画名称是否已注册 + availableAnimations := animEngine.GetRegisteredAnimations() + validAnimation := false + for _, name := range availableAnimations { + if name == req.Name { + validAnimation = true + break + } + } + + if !validAnimation { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("无效的动画类型:%s,可用动画:%v", req.Name, availableAnimations), + }) + return + } + + // 处理速度参数 + speedMs := req.SpeedMs + if speedMs <= 0 { + speedMs = 500 // 默认速度 + } + + // 启动动画 + if err := animEngine.Start(req.Name, speedMs); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("启动动画失败:%v", err), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 的 %s 动画已启动", deviceId, req.Name), + Data: map[string]any{ + "deviceId": deviceId, + "name": req.Name, + "speedMs": speedMs, + }, + }) +} + +// handleStopAnimation 停止动画 +func (s *Server) handleStopAnimation(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 获取动画引擎 + animEngine := dev.GetAnimationEngine() + + // 检查是否有动画在运行 + if !animEngine.IsRunning() { + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 当前没有动画在运行", deviceId), + Data: map[string]any{ + "deviceId": deviceId, + }, + }) + return + } + + // 停止动画 + if err := animEngine.Stop(); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("停止动画失败:%v", err), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 的动画已停止", deviceId), + Data: map[string]any{ + "deviceId": deviceId, + }, + }) +} + +// handleAnimationStatus 获取动画状态 +func (s *Server) handleAnimationStatus(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 获取动画引擎 + animEngine := dev.GetAnimationEngine() + + // 获取已注册的动画列表 + availableAnimations := animEngine.GetRegisteredAnimations() + + // 获取当前状态 + isRunning := animEngine.IsRunning() + currentName := animEngine.GetCurrentAnimation() + + response := AnimationStatusResponse{ + IsRunning: isRunning, + CurrentName: currentName, + AvailableList: availableAnimations, + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} diff --git a/api/device_handlers.go b/api/device_handlers.go new file mode 100644 index 0000000..fedb2e1 --- /dev/null +++ b/api/device_handlers.go @@ -0,0 +1,251 @@ +package api + +import ( + "fmt" + "net/http" + + "hands/define" + "hands/device" + + "github.com/gin-gonic/gin" +) + +// handleGetDevices 获取所有设备列表 +func (s *Server) handleGetDevices(c *gin.Context) { + devices := s.deviceManager.GetAllDevices() + + deviceInfos := make([]DeviceInfo, 0, len(devices)) + for _, dev := range devices { + status, err := dev.GetStatus() + if err != nil { + // 如果获取状态失败,使用默认状态 + status = device.DeviceStatus{ + IsConnected: false, + IsActive: false, + ErrorCount: 1, + LastError: err.Error(), + } + } + + deviceInfo := DeviceInfo{ + ID: dev.GetID(), + Model: dev.GetModel(), + HandType: dev.GetHandType().String(), + Status: status, + } + deviceInfos = append(deviceInfos, deviceInfo) + } + + response := DeviceListResponse{ + Devices: deviceInfos, + Total: len(deviceInfos), + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} + +// handleCreateDevice 创建新设备 +func (s *Server) handleCreateDevice(c *gin.Context) { + var req DeviceCreateRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "无效的设备创建请求:" + err.Error(), + }) + return + } + + // 检查设备是否已存在 + if _, err := s.deviceManager.GetDevice(req.ID); err == nil { + c.JSON(http.StatusConflict, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 已存在", req.ID), + }) + return + } + + // 准备设备配置 + config := req.Config + if config == nil { + config = make(map[string]any) + } + config["id"] = req.ID + + // 设置手型 + if req.HandType != "" { + config["hand_type"] = req.HandType + } + + // 创建设备实例 + dev, err := device.CreateDevice(req.Model, config) + if err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("创建设备失败:%v", err), + }) + return + } + + // 注册设备到管理器 + if err := s.deviceManager.RegisterDevice(dev); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("注册设备失败:%v", err), + }) + return + } + + // 获取设备状态 + status, err := dev.GetStatus() + if err != nil { + status = device.DeviceStatus{ + IsConnected: false, + IsActive: false, + ErrorCount: 1, + LastError: err.Error(), + } + } + + deviceInfo := DeviceInfo{ + ID: dev.GetID(), + Model: dev.GetModel(), + HandType: dev.GetHandType().String(), + Status: status, + } + + c.JSON(http.StatusCreated, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 创建成功", req.ID), + Data: deviceInfo, + }) +} + +// handleGetDevice 获取设备详情 +func (s *Server) handleGetDevice(c *gin.Context) { + deviceId := c.Param("deviceId") + + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + status, err := dev.GetStatus() + if err != nil { + status = device.DeviceStatus{ + IsConnected: false, + IsActive: false, + ErrorCount: 1, + LastError: err.Error(), + } + } + + deviceInfo := DeviceInfo{ + ID: dev.GetID(), + Model: dev.GetModel(), + HandType: dev.GetHandType().String(), + Status: status, + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: deviceInfo, + }) +} + +// handleDeleteDevice 删除设备 +func (s *Server) handleDeleteDevice(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 检查设备是否存在 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 停止设备的动画(如果正在运行) + animEngine := dev.GetAnimationEngine() + if animEngine.IsRunning() { + if err := animEngine.Stop(); err != nil { + // 记录错误但不阻止删除 + fmt.Printf("警告:停止设备 %s 动画时出错:%v\n", deviceId, err) + } + } + + // 从管理器中移除设备 + if err := s.deviceManager.RemoveDevice(deviceId); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("删除设备失败:%v", err), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 已删除", deviceId), + }) +} + +// handleSetHandType 设置设备手型 +func (s *Server) handleSetHandType(c *gin.Context) { + deviceId := c.Param("deviceId") + + var req HandTypeRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "无效的手型设置请求:" + err.Error(), + }) + return + } + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 转换手型字符串为枚举 + var handType define.HandType + handType = define.HandTypeFromString(req.HandType) + if handType == define.HAND_TYPE_UNKNOWN { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "无效的手型,必须是 'left' 或 'right'", + }) + return + } + + // 设置手型 + if err := dev.SetHandType(handType); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设置手型失败:%v", err), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 手型已设置为 %s", deviceId, req.HandType), + Data: map[string]any{ + "deviceId": deviceId, + "handType": req.HandType, + }, + }) +} diff --git a/api/models.go b/api/models.go new file mode 100644 index 0000000..d38193b --- /dev/null +++ b/api/models.go @@ -0,0 +1,111 @@ +package api + +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"` +} diff --git a/api/pose_handlers.go b/api/pose_handlers.go new file mode 100644 index 0000000..3a2ef8f --- /dev/null +++ b/api/pose_handlers.go @@ -0,0 +1,273 @@ +package api + +import ( + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +// handleSetFingerPose 设置手指姿态 +func (s *Server) handleSetFingerPose(c *gin.Context) { + deviceId := c.Param("deviceId") + + var req FingerPoseRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "无效的手指姿态数据:" + err.Error(), + }) + return + } + + // 验证每个值是否在范围内 + for _, v := range req.Pose { + if v > 255 { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "手指姿态值必须在 0-255 范围内", + }) + return + } + } + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 停止当前动画(如果正在运行) + animEngine := dev.GetAnimationEngine() + if animEngine.IsRunning() { + if err := animEngine.Stop(); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("停止动画失败:%v", err), + }) + return + } + } + + // 设置手指姿态 + if err := dev.SetFingerPose(req.Pose); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: "发送手指姿态失败:" + err.Error(), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: "手指姿态指令发送成功", + Data: map[string]any{ + "deviceId": deviceId, + "pose": req.Pose, + }, + }) +} + +// handleSetPalmPose 设置手掌姿态 +func (s *Server) handleSetPalmPose(c *gin.Context) { + deviceId := c.Param("deviceId") + + var req PalmPoseRequest + if err := c.ShouldBindJSON(&req); err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "无效的掌部姿态数据:" + err.Error(), + }) + return + } + + // 验证每个值是否在范围内 + for _, v := range req.Pose { + if v > 255 { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: "掌部姿态值必须在 0-255 范围内", + }) + return + } + } + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 停止当前动画(如果正在运行) + animEngine := dev.GetAnimationEngine() + if animEngine.IsRunning() { + if err := animEngine.Stop(); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("停止动画失败:%v", err), + }) + return + } + } + + // 设置手掌姿态 + if err := dev.SetPalmPose(req.Pose); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: "发送掌部姿态失败:" + err.Error(), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: "掌部姿态指令发送成功", + Data: map[string]any{ + "deviceId": deviceId, + "pose": req.Pose, + }, + }) +} + +// handleSetPresetPose 设置预设姿势 +func (s *Server) handleSetPresetPose(c *gin.Context) { + deviceId := c.Param("deviceId") + pose := c.Param("pose") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 停止当前动画(如果正在运行) + animEngine := dev.GetAnimationEngine() + if animEngine.IsRunning() { + if err := animEngine.Stop(); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("停止动画失败:%v", err), + }) + return + } + } + + // 使用设备的预设姿势方法 + if err := dev.ExecutePreset(pose); err != nil { + c.JSON(http.StatusBadRequest, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("执行预设姿势失败: %v", err), + }) + return + } + + // 获取预设姿势的描述 + description := dev.GetPresetDescription(pose) + message := fmt.Sprintf("已设置预设姿势: %s", pose) + if description != "" { + message = fmt.Sprintf("已设置%s", description) + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: message, + Data: map[string]any{ + "deviceId": deviceId, + "pose": pose, + "description": description, + }, + }) +} + +// handleResetPose 重置姿态 +func (s *Server) handleResetPose(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 停止当前动画(如果正在运行) + animEngine := dev.GetAnimationEngine() + if animEngine.IsRunning() { + if err := animEngine.Stop(); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("停止动画失败:%v", err), + }) + return + } + } + + // 重置姿态 + if err := dev.ResetPose(); err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: "重置姿态失败:" + err.Error(), + }) + return + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: fmt.Sprintf("设备 %s 已重置到默认姿态", deviceId), + Data: map[string]any{ + "deviceId": deviceId, + }, + }) +} + +// handleGetPresetPose 获取设备支持的预设姿势列表 +func (s *Server) handleGetPresetPose(c *gin.Context) { + deviceID := c.Param("deviceId") + + device, err := s.deviceManager.GetDevice(deviceID) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceID), + }) + return + } + + // 使用设备的预设姿势方法 + presets := device.GetSupportedPresets() + + // 构建详细的预设信息 + presetDetails := make([]map[string]string, 0, len(presets)) + for _, presetName := range presets { + description := device.GetPresetDescription(presetName) + presetDetails = append(presetDetails, map[string]string{ + "name": presetName, + "description": description, + }) + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Message: "获取设备支持的预设姿势列表成功", + Data: map[string]any{ + "deviceId": deviceID, + "presets": presetDetails, + "count": len(presets), + }, + }) +} diff --git a/api/router.go b/api/router.go new file mode 100644 index 0000000..5ad18ea --- /dev/null +++ b/api/router.go @@ -0,0 +1,87 @@ +package api + +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 v1 服务器实例 +func NewServer(deviceManager *device.DeviceManager) *Server { + return &Server{ + deviceManager: deviceManager, + startTime: time.Now(), + version: "1.0.0", + } +} + +// SetupRoutes 设置 API v1 路由 +func (s *Server) SetupRoutes(r *gin.Engine) { + r.StaticFile("/", "./static/index.html") + r.Static("/static", "./static") + + // API v1 路由组 + v2 := r.Group("/api/v1") + { + // 设备管理路由 + 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("/reset", s.handleResetPose) // 重置姿态 + + // 新的预设姿势 API + poses.GET("/presets", s.handleGetPresetPose) // 获取支持的预设姿势列表 + poses.POST("/presets/:presetName", s.handleSetPresetPose) // 执行预设姿势 + } + + // 动画控制路由 + 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) // 健康检查 + } + } +} diff --git a/api/sensor_handlers.go b/api/sensor_handlers.go new file mode 100644 index 0000000..67a35f8 --- /dev/null +++ b/api/sensor_handlers.go @@ -0,0 +1,180 @@ +package api + +import ( + "fmt" + "net/http" + "time" + + "hands/device" + + "github.com/gin-gonic/gin" +) + +// handleGetSensors 获取所有传感器数据 +func (s *Server) handleGetSensors(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 获取设备的传感器组件 + sensorComponents := dev.GetComponents(device.SensorComponent) + + sensors := make([]SensorDataResponse, 0, len(sensorComponents)) + + // 遍历所有传感器组件,读取数据 + for _, component := range sensorComponents { + sensorId := component.GetID() + + // 读取传感器数据 + sensorData, err := dev.ReadSensorData(sensorId) + if err != nil { + // 如果读取失败,创建一个错误状态的传感器数据 + sensors = append(sensors, SensorDataResponse{ + SensorID: sensorId, + Timestamp: time.Now(), + Values: map[string]any{ + "error": err.Error(), + "status": "error", + }, + }) + continue + } + + // 转换为响应格式 + sensorResponse := SensorDataResponse{ + SensorID: sensorData.SensorID(), + Timestamp: sensorData.Timestamp(), + Values: sensorData.Values(), + } + sensors = append(sensors, sensorResponse) + } + + response := SensorListResponse{ + Sensors: sensors, + Total: len(sensors), + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} + +// handleGetSensorData 获取特定传感器数据 +func (s *Server) handleGetSensorData(c *gin.Context) { + deviceId := c.Param("deviceId") + sensorId := c.Param("sensorId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 验证传感器是否存在 + sensorComponents := dev.GetComponents(device.SensorComponent) + sensorExists := false + for _, component := range sensorComponents { + if component.GetID() == sensorId { + sensorExists = true + break + } + } + + if !sensorExists { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 上不存在传感器 %s", deviceId, sensorId), + }) + return + } + + // 读取传感器数据 + sensorData, err := dev.ReadSensorData(sensorId) + if err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("读取传感器 %s 数据失败:%v", sensorId, err), + }) + return + } + + // 转换为响应格式 + response := SensorDataResponse{ + SensorID: sensorData.SensorID(), + Timestamp: sensorData.Timestamp(), + Values: sensorData.Values(), + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} + +// handleGetDeviceStatus 获取设备状态 +func (s *Server) handleGetDeviceStatus(c *gin.Context) { + deviceId := c.Param("deviceId") + + // 获取设备 + dev, err := s.deviceManager.GetDevice(deviceId) + if err != nil { + c.JSON(http.StatusNotFound, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("设备 %s 不存在", deviceId), + }) + return + } + + // 获取设备状态 + status, err := dev.GetStatus() + if err != nil { + c.JSON(http.StatusInternalServerError, ApiResponse{ + Status: "error", + Error: fmt.Sprintf("获取设备状态失败:%v", err), + }) + return + } + + // 获取动画引擎状态 + animEngine := dev.GetAnimationEngine() + animationStatus := map[string]any{ + "isRunning": animEngine.IsRunning(), + } + + // 获取传感器组件数量 + sensorComponents := dev.GetComponents(device.SensorComponent) + + // 构建详细的设备状态响应 + deviceInfo := DeviceInfo{ + ID: dev.GetID(), + Model: dev.GetModel(), + HandType: dev.GetHandType().String(), + Status: status, + } + + // 扩展状态信息 + extendedStatus := map[string]any{ + "device": deviceInfo, + "animation": animationStatus, + "sensorCount": len(sensorComponents), + "lastUpdate": status.LastUpdate, + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: extendedStatus, + }) +} diff --git a/api/system_handlers.go b/api/system_handlers.go new file mode 100644 index 0000000..012ba21 --- /dev/null +++ b/api/system_handlers.go @@ -0,0 +1,114 @@ +package api + +import ( + "net/http" + "time" + + "hands/device" + + "github.com/gin-gonic/gin" +) + +// handleGetSupportedModels 获取支持的设备型号 +func (s *Server) handleGetSupportedModels(c *gin.Context) { + // 获取支持的设备型号列表 + models := device.GetSupportedModels() + + response := SupportedModelsResponse{ + Models: models, + Total: len(models), + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} + +// handleGetSystemStatus 获取系统状态 +func (s *Server) handleGetSystemStatus(c *gin.Context) { + // 获取所有设备 + devices := s.deviceManager.GetAllDevices() + + // 统计设备信息 + totalDevices := len(devices) + activeDevices := 0 + deviceInfos := make(map[string]DeviceInfo) + + for _, dev := range devices { + status, err := dev.GetStatus() + if err != nil { + // 如果获取状态失败,使用默认状态 + status = device.DeviceStatus{ + IsConnected: false, + IsActive: false, + ErrorCount: 1, + LastError: err.Error(), + } + } + + if status.IsActive { + activeDevices++ + } + + deviceInfo := DeviceInfo{ + ID: dev.GetID(), + Model: dev.GetModel(), + HandType: dev.GetHandType().String(), + Status: status, + } + deviceInfos[dev.GetID()] = deviceInfo + } + + // 获取支持的设备型号 + supportedModels := device.GetSupportedModels() + + // 计算系统运行时间 + uptime := time.Since(s.startTime) + + response := SystemStatusResponse{ + TotalDevices: totalDevices, + ActiveDevices: activeDevices, + SupportedModels: supportedModels, + Devices: deviceInfos, + Uptime: uptime, + } + + c.JSON(http.StatusOK, ApiResponse{ + Status: "success", + Data: response, + }) +} + +// handleHealthCheck 健康检查 +func (s *Server) handleHealthCheck(c *gin.Context) { + // 执行基本的健康检查 + status := "healthy" + + // 检查设备管理器是否正常 + if s.deviceManager == nil { + status = "unhealthy" + } + + // 可以添加更多健康检查逻辑,比如: + // - 检查关键服务是否可用 + // - 检查数据库连接 + // - 检查外部依赖 + + response := HealthResponse{ + Status: status, + Timestamp: time.Now(), + Version: s.version, + } + + // 根据健康状态返回相应的 HTTP 状态码 + httpStatus := http.StatusOK + if status != "healthy" { + httpStatus = http.StatusServiceUnavailable + } + + c.JSON(httpStatus, ApiResponse{ + Status: "success", + Data: response, + }) +} diff --git a/cli/cli.go b/cli/cli.go index 8bbc25c..9c6f2c3 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -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 { diff --git a/communication/communicator.go b/communication/communicator.go new file mode 100644 index 0000000..978840d --- /dev/null +++ b/communication/communicator.go @@ -0,0 +1,131 @@ +package communication + +import ( + "bytes" + "context" + "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(ctx context.Context, 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(ctx context.Context, msg RawMessage) error { + jsonData, err := json.Marshal(msg) + if err != nil { + return fmt.Errorf("序列化消息失败:%w", err) + } + + url := fmt.Sprintf("%s/api/can", c.serviceURL) + + // 创建带有 context 的请求 + req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return fmt.Errorf("创建 HTTP 请求失败:%w", err) + } + req.Header.Set("Content-Type", "application/json") + + resp, err := c.client.Do(req) + 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 +} diff --git a/component/pressure_sensor.go b/component/pressure_sensor.go new file mode 100644 index 0000000..1c74a69 --- /dev/null +++ b/component/pressure_sensor.go @@ -0,0 +1,78 @@ +package component + +import ( + "fmt" + "hands/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 +} diff --git a/component/sensor.go b/component/sensor.go new file mode 100644 index 0000000..66b9265 --- /dev/null +++ b/component/sensor.go @@ -0,0 +1,42 @@ +package component + +import ( + "hands/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 +} diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..426ee2f --- /dev/null +++ b/config/config.go @@ -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) +} diff --git a/define/define.go b/define/define.go index 0a81583..5f0cdf9 100644 --- a/define/define.go +++ b/define/define.go @@ -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"` } diff --git a/define/hands.go b/define/hands.go new file mode 100644 index 0000000..04e3c8e --- /dev/null +++ b/define/hands.go @@ -0,0 +1,27 @@ +package define + +type HandType int + +const ( + HAND_TYPE_LEFT HandType = 0x28 + HAND_TYPE_RIGHT HandType = 0x27 + HAND_TYPE_UNKNOWN HandType = 0x00 +) + +func (ht HandType) String() string { + if ht == HAND_TYPE_LEFT { + return "左手" + } + return "右手" +} + +func HandTypeFromString(s string) HandType { + switch s { + case "left": + return HAND_TYPE_LEFT + case "right": + return HAND_TYPE_RIGHT + default: + return HAND_TYPE_UNKNOWN + } +} diff --git a/device/animation.go b/device/animation.go new file mode 100644 index 0000000..1cf1718 --- /dev/null +++ b/device/animation.go @@ -0,0 +1,12 @@ +package device + +// Animation 定义了一个动画序列的行为 +type Animation interface { + // Run 执行动画的一个周期或直到被停止 + // executor: 用于执行姿态指令 + // stop: 接收停止信号的通道 + // speedMs: 动画执行的速度(毫秒) + Run(executor PoseExecutor, stop <-chan struct{}, speedMs int) error + // Name 返回动画的名称 + Name() string +} diff --git a/device/commands.go b/device/commands.go new file mode 100644 index 0000000..fb14a71 --- /dev/null +++ b/device/commands.go @@ -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 +} diff --git a/device/device.go b/device/device.go new file mode 100644 index 0000000..2f9d54f --- /dev/null +++ b/device/device.go @@ -0,0 +1,69 @@ +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 // 断开设备连接 + + // --- 新增 --- + PoseExecutor // 嵌入 PoseExecutor 接口,Device 需实现它 + GetAnimationEngine() *AnimationEngine // 获取设备的动画引擎 + + // --- 预设姿势相关方法 --- + GetSupportedPresets() []string // 获取支持的预设姿势列表 + ExecutePreset(presetName string) error // 执行预设姿势 + GetPresetDescription(presetName string) string // 获取预设姿势描述 +} + +// 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 +} diff --git a/device/engine.go b/device/engine.go new file mode 100644 index 0000000..f96e2a8 --- /dev/null +++ b/device/engine.go @@ -0,0 +1,210 @@ +package device + +import ( + "fmt" + "log" + "sync" +) + +// defaultAnimationSpeedMs 定义默认动画速度(毫秒) +const defaultAnimationSpeedMs = 500 + +// AnimationEngine 管理和执行动画 +type AnimationEngine struct { + executor PoseExecutor // 关联的姿态执行器 + animations map[string]Animation // 注册的动画 + stopChan chan struct{} // 当前动画的停止通道 + current string // 当前运行的动画名称 + isRunning bool // 是否有动画在运行 + engineMutex sync.Mutex // 保护引擎状态 (isRunning, current, stopChan) + registerMutex sync.RWMutex // 保护动画注册表 (animations) +} + +// NewAnimationEngine 创建一个新的动画引擎 +func NewAnimationEngine(executor PoseExecutor) *AnimationEngine { + return &AnimationEngine{ + executor: executor, + animations: make(map[string]Animation), + } +} + +// Register 注册一个动画 +func (e *AnimationEngine) Register(anim Animation) { + e.registerMutex.Lock() + defer e.registerMutex.Unlock() + + if anim == nil { + log.Printf("⚠️ 尝试注册一个空动画") + return + } + + name := anim.Name() + if _, exists := e.animations[name]; exists { + log.Printf("⚠️ 动画 %s 已注册,将被覆盖", name) + } + e.animations[name] = anim + log.Printf("✅ 动画 %s 已注册", name) +} + +// getAnimation 安全地获取一个已注册的动画 +func (e *AnimationEngine) getAnimation(name string) (Animation, bool) { + e.registerMutex.RLock() + defer e.registerMutex.RUnlock() + anim, exists := e.animations[name] + return anim, exists +} + +// getDeviceName 尝试获取设备 ID 用于日志记录 +func (e *AnimationEngine) getDeviceName() string { + // 尝试通过接口断言获取 ID + if idProvider, ok := e.executor.(interface{ GetID() string }); ok { + return idProvider.GetID() + } + return "设备" // 默认名称 +} + +// Start 启动一个动画 +func (e *AnimationEngine) Start(name string, speedMs int) error { + e.engineMutex.Lock() + defer e.engineMutex.Unlock() // 确保在任何情况下都释放锁 + + anim, exists := e.getAnimation(name) + if !exists { + return fmt.Errorf("❌ 动画 %s 未注册", name) + } + + // 如果有动画在运行,先发送停止信号 + if e.isRunning { + log.Printf("ℹ️ 正在停止当前动画 %s 以启动 %s...", e.current, name) + close(e.stopChan) + // 注意:我们不在此处等待旧动画结束。 + // 新动画将立即启动,旧动画的 goroutine 在收到信号后会退出。 + // 其 defer 中的 `stopChan` 比较会确保它不会干扰新动画的状态。 + } + + // 设置新动画状态 + e.stopChan = make(chan struct{}) // 创建新的停止通道 + e.isRunning = true + e.current = name + + // 验证并设置速度 + actualSpeedMs := speedMs + if actualSpeedMs <= 0 { + actualSpeedMs = defaultAnimationSpeedMs + } + + log.Printf("🚀 准备启动动画 %s (设备: %s, 速度: %dms)", name, e.getDeviceName(), actualSpeedMs) + + // 启动动画 goroutine + go e.runAnimationLoop(anim, e.stopChan, actualSpeedMs) + + return nil +} + +// Stop 停止当前正在运行的动画 +func (e *AnimationEngine) Stop() error { + e.engineMutex.Lock() + defer e.engineMutex.Unlock() + + if !e.isRunning { + log.Printf("ℹ️ 当前没有动画在运行 (设备: %s)", e.getDeviceName()) + return nil + } + + log.Printf("⏳ 正在发送停止信号给动画 %s (设备: %s)...", e.current, e.getDeviceName()) + close(e.stopChan) // 发送停止信号 + e.isRunning = false // 立即标记为未运行,防止重复停止 + e.current = "" + // 动画的 goroutine 将在下一次检查通道时退出, + // 并在其 defer 块中执行最终的清理(包括 ResetPose)。 + + return nil +} + +// IsRunning 检查是否有动画在运行 +func (e *AnimationEngine) IsRunning() bool { + e.engineMutex.Lock() + defer e.engineMutex.Unlock() + return e.isRunning +} + +// GetRegisteredAnimations 获取已注册的动画名称列表 +func (e *AnimationEngine) GetRegisteredAnimations() []string { + e.registerMutex.RLock() + defer e.registerMutex.RUnlock() + + animations := make([]string, 0, len(e.animations)) + for name := range e.animations { + animations = append(animations, name) + } + return animations +} + +// GetCurrentAnimation 获取当前运行的动画名称 +func (e *AnimationEngine) GetCurrentAnimation() string { + e.engineMutex.Lock() + defer e.engineMutex.Unlock() + return e.current +} + +// runAnimationLoop 是动画执行的核心循环,在单独的 Goroutine 中运行。 +func (e *AnimationEngine) runAnimationLoop(anim Animation, stopChan <-chan struct{}, speedMs int) { + deviceName := e.getDeviceName() + animName := anim.Name() + + // 使用 defer 确保无论如何都能执行清理逻辑 + defer e.handleLoopExit(stopChan, deviceName, animName) + + log.Printf("▶️ %s 动画 %s 已启动", deviceName, animName) + + // 动画主循环 + for { + select { + case <-stopChan: + log.Printf("🛑 %s 动画 %s 被显式停止", deviceName, animName) + return // 接收到停止信号,退出循环 + default: + // 执行一轮动画 + err := anim.Run(e.executor, stopChan, speedMs) + if err != nil { + log.Printf("❌ %s 动画 %s 执行出错: %v", deviceName, animName, err) + return // 出错则退出 + } + + // 再次检查停止信号,防止 Run 结束后才收到信号 + select { + case <-stopChan: + log.Printf("🛑 %s 动画 %s 在周期结束时被停止", deviceName, animName) + return + default: + // 继续下一个循环 + } + } + } +} + +// handleLoopExit 是动画 Goroutine 退出时执行的清理函数。 +func (e *AnimationEngine) handleLoopExit(stopChan <-chan struct{}, deviceName, animName string) { + e.engineMutex.Lock() + defer e.engineMutex.Unlock() + + // --- 关键并发控制 --- + // 检查当前引擎的 stopChan 是否与此 Goroutine 启动时的 stopChan 相同。 + // 如果不相同,说明一个新的动画已经启动,并且接管了引擎状态。 + // 这种情况下,旧的 Goroutine 不应该修改引擎状态或重置姿态, + // 以避免干扰新动画。 + if stopChan == e.stopChan { + // 只有当自己仍然是"活跃"的动画时,才更新状态并重置姿态 + e.isRunning = false + e.current = "" + log.Printf("👋 %s 动画 %s 已完成或停止,正在重置姿态...", deviceName, animName) + if err := e.executor.ResetPose(); err != nil { + log.Printf("⚠️ %s 动画结束后重置姿态失败: %v", deviceName, err) + } else { + log.Printf("✅ %s 姿态已重置", deviceName) + } + } else { + // 如果 stopChan 不同,说明自己是旧的 Goroutine,只需安静退出 + log.Printf("ℹ️ 旧的 %s 动画 %s goroutine 退出,但新动画已启动,无需重置。", deviceName, animName) + } +} diff --git a/device/factory.go b/device/factory.go new file mode 100644 index 0000000..b147563 --- /dev/null +++ b/device/factory.go @@ -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 +} diff --git a/device/manager.go b/device/manager.go new file mode 100644 index 0000000..d7d6e8c --- /dev/null +++ b/device/manager.go @@ -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 +} diff --git a/device/models/init.go b/device/models/init.go new file mode 100644 index 0000000..b9dba65 --- /dev/null +++ b/device/models/init.go @@ -0,0 +1,8 @@ +package models + +import "hands/device" + +func RegisterDeviceTypes() { + // 注册 L10 设备类型 + device.RegisterDeviceType("L10", NewL10Hand) +} diff --git a/device/models/l10.go b/device/models/l10.go new file mode 100644 index 0000000..221853e --- /dev/null +++ b/device/models/l10.go @@ -0,0 +1,380 @@ +package models + +import ( + "context" + "fmt" + "log" + "math/rand/v2" + "sync" + "time" + + "hands/communication" + "hands/component" + "hands/define" + "hands/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" + animationEngine *device.AnimationEngine // 动画引擎 + presetManager *device.PresetManager // 预设姿势管理器 +} + +// 在 base 基础上进行 ±delta 的扰动,范围限制在 [0, 255] +func perturb(base byte, delta int) byte { + offset := rand.IntN(2*delta+1) - delta + v := min(max(int(base)+offset, 0), 255) + return byte(v) +} + +// NewL10Hand 创建 L10 手部设备实例 +// 参数 config 是设备配置,包含以下字段: +// - id: 设备 ID +// - can_service_url: CAN 服务 URL +// - can_interface: CAN 接口名称,如 "can0" +// - hand_type: 手型,可选值为 "left" 或 "right",默认值为 "right" +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" // 默认接口 + } + + handTypeStr, ok := config["hand_type"].(string) + handType := define.HAND_TYPE_RIGHT // 默认右手 + if ok && handTypeStr == "left" { + 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{ + // TODO: 这里需要修改,根据实际连接情况设置,因为当前还没有实现连接和断开路由,先设置为 true + IsConnected: true, + IsActive: true, + LastUpdate: time.Now(), + }, + } + + // 初始化动画引擎,将 hand 自身作为 PoseExecutor + hand.animationEngine = device.NewAnimationEngine(hand) + + // 注册默认动画 + hand.animationEngine.Register(NewL10WaveAnimation()) + hand.animationEngine.Register(NewL10SwayAnimation()) + + // 初始化预设姿势管理器 + hand.presetManager = device.NewPresetManager() + + // 注册 L10 的预设姿势 + for _, preset := range GetL10Presets() { + hand.presetManager.RegisterPreset(preset) + } + + // 初始化组件 + if err := hand.initializeComponents(config); err != nil { + return nil, fmt.Errorf("初始化组件失败:%w", err) + } + + log.Printf("✅ 设备 L10 (%s, %s) 创建成功", id, handType.String()) + return hand, nil +} + +// GetHandType 获取设备手型 +func (h *L10Hand) GetHandType() define.HandType { + h.mutex.RLock() + defer h.mutex.RUnlock() + return h.handType +} + +// SetHandType 设置设备手型 +func (h *L10Hand) SetHandType(handType define.HandType) error { + h.mutex.Lock() + defer h.mutex.Unlock() + if handType != define.HAND_TYPE_LEFT && handType != define.HAND_TYPE_RIGHT { + return fmt.Errorf("无效的手型:%d", handType) + } + h.handType = handType + log.Printf("🔧 设备 %s 手型已更新: %s", h.id, handType.String()) + return nil +} + +// GetAnimationEngine 获取动画引擎 +func (h *L10Hand) GetAnimationEngine() *device.AnimationEngine { + return h.animationEngine +} + +// SetFingerPose 设置手指姿态 (实现 PoseExecutor) +func (h *L10Hand) SetFingerPose(pose []byte) error { + if len(pose) != 6 { + return fmt.Errorf("无效的手指姿态数据长度,需要 6 个字节") + } + + // 添加随机扰动 + perturbedPose := make([]byte, len(pose)) + for i, v := range pose { + perturbedPose[i] = perturb(v, 5) + } + + // 创建指令 + cmd := device.NewFingerPoseCommand("all", perturbedPose) + + // 执行指令 + err := h.ExecuteCommand(cmd) + if err == nil { + log.Printf("✅ %s (%s) 手指动作已发送: [%X %X %X %X %X %X]", + h.id, h.GetHandType().String(), perturbedPose[0], perturbedPose[1], perturbedPose[2], + perturbedPose[3], perturbedPose[4], perturbedPose[5]) + } + return err +} + +// SetPalmPose 设置手掌姿态 (实现 PoseExecutor) +func (h *L10Hand) SetPalmPose(pose []byte) error { + if len(pose) != 4 { + return fmt.Errorf("无效的手掌姿态数据长度,需要 4 个字节") + } + + // 添加随机扰动 + perturbedPose := make([]byte, len(pose)) + for i, v := range pose { + perturbedPose[i] = perturb(v, 8) + } + + // 创建指令 + cmd := device.NewPalmPoseCommand(perturbedPose) + + // 执行指令 + err := h.ExecuteCommand(cmd) + if err == nil { + log.Printf("✅ %s (%s) 掌部姿态已发送: [%X %X %X %X]", + h.id, h.GetHandType().String(), perturbedPose[0], perturbedPose[1], perturbedPose[2], perturbedPose[3]) + } + return err +} + +// ResetPose 重置到默认姿态 (实现 PoseExecutor) +func (h *L10Hand) ResetPose() error { + log.Printf("🔄 正在重置设备 %s (%s) 到默认姿态...", h.id, h.GetHandType().String()) + defaultFingerPose := []byte{64, 64, 64, 64, 64, 64} // 0x40 - 半开 + defaultPalmPose := []byte{128, 128, 128, 128} // 0x80 - 居中 + + if err := h.SetFingerPose(defaultFingerPose); err != nil { + log.Printf("❌ %s 重置手指姿势失败: %v", h.id, err) + return err + } + time.Sleep(20 * time.Millisecond) // 短暂延时 + if err := h.SetPalmPose(defaultPalmPose); err != nil { + log.Printf("❌ %s 重置掌部姿势失败: %v", h.id, err) + return err + } + log.Printf("✅ 设备 %s 已重置到默认姿态", h.id) + return nil +} + +// commandToRawMessage 将通用指令转换为 L10 特定的 CAN 消息 +func (h *L10Hand) commandToRawMessage(cmd device.Command) (communication.RawMessage, error) { + h.mutex.RLock() + defer h.mutex.RUnlock() + + var data []byte + canID := uint32(h.handType) + + switch cmd.Type() { + case "SetFingerPose": + // 添加 0x01 前缀 + data = append([]byte{0x01}, cmd.Payload()...) + if len(data) > 8 { // CAN 消息数据长度限制 + return communication.RawMessage{}, fmt.Errorf("手指姿态数据过长") + } + case "SetPalmPose": + // 添加 0x04 前缀 + data = append([]byte{0x04}, cmd.Payload()...) + if len(data) > 8 { // CAN 消息数据长度限制 + return communication.RawMessage{}, fmt.Errorf("手掌姿态数据过长") + } + default: + return communication.RawMessage{}, fmt.Errorf("L10 不支持的指令类型: %s", cmd.Type()) + } + + return communication.RawMessage{ + Interface: h.canInterface, + ID: canID, + Data: data, + }, nil +} + +// ExecuteCommand 执行一个通用指令 +func (h *L10Hand) ExecuteCommand(cmd device.Command) error { + h.mutex.Lock() // 使用写锁,因为会更新状态 + defer h.mutex.Unlock() + + if !h.status.IsConnected || !h.status.IsActive { + return fmt.Errorf("设备 %s 未连接或未激活", h.id) + } + + // 转换指令为 CAN 消息 + rawMsg, err := h.commandToRawMessage(cmd) + if err != nil { + h.status.ErrorCount++ + h.status.LastError = err.Error() + return fmt.Errorf("转换指令失败:%w", err) + } + + // 创建带有超时的 context,设置 3 秒超时 + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + // 发送到 can-bridge 服务 + if err := h.communicator.SendMessage(ctx, rawMsg); err != nil { + h.status.ErrorCount++ + h.status.LastError = err.Error() + log.Printf("❌ %s (%s) 发送指令失败: %v (ID: 0x%X, Data: %X)", h.id, h.handType.String(), err, rawMsg.ID, rawMsg.Data) + return fmt.Errorf("发送指令失败:%w", err) + } + + h.status.LastUpdate = time.Now() + // 成功的日志记录移到 SetFingerPose 和 SetPalmPose 中,因为那里有更详细的信息 + return nil +} + +// --- 其他 L10Hand 方法 (initializeComponents, GetID, GetModel, ReadSensorData, etc.) 保持不变 --- +// --- 确保它们存在且与您上传的版本一致 --- + +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) 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() + + // TODO: 假设连接总是成功,除非有显式错误 + h.status.IsConnected = true + h.status.IsActive = true + h.status.LastUpdate = time.Now() + log.Printf("🔗 设备 %s 已连接", h.id) + 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() + log.Printf("🔌 设备 %s 已断开", h.id) + return nil +} + +// --- 预设姿势相关方法 --- + +// GetSupportedPresets 获取支持的预设姿势列表 +func (h *L10Hand) GetSupportedPresets() []string { return h.presetManager.GetSupportedPresets() } + +// ExecutePreset 执行预设姿势 +func (h *L10Hand) ExecutePreset(presetName string) error { + preset, exists := h.presetManager.GetPreset(presetName) + if !exists { + return fmt.Errorf("预设姿势 '%s' 不存在", presetName) + } + + log.Printf("🎯 设备 %s (%s) 执行预设姿势: %s", h.id, h.GetHandType().String(), presetName) + + // 执行手指姿态 + if err := h.SetFingerPose(preset.FingerPose); err != nil { + return fmt.Errorf("执行预设姿势 '%s' 的手指姿态失败: %w", presetName, err) + } + + // 如果有手掌姿态数据,也执行 + if len(preset.PalmPose) > 0 { + time.Sleep(20 * time.Millisecond) // 短暂延时 + if err := h.SetPalmPose(preset.PalmPose); err != nil { + return fmt.Errorf("执行预设姿势 '%s' 的手掌姿态失败: %w", presetName, err) + } + } + + log.Printf("✅ 设备 %s 预设姿势 '%s' 执行完成", h.id, presetName) + return nil +} + +// GetPresetDescription 获取预设姿势描述 +func (h *L10Hand) GetPresetDescription(presetName string) string { + return h.presetManager.GetPresetDescription(presetName) +} diff --git a/device/models/l10_animation.go b/device/models/l10_animation.go new file mode 100644 index 0000000..b4e8b56 --- /dev/null +++ b/device/models/l10_animation.go @@ -0,0 +1,125 @@ +package models + +import ( + "hands/device" + "log" + "time" +) + +// --- L10WaveAnimation --- + +// L10WaveAnimation 实现 L10 的波浪动画 +type L10WaveAnimation struct{} + +// NewL10WaveAnimation 创建 L10 波浪动画实例 +func NewL10WaveAnimation() *L10WaveAnimation { return &L10WaveAnimation{} } + +func (w *L10WaveAnimation) Name() string { return "wave" } + +func (w *L10WaveAnimation) Run(executor device.PoseExecutor, stop <-chan struct{}, speedMs int) error { + fingerOrder := []int{0, 1, 2, 3, 4, 5} + open := byte(64) // 0x40 + close := byte(192) // 0xC0 + delay := time.Duration(speedMs) * time.Millisecond + + deviceName := "L10" + + // 波浪张开 + 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 := executor.SetFingerPose(pose); err != nil { + log.Printf("❌ %s 动画 %s 发送失败: %v", deviceName, w.Name(), err) + return err + } + + select { + case <-stop: + return nil // 动画被停止 + 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 := executor.SetFingerPose(pose); err != nil { + log.Printf("❌ %s 动画 %s 发送失败: %v", deviceName, w.Name(), err) + return err + } + + select { + case <-stop: + return nil // 动画被停止 + case <-time.After(delay): + // 继续 + } + } + + return nil // 完成一个周期 +} + +// --- L10SwayAnimation --- + +// L10SwayAnimation 实现 L10 的横向摆动动画 +type L10SwayAnimation struct{} + +// NewL10SwayAnimation 创建 L10 摆动动画实例 +func NewL10SwayAnimation() *L10SwayAnimation { return &L10SwayAnimation{} } + +func (s *L10SwayAnimation) Name() string { return "sway" } + +func (s *L10SwayAnimation) Run(executor device.PoseExecutor, stop <-chan struct{}, speedMs int) error { + leftPose := []byte{48, 48, 48, 48} // 0x30 + rightPose := []byte{208, 208, 208, 208} // 0xD0 + delay := time.Duration(speedMs) * time.Millisecond + + deviceName := "L10" + if idProvider, ok := executor.(interface{ GetID() string }); ok { + deviceName = idProvider.GetID() + } + + // 向左移动 + if err := executor.SetPalmPose(leftPose); err != nil { + log.Printf("❌ %s 动画 %s 发送失败: %v", deviceName, s.Name(), err) + return err + } + + select { + case <-stop: + return nil // 动画被停止 + case <-time.After(delay): + // 继续 + } + + // 向右移动 + if err := executor.SetPalmPose(rightPose); err != nil { + log.Printf("❌ %s 动画 %s 发送失败: %v", deviceName, s.Name(), err) + return err + } + + select { + case <-stop: + return nil // 动画被停止 + case <-time.After(delay): + // 继续 + } + + return nil // 完成一个周期 +} diff --git a/device/models/l10_presets.go b/device/models/l10_presets.go new file mode 100644 index 0000000..f8237be --- /dev/null +++ b/device/models/l10_presets.go @@ -0,0 +1,82 @@ +package models + +import "hands/device" + +// GetL10Presets 获取 L10 设备的所有预设姿势 +func GetL10Presets() []device.PresetPose { + return []device.PresetPose{ + // 基础姿势 + { + Name: "fist", + Description: "握拳姿势", + FingerPose: []byte{64, 64, 64, 64, 64, 64}, + }, + { + Name: "open", + Description: "完全张开姿势", + FingerPose: []byte{192, 192, 192, 192, 192, 192}, + }, + { + Name: "pinch", + Description: "捏取姿势", + FingerPose: []byte{120, 120, 64, 64, 64, 64}, + }, + { + Name: "thumbsup", + Description: "竖起大拇指姿势", + FingerPose: []byte{64, 192, 192, 192, 192, 64}, + }, + { + Name: "point", + Description: "食指指点姿势", + FingerPose: []byte{192, 64, 192, 192, 192, 64}, + }, + + // 数字手势 + { + Name: "1", + Description: "数字 1 手势", + FingerPose: []byte{192, 64, 192, 192, 192, 64}, + }, + { + Name: "2", + Description: "数字 2 手势", + FingerPose: []byte{192, 64, 64, 192, 192, 64}, + }, + { + Name: "3", + Description: "数字 3 手势", + FingerPose: []byte{192, 64, 64, 64, 192, 64}, + }, + { + Name: "4", + Description: "数字 4 手势", + FingerPose: []byte{192, 64, 64, 64, 64, 64}, + }, + { + Name: "5", + Description: "数字 5 手势", + FingerPose: []byte{192, 192, 192, 192, 192, 192}, + }, + { + Name: "6", + Description: "数字 6 手势", + FingerPose: []byte{64, 192, 192, 192, 192, 64}, + }, + { + Name: "7", + Description: "数字 7 手势", + FingerPose: []byte{64, 64, 192, 192, 192, 64}, + }, + { + Name: "8", + Description: "数字 8 手势", + FingerPose: []byte{64, 64, 64, 192, 192, 64}, + }, + { + Name: "9", + Description: "数字 9 手势", + FingerPose: []byte{64, 64, 64, 64, 192, 64}, + }, + } +} diff --git a/device/pose_executor.go b/device/pose_executor.go new file mode 100644 index 0000000..eb79dcb --- /dev/null +++ b/device/pose_executor.go @@ -0,0 +1,20 @@ +package device + +import "hands/define" + +// PoseExecutor 定义了执行基本姿态指令的能力 +type PoseExecutor interface { + // SetFingerPose 设置手指姿态 + // pose: 6 字节数据,代表 6 个手指的位置 + SetFingerPose(pose []byte) error + + // SetPalmPose 设置手掌姿态 + // pose: 4 字节数据,代表手掌的 4 个自由度 + SetPalmPose(pose []byte) error + + // ResetPose 重置到默认姿态 + ResetPose() error + + // GetHandType 获取当前手型 + GetHandType() define.HandType +} diff --git a/device/preset.go b/device/preset.go new file mode 100644 index 0000000..4931a39 --- /dev/null +++ b/device/preset.go @@ -0,0 +1,45 @@ +package device + +// PresetPose 定义预设姿势的结构 +type PresetPose struct { + Name string // 姿势名称 + Description string // 姿势描述 + FingerPose []byte // 手指姿态数据 + PalmPose []byte // 手掌姿态数据(可选) +} + +// PresetManager 预设姿势管理器 +type PresetManager struct{ presets map[string]PresetPose } + +// NewPresetManager 创建新的预设姿势管理器 +func NewPresetManager() *PresetManager { + return &PresetManager{ + presets: make(map[string]PresetPose), + } +} + +// RegisterPreset 注册一个预设姿势 +func (pm *PresetManager) RegisterPreset(preset PresetPose) { pm.presets[preset.Name] = preset } + +// GetPreset 获取指定名称的预设姿势 +func (pm *PresetManager) GetPreset(name string) (PresetPose, bool) { + preset, exists := pm.presets[name] + return preset, exists +} + +// GetSupportedPresets 获取所有支持的预设姿势名称列表 +func (pm *PresetManager) GetSupportedPresets() []string { + presets := make([]string, 0, len(pm.presets)) + for name := range pm.presets { + presets = append(presets, name) + } + return presets +} + +// GetPresetDescription 获取预设姿势的描述 +func (pm *PresetManager) GetPresetDescription(name string) string { + if preset, exists := pm.presets[name]; exists { + return preset.Description + } + return "" +} diff --git a/README_CN.md b/docs/README_CN.md similarity index 94% rename from README_CN.md rename to docs/README_CN.md index 53235a1..32f7d61 100644 --- a/README_CN.md +++ b/docs/README_CN.md @@ -1,5 +1,7 @@ # Dexterous Hand Dashboard 项目文档 +[贡献指南](./contribute_CN.md) + ## 项目概述 **Dexterous Hand Dashboard** 是专为 LinkerHand 灵巧手设备开发的控制仪表盘服务。该服务基于 Golang 开发,提供灵活的 RESTful API 接口,可实现手指与掌部姿态控制、预设动作执行及实时传感器数据监控,并支持动态配置手型(左手或右手)及 CAN 接口。 @@ -8,7 +10,7 @@ * **动态手型配置**:支持左手和右手手型的动态切换。 * **灵活接口配置**:支持多种 CAN 接口(如 `can0`, `can1`),可通过命令行参数或环境变量动态设置。 -* **手指与掌部姿态控制**:提供手指(6字节)和掌部(4字节)姿态数据发送功能。 +* **手指与掌部姿态控制**:提供手指(6 字节)和掌部(4 字节)姿态数据发送功能。 * **预设动作执行**:内置丰富的手势动作,如握拳、张开、捏取、点赞、数字手势等。 * **实时动画控制**:支持波浪、横向摆动等动画效果,用户可动态启动和停止。 * **传感器数据实时监控**:提供接口压力数据的实时模拟和更新。 diff --git a/docs/contribute_CN.md b/docs/contribute_CN.md new file mode 100644 index 0000000..2eeb08f --- /dev/null +++ b/docs/contribute_CN.md @@ -0,0 +1,623 @@ +# 当前架构详解 + +## 设备抽象层 (device 包) + +目标:统一不同型号设备的操作接口,屏蔽底层硬件差异(主要体现在指令到 RawMessage 的转换和设备特定功能的实现上)。 + +核心接口与结构体: + +**Device 接口 (device/device.go): 代表一个可控制的设备单元。** + +```go +type Device interface { + GetID() string + GetModel() string + 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 + + PoseExecutor // 嵌入 PoseExecutor 接口 + GetAnimationEngine() *AnimationEngine + + GetSupportedPresets() []string + ExecutePreset(presetName string) error + GetPresetDescription(presetName string) string +} +``` + +**PoseExecutor 接口 (device/pose_executor.go): 定义了执行基本姿态指令的能力。** + +```go +type PoseExecutor interface { + SetFingerPose(pose []byte) error + SetPalmPose(pose []byte) error + ResetPose() error + GetHandType() define.HandType +} +``` + +**Command 接口 (device/device.go): 代表一个发送给设备的指令。** + +```go +type Command interface { + Type() string + Payload() []byte + TargetComponent() string // 目标组件 ID +} +``` + +具体指令实现位于 device/commands.go,如 FingerPoseCommand, PalmPoseCommand, GenericCommand。 + +**SensorData 接口 (device/device.go): 代表从传感器读取的数据。** + +```go +type SensorData interface { + Timestamp() time.Time + Values() map[string]any + SensorID() string +} +``` + +**ComponentType (device/device.go): 定义组件类型。** + +```go +const ( + SensorComponent ComponentType = "sensor" + SkinComponent ComponentType = "skin" // 示例,可扩展 + ActuatorComponent ComponentType = "actuator" // 示例,可扩展 +) +``` + +**Component 接口 (device/device.go): 代表设备的一个可插拔组件。** + +```go +type Component interface { + GetID() string + GetType() ComponentType + GetConfiguration() map[string]interface{} + IsActive() bool +} +``` + +**具体设备型号实现 (如 device/models/l10.go 中的 L10Hand):** + +1. 实现 Device 和 PoseExecutor 接口。 +2. 管理内部的 AnimationEngine 和 PresetManager。 +3. 包含将通用 Command 转换为发送给 can-bridge 的 RawMessage 的逻辑 (如 commandToRawMessage 方法)。 +4. 管理其配备的传感器等组件 (initializeComponents 方法)。 + +**DeviceManager (device/manager.go): 用于注册、发现和管理可用的设备实例。** + +```go +type DeviceManager struct { /* ... */ } +func NewDeviceManager() *DeviceManager { /* ... */ } +func (m *DeviceManager) RegisterDevice(dev Device) error { /* ... */ } +func (m *DeviceManager) GetDevice(id string) (Device, error) { /* ... */ } +``` + +## 组件化设计 (component 包) + +目标:将“皮肤”、“传感器”等视为可配置、可替换的组件。 + +核心接口与结构体: + +**传感器组件 (Sensor):** + +component/sensor.go 中定义了通用的 Sensor 接口 (嵌入了 device.Component)。 + +```go +type Sensor interface { + device.Component + ReadData() (device.SensorData, error) + GetDataType() string + GetSamplingRate() int + SetSamplingRate(rate int) error +} +``` + +具体的传感器实现,如 component/component.go 中的 PressureSensor,实现了 Sensor 接口。 + +传感器数据的实际获取方式(模拟、通过 can-bridge 的特定端点,或完全独立的数据源)在具体的 Sensor 组件实现中处理。 + +SensorDataImpl (component/sensor.go) 是 device.SensorData 的一个具体实现。 + +皮肤组件 (Skin) 及其他组件: + +如果“皮肤”影响设备的物理特性或参数范围,可以将其抽象为一个 Skin 组件,实现 device.Component 接口。 + +设备可以关联多个不同类型的组件,并在其 initializeComponents 方法中进行初始化。 + +## 动画与姿态控制 + +目标:提供灵活的动画播放和直接的姿态控制能力,与具体设备和通信方式解耦。 + +**AnimationEngine (device/engine.go):** + +每个设备实例拥有一个 AnimationEngine。 + +负责注册、启动、停止和管理动画的生命周期。 + +**使用 PoseExecutor 来执行动画中的姿态变化。** + +```go +type AnimationEngine struct { /* ... */ } +func NewAnimationEngine(executor PoseExecutor) *AnimationEngine { /* ... */ } +func (e *AnimationEngine) Register(anim Animation) { /* ... */ } +func (e *AnimationEngine) Start(name string, speedMs int) error { /* ... */ } +func (e *AnimationEngine) Stop() error { /* ... */ } +``` + +Animation 接口 (device/animation.go): 定义了动画的行为。 + +```go +type Animation interface { + Run(executor PoseExecutor, stop <-chan struct{}, speedMs int) error + Name() string +} +``` + +具体的动画实现与设备型号绑定,例如 device/models/l10_animation.go 中的 L10WaveAnimation。 + +直接姿态控制: + +通过设备实例直接调用其实现的 PoseExecutor 接口方法 (SetFingerPose, SetPalmPose, ResetPose)。 + +或者通过构造 FingerPoseCommand 或 PalmPoseCommand,然后调用 device.ExecuteCommand()。 + +预设姿势 (PresetManager - device/preset.go): + +每个设备实例拥有一个 PresetManager。 + +负责注册和管理预设姿势 (PresetPose 结构体)。 + +Device 接口提供了 GetSupportedPresets, ExecutePreset, GetPresetDescription 方法与预设姿势交互。 + +## 通信层抽象 (communication 包) + +目标:将与 can-bridge Web 服务的 HTTP 通信细节封装起来,对上层透明。 + +RawMessage 结构体 (communication/communicator.go): 匹配 can-bridge 服务期望的 JSON 格式。 + +```go +type RawMessage struct { + Interface string `json:"interface"` + ID uint32 `json:"id"` + Data []byte `json:"data"` +} +``` + +Communicator 接口 (communication/communicator.go): 定义了与 can-bridge Web 服务进行通信的接口。 + +```go +type Communicator interface { + SendMessage(ctx context.Context, msg RawMessage) error + GetInterfaceStatus(ifName string) (isActive bool, err error) + GetAllInterfaceStatuses() (statuses map[string]bool, err error) + SetServiceURL(url string) + IsConnected() bool +} +``` + +**CanBridgeClient (communication/communicator.go): Communicator 接口的实现。** + +1. 内部使用标准的 net/http 包与 can-bridge 服务交互。 +2. 负责构造 HTTP 请求 (POST 到 /api/can 用于发送,GET 到 /api/status/* 用于状态检查)。 +3. 处理 JSON 序列化/反序列化以及 HTTP 错误。 +4. 需要配置 can-bridge 服务的 URL。 + +具体设备实现 (如 L10Hand) 依赖此 Communicator 接口来发送指令。 + +## 指令生成与解析 + +指令生成:上层逻辑(如动画、直接控制)创建 device.Command 类型的对象 (如 NewFingerPoseCommand(...))。 + +设备的 ExecuteCommand 方法接收此 Command。 + +设备内部的 commandToRawMessage (或类似) 方法将通用的 Command 转换为特定于该型号的 RawMessage(包含正确的 Interface, ID, Data)。 + +传感器数据解析: + +L10Hand 的 ReadSensorData 方法委托给相应的 Sensor 组件。 + +Sensor 组件的 ReadData 方法负责获取原始数据(如果通过 CAN,则可能需要 Communicator 支持读取功能,目前 can-bridge 主要用于发送)并将其解析为高层可理解的 SensorData。当前实现中,PressureSensor 是模拟数据。 + +## 配置与注册 + +设备工厂 (device/factory.go): + +使用 DeviceFactory (defaultFactory) 来创建不同型号的 Device 实例。 + +RegisterDeviceType(modelName string, constructor func(config map[string]any) (Device, error)): 注册新的设备型号及其构造函数。 + +CreateDevice(modelName string, config map[string]any) (Device, error): 根据型号和配置创建设备实例。 + +设备构造函数 (如 NewL10Hand) 接收一个 map[string]any 类型的配置参数。 + +动画和预设姿势注册: + +动画通过 AnimationEngine.Register() 在设备实例化时注册。 + +预设姿势通过 PresetManager.RegisterPreset() 在设备实例化时注册。 + +## 如何添加新的设备实现 + +要添加对新型号设备(例如 "L20")的支持,请遵循以下步骤: + +### 创建设备模型文件: + +在 device/models/ 目录下为新设备创建一个 Go 文件,例如 l20.go。 + +如果需要设备特定的动画,创建 l20_animation.go。 + +如果需要设备特定的预设姿势,创建 l20_presets.go。 + +定义设备结构体 (l20.go): + +```go +package models + +import ( + "context" + "fmt" + "log" + "sync" + "time" + // ... 其他必要的 import + "hands/communication" + "hands/component" // 如果需要自定义组件或使用现有组件 + "hands/define" + "hands/device" +) + +type L20Hand 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 + animationEngine *device.AnimationEngine + presetManager *device.PresetManager + // ... L20 特有的字段 +} +``` + +实现构造函数 (NewL20Hand): + +```go +func NewL20Hand(config map[string]any) (device.Device, error) { + // 1. 解析配置 (id, can_service_url, can_interface, hand_type 等) + // ... + + // 2. 创建 communicator + comm := communication.NewCanBridgeClient(serviceURL) // serviceURL from config + + hand := &L20Hand{ + id: id, // from config + model: "L20", + handType: handType, // from config or default + communicator: comm, + components: make(map[device.ComponentType][]device.Component), + canInterface: canInterface, // from config or default + status: device.DeviceStatus{ /* initial status */ }, + // ... 初始化 L20 特有字段 + } + + // 3. 初始化 AnimationEngine + hand.animationEngine = device.NewAnimationEngine(hand) // hand 实现了 PoseExecutor + // 注册 L20 特定的动画 (见步骤 6) + // hand.animationEngine.Register(NewL20WaveAnimation()) // 示例 + + // 4. 初始化 PresetManager + hand.presetManager = device.NewPresetManager() + // 注册 L20 特定的预设姿势 (见步骤 7) + // for _, preset := range GetL20Presets() { hand.presetManager.RegisterPreset(preset) } // 示例 + + // 5. 初始化组件 + if err := hand.initializeComponents(config); err != nil { + return nil, fmt.Errorf("L20 初始化组件失败:%w", err) + } + + log.Printf("✅ 设备 L20 (%s, %s) 创建成功", hand.id, hand.handType.String()) + return hand, nil +} +``` + +**实现 device.Device 和 device.PoseExecutor 接口:** + +基本方法:GetID(), GetModel(), GetHandType(), SetHandType(), GetStatus(), Connect(), Disconnect()。这些通常比较直接。 + +**PoseExecutor 方法:** + +1. SetFingerPose(pose []byte) error +2. SetPalmPose(pose []byte) error +3. ResetPose() error + +这些方法内部会调用 ExecuteCommand,或者直接构造 RawMessage 发送(如果 L20 的姿态设置非常特殊)。通常建议通过 ExecuteCommand。 + +```go +ExecuteCommand(cmd device.Command) error: + +func (h *L20Hand) ExecuteCommand(cmd device.Command) error { + h.mutex.Lock() + defer h.mutex.Unlock() + // 1. 检查设备状态 + // 2. 调用 h.commandToRawMessage(cmd) 将通用指令转换为 L20 特定的 RawMessage + // 3. 使用 h.communicator.SendMessage(ctx, rawMsg) 发送 + // 4. 更新设备状态和日志 + return nil // or error +} +``` + +commandToRawMessage(cmd device.Command) (communication.RawMessage, error): 这个辅助方法是设备差异化的关键。它需要根据 L20 的 CAN 协议,将 cmd.Type() 和 cmd.Payload() 转换为正确的 RawMessage.ID 和 RawMessage.Data。 + +组件和传感器方法:ReadSensorData(), GetComponents()。 + +动画和预设方法:GetAnimationEngine(), GetSupportedPresets(), ExecutePreset(), GetPresetDescription()。这些通常直接委托给内部的 animationEngine 和 presetManager。 + +实现设备特定逻辑:initializeComponents(config map[string]any) error: 根据 L20 的硬件配置,创建并注册其传感器、执行器等组件到 h.components。 + +```go +func (h *L20Hand) initializeComponents(config map[string]any) error { + // 示例:添加一个 L20 特有的传感器 + // l20Sensor := component.NewL20SpecificSensor("l20_sensor_1", nil) + // h.components[device.SensorComponent] = append(h.components[device.SensorComponent], l20Sensor) + return nil +} +``` + +添加设备特定动画 (l20_animation.go): + +定义实现 device.Animation 接口的动画结构体,如 L20WaveAnimation。 + +在 NewL20Hand 中,使用 hand.animationEngine.Register(NewL20WaveAnimation()) 注册它们。 + +添加设备特定预设姿势 (l20_presets.go): + +定义一个函数如 GetL20Presets() []device.PresetPose,返回 L20 的预设姿势列表。 + +在 NewL20Hand 中,遍历这些预设并使用 hand.presetManager.RegisterPreset(preset) 注册它们。 + +注册设备类型: + +在 device/models/init.go 的 RegisterDeviceTypes() 函数中添加一行: + +device.RegisterDeviceType("L20", NewL20Hand) + +## 如何添加新的动画/预设姿势 + +这里主要指实现项目已定义的 Go 接口,如 device.Animation 或 component.Sensor。 + +### 添加新的动画 (实现 device.Animation) + +定义动画结构体:在设备模型相关的动画文件内 (例如,若为 L10 添加新动画,则在 device/models/l10_animation.go 中),或为通用动画创建新文件。 + +示例: + +```go +// device/models/l10_animation.go +type L10GreetingAnimation struct{} + +func NewL10GreetingAnimation() *L10GreetingAnimation { return &L10GreetingAnimation{} } +``` + +实现 device.Animation 接口: + +```go +func (a *L10GreetingAnimation) Name() string { return "greeting" } + +func (a *L10GreetingAnimation) Run(executor device.PoseExecutor, stop <-chan struct{}, speedMs int) error { + log.Printf("Running %s animation on %s", a.Name(), executor.GetHandType()) + delay := time.Duration(speedMs) * time.Millisecond + + // 示例:挥手动作 + poses := [][]byte{ + {192, 192, 192, 192, 192, 192}, // 张开 + {160, 160, 160, 160, 160, 160}, // 稍弯曲 + } + palmPoses := [][]byte{ + {100, 128, 128, 128}, // 手掌姿态 1 + {150, 128, 128, 128}, // 手掌姿态 2 + } + + for i := 0; i < 3; i++ { // 重复几次 + for j, pose := range poses { + if err := executor.SetFingerPose(pose); err != nil { return err } + if err := executor.SetPalmPose(palmPoses[j%len(palmPoses)]); err != nil { return err } // 循环使用手掌姿态 + + select { + case <-stop: + log.Printf("%s animation stopped.", a.Name()) + return nil + case <-time.After(delay): + // continue + } + } + } + return nil +} +``` + +注册动画:在对应设备的构造函数中 (例如 NewL10Hand),获取 AnimationEngine 实例并注册新动画: + +```go +// 在 NewL10Hand 中: +hand.animationEngine.Register(NewL10GreetingAnimation()) +``` + +### 添加新的传感器类型 (实现 component.Sensor 和 device.Component) + +定义传感器结构体: + +在 component/ 目录下创建新文件,例如 temperature_sensor.go。 + +定义结构体: + +```go +// component/temperature_sensor.go +package component + +import ( + "hands/device" + "math/rand/v2" + "time" + "fmt" +) + +type TemperatureSensor struct { + id string + config map[string]any + isActive bool + samplingRate int // Hz +} + +func NewTemperatureSensor(id string, config map[string]any) Sensor { // 返回 Sensor 接口 + return &TemperatureSensor{ + id: id, + config: config, + isActive: true, + samplingRate: 1, // 默认 1Hz + } +} +``` + +实现 device.Component 接口: + +```go +func (ts *TemperatureSensor) GetID() string { return ts.id } +func (ts *TemperatureSensor) GetType() device.ComponentType { return device.SensorComponent } +func (ts *TemperatureSensor) GetConfiguration() map[string]any { return ts.config } +func (ts *TemperatureSensor) IsActive() bool { return ts.isActive } +``` + +实现 component.Sensor 接口: + +```go +func (ts *TemperatureSensor) ReadData() (device.SensorData, error) { + if !ts.isActive { + return nil, fmt.Errorf("sensor %s is not active", ts.id) + } + // 模拟读取温度数据 + tempValue := 20.0 + rand.Float64()*15.0 // 20-35 度 + values := map[string]any{ + "temperature": tempValue, + "unit": "Celsius", + } + return NewSensorData(ts.id, values), nil // 使用 component.NewSensorData +} + +func (ts *TemperatureSensor) GetDataType() string { return "temperature" } + +func (ts *TemperatureSensor) GetSamplingRate() int { return ts.samplingRate } + +func (ts *TemperatureSensor) SetSamplingRate(rate int) error { + if rate <= 0 { + return fmt.Errorf("sampling rate must be positive") + } + ts.samplingRate = rate + return nil +} +``` + +集成到设备:在具体设备模型 (如 L10Hand 或 L20Hand) 的 initializeComponents 方法中,创建并添加此传感器的实例: + +```go +// 在 L10Hand.initializeComponents 中: +tempSensor1 := component.NewTemperatureSensor("temp_palm", map[string]any{"location": "palm"}) +h.components[device.SensorComponent] = append(h.components[device.SensorComponent], tempSensor1) +``` + +### 如何添加新的 Component + +添加一个新的通用组件(非特指传感器)与添加传感器类似,主要区别在于它可能不会实现 component.Sensor 接口,而是直接实现 device.Component 以及任何该组件特有的接口。 + +定义组件类型 (如果需要新的 ComponentType): 在 device/device.go 中为新的组件类型添加一个常量: + +```go +const ( + // ... + MyCustomComponentType ComponentType = "my_custom_type" +) +``` + +定义组件特定接口:如果该组件有特定行为,可以在 component/ 目录下或与组件实现同文件中定义一个接口: + +```go +// component/my_custom_component.go +package component + +import "hands/device" + +type MyCustomFunctionality interface { + PerformAction(param string) (string, error) +} +``` + +定义组件结构体:在 component/ 目录下创建新文件,例如 my_custom_component.go。 + +定义结构体: + +```go +type MyCustomComponent struct { + id string + config map[string]any + isActive bool + // ... 其他字段 +} + +func NewMyCustomComponent(id string, config map[string]any) device.Component { // 返回 device.Component + return &MyCustomComponent{ + id: id, + config: config, + isActive: true, + } +} +``` + +实现 device.Component 接口: + +```go +func (mcc *MyCustomComponent) GetID() string { return mcc.id } +func (mcc *MyCustomComponent) GetType() device.ComponentType { return MyCustomComponentType } // 使用新定义的类型 +func (mcc *MyCustomComponent) GetConfiguration() map[string]any { return mcc.config } +func (mcc *MyCustomComponent) IsActive() bool { return mcc.isActive } +``` + +实现组件特定接口: + +```go +// 确保 MyCustomComponent 也实现了 MyCustomFunctionality +func (mcc *MyCustomComponent) PerformAction(param string) (string, error) { + // 实现特定功能 + return "Action performed with " + param, nil +} +``` + +在这种情况下,NewMyCustomComponent 的返回类型可能需要同时满足 device.Component 和 MyCustomFunctionality,或者在使用时进行类型断言。一个常见的做法是返回具体类型指针 *MyCustomComponent,它自然实现了所有嵌入或直接定义的方法。或者,如果希望返回接口,可以返回 device.Component,然后在需要特定功能时进行类型断言。 + +集成到设备:在具体设备模型的 initializeComponents 方法中,创建并添加此组件的实例: + +```go +// 在 L10Hand.initializeComponents 中: +customComp := component.NewMyCustomComponent("custom_1", map[string]any{"setting": "value"}) +h.components[component.MyCustomComponentType] = append(h.components[component.MyCustomComponentType], customComp) +``` + +设备代码可能需要通过 GetComponents(component.MyCustomComponentType) 获取这些组件,并进行类型断言以调用其特定方法: + +```go +comps := h.GetComponents(component.MyCustomComponentType) +for _, comp := range comps { + if customComp, ok := comp.(component.MyCustomFunctionality); ok { // 或 *component.MyCustomComponent + result, err := customComp.PerformAction("test") + // ... + } +} +``` diff --git a/main.go b/main.go index f8daab4..9d5dcda 100644 --- a/main.go +++ b/main.go @@ -1,1162 +1,31 @@ package main import ( - "bytes" - "encoding/json" "fmt" + "hands/api" "hands/cli" - "hands/define" + "hands/config" + "hands/device" + "hands/device/models" "log" - "math/rand" - "net/http" "os" - "strings" - "sync" "time" "github.com/gin-contrib/cors" "github.com/gin-gonic/gin" ) -const HAND_TYPE_LEFT = 0x28 -const HAND_TYPE_RIGHT = 0x27 - -// 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"` -} - -// CAN 服务请求结构体 -type CanMessage struct { - Interface string `json:"interface"` - ID uint32 `json:"id"` - Data []byte `json:"data"` -} - -// 传感器数据结构体 -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"` -} - -// 手型配置结构体 -type HandConfig struct { - HandType string `json:"handType"` - HandId uint32 `json:"handId"` -} - -// 全局变量 -var ( - sensorDataMap map[string]*SensorData // 每个接口的传感器数据 - sensorMutex sync.RWMutex - animationActive map[string]bool // 每个接口的动画状态 - animationMutex sync.Mutex - stopAnimationMap map[string]chan struct{} // 每个接口的停止动画通道 - handConfigs map[string]*HandConfig // 每个接口的手型配置 - handConfigMutex sync.RWMutex - config *define.Config - serverStartTime time.Time -) - -// 验证接口是否可用 -func isValidInterface(ifName string) bool { - for _, validIface := range config.AvailableInterfaces { - if ifName == validIface { - return true - } - } - return false -} - -// 获取或创建手型配置 -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: HAND_TYPE_RIGHT, - } - - log.Printf("🆕 为接口 %s 创建默认手型配置: 右手 (0x%X)", ifName, HAND_TYPE_RIGHT) - return handConfigs[ifName] -} - -// 设置手型配置 -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 parseHandType(handType string, handId uint32, ifName string) uint32 { - // 如果提供了有效的handId,直接使用 - if handId != 0 { - return handId - } - - // 根据handType字符串确定ID - switch strings.ToLower(handType) { - case "left": - return HAND_TYPE_LEFT - case "right": - return HAND_TYPE_RIGHT - default: - // 使用接口的配置 - handConfig := getHandConfig(ifName) - return handConfig.HandId - } -} - // 初始化服务 func initService() { - log.Printf("🔧 服务配置:") - log.Printf(" - CAN 服务 URL: %s", config.CanServiceURL) - log.Printf(" - Web 端口: %s", config.WebPort) - log.Printf(" - 可用接口: %v", config.AvailableInterfaces) - log.Printf(" - 默认接口: %s", config.DefaultInterface) - - // 初始化传感器数据映射 - sensorDataMap = make(map[string]*SensorData) - for _, ifName := range 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(), - } - } - - // 初始化动画状态映射 - animationActive = make(map[string]bool) - stopAnimationMap = make(map[string]chan struct{}) - for _, ifName := range config.AvailableInterfaces { - animationActive[ifName] = false - stopAnimationMap[ifName] = make(chan struct{}, 1) - } - - // 初始化手型配置映射 - handConfigs = make(map[string]*HandConfig) + log.Printf("🔧 服务配置:") + log.Printf(" - CAN 服务 URL: %s", config.Config.CanServiceURL) + log.Printf(" - Web 端口: %s", config.Config.WebPort) + log.Printf(" - 可用接口: %v", config.Config.AvailableInterfaces) + log.Printf(" - 默认接口: %s", config.Config.DefaultInterface) log.Println("✅ 控制服务初始化完成") } -// 发送请求到 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.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 -} - -// 发送手指姿态指令 - 支持手型参数 -func sendFingerPose(ifName string, pose []byte, handType string, handId uint32) error { - if len(pose) != 6 { - return fmt.Errorf("无效的姿态数据长度,需要 6 个字节") - } - - // 如果未指定接口,使用默认接口 - if ifName == "" { - ifName = config.DefaultInterface - } - - // 验证接口 - if !isValidInterface(ifName) { - return fmt.Errorf("无效的接口 %s,可用接口: %v", ifName, 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 == 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 -} - -// 发送掌部姿态指令 - 支持手型参数 -func sendPalmPose(ifName string, pose []byte, handType string, handId uint32) error { - if len(pose) != 4 { - return fmt.Errorf("无效的姿态数据长度,需要 4 个字节") - } - - // 如果未指定接口,使用默认接口 - if ifName == "" { - ifName = config.DefaultInterface - } - - // 验证接口 - if !isValidInterface(ifName) { - return fmt.Errorf("无效的接口 %s,可用接口: %v", ifName, 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 == 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 -} - -// 在 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 startWaveAnimation(ifName string, speed int, handType string, handId uint32) { - if speed <= 0 { - speed = 500 // 默认速度 - } - - // 如果未指定接口,使用默认接口 - if ifName == "" { - ifName = config.DefaultInterface - } - - // 验证接口 - if !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.DefaultInterface - } - - // 验证接口 - if !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.AvailableInterfaces { - stopAllAnimations(validIface) - } - return - } - - // 验证接口 - if !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) - } -} - -// 重置到默认姿势 -func resetToDefaultPose(ifName string) { - // 如果未指定接口,重置所有接口 - if ifName == "" { - for _, validIface := range config.AvailableInterfaces { - resetToDefaultPose(validIface) - } - return - } - - // 验证接口 - if !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) -} - -// 读取传感器数据 (模拟) -func readSensorData() { - go func() { - for { - sensorMutex.Lock() - // 为每个接口模拟压力数据 (0-100) - for _, ifName := range 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) - } - }() -} - -// 检查 CAN 服务状态 -func checkCanServiceStatus() map[string]bool { - resp, err := http.Get(config.CanServiceURL + "/api/status") - if err != nil { - log.Printf("❌ CAN 服务状态检查失败: %v", err) - result := make(map[string]bool) - for _, ifName := range 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.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.AvailableInterfaces { - result[ifName] = false - } - return result - } - - // 检查状态数据 - result := make(map[string]bool) - for _, ifName := range 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 -} - -// API 路由设置 -func setupRoutes(r *gin.Engine) { - r.StaticFile("/", "./static/index.html") - r.Static("/static", "./static") - - api := r.Group("/api") - { - // 手型设置 API - 新增 - api.POST("/hand-type", func(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 !isValidInterface(req.Interface) { - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.AvailableInterfaces), - }) - return - } - - // 验证手型ID - if req.HandType == "left" && req.HandId != HAND_TYPE_LEFT { - req.HandId = HAND_TYPE_LEFT - } else if req.HandType == "right" && req.HandId != HAND_TYPE_RIGHT { - req.HandId = HAND_TYPE_RIGHT - } - - // 设置手型配置 - 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]interface{}{ - "interface": req.Interface, - "handType": req.HandType, - "handId": req.HandId, - }, - }) - }) - - // 手指姿态 API - 更新支持手型 - api.POST("/fingers", func(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.DefaultInterface - } - - // 验证接口 - if !isValidInterface(req.Interface) { - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.AvailableInterfaces), - }) - return - } - - stopAllAnimations(req.Interface) - - if err := 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]interface{}{"interface": req.Interface, "pose": req.Pose}, - }) - }) - - // 掌部姿态 API - 更新支持手型 - api.POST("/palm", func(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.DefaultInterface - } - - // 验证接口 - if !isValidInterface(req.Interface) { - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.AvailableInterfaces), - }) - return - } - - stopAllAnimations(req.Interface) - - if err := 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]interface{}{"interface": req.Interface, "pose": req.Pose}, - }) - }) - - // 预设姿势 API - 更新支持手型 - api.POST("/preset/:pose", func(c *gin.Context) { - pose := c.Param("pose") - - // 从查询参数获取接口名称和手型 - ifName := c.Query("interface") - handType := c.Query("handType") - - if ifName == "" { - ifName = config.DefaultInterface - } - - // 验证接口 - if !isValidInterface(ifName) { - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", ifName, config.AvailableInterfaces), - }) - return - } - - 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 = parseHandType(handType, 0, ifName) - } - - if err := 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]interface{}{"interface": ifName, "pose": fingerPose}, - }) - }) - - // 动画控制 API - 更新支持手型 - api.POST("/animation", func(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.DefaultInterface - } - - // 验证接口 - if !isValidInterface(req.Interface) { - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", req.Interface, config.AvailableInterfaces), - }) - return - } - - // 停止当前动画 - 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": - 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]interface{}{"interface": req.Interface, "speed": req.Speed}, - }) - case "sway": - 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]interface{}{"interface": req.Interface, "speed": req.Speed}, - }) - default: - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: "无效的动画类型", - }) - } - }) - - // 获取传感器数据 API - api.GET("/sensors", func(c *gin.Context) { - // 从查询参数获取接口名称 - ifName := c.Query("interface") - - sensorMutex.RLock() - defer sensorMutex.RUnlock() - - if ifName != "" { - // 验证接口 - if !isValidInterface(ifName) { - c.JSON(http.StatusBadRequest, define.ApiResponse{ - Status: "error", - Error: fmt.Sprintf("无效的接口 %s,可用接口: %v", ifName, config.AvailableInterfaces), - }) - return - } - - // 请求特定接口的数据 - if sensorData, ok := 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: sensorDataMap, - }) - } - }) - - // 系统状态 API - 更新包含手型配置 - api.GET("/status", func(c *gin.Context) { - animationMutex.Lock() - animationStatus := make(map[string]bool) - for _, ifName := range config.AvailableInterfaces { - animationStatus[ifName] = animationActive[ifName] - } - animationMutex.Unlock() - - // 检查 CAN 服务状态 - canStatus := checkCanServiceStatus() - - // 获取手型配置 - handConfigMutex.RLock() - handConfigsData := make(map[string]interface{}) - for ifName, handConfig := range handConfigs { - handConfigsData[ifName] = map[string]interface{}{ - "handType": handConfig.HandType, - "handId": handConfig.HandId, - } - } - handConfigMutex.RUnlock() - - interfaceStatuses := make(map[string]interface{}) - for _, ifName := range config.AvailableInterfaces { - interfaceStatuses[ifName] = map[string]interface{}{ - "active": canStatus[ifName], - "animationActive": animationStatus[ifName], - "handConfig": handConfigsData[ifName], - } - } - - c.JSON(http.StatusOK, define.ApiResponse{ - Status: "success", - Data: map[string]interface{}{ - "interfaces": interfaceStatuses, - "uptime": time.Since(serverStartTime).String(), - "canServiceURL": config.CanServiceURL, - "defaultInterface": config.DefaultInterface, - "availableInterfaces": config.AvailableInterfaces, - "activeInterfaces": len(canStatus), - "handConfigs": handConfigsData, - }, - }) - }) - - // 获取可用接口列表 API - 修复数据格式 - api.GET("/interfaces", func(c *gin.Context) { - // 确保返回前端期望的数据格式 - responseData := map[string]interface{}{ - "availableInterfaces": config.AvailableInterfaces, - "defaultInterface": config.DefaultInterface, - } - - c.JSON(http.StatusOK, define.ApiResponse{ - Status: "success", - Data: responseData, - }) - }) - - // 获取手型配置 API - 新增 - api.GET("/hand-configs", func(c *gin.Context) { - handConfigMutex.RLock() - defer handConfigMutex.RUnlock() - - result := make(map[string]interface{}) - for _, ifName := range config.AvailableInterfaces { - if handConfig, exists := handConfigs[ifName]; exists { - result[ifName] = map[string]interface{}{ - "handType": handConfig.HandType, - "handId": handConfig.HandId, - } - } else { - // 返回默认配置 - result[ifName] = map[string]interface{}{ - "handType": "right", - "handId": HAND_TYPE_RIGHT, - } - } - } - - c.JSON(http.StatusOK, define.ApiResponse{ - Status: "success", - Data: result, - }) - }) - - // 健康检查端点 - 新增,用于调试 - api.GET("/health", func(c *gin.Context) { - c.JSON(http.StatusOK, define.ApiResponse{ - Status: "success", - Message: "CAN Control Service is running", - Data: map[string]interface{}{ - "timestamp": time.Now(), - "availableInterfaces": config.AvailableInterfaces, - "defaultInterface": config.DefaultInterface, - "serviceVersion": "1.0.0-hand-type-support", - }, - }) - }) - } -} - func printUsage() { fmt.Println("CAN Control Service with Hand Type Support") fmt.Println("Usage:") @@ -1192,31 +61,22 @@ func main() { } // 解析配置 - config = cli.ParseConfig() + config.Config = cli.ParseConfig() // 验证配置 - if len(config.AvailableInterfaces) == 0 { + if len(config.Config.AvailableInterfaces) == 0 { log.Fatal("❌ 没有可用的 CAN 接口") } - if config.DefaultInterface == "" { + if config.Config.DefaultInterface == "" { log.Fatal("❌ 没有设置默认 CAN 接口") } - // 记录启动时间 - serverStartTime = time.Now() - log.Printf("🚀 启动 CAN 控制服务 (支持左右手配置)") - // 初始化随机数种子 - rand.Seed(time.Now().UnixNano()) - // 初始化服务 initService() - // 启动传感器数据模拟 - readSensorData() - // 设置 Gin 模式 gin.SetMode(gin.ReleaseMode) @@ -1232,17 +92,19 @@ func main() { MaxAge: 12 * time.Hour, })) + models.RegisterDeviceTypes() + // 设置 API 路由 - setupRoutes(r) + api.NewServer(device.NewDeviceManager()).SetupRoutes(r) // 启动服务器 - log.Printf("🌐 CAN 控制服务运行在 http://localhost:%s", config.WebPort) - log.Printf("📡 连接到 CAN 服务: %s", config.CanServiceURL) - log.Printf("🎯 默认接口: %s", config.DefaultInterface) - log.Printf("🔌 可用接口: %v", config.AvailableInterfaces) + log.Printf("🌐 CAN 控制服务运行在 http://localhost:%s", config.Config.WebPort) + log.Printf("📡 连接到 CAN 服务: %s", config.Config.CanServiceURL) + log.Printf("🎯 默认接口: %s", config.Config.DefaultInterface) + log.Printf("🔌 可用接口: %v", config.Config.AvailableInterfaces) log.Printf("🤖 支持左右手动态配置") - if err := r.Run(":" + config.WebPort); err != nil { + if err := r.Run(":" + config.Config.WebPort); err != nil { log.Fatalf("❌ 服务启动失败: %v", err) } } diff --git a/static/index.html b/static/index.html index a7fff2c..f565dd9 100644 --- a/static/index.html +++ b/static/index.html @@ -39,7 +39,7 @@