Sau khi bạn tạo một quy trình tích hợpCloud-to-cloud, bước tiếp theo là thêm chức năng vào quá trình thực hiện để xử lý ý định nhà thông minh và trả về các phản hồi mà Google Assistant nhận dạng được.
Xác định người dùng
Assistant sẽ gửi yêu cầu đến quá trình thực hiện Hành động
smart home của bạn bằng mã truy cập do
máy chủ OAuth 2.0 cung cấp trong tiêu đề Authorization.
POST /fulfillment HTTP/1.1 Host: smarthome.example.com Content-Type: application/json Authorization: Bearer ACCESS_TOKEN
Trước khi phản hồi mọi yêu cầu, logic thực hiện của bạn phải xác minh rằng thông tin đăng nhập mã thông báo này là hợp lệ và xác định tài khoản người dùng được liên kết. Nếu mã truy cập không hợp lệ, quá trình thực hiện của bạn sẽ trả về lỗi HTTP 401 Unauthorized.
Liệt kê các thiết bị và chức năng của chúng
Assistant gửi ý định
action.devices.SYNC đến quá trình thực hiện của bạn để yêu cầu danh sách các thiết bị được liên kết với người dùng đã cho và
các chức năng của chúng. Quá trình thực hiện của bạn phải trả về một mã nhận dạng riêng biệt cho mỗi người dùng trong trường agentUserId của phản hồi SYNC. Mã nhận dạng này phải là một giá trị bất biến để đại diện cho người dùng đối với dịch vụ đám mây của bạn. Bạn không nên cung cấp địa chỉ email hoặc các thuộc tính khác dựa trên những chế độ cài đặt mà người dùng có thể thay đổi.
Trường devices của phản hồi SYNC chứa tất cả các thiết bị mà người dùng đã cho phép Assistant truy cập, các loại và đặc điểm mà chúng hỗ trợ, cũng như các thuộc tính cần thiết để định cấu hình hành vi của đặc điểm cho thiết bị cụ thể đó.
Ý định SYNC được kích hoạt trong quá trình liên kết tài khoản hoặc khi người dùng tự động đồng bộ lại thiết bị của họ. Nếu danh sách thiết bị, đặc điểm được hỗ trợ
hoặc giá trị thuộc tính của người dùng thay đổi, hãy sử dụng
Yêu cầu đồng bộ hoá để kích hoạt ý định
SYNC mới và báo cáo các bản cập nhật cho 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); }
Để biết thêm thông tin, hãy xem tài liệu tham khảo về ý định SYNC.