Structure API ใน Android

คุณเข้าถึง Structure API ได้ผ่าน Home API สำหรับ Android โดยนำเข้าแพ็กเกจต่อไปนี้ลงในแอป

import com.google.home.Home
import com.google.home.Id
import com.google.home.Structure

การจัดการข้อผิดพลาด

เมธอดใดก็ตามใน Home API อาจแสดง HomeException ดังนั้นเราขอแนะนำให้คุณใช้บล็อก try-catch เพื่อ ดักจับ HomeException ในการเรียกทั้งหมด

เมื่อจัดการ HomeException ให้ตรวจสอบฟิลด์ error.code และ error.message เพื่อดูว่าเกิดข้อผิดพลาดอะไรขึ้น นอกจากนี้ อาจมีรหัสข้อผิดพลาดย่อยด้วย ดังนั้นให้เรียกใช้เมธอด getSubErrorCodes() และตรวจสอบผลลัพธ์

ข้อผิดพลาดที่ไม่ได้จัดการจะทำให้แอปขัดข้อง

ดูข้อมูลเพิ่มเติมได้ที่ การจัดการข้อผิดพลาด

ตัวอย่างการเรียก

รับรายการโครงสร้าง

เมื่อเริ่มต้นแล้ว การเรียก structures() จะแสดง Flow ของโครงสร้างที่คุณเข้าถึงได้

// Get a flow of all structures accessible to the user
val allStructuresFlow: HomeObjectsFlow<Structure> = home.structures()

// Calling list() on a HomeObjectsFlow returns the first Set of elements.
val allStructures: Set<Structure> = allStructuresFlow.list()

structures() API เป็น Flow ที่อาจไม่แสดงรายการโครงสร้างที่ถูกต้องในทันที หากแอปของคุณเป็นแบบ Reactive และติดตาม Flow นั้นเพื่อขับเคลื่อน UI ระบบควรแสดงรายการโครงสร้างที่ถูกต้องในที่สุด นอกจากนี้ ยังมีสถานการณ์อื่นๆ ที่อาจแสดงรายการโครงสร้างที่ว่างเปล่า เช่น หากโทรศัพท์ของผู้ใช้ขาดการเชื่อมต่อหรือหากผู้ใช้เพิกถอนสิทธิ์เข้าถึงแอปของคุณ คุณควรจัดการกรณีเหล่านี้ในแอป

หรือหากจำเป็นต้องใช้การเขียนโปรแกรมแบบ Imperative แทนการเขียนโปรแกรมแบบ Reactive อย่างยิ่ง คุณสามารถใช้โอเปอเรเตอร์ Flow แบบ Terminal ได้

val everyStructure = withTimeout(5000) { home.structures().first { it.isNotEmpty() } }

การเรียกนี้จะรอให้รายการโครงสร้างที่ถูกต้องผ่าน Flow และหมดเวลาหากไม่ได้รับรายการภายในระยะหมดเวลาที่แอปกำหนด

รับพร็อพเพอร์ตี้ของโครงสร้าง

เมื่อมีรายการโครงสร้างแล้ว คุณจะเข้าถึงพร็อพเพอร์ตี้ของโครงสร้างเหล่านั้นได้

// Get a flow on a structure. Flow emits new values on structure metadata changes: name.
val structureFlow: Flow<Structure> = home.structures().itemFlow(myStructureId)

// Get a snapshot of the structure.
val structure: Structure = structureFlow.first()

// Get structure properties
println("id ${structure.id}")
println("name ${structure.name}")

ค้นหาโครงสร้างตามชื่อ

หากทราบชื่อโครงสร้าง คุณจะเข้าถึงโครงสร้างนั้นได้โดยใช้พร็อพเพอร์ตี้ name

val myHome = home.structures().list().first { it.name == "My home" }

จากนั้น คุณจะเข้าถึงพร็อพเพอร์ตี้ ห้อง และอุปกรณ์ของแต่ละโครงสร้างได้

ทำงานกับโครงสร้างหลายรายการ

หากต้องการใช้โครงสร้างมากกว่า 1 รายการ ให้รับการอ้างอิงแยกต่างหากสำหรับแต่ละโครงสร้าง

var structure1: Structure? = null
var structure2: Structure? = null

try {
  structure1 = home.structures().list().firstOrNull { it.name == "Main House" }
} catch (e: HomeException) {
  // Code for handling the exception
}
try {
  structure2 = home.structures().list().firstOrNull { it.name == "Guest Cottage" }
} catch (e: HomeException) {
  // Code for handling the exception
}

รับรายการห้อง

เมื่อมีโครงสร้างแล้ว คุณจะได้รับรายการห้องและเข้าถึงพร็อพเพอร์ตี้ของห้องเหล่านั้นได้

val allRoomsFlow: HomeObjectsFlow<Room> = structure.rooms()
val allRooms: Set<Room> = allRoomsFlow.list()
val room: Room = allRooms.first()

println("id ${room.id}")
println("name ${room.name}")

สร้างห้อง

วิธีสร้างห้องใหม่

val testName = "Test Room Name"
val newRoom: Room = structure.createRoom(testName)

ลบห้อง

หรือจะลบห้องด้วยวิธีอื่นก็ได้

val roomToDelete = structure.rooms().list().filter { it.name == "room_id1" }.firstOrNull()
    structure.deleteRoom(roomToDelete!!)

นอกจากนี้ คุณยังลบห้องได้โดยใช้เพียงรหัส

val roomToDelete1 = allRooms.filter { it.id == testRoomId }.firstOrNull()
structure.deleteRoom(roomToDelete1!!)

หากลบห้องที่มีอุปกรณ์ อุปกรณ์จะยังคงอยู่ในโครงสร้างแต่จะไม่ได้รับการกำหนดให้เป็นห้องอีกต่อไป

ย้ายอุปกรณ์ไปยังห้องอื่น

เมื่อมีโครงสร้างแล้ว คุณจะย้ายอุปกรณ์ไปยังห้องอื่นภายในโครงสร้างนั้นได้

val room2 = structure.rooms().get(Id("room_id_other_structure"))
    val device1 = structure.devices().get(Id("device_id1"))
    structure.moveDevicesToRoom(room2!!, listOf(device1!!))

หากมีเพียงรหัสอุปกรณ์และรหัสห้อง คุณก็ย้ายอุปกรณ์ได้เช่นกัน

structure.moveDevicesToRoom(Id("room_id_other_structure"), listOf(Id("device_id1")))

เปลี่ยนชื่อห้อง

เรียกใช้ setName() เมธอด เพื่อเปลี่ยนชื่อห้อง

livingRoom.setName("Living Room")

ระบบจะตัดชื่อให้สั้นลงหากยาวเกินขีดจำกัด Code Point ของ Unicode (อักขระ) ที่ 60 และจะไม่แสดงข้อผิดพลาด นักพัฒนาแอปมีหน้าที่จัดการชื่อที่ยาว และสามารถตัดสินใจได้ว่าจะแจ้งให้ผู้ใช้ทราบว่าระบบจะตัดชื่อให้สั้นลงหรือไม่

ในระบบนิเวศของ Google Home ผู้ใช้สามารถให้สิทธิ์อุปกรณ์ทั้งหมดในประเภทนั้นพร้อมกันได้สำหรับอุปกรณ์ส่วนใหญ่ สำหรับอุปกรณ์ประเภทที่มีความละเอียดอ่อนหรือถูกจำกัด เช่น ล็อก กล้อง หรือกริ่งประตู ผู้ใช้ต้องให้สิทธิ์อุปกรณ์เหล่านั้นทีละรายการ

หากต้องการดูว่าผู้ใช้ให้สิทธิ์เข้าถึงอุปกรณ์ประเภทที่มีความละเอียดอ่อนหรือถูกจำกัดหรือไม่ ให้ใช้ฟังก์ชัน consentedDeviceTypes() ระดับโครงสร้าง

import com.google.home.Structure
import com.google.home.DeviceType
import com.google.home.DeviceTypeFactory
import com.google.home.consentedDeviceTypes // Extension function from the SDK
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

/**
 * Example of how an app may monitor which device types have been granted access by a user.
 */
fun monitorDeviceConsent(structure: Structure, myScope: CoroutineScope) {
    // Obtain the flow of consented device type factories
    val consentedTypesFlow: Flow<Set<DeviceTypeFactory<out DeviceType>>> =
        structure.consentedDeviceTypes()

    myScope.launch {
        consentedTypesFlow.collect { consentedSet ->
            // Check if the user has consented to share a specific restricted
            // type, such as a Doorbell or Camera.
            val hasCameraAccess = consentedSet.any {
                it.toString() == "matter.google.type.GoogleDoorbellDevice"
            }

            if (hasCameraAccess) {
                // Enable features that require camera access
            } else {
                // Inform the user or disable camera-specific features
            }
        }
    }
}

การทำงานอัตโนมัติ

จุดแรกเข้าของ Automation API คือผ่านโครงสร้าง ดูข้อมูลเพิ่มเติมเกี่ยวกับการทำงานอัตโนมัติใน Home API ได้ที่ ภาพรวม Automation API ใน Android