ด้วยโทเค็นเพื่อการเข้าถึงในมือและการเรียกรายการอุปกรณ์ครั้งแรก ตอนนี้คุณก็พร้อมที่จะใช้ 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 อื่นๆ
รับข้อมูลสําหรับอุปกรณ์
หากต้องการรับข้อมูลอุปกรณ์ที่เฉพาะเจาะจง ให้เรียก GET ไปยังปลายทาง 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 ทั้งหมด