क्वेरी और एक्ज़ीक्यूट करें

जब उपयोगकर्ता किसी डिवाइस की मौजूदा स्थिति जानने के लिए Google Assistant से इंटरैक्ट करते हैं, तो आपके फ़ुलफ़िलमेंट को action.devices.QUERY इंटेंट मिलता है, जिसमें डिवाइस आईडी की सूची शामिल होती है (जैसा कि आपके SYNC जवाब में बताया गया है). जब उपयोगकर्ता आपके डिवाइस को कंट्रोल करने के लिए Assistant को निर्देश भेजते हैं, तब आपके फ़ुलफ़िलमेंट को action.devices.EXECUTE इंटेंट मिलता है.

QUERY इंटेंट हैंडल करें

QUERY के जवाब में, उन सभी Trait के लिए स्थितियों का पूरा सेट शामिल होता है जिनका इस्तेमाल करने के लिए अनुरोध किया गया है.

अनुरोध करें
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.QUERY",
      "payload": {
        "devices": [{
          "id": "123",
          "customData": {
            "fooValue": 74,
            "barValue": true,
            "bazValue": "foo"
          }
        }, {
          "id": "456",
          "customData": {
            "fooValue": 12,
            "barValue": false,
            "bazValue": "bar"
          }
        }]
      }
    }]
}
JSON
{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "devices": {
      "123": {
        "on": true,
        "online": true
      },
      "456": {
        "on": true,
        "online": true,
        "brightness": 80,
        "color": {
          "name": "cerulean",
          "spectrumRGB": 31655
        }
      }
    }
  }
}
Node.js
const {smarthome} = require('actions-on-google');
const app = smarthome();
// ...
app.onQuery((body, headers) => {
  // TODO Get device state
  return {
    requestId: body.requestId,
    payload: {
      devices: {
        123: {
          on: true,
          online: true
        },
        456: {
          on: true,
          online: true,
          brightness: 80,
          color: {
            name: "cerulean",
            spectrumRGB: 31655
          }
        }
      }
    }
  };
});
Java
@NotNull
@Override
public QueryResponse onQuery(@NotNull QueryRequest queryRequest, @Nullable Map<?, ?> map) {
  QueryResponse.Payload payload = new QueryResponse.Payload();
  payload.setDevices(
      new HashMap<String, Map<String, Object>>() {
        {
          put(
              "123",
              new HashMap<String, Object>() {
                {
                  put("on", true);
                  put("online", true);
                }
              });
          put(
              "456",
              new HashMap<String, Object>() {
                {
                  put("on", true);
                  put("online", true);
                  put("brightness", 80);
                  put(
                      "color",
                      new HashMap<String, Object>() {
                        {
                          put("name", "cerulean");
                          put("spectrumRGB", 31655);
                        }
                      });
                }
              });
        }
      });

  return new QueryResponse(queryRequest.getRequestId(), payload);
}

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

EXECUTE इंटेंट हैंडल करें

QUERY की तरह, एक इंटेंट ही कई डिवाइस आईडी को टारगेट कर सकता है. किसी EXECUTE इंटेंट में, डिवाइसों के किसी ग्रुप को दिए जाने वाले कई अलग-अलग निर्देश भी हो सकते हैं. उदाहरण के लिए, ट्रिगर किया गया इंटेंट, लाइटों के ग्रुप पर चमक और रंग, दोनों सेट कर सकता है या हर लाइट के लिए अलग-अलग रंग सेट कर सकता है. कार्रवाई के बाद, EXECUTE के जवाब में डिवाइस की नई स्थिति दिखनी चाहिए.

उपयोगकर्ता के डिवाइस की स्थिति बदलने पर, Report State का इस्तेमाल करें. उदाहरण के लिए, EXECUTE के इंटेंट या स्थानीय स्थिति में बदलाव की वजह से (जैसे, लाइट स्विच को मैन्युअल तरीके से बदलना). इससे, Google Home Graph आपकी क्लाउड सेवा के साथ सिंक रहता है.

अनुरोध करें
{
    "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
    "inputs": [{
      "intent": "action.devices.EXECUTE",
      "payload": {
        "commands": [{
          "devices": [{
            "id": "123",
            "customData": {
              "fooValue": 74,
              "barValue": true,
              "bazValue": "sheepdip"
            }
          }, {
            "id": "456",
            "customData": {
              "fooValue": 36,
              "barValue": false,
              "bazValue": "moarsheep"
            }
          }],
          "execution": [{
            "command": "action.devices.commands.OnOff",
            "params": {
              "on": true
            }
          }]
        }]
      }
    }]
}
JSON
{
  "requestId": "ff36a3cc-ec34-11e6-b1a0-64510650abcf",
  "payload": {
    "commands": [
      {
        "ids": [
          "123"
        ],
        "status": "SUCCESS",
        "states": {
          "on": true,
          "online": true
        }
      },
      {
        "ids": [
          "456"
        ],
        "status": "ERROR",
        "errorCode": "deviceTurnedOff"
      }
    ]
  }
}
Node.js
const {smarthome} = require('actions-on-google');
const app = smarthome();
// ...
app.onExecute((body, headers) => {
  // TODO Send command to device
  return {
    requestId: body.requestId,
    payload: {
      commands: [{
        ids: ["123"],
        status: "SUCCESS",
        states: {
          on: true,
          online: true
        }
      }, {
        ids: ["456"],
        status: "ERROR",
        errorCode: "deviceTurnedOff"
      }]
    }
  };
});
Java
@NotNull
@Override
public ExecuteResponse onExecute(
    @NotNull ExecuteRequest executeRequest, @Nullable Map<?, ?> map) {
  ExecuteResponse.Payload payload = new ExecuteResponse.Payload();

  payload.setCommands(
      new Commands[] {
        new Commands(
            new String[] {"123"},
            "SUCCESS",
            new HashMap<String, Object>() {
              {
                put("on", true);
                put("online", true);
              }
            },
            null,
            null),
        new Commands(new String[] {"456"}, "ERROR", null, "deviceTurnedOff", null)
      });
  return new ExecuteResponse(executeRequest.getRequestId(), payload);
}

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

स्थिति से जुड़े जवाब

आपके QUERY और EXECUTE जवाबों में एक status फ़ील्ड शामिल होता है, जो अनुरोध के नतीजे की रिपोर्ट करता है. स्थिति के हर रिस्पॉन्स में इनमें से कोई एक वैल्यू मिल सकती है:

  • SUCCESS: अनुरोध पूरा हो गया.
  • OFFLINE: टारगेट डिवाइस ऑफ़लाइन है या पहुंच से बाहर है.
  • EXCEPTIONS: इस अनुरोध से जुड़ी कोई समस्या या चेतावनी है.
  • ERROR: errorCode से जुड़े अनुरोध को पूरा नहीं किया जा सका.

ERROR और EXCEPTIONS के लिए, ज़्यादा जानकारी के लिए गड़बड़ियां और अपवाद मैनेज करना और गड़बड़ियां और अपवाद देखें.