函数联动

函数联动让模板先声明可被外部调用的能力,再由 SDK、本地 Bridge 和网关完成同设备或跨设备调用。

整体流程

  1. 模板在 configJson.functions 中声明函数元数据。
  2. 构建产物在 valueMap.__templateSdk.functions 中写入函数 manifest。
  3. 管理后台读取 manifest,配置 A 函数触发 B/N 函数的绑定规则。
  4. 模板运行时通过 registerTemplateFunction 注册真实 handler。
  5. 模板通过 invokeTemplateFunction 发起调用。
  6. 网关按规则把调用转发给目标设备模板或硬件 adapter。

声明函数

functions: {
  playVideo: {
    label: '播放视频',
    description: '按视频 ID 播放指定内容',
    direction: 'inout',
    params: [
      { name: 'id', type: 'string', required: true },
      { name: 'seekTo', type: 'number', defaultValue: 0 }
    ],
    transport: {
      qos: 'at_most_once',
      timeoutMs: 3000
    },
    tags: ['video']
  }
}

注册运行时 handler

import { registerTemplateFunction } from 'auto-exhibition-template-sdk'

const stop = registerTemplateFunction('playVideo', async (args, context) => {
  await player.play(args.id, { seekTo: args.seekTo || 0 })
  return { ok: true, currentId: args.id, messageId: context.messageId }
})

// 页面卸载时注销
stop()

触发绑定规则

import { invokeTemplateFunction } from 'auto-exhibition-template-sdk'

await invokeTemplateFunction(
  { mode: 'binding', sourceFunction: 'playVideo' },
  'playVideo',
  { id: 'intro', seekTo: 0 },
  { timeoutMs: 3000 }
)

本地调试

同一设备内有多个模板页时,可以使用 mode: 'local' 测试函数调用链路,不经过网关。

await invokeTemplateFunction(
  { mode: 'local' },
  'showDetail',
  { id: 'item-01' }
)

硬件 endpoint

网关可以把硬件能力注册成 adapter。规则目标可以是模板,也可以是硬件:

[
  {
    "endpointType": "template",
    "clientId": "SCREEN-B",
    "functionName": "showDetail"
  },
  {
    "endpointType": "hardware",
    "hardwareId": "mock-hardware",
    "functionName": "echo"
  }
]

参数映射

后台绑定规则可以把来源参数、上下文或固定值映射到目标函数参数。

{
  "mappings": [
    { "targetParam": "id", "source": "args.id" },
    { "targetParam": "fromClientId", "source": "context.from.clientId" },
    { "targetParam": "value", "value": 80 }
  ]
}
边界: SDK 负责模板侧声明、注册和调用;跨设备规则转发、调用日志和硬件协议适配放在管理后台与网关侧实现。