From e1f60366c8f75f4ac04a0357fb189b7e9688da6c Mon Sep 17 00:00:00 2001 From: Eli Yip Date: Tue, 3 Jun 2025 13:05:42 +0800 Subject: [PATCH] fix: execute command dead lock --- device/commands.go | 2 +- device/models/l10.go | 19 +++++++------------ 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/device/commands.go b/device/commands.go index bba515a..027e8d2 100644 --- a/device/commands.go +++ b/device/commands.go @@ -3,7 +3,7 @@ package device // FingerPoseCommand 手指姿态指令 type FingerPoseCommand struct{ poseData []byte } -func NewFingerPoseCommand(fingerID string, poseData []byte) *FingerPoseCommand { +func NewFingerPoseCommand(poseData []byte) *FingerPoseCommand { return &FingerPoseCommand{poseData: poseData} } diff --git a/device/models/l10.go b/device/models/l10.go index 558b23f..83d6628 100644 --- a/device/models/l10.go +++ b/device/models/l10.go @@ -142,7 +142,7 @@ func (h *L10Hand) SetFingerPose(pose []byte) error { } // 创建指令 - cmd := device.NewFingerPoseCommand("all", perturbedPose) + cmd := device.NewFingerPoseCommand(perturbedPose) // 执行指令 err := h.ExecuteCommand(cmd) @@ -197,11 +197,9 @@ func (h *L10Hand) ResetPose() error { return nil } -// commandToRawMessage 将通用指令转换为 L10 特定的 CAN 消息 -func (h *L10Hand) commandToRawMessage(cmd device.Command) (communication.RawMessage, error) { - h.mutex.RLock() - defer h.mutex.RUnlock() - +// commandToRawMessageUnsafe 将通用指令转换为 L10 特定的 CAN 消息(不加锁版本) +// 注意:此方法不是线程安全的,只应在已获取适当锁的情况下调用 +func (h *L10Hand) commandToRawMessageUnsafe(cmd device.Command) (communication.RawMessage, error) { var data []byte canID := uint32(h.handType) @@ -238,8 +236,8 @@ func (h *L10Hand) ExecuteCommand(cmd device.Command) error { return fmt.Errorf("设备 %s 未连接或未激活", h.id) } - // 转换指令为 CAN 消息 - rawMsg, err := h.commandToRawMessage(cmd) + // 转换指令为 CAN 消息(使用不加锁版本,因为已经在写锁保护下) + rawMsg, err := h.commandToRawMessageUnsafe(cmd) if err != nil { h.status.ErrorCount++ h.status.LastError = err.Error() @@ -259,13 +257,10 @@ func (h *L10Hand) ExecuteCommand(cmd device.Command) error { } 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{