پساز ایجاد ادغام Cloud-to-cloud، مرحله بعدی افزودن عملکرد در اجرای شما برای پردازش اهداف خانه هوشمند و برگرداندن پاسخهایی است که Google Assistant تشخیص میدهد.
استفاده کنید.شناسایی کاربر
Assistant درخواستهایی را به
smart home تحقق «کنش» شما با کد دسترسی ارائهشده
توسط سرور OAuth 2.0 شما در سرصفحه Authorization ارسال میکند.
POST /fulfillment HTTP/1.1 Host: smarthome.example.com Content-Type: application/json Authorization: Bearer ACCESS_TOKEN
پیشاز پاسخ دادن به هر درخواستی، منطق اجرای شما باید درستیسنجی کند که
این اعتبارنامه کد معتبر است و حساب کاربری مرتبط را تعیین کند. اگر
کد دسترسی نامعتبر باشد، اجرای شما باید خطای
HTTP 401 Unauthorized را برگرداند.
فهرست کردن دستگاهها و قابلیتهای آنها
Assistant یک هدف
action.devices.SYNC به تحقق شما ارسال میکند تا فهرست دستگاههای مرتبط با کاربر دادهشده و قابلیتهای آنها را درخواست کند. تحقق شما باید شناسه یکتایی برای هر کاربر در
فیلد agentUserId پاسخ SYNC برگرداند. این شناسه باید مقدار تغییرناپذیری باشد تا کاربر را به سرویس ابری شما معرفی کند. توصیه نمیشود نشانیهای ایمیل یا مشخصههای دیگری را که براساس تنظیمات قابلتغییر توسط کاربر هستند ارائه دهید.
فیلد devices در پاسخ SYNC شما شامل همه دستگاههایی است که کاربر به Assistant اجازه دسترسی به آنها را داده است، انواع و ویژگیهایی که آنها پشتیبانی میکنند، و مشخصههای لازم برای پیکربندی رفتار ویژگی برای آن دستگاه خاص.
هدف SYNC درطول پیوند دادن حساب یا زمانیکه کاربر
دستگاههایش را بهطور دستی همگامسازی مجدد میکند راهاندازی میشود. اگر فهرست دستگاههای کاربر، ویژگیهای پشتیبانیشده، یا مقادیر مشخصه تغییر کند، از درخواست همگامسازی برای راهاندازی قصد 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" } }] } }; });
جاوا
@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); }
برای اطلاعات بیشتر، به سند مرجع هدف SYNC مراجعه کنید.