Quando gli utenti interagiscono con Google Assistant per eseguire query sullo stato attuale di un dispositivo, il tuo fulfillment riceve un intent action.devices.QUERY
contenente un elenco di ID dispositivo (fornito dalla risposta SYNC
).
Il fulfillment riceve un intent action.devices.EXECUTE
quando gli utenti inviano comandi a Assistant per controllare il tuo dispositivo.
Gestisci QUERY
intent
La tua risposta QUERY
include un insieme completo di stati per ciascuno dei trait supportati 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 }, "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); }
Per ulteriori informazioni, consulta la documentazione di riferimento per l'intent QUERY
.
Gestisci EXECUTE
intent
Analogamente a QUERY
, un singolo intent può avere come target più ID dispositivo. Un singolo intent EXECUTE
può anche contenere più comandi distinti dati a un gruppo di dispositivi. Ad esempio, un intent attivato potrebbe impostare la luminosità e il colore di un gruppo di luci o impostare più luci su un colore diverso. La tua risposta EXECUTE
deve restituire il nuovo stato del dispositivo dopo l'esecuzione.
Utilizza Report State quando lo stato del dispositivo dell'utente cambia. Ad esempio, a causa di un intent EXECUTE
o di un cambiamento di stato locale (ad esempio l'attivazione 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 ulteriori informazioni, consulta la documentazione di riferimento dell'intent EXECUTE
.
Risposte relative allo 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 è riuscita.OFFLINE
: il dispositivo di destinazione è offline o comunque irraggiungibile.EXCEPTIONS
: si è verificato un problema o un avviso associato alla richiesta.ERROR
: la richiesta non è riuscita con il criterioerrorCode
corrispondente.
Per ERROR
e EXCEPTIONS
, consulta
Gestire errori ed eccezioni e
Errori ed eccezioni per maggiori
dettagli.