Sorgu ve yürütme

Kullanıcılar bir cihazın mevcut durumunu sorgulamak için Google Assistant ile etkileşimde bulunduğunda, istek karşılama işleminiz cihaz kimliklerinin listesini içeren bir action.devices.QUERY niyeti alır (SYNC yanıtınızda belirtildiği şekilde). Kullanıcılar cihazınızı kontrol etmek için Assistant komutuna komut gönderdiğinde, siparişiniz action.devices.EXECUTE niyetini alır.

QUERY niyeti yerine getirin

QUERY yanıtınız, istenen cihazlar tarafından desteklenen her özelliğin her biri için tam bir durum grubu içerir.

İstek
{
    "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);
}

Daha fazla bilgi için QUERY amaç referansı belgelerine bakın.

EXECUTE niyeti yerine getirin

QUERY'a benzer şekilde, tek bir amaç birden fazla cihaz kimliğini hedefleyebilir. Tek bir EXECUTE amacı da bir cihaz grubuna verilen birden fazla farklı komut içerebilir. Örneğin, tetiklenen bir amaç bir grup ışık için hem parlaklığı hem de rengi ayarlayabilir veya birden fazla ışığı farklı bir renge ayarlayabilir. EXECUTE yanıtınız, yürütme işleminden sonra cihazın yeni durumunu döndürecektir.

Kullanıcıların cihazının durumu değiştiğinde Report State kullanın. Örneğin, EXECUTE amacı veya yerel durum değişikliği (ör. bir ışık anahtarını manuel olarak çevirme) nedeniyle. Bu sayede Google Home Graph, bulut hizmetinizle senkronize edilir.

İstek
{
    "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 edinmek için EXECUTE amaç referans belgelerini inceleyin.

Durum yanıtları

QUERY ve EXECUTE yanıtlarınızda, isteğin sonucunu raporlamak için bir status alanı bulunuyor. 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 şekilde erişilemiyor.
  • EXCEPTIONS: İstekle ilişkili bir sorun veya uyarı var.
  • ERROR: İstek, ilgili errorCode ile başarısız oldu.

ERROR ve EXCEPTIONS için daha fazla ayrıntı için Hataları ve istisnaları işleme ve Hatalar ve istisnalar bölümlerine bakın.