refactor: keep legacy sensor data
This commit is contained in:
parent
e1f60366c8
commit
f8aa2138ab
@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"hands/config"
|
"hands/config"
|
||||||
"hands/define"
|
"hands/define"
|
||||||
"hands/device"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
@ -430,7 +429,6 @@ func (s *LegacyServer) handleAnimation(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// handleSensors 获取传感器数据处理函数
|
// handleSensors 获取传感器数据处理函数
|
||||||
// TODO: 现在的传感器数据都是模拟的,先不进行严格定义,等到有文档了之后修改设备层和这里
|
|
||||||
func (s *LegacyServer) handleSensors(c *gin.Context) {
|
func (s *LegacyServer) handleSensors(c *gin.Context) {
|
||||||
// 从查询参数获取接口名称
|
// 从查询参数获取接口名称
|
||||||
ifName := c.Query("interface")
|
ifName := c.Query("interface")
|
||||||
@ -455,32 +453,17 @@ func (s *LegacyServer) handleSensors(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取设备的传感器组件
|
sensorData, err := dev.ReadSensorData()
|
||||||
sensorComponents := dev.GetComponents(device.SensorComponent)
|
if err != nil {
|
||||||
|
c.JSON(http.StatusInternalServerError, define.ApiResponse{
|
||||||
// 构建传感器数据响应
|
Status: "error",
|
||||||
sensorData := make(map[string]any)
|
Error: "获取传感器数据失败:" + err.Error(),
|
||||||
for _, component := range sensorComponents {
|
})
|
||||||
sensorId := component.GetID()
|
|
||||||
data, err := dev.ReadSensorData(sensorId)
|
|
||||||
if err != nil {
|
|
||||||
// 如果读取失败,记录错误状态
|
|
||||||
sensorData[sensorId] = map[string]any{
|
|
||||||
"error": err.Error(),
|
|
||||||
"timestamp": time.Now(),
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
sensorData[sensorId] = map[string]any{
|
|
||||||
"values": data.Values(),
|
|
||||||
"timestamp": data.Timestamp(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, define.ApiResponse{
|
c.JSON(http.StatusOK, define.ApiResponse{
|
||||||
Status: "success",
|
Status: "success",
|
||||||
Data: sensorData,
|
Data: sensorData.Values(),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// 返回所有接口的传感器数据
|
// 返回所有接口的传感器数据
|
||||||
@ -490,35 +473,17 @@ func (s *LegacyServer) handleSensors(c *gin.Context) {
|
|||||||
// 获取对应的设备
|
// 获取对应的设备
|
||||||
dev, err := s.mapper.GetDeviceForInterface(ifName)
|
dev, err := s.mapper.GetDeviceForInterface(ifName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
allSensorData[ifName] = map[string]any{
|
allSensorData[ifName] = map[string]any{"error": "设备不可用:" + err.Error()}
|
||||||
"error": "设备不可用:" + err.Error(),
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取设备的传感器组件
|
sensorData, err := dev.ReadSensorData()
|
||||||
sensorComponents := dev.GetComponents(device.SensorComponent)
|
if err != nil {
|
||||||
|
allSensorData[ifName] = map[string]any{"error": "设备不可用:" + err.Error()}
|
||||||
// 构建传感器数据响应
|
continue
|
||||||
sensorData := make(map[string]any)
|
|
||||||
for _, component := range sensorComponents {
|
|
||||||
sensorId := component.GetID()
|
|
||||||
data, err := dev.ReadSensorData(sensorId)
|
|
||||||
if err != nil {
|
|
||||||
sensorData[sensorId] = map[string]any{
|
|
||||||
"error": err.Error(),
|
|
||||||
"timestamp": time.Now(),
|
|
||||||
}
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
sensorData[sensorId] = map[string]any{
|
|
||||||
"values": data.Values(),
|
|
||||||
"timestamp": data.Timestamp(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
allSensorData[ifName] = sensorData
|
allSensorData[ifName] = sensorData.Values()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, define.ApiResponse{
|
c.JSON(http.StatusOK, define.ApiResponse{
|
||||||
|
@ -67,8 +67,7 @@ func (s *Server) SetupRoutes(r *gin.Engine) {
|
|||||||
// 传感器数据路由
|
// 传感器数据路由
|
||||||
sensors := deviceRoutes.Group("/sensors")
|
sensors := deviceRoutes.Group("/sensors")
|
||||||
{
|
{
|
||||||
sensors.GET("", s.handleGetSensors) // 获取所有传感器数据
|
sensors.GET("", s.handleGetSensors) // 获取所有传感器数据
|
||||||
sensors.GET("/:sensorId", s.handleGetSensorData) // 获取特定传感器数据
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设备状态路由
|
// 设备状态路由
|
||||||
|
@ -3,7 +3,6 @@ package api
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"hands/device"
|
"hands/device"
|
||||||
|
|
||||||
@ -24,103 +23,17 @@ func (s *Server) handleGetSensors(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取设备的传感器组件
|
sensorData, err := dev.ReadSensorData()
|
||||||
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 {
|
if err != nil {
|
||||||
c.JSON(http.StatusInternalServerError, ApiResponse{
|
c.JSON(http.StatusInternalServerError, ApiResponse{
|
||||||
Status: "error",
|
Status: "error",
|
||||||
Error: fmt.Sprintf("读取传感器 %s 数据失败:%v", sensorId, err),
|
Error: fmt.Sprintf("读取传感器数据失败:%v", err),
|
||||||
})
|
})
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 转换为响应格式
|
|
||||||
response := SensorDataResponse{
|
|
||||||
SensorID: sensorData.SensorID(),
|
|
||||||
Timestamp: sensorData.Timestamp(),
|
|
||||||
Values: sensorData.Values(),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
c.JSON(http.StatusOK, ApiResponse{
|
c.JSON(http.StatusOK, ApiResponse{
|
||||||
Status: "success",
|
Status: "success",
|
||||||
Data: response,
|
Data: sensorData.Values(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,78 +0,0 @@
|
|||||||
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
|
|
||||||
}
|
|
@ -2,41 +2,69 @@ package component
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"hands/device"
|
"hands/device"
|
||||||
|
"math/rand/v2"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Sensor 传感器组件接口
|
// Sensor 传感器组件接口
|
||||||
type Sensor interface {
|
type Sensor interface {
|
||||||
device.Component
|
// device.Component
|
||||||
ReadData() (device.SensorData, error)
|
ReadData() (device.SensorData, error)
|
||||||
GetDataType() string
|
// GetDataType() string
|
||||||
GetSamplingRate() int
|
// GetSamplingRate() int
|
||||||
SetSamplingRate(rate int) error
|
// SetSamplingRate(rate int) error
|
||||||
|
MockData()
|
||||||
}
|
}
|
||||||
|
|
||||||
// SensorDataImpl 传感器数据的具体实现
|
// SensorDataImpl 传感器数据的具体实现
|
||||||
type SensorDataImpl struct {
|
type SensorDataImpl struct {
|
||||||
timestamp time.Time
|
Interface string `json:"interface"`
|
||||||
values map[string]any
|
Thumb int `json:"thumb"`
|
||||||
sensorID string
|
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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSensorData(sensorID string, values map[string]any) *SensorDataImpl {
|
func NewSensorData(ifName string) *SensorDataImpl {
|
||||||
return &SensorDataImpl{
|
return &SensorDataImpl{
|
||||||
timestamp: time.Now(),
|
Interface: ifName,
|
||||||
values: values,
|
Thumb: 0,
|
||||||
sensorID: sensorID,
|
Index: 0,
|
||||||
|
Middle: 0,
|
||||||
|
Ring: 0,
|
||||||
|
Pinky: 0,
|
||||||
|
PalmPosition: []byte{128, 128, 128, 128},
|
||||||
|
LastUpdate: time.Now(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SensorDataImpl) Timestamp() time.Time {
|
func (s *SensorDataImpl) MockData() {
|
||||||
return s.timestamp
|
go func() {
|
||||||
|
for {
|
||||||
|
s.Thumb = rand.IntN(101)
|
||||||
|
s.Index = rand.IntN(101)
|
||||||
|
s.Middle = rand.IntN(101)
|
||||||
|
s.Ring = rand.IntN(101)
|
||||||
|
s.Pinky = rand.IntN(101)
|
||||||
|
s.LastUpdate = time.Now()
|
||||||
|
time.Sleep(500 * time.Millisecond)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SensorDataImpl) Values() map[string]any {
|
func (s *SensorDataImpl) Values() map[string]any {
|
||||||
return s.values
|
return map[string]any{
|
||||||
|
"thumb": s.Thumb,
|
||||||
|
"index": s.Index,
|
||||||
|
"middle": s.Middle,
|
||||||
|
"ring": s.Ring,
|
||||||
|
"pinky": s.Pinky,
|
||||||
|
"palmPosition": s.PalmPosition,
|
||||||
|
"lastUpdate": s.LastUpdate,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SensorDataImpl) SensorID() string {
|
func (s *SensorDataImpl) ReadData() (device.SensorData, error) { return s, nil }
|
||||||
return s.sensorID
|
|
||||||
}
|
|
||||||
|
@ -12,7 +12,7 @@ type Device interface {
|
|||||||
GetHandType() define.HandType // 获取设备手型
|
GetHandType() define.HandType // 获取设备手型
|
||||||
SetHandType(handType define.HandType) error // 设置设备手型
|
SetHandType(handType define.HandType) error // 设置设备手型
|
||||||
ExecuteCommand(cmd Command) error // 执行一个通用指令
|
ExecuteCommand(cmd Command) error // 执行一个通用指令
|
||||||
ReadSensorData(sensorID string) (SensorData, error) // 读取特定传感器数据
|
ReadSensorData() (SensorData, error) // 读取特定传感器数据
|
||||||
GetComponents(componentType ComponentType) []Component // 获取指定类型的组件
|
GetComponents(componentType ComponentType) []Component // 获取指定类型的组件
|
||||||
GetStatus() (DeviceStatus, error) // 获取设备状态
|
GetStatus() (DeviceStatus, error) // 获取设备状态
|
||||||
Connect() error // 连接设备
|
Connect() error // 连接设备
|
||||||
@ -37,9 +37,9 @@ type Command interface {
|
|||||||
|
|
||||||
// SensorData 代表从传感器读取的数据
|
// SensorData 代表从传感器读取的数据
|
||||||
type SensorData interface {
|
type SensorData interface {
|
||||||
Timestamp() time.Time
|
// Timestamp() time.Time
|
||||||
Values() map[string]any // 例如 {"pressure": 100, "angle": 30.5}
|
Values() map[string]any // 例如 {"pressure": 100, "angle": 30.5}
|
||||||
SensorID() string
|
// SensorID() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ComponentType 定义组件类型
|
// ComponentType 定义组件类型
|
||||||
@ -53,10 +53,10 @@ const (
|
|||||||
|
|
||||||
// Component 代表设备的一个可插拔组件
|
// Component 代表设备的一个可插拔组件
|
||||||
type Component interface {
|
type Component interface {
|
||||||
GetID() string
|
// GetID() string
|
||||||
GetType() ComponentType
|
// GetType() ComponentType
|
||||||
GetConfiguration() map[string]interface{} // 组件的特定配置
|
// GetConfiguration() map[string]interface{} // 组件的特定配置
|
||||||
IsActive() bool
|
// IsActive() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeviceStatus 代表设备状态
|
// DeviceStatus 代表设备状态
|
||||||
|
@ -263,13 +263,9 @@ func (h *L10Hand) ExecuteCommand(cmd device.Command) error {
|
|||||||
|
|
||||||
func (h *L10Hand) initializeComponents(_ map[string]any) error {
|
func (h *L10Hand) initializeComponents(_ map[string]any) error {
|
||||||
// 初始化传感器组件
|
// 初始化传感器组件
|
||||||
sensors := []device.Component{
|
defaultSensor := component.NewSensorData(h.canInterface)
|
||||||
component.NewPressureSensor("pressure_thumb", map[string]any{"location": "thumb"}),
|
defaultSensor.MockData()
|
||||||
component.NewPressureSensor("pressure_index", map[string]any{"location": "index"}),
|
sensors := []device.Component{defaultSensor}
|
||||||
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
|
h.components[device.SensorComponent] = sensors
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -282,19 +278,17 @@ func (h *L10Hand) GetModel() string {
|
|||||||
return h.model
|
return h.model
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *L10Hand) ReadSensorData(sensorID string) (device.SensorData, error) {
|
func (h *L10Hand) ReadSensorData() (device.SensorData, error) {
|
||||||
h.mutex.RLock()
|
h.mutex.RLock()
|
||||||
defer h.mutex.RUnlock()
|
defer h.mutex.RUnlock()
|
||||||
|
|
||||||
sensors := h.components[device.SensorComponent]
|
sensors := h.components[device.SensorComponent]
|
||||||
for _, comp := range sensors {
|
for _, comp := range sensors {
|
||||||
if comp.GetID() == sensorID {
|
if sensor, ok := comp.(component.Sensor); ok {
|
||||||
if sensor, ok := comp.(component.Sensor); ok {
|
return sensor.ReadData()
|
||||||
return sensor.ReadData()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("传感器 %s 不存在", sensorID)
|
return nil, fmt.Errorf("传感器不存在")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *L10Hand) GetComponents(componentType device.ComponentType) []device.Component {
|
func (h *L10Hand) GetComponents(componentType device.ComponentType) []device.Component {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user