Quando gli utenti interagiscono con Google Assistant per eseguire query sullo stato attuale di un dispositivo, il tuo fulfillment riceve un
action.devices.QUERY intent contenente un
elenco di ID dispositivo (forniti dalla risposta SYNC).
Il tuo fulfillment riceve un
action.devices.EXECUTE intent
quando gli utenti inviano comandi a Assistant per controllare il tuo
dispositivo.
Gestire gli intent QUERY
La risposta QUERY include un set completo di stati per ogni tratto supportato dai dispositivi richiesti.
{ "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, "status": "SUCCESS" }, "456": { "on": true, "online": true, "status": "SUCCESS", "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, status: "SUCCESS" }, 456: { on: true, online: true, status: "SUCCESS", 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("status", "SUCCESS"); } }); put( "456", new HashMap<String, Object>() { { put("on", true); put("online", true); put("status", "SUCCESS"); put("brightness", 80); put( "color", new HashMap<String, Object>() { { put("name", "cerulean"); put("spectrumRGB", 31655); } }); } }); } }); return new QueryResponse(queryRequest.getRequestId(), payload); }
Per saperne di più, consulta la documentazione di riferimento dell'intent QUERY.
Gestire gli intent EXECUTE
Analogamente a QUERY, un singolo intent può essere destinato a più ID dispositivo. Un singolo intent EXECUTE può anche contenere più comandi distinti forniti a un gruppo di dispositivi. Ad esempio, un intent attivato può impostare sia la luminosità sia il colore di un gruppo di luci oppure impostare colori diversi per più luci. La risposta EXECUTE deve restituire il nuovo stato del dispositivo dopo l'esecuzione.
Utilizza Report State
quando lo stato del dispositivo degli utenti cambia. Ad esempio, a causa di un intent EXECUTE o di una modifica dello stato locale (ad esempio, l'accensione manuale di un interruttore della luce).
In questo modo, Google Home Graph rimane sincronizzato con il tuo servizio cloud.
{ "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); }
Per saperne di più, consulta la EXECUTE
documentazione di riferimento dell'intent.
Risposte di stato
Le risposte QUERY e EXECUTE includono un campo status per segnalare il risultato della richiesta. Ogni risposta di stato può fornire uno dei seguenti valori:
SUCCESS: la richiesta è andata a buon fine.OFFLINE: il dispositivo di destinazione è offline o non è raggiungibile.EXCEPTIONS: è presente un problema o un avviso associato alla richiesta.ERROR: la richiesta non è andata a buon fine con il codice di erroreerrorCodecorrispondente.
Per ERROR e EXCEPTIONS, consulta
Gestire errori ed eccezioni ed
Errori ed eccezioni per maggiori
dettagli.