アクセス トークンを取得し、デバイスリストの初期呼び出しを行ったので、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 呼び出しが成功してアクセスが検証されたら、承認したデバイスのタイプに応じてコマンドを実行します。
THERMOSTAT
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" : {}
}'呼び出しが成功すると、次の結果が得られます。
THERMOSTAT
空のレスポンスが返され、物理サーモスタットの現在のモードがコマンド パラメータで指定されたモードに変更されます。
{}カメラ
ライブ ストリームの 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 時間のみ有効です。UNAUTHENTICATED レスポンスが返された場合は、トークンの有効期限が切れている可能性があります。更新トークンを使用して、新しいアクセス トークンを取得します。
その他のエラー
Device Access エラーコードの完全なリストについては、エラーコード リファレンスをご覧ください。