【问题标题】:Report state for google home action报告 Google Home 操作的状态
【发布时间】:2018-10-31 18:12:14
【问题描述】:

我正在处理报告状态。我使用 java 作为我的服务器语言。我能够成功验证用户身份。我的智能开关具有开/关特性。除报告状态外,一切正常。我不清楚。

作为 node.js 和 google home 智能操作的新手,我有以下疑问:

  1. 必须在哪里实现报告状态?在 node.js(action) 还是服务器端?
  2. 是否有任何示例代码可供我参考学习并遵循流程?

【问题讨论】:

    标签: actions-on-google google-home google-smart-home


    【解决方案1】:

    Report State 应该在您的服务器上实现,因为它确实需要一个您可能不想公开泄露的服务密钥。 (不确定您的 Node.js 与 Java 相比有何用处)

    除了该指南之外,它可以在任何允许您将状态发送到 Homegraph 的地方实施。

    查看示例代码的好地方是codelab,它是用 Node.js 编写的。它展示了如何使用actions-on-google 库来报告状态(Java 没有库)。

    const postData = {
      requestId: 'ff36a3cc', /* Any unique ID */
      agentUserId: '123', /* Hardcoded user ID */
      payload: {
        devices: {
          states: {
            /* Report the current state of our washer */
            [event.params.deviceId]: {
              on: snapshotVal.OnOff.on,
              isPaused: snapshotVal.StartStop.isPaused,
              isRunning: snapshotVal.StartStop.isRunning,
            },
          },
        },
      },
    };
    
    return app.reportState(postData)
      .then((data) => {
        console.log('Report state came back');
        console.info(data);
      });
    

    【讨论】:

    • 我正在实现相同但我收到错误:TypeError:无法读取exports.reportstate.functions.database.ref.onWrite处未定义的属性'deviceId'。我的事件只返回 {"before":{"OnOff":{"on":false}},"after":{"OnOff":{"on":true}}}
    • 我认为示例使用实时数据库,而不是 Firestore。你用的是哪个?
    • 我正在使用实时数据库
    【解决方案2】:

    在 onWrite((event,context) 和 [context.params.deviceId] 中添加“context”

    /**
     * Send a REPORT STATE call to the homegraph when data for any device id
     * has been changed.
     */
    exports.reportstate = functions.database.ref('/{deviceId}').onWrite((event,context) => {
      console.info('Firebase write event triggered this cloud function');
    
      const snapshotVal = event.after.val();
    
      const postData = {
        requestId: 'ff36a3cc', /* Any unique ID */
        agentUserId: '123', /* Hardcoded user ID */
        payload: {
          devices: {
            states: {
              /* Report the current state of our washer */
              [context.params.deviceId]: {
                on: snapshotVal.OnOff.on,
              },
            },
          },
        },
      };
    
      return app.reportState(postData)
        .then((data) => {
          console.log('Report state came back');
          console.info(data);
        });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多