ระบุและซิงค์

เมื่อคุณสร้างการดำเนินการ smart home แล้ว ขั้นตอนต่อไปคือเพิ่มฟังก์ชันใน Fulfillment เพื่อประมวลผล Intent ของสมาร์ทโฮมและแสดงผลการตอบกลับที่ Google Assistant รู้จัก

ระบุผู้ใช้

Assistant ส่งคำขอไปยัง Fulfillment ของ smart home การดำเนินการของคุณด้วยโทเค็นเพื่อการเข้าถึงที่เซิร์ฟเวอร์ OAuth 2.0 ระบุในส่วนหัว Authorization

POST /fulfillment HTTP/1.1
Host: smarthome.example.com
Content-Type: application/json
Authorization: Bearer ACCESS_TOKEN

ก่อนที่จะตอบกลับคำขอ ตรรกะ Fulfillment ของคุณควรตรวจสอบว่าข้อมูลเข้าสู่ระบบของโทเค็นนี้ถูกต้องและระบุบัญชีผู้ใช้ที่เชื่อมโยง หากโทเค็นเพื่อการเข้าถึงไม่ถูกต้อง Fulfillment ของคุณควรแสดงข้อผิดพลาด HTTP 401 Unauthorized

แสดงรายการอุปกรณ์และความสามารถต่างๆ

เครื่องมือที่แนะนำสำหรับงานนี้

Assistant ส่ง Intent ของ action.devices.SYNC ไปยัง Fulfillment ของคุณเพื่อขอรายการอุปกรณ์ที่เชื่อมโยงกับผู้ใช้และความสามารถของผู้ใช้ Fulfillment ของคุณต้องแสดงรหัสที่ไม่ซ้ำกันสำหรับผู้ใช้แต่ละรายในช่อง agentUserId ของการตอบกลับ SYNC รหัสนี้ต้องเป็นค่าที่เปลี่ยนแปลงไม่ได้เพื่อแสดงถึงผู้ใช้ในบริการระบบคลาวด์ของคุณ ไม่แนะนําให้ระบุอีเมลหรือแอตทริบิวต์อื่นๆ ตามการตั้งค่าที่ผู้ใช้เปลี่ยนแปลงได้

ช่อง devices ของคำตอบ SYNC ประกอบด้วยอุปกรณ์ทั้งหมดที่ผู้ใช้อนุญาตให้ Assistant เข้าถึง, ประเภทและลักษณะที่รองรับ และแอตทริบิวต์ที่จำเป็นในการกำหนดค่าพฤติกรรมของลักษณะเฉพาะของอุปกรณ์นั้นๆ

Intent SYNC จะเกิดขึ้นเมื่อลิงก์บัญชีหรือเมื่อผู้ใช้ซิงค์อุปกรณ์อีกครั้งด้วยตนเอง หากรายการอุปกรณ์ ลักษณะที่รองรับ หรือค่าแอตทริบิวต์ของผู้ใช้มีการเปลี่ยนแปลง ให้ใช้ส่งคำขอซิงค์เพื่อทริกเกอร์ Intent SYNC ใหม่และรายงานการอัปเดตไปยัง Google

คำขอ
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.SYNC"
    }]
}
JSON
{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "agentUserId": "1836.15267389",
    "devices": [
      {
        "id": "123",
        "type": "action.devices.types.OUTLET",
        "traits": [
          "action.devices.traits.OnOff"
        ],
        "name": {
          "defaultNames": [
            "My Outlet 1234"
          ],
          "name": "Night light",
          "nicknames": [
            "wall plug"
          ]
        },
        "willReportState": false,
        "roomHint": "kitchen",
        "deviceInfo": {
          "manufacturer": "lights-out-inc",
          "model": "hs1234",
          "hwVersion": "3.2",
          "swVersion": "11.4"
        },
        "otherDeviceIds": [
          {
            "deviceId": "local-device-id"
          }
        ],
        "customData": {
          "fooValue": 74,
          "barValue": true,
          "bazValue": "foo"
        }
      },
      {
        "id": "456",
        "type": "action.devices.types.LIGHT",
        "traits": [
          "action.devices.traits.OnOff",
          "action.devices.traits.Brightness",
          "action.devices.traits.ColorSetting"
        ],
        "name": {
          "defaultNames": [
            "lights out inc. bulb A19 color hyperglow"
          ],
          "name": "lamp1",
          "nicknames": [
            "reading lamp"
          ]
        },
        "willReportState": false,
        "roomHint": "office",
        "attributes": {
          "colorModel": "rgb",
          "colorTemperatureRange": {
            "temperatureMinK": 2000,
            "temperatureMaxK": 9000
          },
          "commandOnlyColorSetting": false
        },
        "deviceInfo": {
          "manufacturer": "lights out inc.",
          "model": "hg11",
          "hwVersion": "1.2",
          "swVersion": "5.4"
        },
        "customData": {
          "fooValue": 12,
          "barValue": false,
          "bazValue": "bar"
        }
      }
    ]
  }
}
Node.js
const {smarthome} = require('actions-on-google');
const app = smarthome();
// ...
app.onSync((body, headers) => {
  // TODO Get devices for user
  return {
    requestId: body.requestId,
    payload: {
      agentUserId: "1836.15267389",
      devices: [{
        id: "123",
        type: "action.devices.types.OUTLET",
        traits: [
          "action.devices.traits.OnOff"
        ],
        name: {
          defaultNames: ["My Outlet 1234"],
          name: "Night light",
          nicknames: ["wall plug"]
        },
        willReportState: false,
        roomHint: "kitchen",
        deviceInfo: {
          manufacturer: "lights-out-inc",
          model: "hs1234",
          hwVersion: "3.2",
          swVersion: "11.4"
        },
        otherDeviceIds: [{
          deviceId: "local-device-id"
        }],
        customData: {
          fooValue: 74,
          barValue: true,
          bazValue: "foo"
        }
      }, {
        id: "456",
        type: "action.devices.types.LIGHT",
        traits: [
          "action.devices.traits.OnOff",
          "action.devices.traits.Brightness",
          "action.devices.traits.ColorSetting"
        ],
        name: {
          defaultNames: ["lights out inc. bulb A19 color hyperglow"],
          name: "lamp1",
          nicknames: ["reading lamp"]
        },
        willReportState: false,
        roomHint: "office",
        attributes: {
          colorModel: 'rgb',
          colorTemperatureRange: {
            temperatureMinK: 2000,
            temperatureMaxK: 9000
          },
          commandOnlyColorSetting: false
        },
        deviceInfo: {
          manufacturer: "lights out inc.",
          model: "hg11",
          hwVersion: "1.2",
          swVersion: "5.4"
        },
        customData: {
          fooValue: 12,
          barValue: false,
          bazValue: "bar"
        }
      }]
    }
  };
});
Java
@NotNull
@Override
public SyncResponse onSync(@NotNull SyncRequest syncRequest, @Nullable Map<?, ?> map) {
  Payload payload = new Payload();
  payload.setAgentUserId("1836.15267389");
  payload.setDevices(
      new Device[] {
        new Device.Builder()
            .setId("123")
            .setType("action.devices.types.OUTLET")
            .addTrait("action.devices.traits.OnOff")
            .setName(
                Collections.singletonList("My Outlet 1234"),
                "Night light",
                Collections.singletonList("Wall plug"))
            .setWillReportState(true)
            .setDeviceInfo("lights-out-inc", "hs1234", "3.2", "11.4")
            .setCustomData(
                new JSONObject()
                    .put("fooValue", 74)
                    .put("barValue", true)
                    .put("bazValue", "foo"))
            .build(),
        new Device.Builder()
            .setId("456")
            .setType("action.devices.types.LIGHT")
            .addTrait("action.devices.traits.OnOff")
            .addTrait("action.devices.traits.Brightness")
            .addTrait("action.devices.traits.ColorTemperature")
            .addTrait("action.devices.traits.ColorSpectrum")
            .setName(
                Collections.singletonList("Lights Out Inc. bulb A19 color hyperglow"),
                "Lamp",
                Collections.singletonList("Reading lamp"))
            .setWillReportState(true)
            .setDeviceInfo("Lights Out Inc.", "hg11", "1.2", "5.4")
            .setCustomData(
                new JSONObject()
                    .put("fooValue", 12)
                    .put("barValue", false)
                    .put("bazValue", "bar"))
            .build(),
      });
  return new SyncResponse(syncRequest.getRequestId(), payload);
}

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