อุปกรณ์ Matter บางเครื่องประกอบด้วยอุปกรณ์ปลายทางหลายรายการที่มี อุปกรณ์ประเภทเดียวกัน Matterอุปกรณ์อื่นๆMatterเป็นแบบลำดับชั้น โดยมีอุปกรณ์ปลายทางซ้อนอยู่ภายในอุปกรณ์ปลายทางอื่นๆ ใน Home API อุปกรณ์ทั้ง 2 ประเภทเรียกว่าอุปกรณ์หลายส่วน
การแสดงแบบแบน
ก่อนที่จะเปิดตัว Home API เวอร์ชัน 1.8 Home API จะแสดงอุปกรณ์แบบหลายส่วนเป็นชุดอุปกรณ์ที่แยกกันและไม่เกี่ยวข้อง ซึ่งเรียกว่าการแสดงผลแบบแบน
ตัวอย่างเช่น อุปกรณ์แผงติดผนังเดียวที่มีสวิตช์ 4 ตัวจะปรากฏใน Home API เป็นอุปกรณ์ 4 ตัวที่แตกต่างกันและไม่เกี่ยวข้อง และMatterอุปกรณ์แบบลำดับชั้น เช่น ตู้เย็น อาจแสดงใน Home API เป็นชุดอุปกรณ์ โดยแต่ละอุปกรณ์จะสอดคล้องกับปลายทางหนึ่งๆ
ภาพแบนของแผงติดผนังแบบ 4 สวิตช์
ภาพตู้เย็นแบบแบน
การแสดงข้อมูลหลายส่วน
ตั้งแต่รุ่น 1.8 ของ Home API เป็นต้นไป อุปกรณ์แบบหลายส่วนจะ
แสดงใน API เป็นอุปกรณ์เดียวได้ หากต้องการเปิดใช้ลักษณะการทำงานนี้ ให้เรียกใช้เมธอด
devices() ใน
Structure, Room หรือ HomeManager
และตั้งค่าพารามิเตอร์ enableMultipartDevices เป็น true ดังนี้
let devices = try await self.home.devices(enableMultipartDevices: true).list() let device = try XCTUnwrap(devices.first { $0.id == powerstripID }) let outlets = try await device.types.getAll(of: OnOffPluginUnitDeviceType.self)
แผนภาพต่อไปนี้แสดงวิธีที่ตัวเลือก enableMultipartDevices
ส่งผลต่อการแสดงอุปกรณ์แบบหลายส่วนใน Home API
การแสดงแผงผนังแบบหลายส่วน
การแสดงตู้เย็นแบบหลายส่วน
คุณมีตัวเลือกในการรับการแสดงแบบแบนเสมอโดยละเว้นพารามิเตอร์ enableMultipartDevices หรือตั้งค่าเป็น false
ไปยังส่วนต่างๆ ของอุปกรณ์ที่มีหลายส่วน
ในอุปกรณ์แบบหลายส่วน อินสแตนซ์คอมโพเนนต์แต่ละรายการของประเภทอุปกรณ์เรียกว่าส่วน
โดยเข้าถึงชิ้นส่วนได้โดยตรงในอุปกรณ์หลักหรือชิ้นส่วนในลักษณะ
ลำดับชั้น โดยใช้ประเภทอุปกรณ์หรือแท็กMatterเชิงความหมาย
แท็กความหมายจะใช้งานใน Home API ด้วย DescriptorTrait.SemanticTagStruct
คลาส DeviceType Abstract
ใช้
อินเทอร์เฟซ HasParts
ซึ่งช่วยให้นักพัฒนาซอฟต์แวร์ไปยังส่วนต่างๆ ของแผนผังอุปกรณ์ผ่านพร็อพเพอร์ตี้ parts และเมธอด part()
ได้ อินสแตนซ์ของแต่ละส่วนยังใช้HasPartsอินเทอร์เฟซด้วย เพื่อให้การเรียกใช้ parts() ในส่วนหนึ่งๆ สร้างรายการส่วนย่อย 0 รายการขึ้นไป
ตัวอย่างต่อไปนี้แสดงวิธีเข้าถึงส่วนต่างๆ ของอุปกรณ์ที่มีสวิตช์หลายตัว
val device = homeManager .devices(enableMultipartDevices = true) .itemFlow(Id(MULTI_SWITCH_DEVICE)) .first() // Here at top-level, we are using the homeDevice.parts() API to access flow of // all the switches. Then we get the part ids. val partIds = device .parts() .map { parts -> parts.filter { it.has(Switch) }.mapNotNull { it.metadata.partId } } .first() .toSet()
ตัวอย่างถัดไปแสดงวิธีเข้าถึงชิ้นส่วนของอุปกรณ์ตู้เย็น
val rootDevice = homeManager.devices(true).itemFlow(Id("device@uuid1")) // On the top level, HomeDevice provides both plural (parts) // and singular (part) APIs. // The parts() API returns all the parts accessible from the top level, // including Endpoint 0 and its children. val childParts = rootDevice.parts().first() // childParts contain (EP0 as RootNode, EP1 as Refrigerator) // The singular part() API accepts DeviceType and tags (optional). val refrigerator = rootDevice.part(Refrigerator).first() // Get the refrigerator device which in this case is just device@uuid1 val refrigeratorDevice = homeManager.devices(false).itemFlow(refrigerator.metadata.partId.deviceId) // DeviceType uses a synchronous API for providing access to parts val cabinets = refrigerator.parts // [EP2, EP3] // Get the HomeDevice for these cabinets (device@uuid2 and device@uuid3) val cabinetDeviceIds = cabinets.map { it.metadata.partId } // Now use the devices API with enableMultipartDevices = false. val cabinetDevices = homeManager.devices(false) .map { devices -> devices.filter { it.id in cabinetDeviceIds } }.first()