|
|
@@ -169,23 +169,30 @@ func (c *MQTTCoupler) doCmd(method string, params any, id ...int) (*jsonrpc2.Res
|
|
|
return zero, token.Error()
|
|
|
}
|
|
|
|
|
|
- var timer *time.Timer
|
|
|
- var timeout <-chan time.Time
|
|
|
- if c.isCtrlCommand(method) {
|
|
|
+ if c.isCtrlCommand(method) { // 控制指令, 等待结果或超时
|
|
|
+ var timer *time.Timer
|
|
|
+ var timeout <-chan time.Time
|
|
|
timer = time.NewTimer(3 * time.Second)
|
|
|
timeout = timer.C
|
|
|
defer timer.Stop()
|
|
|
- }
|
|
|
|
|
|
- select {
|
|
|
- case <-c.ctx.Done():
|
|
|
- return zero, c.ctx.Err()
|
|
|
- case resp := <-ch:
|
|
|
- return resp, nil
|
|
|
- case <-timeout:
|
|
|
- return zero, fmt.Errorf("command timeout")
|
|
|
- case <-c.interrupted:
|
|
|
- return zero, fmt.Errorf("command interrupted by user")
|
|
|
+ select {
|
|
|
+ case <-c.ctx.Done():
|
|
|
+ return zero, c.ctx.Err()
|
|
|
+ case resp := <-ch:
|
|
|
+ return resp, nil
|
|
|
+ case <-timeout:
|
|
|
+ return zero, fmt.Errorf("command timeout")
|
|
|
+ }
|
|
|
+ } else { // 用户指令, 等待结果或用户主动中断取消: Ctrl+C
|
|
|
+ select {
|
|
|
+ case <-c.ctx.Done():
|
|
|
+ return zero, c.ctx.Err()
|
|
|
+ case resp := <-ch:
|
|
|
+ return resp, nil
|
|
|
+ case <-c.interrupted:
|
|
|
+ return zero, fmt.Errorf("command interrupted by user")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|