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.GenerateRtspStream",
    "params" : {}
  }'

呼び出しが成功すると、次のような結果になります。

サーモスタット

空のレスポンスが返され、物理サーモスタットが現在のモードをコマンド パラメータで指定されたモードに変更します。

{}

カメラ

ライブ ストリームの URL と関連トークンが届きます。

{
  "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