smart home कार्रवाई बनाने के बाद, स्मार्ट होम इंटेंट और प्रोसेस करने में इस्तेमाल होने वाले रिस्पॉन्स प्रोसेस करने के लिए,Google Assistant अगला तरीका जोड़ा जाता है.
उपयोगकर्ता की पहचान करें
Assistant, Authorization
हेडर में आपके OAuth 2.0 सर्वर से मिले ऐक्सेस टोकन के ज़रिए
smart home कार्रवाई पूरी करने का अनुरोध करता है.
POST /fulfillment HTTP/1.1 Host: smarthome.example.com Content-Type: application/json Authorization: Bearer ACCESS_TOKEN
किसी भी अनुरोध का जवाब देने से पहले, अपने ऑर्डर पूरा करने के लिए इस बात की पुष्टि करनी चाहिए कि
यह टोकन क्रेडेंशियल मान्य है और इससे जुड़ा उपयोगकर्ता खाता तय करता है. अगर
ऐक्सेस टोकन अमान्य है, तो आपकी कार्रवाई में एचटीटीपी
401 Unauthorized
गड़बड़ी दिखनी चाहिए.
डिवाइसों और उनकी सुविधाओं की सूची बनाएं
दिए गए उपयोगकर्ता और उनकी क्षमताओं से जुड़े
डिवाइसों की सूची का अनुरोध करने के लिए, Assistant
action.devices.SYNC
इंटेंट भेजता है. आपका जवाब, SYNC
रिस्पॉन्स के agentUserId
फ़ील्ड में मौजूद हर उपयोगकर्ता के लिए एक यूनीक आईडी होना चाहिए. उपयोगकर्ता को आपकी क्लाउड सेवा में दिखाने के लिए इस आईडी में
कोई बदलाव नहीं होना चाहिए. हमारा सुझाव है कि आप
उन सेटिंग के हिसाब से ईमेल पते या दूसरे एट्रिब्यूट न दें जिन्हें उपयोगकर्ता
बदल सकता है.
आपके SYNC
जवाब के devices
फ़ील्ड में वे सभी डिवाइस शामिल होते हैं जिन्हें उपयोगकर्ता ने ऐक्सेस करने के लिए 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
इंटेंट
दस्तावेज़ देखें.