ใช้ API

เมื่อคุณมีโทเค็นการเข้าถึงและทำการเรียกใช้รายการอุปกรณ์ครั้งแรกแล้ว คุณก็พร้อมที่จะใช้ SDM API เพื่อเข้าถึงและควบคุมอุปกรณ์

แสดงรายการโครงสร้างและอุปกรณ์

ใช้ curl เพื่อเรียก GET แบบง่ายไปยังปลายทาง structures

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

หากยังไม่ได้ดำเนินการ ให้เรียกใช้ GET ที่ปลายทาง devices เพื่อ ดูรายการอุปกรณ์

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 ปลายทาง:

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