Kullanıcılar bir cihazın mevcut durumunu sorgulamak için Google Assistant ile etkileşim kurduğunda, yerine getirme işleminiz cihaz kimliklerinin listesini (SYNC yanıtınız tarafından sağlandığı şekilde) içeren bir action.devices.QUERY intent'i alır.
Kullanıcılar cihazınızı kontrol etmek için Assistant uygulamasına komut gönderdiğinde, karşılama hizmetiniz bir action.devices.EXECUTE intent'i alır.
QUERY niyetlerini işleme
QUERY yanıtınız, istenen cihazlar tarafından desteklenen özelliklerin her biri için tam bir durum grubu içerir.
{ "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); }
Daha fazla bilgi için QUERY intent referans belgelerine bakın.
EXECUTE niyetlerini işleme
QUERY gibi, tek bir intent birden fazla cihaz kimliğini hedefleyebilir. Tek bir EXECUTE intent, bir cihaz grubuna verilen birden fazla farklı komut da içerebilir. Örneğin, tetiklenen bir niyet, bir grup ışığın hem parlaklığını hem de rengini ayarlayabilir veya birden fazla ışığı farklı renklere ayarlayabilir. EXECUTE yanıtınız, yürütme işleminden sonra cihazın yeni durumunu döndürmelidir.
Kullanıcıların cihaz durumu değiştiğinde Report State kullanın. Örneğin, bir EXECUTE intent'i veya yerel durum değişikliği (ör. bir ışık anahtarını manuel olarak açma) nedeniyle.
Bu işlem, Google Home Graph uygulamasının bulut hizmetinizle senkronize kalmasını sağlar.
{ "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); }
Daha fazla bilgi için EXECUTE intent referans dokümanlarını inceleyin.
Durum yanıtları
QUERY ve EXECUTE yanıtlarınız, isteğin sonucunu bildirmek için bir status alanı içerir. Her durum yanıtı aşağıdaki değerlerden birini sağlayabilir:
SUCCESS: İstek başarılı oldu.OFFLINE: Hedef cihaz çevrimdışı veya başka bir nedenle erişilemiyor.EXCEPTIONS: İstekle ilgili bir sorun veya uyarı var.ERROR: İstek, ilgilierrorCodeile başarısız oldu.
ERROR ve EXCEPTIONS için daha fazla bilgi edinmek üzere Hataları ve istisnaları ele alma ile Hatalar ve istisnalar bölümlerine bakın.