Sử dụng API

Sau khi có mã truy cập và thực hiện lệnh gọi danh sách thiết bị ban đầu, giờ đây, bạn đã sẵn sàng sử dụng SDM API để truy cập và điều khiển thiết bị của mình.

Liệt kê các cấu trúc và thiết bị

Sử dụng curl để thực hiện một lệnh gọi GET đơn giản đến điểm cuối structures:

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

Một lệnh gọi thành công sẽ trả về danh sách các cấu trúc cho những tài khoản được liên kết với dự ánDevice Access của bạn:

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

Nếu bạn chưa thực hiện, hãy thực hiện lệnh gọi GET đến điểm cuối devices để lấy danh sách thiết bị:

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

Một lệnh gọi thành công sẽ trả về danh sách các thiết bị được liên kết với dự án Device Accesscủa bạn. Mỗi thiết bị có danh sách riêng biệt gồm các đặc điểm có sẵn:

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

Sao chép device-id cho từng thiết bị, bạn sẽ cần mã này cho các lệnh gọi API khác.

Xem thông tin về một thiết bị

Để lấy thông tin về một thiết bị cụ thể, hãy thực hiện lệnh gọi GET đến điểm cuối device-id:

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

Phản hồi sẽ tương tự như trước, nhưng chỉ dành cho thiết bị cụ thể:

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

Thực thi một lệnh

Sau khi xác thực quyền truy cập bằng một lệnh gọi GET thành công, hãy thử thực thi một lệnh tuỳ thuộc vào loại thiết bị mà bạn đã uỷ quyền:

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

Nếu lệnh gọi của bạn thành công, những kết quả sau sẽ xảy ra:

THERMOSTAT

Bạn nhận được một phản hồi trống và máy điều nhiệt thực tế sẽ thay đổi chế độ hiện tại thành chế độ được chỉ định trong các tham số lệnh.

{}

CAMERA

Bạn nhận được URL của sự kiện phát trực tiếp và các mã thông báo liên quan.

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

Khắc phục sự cố

Chưa được xác thực

Mã truy cập cho SDM API chỉ có hiệu lực trong 1 giờ. Nếu bạn nhận được phản hồi UNAUTHENTICATED, thì có thể mã thông báo đã hết hạn. Sử dụng mã làm mới để nhận mã truy cập mới.

Các lỗi khác

Hãy xem Tài liệu tham khảo về mã lỗi để biết danh sách đầy đủ các mã lỗi Device Access .