0

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?
James Z
  • 12,209
  • 10
  • 24
  • 44
iSukhi
  • 25
  • 1
  • 10

2 Answers2

0

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);
  });
Nick Felker
  • 11,536
  • 1
  • 21
  • 35
  • I am implementing the same but I am getting error :TypeError: Cannot read property 'deviceId' of undefined at exports.reportstate.functions.database.ref.onWrite. My event returns only {"before":{"OnOff":{"on":false}},"after":{"OnOff":{"on":true}}} – iSukhi Nov 12 '18 at 13:30
  • I think the sample uses the realtime database, not Firestore. Which are you using? – Nick Felker Nov 12 '18 at 18:18
  • I am using realtime database – iSukhi Nov 12 '18 at 19:09
0

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