简体   繁体   中英

Report state for google home action

I am working on report state. I'm using java as my server language. 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:

  1. Where report state has to be implemented? In node.js(action) or server side?
  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)

Other than that guideline, it can be implemented in any place that would allow you to send the state to the Homegraph.

A good place to see the sample code would be in the codelab which is written in Node.js. It shows how to use the actions-on-google library to do report state (there is no library for 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]

/**
 * 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);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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