使用 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 调用验证您的访问权限后,请尝试执行命令,具体取决于您已授权的设备类型:

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"
    }
  }'

CAMERA

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

您会收到空响应,并且实体温控器会将当前模式更改为命令参数中指定的模式。

{}

CAMERA

您会收到直播网址和相关令牌。

{
  "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 错误代码列表,请参阅错误代码参考文档