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