当用户与 Google Assistant 互动以查询设备的当前状态时,您的执行方式会接收到 action.devices.QUERY
intent,其中包含一个设备 ID 列表(由您的 SYNC
响应提供)。当用户向 Assistant 发送命令来控制您的设备时,您的执行方式会接收到 action.devices.EXECUTE
intent。
处理 QUERY
intent
你的 QUERY
响应包含所请求的设备支持的每种特征的全部状态。
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.QUERY", "payload": { "devices": [{ "id": "123", "customData": { "fooValue": 74, "barValue": true, "bazValue": "foo" } }, { "id": "456", "customData": { "fooValue": 12, "barValue": false, "bazValue": "bar" } }] } }] }
JSON
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "devices": { "123": { "on": true, "online": true }, "456": { "on": true, "online": true, "brightness": 80, "color": { "name": "cerulean", "spectrumRGB": 31655 } } } } }
Node.js
const {smarthome} = require('actions-on-google'); const app = smarthome(); // ... app.onQuery((body, headers) => { // TODO Get device state return { requestId: body.requestId, payload: { devices: { 123: { on: true, online: true }, 456: { on: true, online: true, brightness: 80, color: { name: "cerulean", spectrumRGB: 31655 } } } } }; });
Java
@NotNull @Override public QueryResponse onQuery(@NotNull QueryRequest queryRequest, @Nullable Map<?, ?> map) { QueryResponse.Payload payload = new QueryResponse.Payload(); payload.setDevices( new HashMap<String, Map<String, Object>>() { { put( "123", new HashMap<String, Object>() { { put("on", true); put("online", true); } }); put( "456", new HashMap<String, Object>() { { put("on", true); put("online", true); put("brightness", 80); put( "color", new HashMap<String, Object>() { { put("name", "cerulean"); put("spectrumRGB", 31655); } }); } }); } }); return new QueryResponse(queryRequest.getRequestId(), payload); }
如需了解详情,请参阅 QUERY
intent 参考文档。
处理 EXECUTE
intent
与 QUERY
类似,一个 intent 可以定位多个设备 ID。一个 EXECUTE
intent 还可能包含多个提供给一组设备的多个不同命令。例如,一个被触发的 intent 可能会为一组灯设置亮度和颜色,或将多个灯设置为不同的颜色。执行后,您的 EXECUTE
响应应会返回设备的新状态。
当用户设备的状态发生变化时,请使用 Report State。例如,当由于 EXECUTE
intent 或本地状态改变(例如手动开关灯)等原因而导致状态变化时。这可以确保 Google Home Graph 与您的云服务保持同步。
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "inputs": [{ "intent": "action.devices.EXECUTE", "payload": { "commands": [{ "devices": [{ "id": "123", "customData": { "fooValue": 74, "barValue": true, "bazValue": "sheepdip" } }, { "id": "456", "customData": { "fooValue": 36, "barValue": false, "bazValue": "moarsheep" } }], "execution": [{ "command": "action.devices.commands.OnOff", "params": { "on": true } }] }] } }] }
JSON
{ "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf", "payload": { "commands": [ { "ids": [ "123" ], "status": "SUCCESS", "states": { "on": true, "online": true } }, { "ids": [ "456" ], "status": "ERROR", "errorCode": "deviceTurnedOff" } ] } }
Node.js
const {smarthome} = require('actions-on-google'); const app = smarthome(); // ... app.onExecute((body, headers) => { // TODO Send command to device return { requestId: body.requestId, payload: { commands: [{ ids: ["123"], status: "SUCCESS", states: { on: true, online: true } }, { ids: ["456"], status: "ERROR", errorCode: "deviceTurnedOff" }] } }; });
Java
@NotNull @Override public ExecuteResponse onExecute( @NotNull ExecuteRequest executeRequest, @Nullable Map<?, ?> map) { ExecuteResponse.Payload payload = new ExecuteResponse.Payload(); payload.setCommands( new Commands[] { new Commands( new String[] {"123"}, "SUCCESS", new HashMap<String, Object>() { { put("on", true); put("online", true); } }, null, null), new Commands(new String[] {"456"}, "ERROR", null, "deviceTurnedOff", null) }); return new ExecuteResponse(executeRequest.getRequestId(), payload); }
如需了解详情,请参阅 EXECUTE
intent 参考文档。
状态响应
你的 QUERY
和 EXECUTE
响应包含一个用于报告请求结果的 status
字段。每个状态响应都可以提供以下某一值:
SUCCESS
:请求成功。OFFLINE
:目标设备处于离线状态或无法访问。EXCEPTIONS
:相应请求存在问题或提醒。ERROR
:请求失败,错误提示:errorCode
。