使用 API

備妥存取權杖並發起初始裝置清單呼叫後,您就可以使用 SDM API 存取及控制裝置。

列出結構和裝置

使用 curlstructures 端點發出簡單的 GET 呼叫:

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/structures' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer access-token'

如果呼叫成功,會傳回與Device Access 專案連結的帳戶結構清單:

{
  "structures": [
    {
      "name": "enterprises/project-id/structures/structure-id",
      "traits": {
        "sdm.structures.traits.Info": {
          "customName": "structure-name"
        }
      }
    }
  ]
}

如果您還沒這麼做,請對 devices 端點發出 GET 呼叫,以便取得裝置清單:

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer access-token'

成功呼叫會傳回與 Device Access專案相連結的裝置清單。每部裝置都有專屬的可用清單:

{
  "devices": [
    {
      "name": "enterprises/project-id/devices/device-id",
      "type": "sdm.devices.types.device-type",
      "traits": { ... },
      "parentRelations": [
        {
          "parent": "enterprises/project-id/structures/structure-id/rooms/room-id",
          "displayName": "device-room-name"
        }
      ]
    }
  ]
}

複製每部裝置的 device-id,其他 API 呼叫將需要這些資訊。

取得裝置資訊

如要取得特定裝置的資訊,請向 device-id 端點發出 GET 呼叫:

curl -X GET 'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id' \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer access-token'

回應應與先前類似,但僅限特定裝置:

{
  "name": "enterprises/project-id/devices/device-id",
  "type": "sdm.devices.types.device-type",
  "traits": { ... },
  "parentRelations": [
    {
      "parent": "enterprises/project-id/structures/structure-id/rooms/room-id",
      "displayName": "device-room-name"
    }
  ]
}

執行指令

使用成功的 GET 呼叫驗證存取權後,請根據已授權的裝置類型嘗試執行指令:

溫度控制器

curl -X POST \
  'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer access-token' \
  --data-raw '{
    "command" : "sdm.devices.commands.ThermostatMode.SetMode",
    "params" : {
      "mode" : "HEAT"
    }
  }'

相機

curl -X POST \
  'https://smartdevicemanagement.googleapis.com/v1/enterprises/project-id/devices/device-id:executeCommand' \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer access-token' \
  --data-raw '{
    "command" : "sdm.devices.commands.CameraLiveStream.GenerateRtspStream",
    "params" : {}
  }'

如果通話成功,則會產生下列結果:

溫度控制器

收到空白回應,且實體溫度控制器將目前的模式變更為指令指令中指定的模式。

{}

相機

您會收到直播網址和相關權杖。

{
  "results" : {
    "streamUrls" : {
      "rtspUrl" : "rtsps://someurl.com/CjY5Y3VKaTZwR3o4Y19YbTVfMF...?auth=g.0.streamingToken"
    },
    "streamExtensionToken" : "CjY5Y3VKaTZwR3o4Y19YbTVfMF...",
    "streamToken" : "g.0.streamingToken",
    "expiresAt" : "2018-01-04T18:30:00.000Z"
  }
}

疑難排解

尚未驗證

SDM API 的存取權杖有效期限只有 1 小時。如果您收到「未經驗證」的回應,表示該權杖可能已過期。使用更新權杖取得新的存取權杖。

其他錯誤

如需完整的 Device Access 錯誤代碼,請參閱錯誤代碼參考資料