เมื่อมีโทเค็นเพื่อการเข้าถึงและได้ทำการเรียกรายการอุปกรณ์เริ่มต้นแล้ว ตอนนี้คุณก็พร้อมที่จะใช้ 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 ที่สำเร็จแล้ว ให้ลองเรียกใช้คำสั่ง โดยขึ้นอยู่กับประเภทอุปกรณ์ที่คุณให้สิทธิ์
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"
}
}'
กล้อง
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" : {}
}'
หากการเรียกใช้สำเร็จ จะเกิดผลลัพธ์ต่อไปนี้
THERMOSTAT
คุณจะได้รับการตอบกลับที่ว่างเปล่าและตัวควบคุมอุณหภูมิจะเปลี่ยน โหมดปัจจุบันเป็นโหมดที่ระบุในพารามิเตอร์คำสั่ง
{}
กล้อง
คุณจะได้รับ 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 ชั่วโมงเท่านั้น หาก คุณได้รับการตอบกลับว่า UNAUTHENTICATED แสดงว่าโทเค็นน่าจะหมดอายุแล้ว ใช้ โทเค็นการรีเฟรชเพื่อรับ โทเค็นเพื่อการเข้าถึงใหม่
ข้อผิดพลาดอื่นๆ
ดูข้อมูลอ้างอิงรหัสข้อผิดพลาดสำหรับรายการรหัสข้อผิดพลาดทั้งหมด Device Access