报告谷歌家庭行动的状态

问题描述 投票:0回答:2

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

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

  1. 报告状态必须在哪里实施?在node.js(action)或服务器端?
  2. 有没有示例代码我可以参考研究并遵循流程?
actions-on-google google-home google-smart-home
2个回答
0
投票

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

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

查看示例代码的好地方是在用Node.js编写的codelab中。它显示了如何使用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);
  });

0
投票

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

/**
 * 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);
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.