ค้นหาและดําเนินการ

เมื่อผู้ใช้โต้ตอบกับ Google Assistant เพื่อค้นหาสถานะปัจจุบันของอุปกรณ์ Fulfillment ของคุณจะได้รับ Intent action.devices.QUERY ที่มีรายการรหัสอุปกรณ์ (ตามที่ระบุโดยการตอบกลับ SYNC ของคุณ) Fulfillment ของคุณได้รับ Intent action.devices.EXECUTE เมื่อผู้ใช้ส่งคำสั่งไปยัง Assistant เพื่อควบคุมอุปกรณ์

จัดการ Intent QUERY

การตอบกลับ 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);
}

ดูข้อมูลเพิ่มเติมได้ในเอกสารการอ้างอิง Intent ของ QUERY

จัดการ Intent EXECUTE

เช่นเดียวกับ QUERY Intent รายการเดียวจะกำหนดเป้าหมายเป็นรหัสอุปกรณ์หลายรหัสได้ Intent EXECUTE รายการเดียวอาจมีคำสั่งที่ไม่ซ้ำกันหลายคำสั่งที่ใช้กับกลุ่มอุปกรณ์ได้ ตัวอย่างเช่น Intent ที่ทริกเกอร์อาจตั้งค่าทั้งความสว่างและสี ในกลุ่มหลอดไฟ หรือตั้งค่าหลอดไฟหลายดวงให้มีสีต่างกัน การตอบกลับ EXECUTE ควรคืนสถานะใหม่ของอุปกรณ์หลังจากการดำเนินการ

ใช้ Report State เมื่อสถานะของอุปกรณ์ของผู้ใช้มีการเปลี่ยนแปลง ตัวอย่างเช่น เนื่องจาก EXECUTE หรือเปลี่ยนสถานะในเครื่อง (เช่น การกดสวิตช์ไฟด้วยตนเอง) การดำเนินการนี้จะทำให้ 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);
}

ดูข้อมูลเพิ่มเติมได้ในเอกสารอ้างอิง Intent ของ EXECUTE

การตอบกลับสถานะ

การตอบกลับ QUERY และ EXECUTE จะมีช่อง status สำหรับรายงานผลลัพธ์ของคำขอ การตอบกลับสถานะแต่ละรายการสามารถระบุค่าใดค่าหนึ่งต่อไปนี้

  • SUCCESS: คำขอสำเร็จแล้ว
  • OFFLINE: อุปกรณ์เป้าหมายออฟไลน์อยู่หรือเข้าถึงไม่ได้
  • EXCEPTIONS: มีปัญหาหรือการแจ้งเตือนที่เชื่อมโยงกับคำขอ
  • ERROR: คำขอล้มเหลวโดยมี errorCode ที่เกี่ยวข้อง

สำหรับ ERROR และ EXCEPTIONS โปรดดูรายละเอียดเพิ่มเติมในหัวข้อจัดการข้อผิดพลาดและข้อยกเว้น และข้อผิดพลาดและข้อยกเว้น