我正在研究报告状态。我使用java作为我的服务器语言。我能够成功验证用户身份。我的智能开关具有开/关特性。除报告状态外,一切正常。我不清楚。
作为node.js和google home智能操作的新手,我有以下查询:
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);
});
将“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);
});
});