Sử dụng API

Với mã truy cập trong tay và lệnh gọi danh sách thiết bị ban đầu được thực hiện, bạn hiện sẵn sàng sử dụng API SDM để truy cập và kiểm soát thiết bị của bạn.

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

Sử dụng curl để thực hiện 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'

Cuộc gọi thành công trả về danh sách cấu trúc cho các tài khoản được liên kết với Device Access dự á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 thao tác này, hãy thực hiện lệnh gọi GET đến điểm cuối của devices để xem 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'

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

{
  "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 mỗi thiết bị. Bạn sẽ cần dữ liệu đó cho các lệnh gọi API khác.

Lấy thông tin về thiết bị

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

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ỉ áp dụng 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 lệnh

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

MÁY ĐIỀU NHIỆT

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

MÁY ẢNH

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 cuộc gọi của bạn thành công, các kết quả sau sẽ xảy ra:

MÁY ĐIỀU NHIỆT

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

{}

MÁY ẢNH

Bạn sẽ nhận được URL của sự kiện phát trực tiếp và các mã thông báo có 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 API SDM chỉ có hiệu lực trong 1 giờ. Nếu bạn nhận được phản hồi KHÔNG XÁC THỰC, thì mã thông báo đó có thể đã hết hạn. Sử dụng mã làm mới để nhận mã mới mã truy cập.

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 đủ Device Access mã lỗi.