From 4f5736fc5c775c2cb72cb41525e5e55ed4e8bf56 Mon Sep 17 00:00:00 2001 From: yibaiba <946100224@qq.com> Date: Sun, 16 Aug 2026 17:04:24 +0800 Subject: [PATCH] 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. --- internal/api/device_vowifi_voice.go | 12 ++++++- internal/device/pool_vowifi_wiring.go | 52 +++++++++++++++++++++++++++ internal/notify/commands.go | 14 ++++++-- internal/notify/feishu.go | 3 +- internal/notify/telegram_command.go | 4 ++- 5 files changed, 80 insertions(+), 5 deletions(-) diff --git a/internal/api/device_vowifi_voice.go b/internal/api/device_vowifi_voice.go index bd912f62..f9988217 100644 --- a/internal/api/device_vowifi_voice.go +++ b/internal/api/device_vowifi_voice.go @@ -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) +} diff --git a/internal/device/pool_vowifi_wiring.go b/internal/device/pool_vowifi_wiring.go index c3839e03..4978c6a0 100644 --- a/internal/device/pool_vowifi_wiring.go +++ b/internal/device/pool_vowifi_wiring.go @@ -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 +} diff --git a/internal/notify/commands.go b/internal/notify/commands.go index 49c24b20..66a75758 100644 --- a/internal/notify/commands.go +++ b/internal/notify/commands.go @@ -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 diff --git a/internal/notify/feishu.go b/internal/notify/feishu.go index 22b1f6e9..ea737f52 100644 --- a/internal/notify/feishu.go +++ b/internal/notify/feishu.go @@ -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 { diff --git a/internal/notify/telegram_command.go b/internal/notify/telegram_command.go index 827acf72..d630e7ac 100644 --- a/internal/notify/telegram_command.go +++ b/internal/notify/telegram_command.go @@ -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 {