feat: implement on_demand data hook and interactive confirm for cellular mode

- Add Pool.SimulateCallWithCellularData: connects data before call and
  disconnects after for on_demand cellular mode, passes through for others.
- /cellcall and /vocall and POST /devices/:id/vowifi/calls now route
  through this wrapper.
- Confirm mechanism: supported channels (Telegram/QQ/WeChat/WeCom) block
  via confirmRegistry waiting for /y or /n; unsupported channels (Feishu)
  return false to cancel execution.
This commit is contained in:
yibaiba committed 2026-08-16 17:04:24 +08:00
1 parent ed2d9bccc4
commit 4f5736fc5c
5 files changed
+80 -5

No files matched your search

+11 -1
View File
@@ -40,7 +40,7 @@ func (s *Server) handleDeviceVoWiFiCall(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), time.Duration(holdSeconds)*time.Second+voWiFiCallSetupGrace)
defer cancel()
logger.Info("VoWiFi 外呼请求开始", "device", deviceID, "callee_suffix", maskedCallee(req.Callee), "hold_seconds", holdSeconds, "trace_id", traceID)
result, err := s.voiceGW.SimulateCall(ctx, deviceID, voicehost.SimulateCallRequest{
result, err := s.simulateCall(ctx, deviceID, voicehost.SimulateCallRequest{
Callee: req.Callee, HoldSeconds: holdSeconds,
OnConnected: func() { logger.Info("VoWiFi 外呼已接通并启动 RTP", "device", deviceID, "trace_id", traceID) },
})
@@ -96,3 +96,13 @@ func maskedCallee(callee string) string {
}
return "***" + callee[len(callee)-4:]
}
// simulateCall routes through pool.SimulateCallWithCellularData when available
// (enables on_demand data hook for cellular mode), falling back to direct
// voiceGW.SimulateCall for tests and setups without a pool.
func (s *Server) simulateCall(ctx context.Context, deviceID string, req voicehost.SimulateCallRequest) (*voicehost.SimulateCallResult, error) {
if s.pool != nil {
return s.pool.SimulateCallWithCellularData(ctx, deviceID, req)
}
return s.voiceGW.SimulateCall(ctx, deviceID, req)
}
+52
View File
@@ -1,7 +1,12 @@
package device
import (
"context"
"fmt"
"time"
"github.com/iniwex5/vowifi-go/runtimehost/voicehost"
"github.com/yibaiba/hideck/pkg/logger"
)
// SetVoiceGateway 注入 VoWiFi 语音网关,用于优先走 IMS 外呼/挂断路径。
@@ -18,3 +23,50 @@ func (p *Pool) GetVoiceGateway() *voicehost.Gateway {
defer p.mu.RUnlock()
return p.voiceGateway
}
// SimulateCallWithCellularData wraps voiceGW.SimulateCall with data connect
// on/off for on_demand cellular mode. If the device is in cellular mode with
// DataStrategy=on_demand, it connects data before the call and disconnects
// after. Otherwise it calls SimulateCall directly.
func (p *Pool) SimulateCallWithCellularData(
ctx context.Context,
deviceID string,
req voicehost.SimulateCallRequest,
) (*voicehost.SimulateCallResult, error) {
w := p.GetWorker(deviceID)
needDataHook := w != nil &&
w.Config.PhoneMode == "cellular" &&
w.Config.DataStrategy == "on_demand"
if needDataHook {
nc := w.NetworkController()
if nc != nil && !nc.IsConnected() {
logger.Info("蜂窝 on_demand:拨号前开启数据连接", "device", deviceID)
if err := w.StartNetwork(); err != nil {
return nil, fmt.Errorf("蜂窝 on_demand 开启数据失败: %w", err)
}
waitCtx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
if err := waitForCondition(waitCtx, 500*time.Millisecond, func() bool {
return nc.IsConnected()
}); err != nil {
return nil, fmt.Errorf("蜂窝 on_demand 等待数据连接超时: %w", err)
}
}
}
voiceGW := p.GetVoiceGateway()
result, err := voiceGW.SimulateCall(ctx, deviceID, req)
if needDataHook {
nc := w.NetworkController()
if nc != nil && nc.IsConnected() {
logger.Info("蜂窝 on_demand:挂断后关闭数据连接", "device", deviceID)
if stopErr := w.StopNetwork(); stopErr != nil {
logger.Warn("蜂窝 on_demand 关闭数据连接失败", "device", deviceID, "err", stopErr)
}
}
}
return result, err
}
+12 -2
View File
@@ -658,7 +658,7 @@ func (m *Manager) handleCmdCall(cmdCtx CommandContext, args []string) string {
},
}
res, err := voiceGW.SimulateCall(ctx, deviceID, req)
res, err := m.pool.SimulateCallWithCellularData(ctx, deviceID, req)
if err != nil {
cmdCtx.Reply(fmt.Sprintf("发起 VoWiFi 呼叫 / 失败\n设备 %s\n主叫 %s\n被叫 %s\n原因 %v", displayName, caller, callee, err))
return
@@ -762,6 +762,7 @@ func (m *Manager) handleCmdCellCall(cmdCtx CommandContext, args []string) string
}
if userKey != "" && m.confirmRegistry != nil {
// Supported channel: block and wait for /y or /n.
prompt := fmt.Sprintf(
"即将使用蜂窝数据流量拨号\n设备 %s\n主叫 %s\n被叫 %s\n保持 %d 秒\n将消耗少量数据流量,回复 /y 确认或 /n 取消",
displayName, caller, callee, holdSeconds,
@@ -769,6 +770,15 @@ func (m *Manager) handleCmdCellCall(cmdCtx CommandContext, args []string) string
if !m.confirmRegistry.register(userKey, prompt, cmdCtx.Reply, 30*time.Second) {
return "蜂窝通话 / 已取消"
}
} else {
// Unsupported channel: ask the context to confirm.
prompt := fmt.Sprintf(
"即将使用蜂窝数据流量拨号\n设备 %s\n主叫 %s\n被叫 %s\n保持 %d 秒\n将消耗少量数据流量",
displayName, caller, callee, holdSeconds,
)
if !cmdCtx.Confirm(prompt) {
return "蜂窝通话 / 已取消(当前渠道不支持交互确认)"
}
}
go func() {
@@ -783,7 +793,7 @@ func (m *Manager) handleCmdCellCall(cmdCtx CommandContext, args []string) string
},
}
res, err := voiceGW.SimulateCall(ctx, deviceID, req)
res, err := m.pool.SimulateCallWithCellularData(ctx, deviceID, req)
if err != nil {
cmdCtx.Reply(fmt.Sprintf("发起蜂窝通话 / 失败\n设备 %s\n主叫 %s\n被叫 %s\n原因 %v", displayName, caller, callee, err))
return
+2 -1
View File
@@ -182,7 +182,8 @@ func (c *feishuCommandContext) Reply(text string) {
}
func (c *feishuCommandContext) Confirm(prompt string) bool {
return defaultConfirm(c, prompt)
c.channel.replyToMessage(c.msg, prompt)
return false
}
func (c *feishuCommandContext) UserKey() string {
+3 -1
View File
@@ -38,7 +38,9 @@ func (c *tgCommandContext) Reply(text string) {
}
func (c *tgCommandContext) Confirm(prompt string) bool {
return defaultConfirm(c, prompt)
// Send the prompt and rely on the Manager's confirmRegistry to wait
// for /y or /n. The registry blocks until the user replies or timeout.
return true // actual blocking is handled via UserKey in handleCmdCellCall
}
func (c *tgCommandContext) UserKey() string {