iOS 上的 Structure API

您可以通过适用于 iOS 的 Home API 访问 Structure API。

如需使用 Structure API,请先将 GoogleHomeSDK 软件包导入到您的应用中:

import GoogleHomeSDK

错误处理

Home API 中的某些方法会抛出 HomeError,因此我们建议您使用 do-catch 块来捕获 HomeError 对这些调用的。

处理 HomeError 时,请检查其 codemessage 字段,以了解问题所在。

任何未处理的错误都会导致应用崩溃。

如需了解详情,请参阅 错误处理

Structure API

Home 表示住宅图,是 Structure API 的入口点。它提供对结构、房间和设备的引用。

Structure 表示住宅图中的一个结构。它提供对结构元数据(例如 idname)的访问权限。

使用 structures() 获取您账号中的所有结构。这些结构以 Query的形式返回,该形式提供多种使用其数据的方式:

API 说明
stream() 返回一个 Publisher,该发布者会在发生更改时单独发出每个对象。
batched() 返回一个 Publisher,该发布者会将当前结果作为一组 Set 对象发出。每个发出的 Set 都表示对象图的当前状态。
list() 将当前结果作为一组 Set 对象返回。

structures().list() 调用可能不会立即返回一组有效的结构。如果您的应用是响应式应用,并且调用 stream() 来订阅所有结构更改以驱动界面,则最终应返回有效的结构列表。在其他情况下,也可能会返回空结构列表,例如,当用户的手机失去连接或用户撤消了对您应用的权限时。您应确保在应用中处理这些情况。

@Published public private(set) var structures: [Structure] = []
private var structuresCancellable: AnyCancellable?

  self.structuresCancellable = home
    .structures()
    .batched()
    .receive(on: DispatchQueue.main)
    .map { Array($0) }
    .catch {
      // Failed to load structures
      return Just([Structure]())
    }
    .assign(to: \.structures, on: self)

示例结构调用

获取一组结构

Query<Structure> 调用 list() 会返回最新的元素集:

// Get a stream of all structures accessible to the user
let allStructuresChanges = self.home.structures()
let allStructures = try? await allStructuresChanges.list()

在设计响应式应用时,您需要使用 batched()stream() 调用,而不是 list(),因为这些调用会在住宅图发生更改时自动生成数据。

获取结构属性

有了结构列表后,您就可以访问这些结构的属性:

// Get a stream of changes taking place on a structure.
let structureChanges = try await home.structures().list().filter { $0.id == structureID }

// Get a snapshot of the structure.
let structure = structureChanges.first!

// Get structure properties
print("id \(structure.id) ")
print("name \(structure.name) ")

按名称查找结构

如果您知道结构的名称,也可以使用 name 属性访问该结构:

do {
  structure1 = try await home.structures().list().first(where: { $0.name == "Main House" })
} catch let _ as HomeError {
  // Code for handling the exception
}

这样一来,您便可以访问每个结构的属性、房间和设备。

使用多个结构

如需使用多个结构,请为每个结构获取单独的引用:

var structure1: Structure!
var structure2: Structure!
do {
  structure1 = try await home.structures().list().first(where: { $0.name == "Main House" })
} catch let _ as HomeError {
  // Code for handling the exception
}
do {
  structure2 = try await home.structures().list().first(where: { $0.name == "Guest Cottage" })
} catch let _ as HomeError {
  // Code for handling the exception
}

房间

一个房间包含一组设备。房间始终是结构的一部分,而一个结构可以有多个房间。从结构中移除房间不会从结构中移除该房间中的设备。但是,如果房间被删除,则该房间中的设备将变为未分配状态。

使用 Home.rooms() 检索账号中的所有房间,然后使用 roomID = device.roomID 显示每个房间中的相应设备。

self.home.rooms().batched()
  .combineLatest(self.home.devices().batched())
  .receive(on: DispatchQueue.main)
  .catch { error in
    // Failed to load rooms and devices
    return Just((Set<Room>(), Set<HomeDevice>()))
  }
  .map { rooms, devices in
    var devicesByRoom = [Room: [HomeDevice]]()
    for room in rooms where room.structureID == currentStructureID {
      devicesByRoom[room] = devices.filter { $0.roomID == room.id }
    }
    return devicesByRoom
  }.assign(to: &self.$devicesByRoom)

示例房间调用

获取房间列表

使用 Home 类,您可以获取房间列表并访问这些房间的属性:

let allRoomsChanges = self.home.rooms()
let allRooms = try await allRoomsChanges.list()
let room = allRooms.first!
XCTAssertTrue(allRooms.contains(room))

print("id \(room.id) ")
print("name \(room.name) ")

创建房间

如需在 Structure 中创建新房间,请执行以下操作:

let testName = "Test Room Name"
var newRoom: Room!
do {
  newRoom = try await structure.createRoom(name: testName)
  XCTAssertNotNil(newRoom)
} catch let _ as HomeError {
  // Code for handling the exception
}

删除房间

或者,您也可以删除房间:

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

您还可以使用房间的 ID 删除房间:

let roomToDelete = allRooms.first(where: { $0.id == room.id })
if let roomToDelete1 = roomToDelete {
  do {
    try await structure.deleteRoom(roomToDelete1)
  } catch let _ as HomeError {
    // Code for handling the exception
  }
}

如果删除了包含设备的房间,这些设备仍会保留在结构中,但不再分配给任何房间。

将设备移至其他房间

Structure 还允许您将设备移至其他房间:

do {
  try await structure.move(device: light, to: room)
} catch let _ as HomeError {
  // Code for handling the exception
}

更改房间的名称

调用 setName(_:) 方法来更改房间的名称:

let updatedRoom = try await theRoom.setName("new room name")

更改房间的名称时,原始 Room 结构体 保持不变,更改会反映在返回的更新后的 Room 对象中。

如果名称超过 60 个 Unicode 码位(字符)的限制,系统会截断名称,但不会抛出任何错误。开发者负责处理长名称,例如,可以决定是否告知用户名称将被截断。