पहचानें और सिंक करें

Cloud-to-cloud इंटिग्रेशन बनाने के बाद, अगला चरण फ़ुलफ़िलमेंट में फ़ंक्शनलिटी जोड़ना है. इससे स्मार्ट होम इंटेंट को प्रोसेस किया जा सकेगा और ऐसे जवाब दिए जा सकेंगे जिन्हें Google Assistant पहचानता है.

उपयोगकर्ता की पहचान करना

Assistant, आपके smart home ऐक्शन के फ़ुलफ़िलमेंट के लिए अनुरोध करता है. इसके लिए, Authorization हेडर में आपके OAuth 2.0 सर्वर से मिले ऐक्सेस टोकन का इस्तेमाल किया जाता है.

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

किसी भी अनुरोध का जवाब देने से पहले, फ़ुलफ़िलमेंट लॉजिक को यह पुष्टि करनी चाहिए कि यह टोकन क्रेडेंशियल मान्य है. साथ ही, उसे इससे जुड़े उपयोगकर्ता खाते का पता लगाना चाहिए. अगर ऐक्सेस टोकन अमान्य है, तो आपको फ़ुलफ़िलमेंट के लिए, HTTP 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"
        }
      }]
    }
  };
});
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);
}

ज़्यादा जानकारी के लिए, SYNC इंटेंट के रेफ़रंस दस्तावेज़ देखें.