简体   繁体   English

报告Google首页操作的状态

[英]Report state for google home action

I am working on report state. 我正在报告状态。 I'm using java as my server language. 我使用Java作为服务器语言。 I am able to successfully authenticate user. 我能够成功验证用户身份。 My smart switch has on/off trait. 我的智能开关具有开/关特性。 All things are working fine except report state. 除报告状态外,其他所有东西都工作正常。 Which I am not clear about. 我不清楚。

As new to node.js and google home smart action I have following queries: 作为Node.js和Google Home智能操作的新手,我有以下查询:

  1. Where report state has to be implemented? 报告状态必须在哪里实施? In node.js(action) or server side? 在node.js(action)还是服务器端?
  2. Is there any sample code which I can refer to study and follow the process? 我可以参考任何示例代码来学习并遵循该过程吗?

Report State should be implemented on your server, as it does require a service key that you may not want to leak publicly. 报告状态应在您的服务器上实现,因为它确实需要一个服务密钥,您可能不想公开泄漏该服务密钥。 (Not sure where your Node.js comes in compared to the Java) (不确定与Java相比,Node.js的位置)

Other than that guideline, it can be implemented in any place that would allow you to send the state to the Homegraph. 除了该准则之外,它还可以在允许您将状态发送到Homegraph的任何地方实施。

A good place to see the sample code would be in the codelab which is written in Node.js. 在Node.js编写的代码实验室中可以找到示例代码的好地方。 It shows how to use the actions-on-google library to do report state (there is no library for Java). 它显示了如何使用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);
  });

Add "context" to onWrite((event,context) and [context.params.deviceId] 在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);
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM