เมื่อมีโทเค็นเพื่อการเข้าถึงอยู่ในมือ และมีการเรียกรายการอุปกรณ์ครั้งแรก พร้อมที่จะใช้ 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