使用 API

取得存取權杖並完成初始裝置清單呼叫後,您就可以使用 SDM API 存取及控制裝置。

列出住家結構體和裝置

使用 curl 對 structures 端點發出簡單的 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.GenerateWebRtcStream",
    "params" : {
      "offerSdp" : "offerSdp"
    }
  }'

如果呼叫成功,會發生下列結果:

溫度控制器

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

{}

相機

您會收到 SDP 回覆和媒體工作階段 ID,用於建立 WebRTC 直播。

{
  "results" : {
    "answerSdp" : "answerSdp",
    "expiresAt" : "2020-01-04T18:30:00.000Z",
    "mediaSessionId" : "zMaipYO0qEoVl8sBYFIyGLPxXw..."
  }
}

疑難排解

未經驗證

SDM API 的存取權杖有效期限只有 1 小時。如果收到 UNAUTHENTICATED 回應,表示權杖可能已過期。使用更新權杖取得新的存取權杖。

其他錯誤

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