بعد إنشاء smart home Action، تتمثل الخطوة التالية في إضافة وظيفة في توصيل طلباتك لمعالجة الأغراض المنزلية الذكية وإرجاع الردود Google Assistant recognizes.
.تحديد هوية المستخدم
يرسل 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 intent في خدمة action.devices.SYNC
إلى خدمة توصيل الطلبات هذه لطلب قائمة الأجهزة المرتبطة بالمستخدم المحدّد وإمكاناته. يجب أن يوفّر تنفيذ الطلب معرّفًا فريدًا لكل مستخدم في الحقل agentUserId
في استجابة SYNC
. يجب أن يكون هذا المعرّف قيمة غير ثابتة لتمثيل المستخدم في خدمة السحاب. وليس من المستحسن تقديم عناوين البريد الإلكتروني أو السمات الأخرى استنادًا إلى الإعدادات التي يمكن للمستخدم تغييرها.
يحتوي الحقل devices
في استجابة SYNC
على جميع الأجهزة التي سمح المستخدم لـ Assistant بالدخول إليها، بالإضافة إلى الأنواع والسمات التي يدعمها، والسمات المطلوبة لضبط سلوك السمة لذلك الجهاز المحدد.
يتم تشغيل intent 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); }
لمزيد من المعلومات، اطّلع على الوثائق المرجعية
لأغراض intent في SYNC
.