ยินดีต้อนรับสู่ Google Home Developer Center แหล่งใหม่เรียนรู้วิธีพัฒนาการดําเนินการในบ้านอัจฉริยะ หมายเหตุ: คุณจะสร้างการดําเนินการต่างๆ ต่อไปในคอนโซลการดําเนินการ

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

จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ

เมื่อผู้ใช้โต้ตอบกับ Google Assistant to query the current state of a device, your fulfillment receives an action.devices.QUERY intent containing a list of device IDs (as provided by your SYNC response). Your fulfillment receives an action.devices.EXECUTE intent when users send commands to Assistant to control your device.

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

ใช้ Report State เมื่อสถานะของอุปกรณ์ของผู้ใช้มีการเปลี่ยนแปลง ตัวอย่างเช่น ความตั้งใจของ EXECUTE หรือการเปลี่ยนแปลงสถานะในเครื่อง (เช่น การสลับสวิตช์ไฟด้วยตนเอง) การดําเนินการนี้จะเก็บ Google Home Graph synchronized with your cloud service. ไว้

คําขอ
{
    "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"
      }]
    }
  };
});
จาวา
@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 ดูรายละเอียดเพิ่มเติมได้ที่จัดการข้อผิดพลาดและข้อยกเว้น รวมถึงข้อผิดพลาดและข้อยกเว้น