mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 09:28:14 +00:00
feat(waiter,devicebridge): 设备桥连接带授权态,命令处理器类型统一
- `runDeviceBridgeLoop`/`connectDeviceBridge` 增加 `authorized` 入参(未授权不再尝试建立桥连); - `Bridge.OnCmd` 的处理器类型改为具名 `BridgeCmdHandler`,与 waiter 侧签名对齐。
This commit is contained in:
@ -39,7 +39,7 @@ type msgEntry struct {
|
|||||||
|
|
||||||
type daemonHandler struct {
|
type daemonHandler struct {
|
||||||
// homed 连接
|
// homed 连接
|
||||||
homeMu sync.Mutex
|
homeMu sync.Mutex
|
||||||
homeConn net.Conn
|
homeConn net.Conn
|
||||||
homeR *bufio.Reader
|
homeR *bufio.Reader
|
||||||
homeCfg *Config
|
homeCfg *Config
|
||||||
@ -304,14 +304,14 @@ func startDaemonDeviceBridge(cfg *Config) {
|
|||||||
if dg == "" || dt == "" {
|
if dg == "" || dt == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
// 设备桥重连循环:WS 断开时自动重连
|
// 设备桥重连循环:WS 断开时自动重连,并保留配置中的本地授权状态。
|
||||||
go runDeviceBridgeLoop(dg, dt)
|
go runDeviceBridgeLoop(dg, dt, cfg.DeviceAuthorized)
|
||||||
}
|
}
|
||||||
|
|
||||||
// runDeviceBridgeLoop 无限重连循环:建立设备桥 → 等待断开 → 重连。
|
// runDeviceBridgeLoop 无限重连循环:建立设备桥 → 等待断开 → 重连。
|
||||||
func runDeviceBridgeLoop(gateway, token string) {
|
func runDeviceBridgeLoop(gateway, token string, authorized bool) {
|
||||||
for {
|
for {
|
||||||
bridge, err := connectDeviceBridge(gateway, token)
|
bridge, err := connectDeviceBridge(gateway, token, authorized)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[daemon] device bridge connect failed: %v, retrying in 5s", err)
|
log.Printf("[daemon] device bridge connect failed: %v, retrying in 5s", err)
|
||||||
time.Sleep(5 * time.Second)
|
time.Sleep(5 * time.Second)
|
||||||
@ -325,7 +325,7 @@ func runDeviceBridgeLoop(gateway, token string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// connectDeviceBridge 创建并启动一次设备桥,返回 bridge 实例供 Wait()。
|
// connectDeviceBridge 创建并启动一次设备桥,返回 bridge 实例供 Wait()。
|
||||||
func connectDeviceBridge(gateway, token string) (*client.Bridge, error) {
|
func connectDeviceBridge(gateway, token string, authorized bool) (*client.Bridge, error) {
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
if hostname == "" {
|
if hostname == "" {
|
||||||
hostname = "local"
|
hostname = "local"
|
||||||
@ -355,12 +355,17 @@ func connectDeviceBridge(gateway, token string) (*client.Bridge, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bridge := client.New(gw, token, deviceID, hostname, caps, info)
|
bridge := client.New(gw, token, deviceID, hostname, caps, info)
|
||||||
|
bridge.SetAuthorized(authorized)
|
||||||
|
|
||||||
// 注册命令处理器
|
// 注册命令处理器:cmd_type 是主信号,同时兼容旧版 homeagent-* 文本前缀。
|
||||||
cr := client.NewCmdRouter()
|
cr := client.NewCmdRouter()
|
||||||
cr.Handle("homeagent-", handleHomeagentCmd)
|
cr.Handle("homeagent-", handleHomeagentCmd)
|
||||||
cr.HandleDefault(handleShellCmd)
|
cr.HandleDefault(handleShellCmd)
|
||||||
bridge.OnCmd(func(reqID, command string) {
|
bridge.OnCmd(func(reqID, command, cmdType string) {
|
||||||
|
if cmdType == "homeagent" {
|
||||||
|
handleHomeagentCmd(reqID, command)
|
||||||
|
return
|
||||||
|
}
|
||||||
cr.Dispatch(reqID, command)
|
cr.Dispatch(reqID, command)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -371,8 +376,6 @@ func connectDeviceBridge(gateway, token string) (*client.Bridge, error) {
|
|||||||
// 设置全局变量供 sendBridgeResult 使用
|
// 设置全局变量供 sendBridgeResult 使用
|
||||||
deviceBridge = bridge
|
deviceBridge = bridge
|
||||||
deviceBridgeID = deviceID
|
deviceBridgeID = deviceID
|
||||||
auth := true // daemon 模式默认授权(配置已指定)
|
|
||||||
bridge.SetAuthorized(auth)
|
|
||||||
|
|
||||||
return bridge, nil
|
return bridge, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -61,10 +61,14 @@ func startDeviceBridge(addr, token string) error {
|
|||||||
bridge := client.New(gateway, token, deviceID, "HomeAgent CLI", caps, info)
|
bridge := client.New(gateway, token, deviceID, "HomeAgent CLI", caps, info)
|
||||||
cmdRouter = client.NewCmdRouter()
|
cmdRouter = client.NewCmdRouter()
|
||||||
|
|
||||||
// 注册命令处理器
|
// cmd_type 是主路由信号;保留 homeagent-* 文本前缀兼容旧服务端。
|
||||||
cmdRouter.Handle("homeagent-", handleHomeagentCmd)
|
cmdRouter.Handle("homeagent-", handleHomeagentCmd)
|
||||||
cmdRouter.HandleDefault(handleShellCmd)
|
cmdRouter.HandleDefault(handleShellCmd)
|
||||||
bridge.OnCmd(func(reqID, command string) {
|
bridge.OnCmd(func(reqID, command, cmdType string) {
|
||||||
|
if cmdType == "homeagent" {
|
||||||
|
handleHomeagentCmd(reqID, command)
|
||||||
|
return
|
||||||
|
}
|
||||||
cmdRouter.Dispatch(reqID, command)
|
cmdRouter.Dispatch(reqID, command)
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -767,4 +771,4 @@ func sanitizeID(s string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return sb.String()
|
return sb.String()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,10 +10,12 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CmdHandler 是命令处理回调类型。
|
// CmdHandler 是本地命令路由回调类型。
|
||||||
// 当收到 remotedevice 下发的 cmd 时调用,reqID 用于回执,command 是命令内容。
|
|
||||||
type CmdHandler func(reqID, command string)
|
type CmdHandler func(reqID, command string)
|
||||||
|
|
||||||
|
// BridgeCmdHandler 接收服务端明确下发的路由信号(shell 或 homeagent)。
|
||||||
|
type BridgeCmdHandler func(reqID, command, cmdType string)
|
||||||
|
|
||||||
// CmdResult 是命令执行结果回调(用于异步通知 GUI 层)。
|
// CmdResult 是命令执行结果回调(用于异步通知 GUI 层)。
|
||||||
type CmdResultHandler func(reqID, status, output, errMsg string)
|
type CmdResultHandler func(reqID, status, output, errMsg string)
|
||||||
|
|
||||||
@ -41,7 +43,7 @@ type Bridge struct {
|
|||||||
started bool
|
started bool
|
||||||
|
|
||||||
// 回调
|
// 回调
|
||||||
cmdHandler CmdHandler
|
cmdHandler BridgeCmdHandler
|
||||||
resultHandler CmdResultHandler
|
resultHandler CmdResultHandler
|
||||||
dataHandler DataHandler
|
dataHandler DataHandler
|
||||||
|
|
||||||
@ -135,7 +137,7 @@ func (b *Bridge) Authorized() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OnCmd 注册命令处理器。当收到 remotedevice 下发的 cmd 时调用。
|
// OnCmd 注册命令处理器。当收到 remotedevice 下发的 cmd 时调用。
|
||||||
func (b *Bridge) OnCmd(handler CmdHandler) {
|
func (b *Bridge) OnCmd(handler BridgeCmdHandler) {
|
||||||
b.mu.Lock()
|
b.mu.Lock()
|
||||||
defer b.mu.Unlock()
|
defer b.mu.Unlock()
|
||||||
b.cmdHandler = handler
|
b.cmdHandler = handler
|
||||||
@ -436,7 +438,7 @@ func (b *Bridge) handleMessage(msg map[string]interface{}) {
|
|||||||
log.Printf("[devicebridge] cmd req=%s type=%s cmd=%s", reqID, cmdType, truncateString(command, 60))
|
log.Printf("[devicebridge] cmd req=%s type=%s cmd=%s", reqID, cmdType, truncateString(command, 60))
|
||||||
|
|
||||||
if handler != nil {
|
if handler != nil {
|
||||||
handler(reqID, command)
|
handler(reqID, command, cmdType)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "hello_ack", "bind_ack":
|
case "hello_ack", "bind_ack":
|
||||||
|
|||||||
Reference in New Issue
Block a user