Tính năng tự động hoá nhiều thiết bị trên iOS

Một quy trình tự động hoá có thể tham chiếu đến các thiết bị có nhiều phần tương tự như một quy trình tự động hoá không sử dụng các thiết bị có nhiều phần.

Trước tiên, hãy lấy(các) bộ phận cấu thành như bình thường. Hãy xem phần Thiết bị nhiều phần để biết cách sử dụng thiết bị nhiều phần.

Sau đó, đối với mỗi phần mà bạn muốn sử dụng trong quy trình tự động hoá, hãy tạo một AutomationPartPath. Thao tác này cho phép bạn tham chiếu phần đó trong các điều kiện, hành động và điểm bắt đầu tự động hoá.

//  Obtain a reference to the device:
let multipartDevices = try await self.home.devices(enableMultipartDevices: true).list()

let light = multipartDevices.first(where: {
  $0.parts.contains(OnOffLightDeviceType.self) && $0.structureID == structure.id
})

let lightDeviceType = await light.parts.get(OnOffLightDeviceType.self)

let lightPartPath = light.automationPart(lightDeviceType)

Một điều khác cần lưu ý là các thao tác tự động hoá thường lấy một thiết bị và loại thiết bị làm tham số. Tuy nhiên, một thao tác tự động hoá tham chiếu đến một thiết bị bộ phận cấu thành – một lần nữa, sử dụng AutomationPartPath – chỉ cần AutomationPartPath, vì AutomationPartPath đã chứa một tham chiếu đến loại thiết bị ngoài tham chiếu đến thiết bị bộ phận cấu thành.

Ví dụ: trong Automation API, loại thiết bị Refrigerator có thể được coi là một thiết bị nhiều phần. Nó bao gồm một RefrigeratorDevice gốc có thể chứa nhiều tủ phụ, chẳng hạn như tủ đông hoặc tủ tiêu chuẩn thuộc loại TemperatureControlledCabinetDevice.

Để tạo các quy trình tự động hoá tủ lạnh, bạn chủ yếu tương tác với 2 đặc điểm Matter tiêu chuẩn:

  • RefrigeratorAlarm: Hiển thị các cảnh báo về trạng thái cửa thông qua thuộc tính doorOpen của trường state.
  • RefrigeratorAndTemperatureControlledCabinetMode: Cho phép đọc và ra lệnh ở các chế độ. Ví dụ: chạy các lệnh như changeToMode để chuyển sang các chế độ như LowEnergy, RapidCool hoặc LowNoise.

Chương trình tự động hoá ví dụ này sẽ kích hoạt khi cửa tủ lạnh mở. Nếu cửa mở quá 2 phút, quy trình tự động hoá sẽ phát thông báo bằng giọng nói trên loa thông minh, nhấp nháy đèn nhà bếp và gửi thông báo đẩy. Xin lưu ý rằng chế độ tự động hoá này chỉ ảnh hưởng đến ngăn lạnh của thiết bị, bỏ qua ngăn đá (nếu có):

import GoogleHomeSDK
import GoogleHomeTypes

typealias RefrigeratorAlarmTrait = Matter.RefrigeratorAlarmTrait
typealias OnOffTrait = Matter.OnOffTrait

// Fetch devices using the multipart device model.
let multipartDevices = try await self.home.devices(enableMultipartDevices: true).list()

// Obtain a reference to the refrigerator device.
guard let refrigeratorDevice = multipartDevices.first {
    $0.types.contains(TemperatureControlledCabinetDeviceType.self) &&
    $0.traits.contains(Matter.RefrigeratorAndTemperatureControlledCabinetModeTrait.self) &&
    $0.traits.contains(Matter.RefrigeratorAlarmTrait.self)
  }

let refrigeratorDeviceType = await refrigeratorDevice.parts.get(RefrigeratorDeviceType.self).first

// Get all temperature-controlled cabinet parts of the refrigerator
let cabinets = refrigeratorDeviceType.parts(type: TemperatureControlledCabinetDeviceType.self)

// Find the cabinet part with the 'refrigerator' semantic tag
let refrigeratorCabinet = cabinets.first {
  $0.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}
var cabinetPartPath = refrigeratorDevice.automationPart(refrigeratorCabinet)

let structure = home.structures().list().first

let speaker = multipartDevices.first(where: {
        $0.types.contains(SpeakerDeviceType.self) && $0.structureID == structure.id
      })

let refrigeratorDoorAlert = automation(
  name: "Refrigerator Door Open Alert",
  description: "Warn when the refrigerator door has been open for over 2 min."
) {
  // 1. Starter: Monitor the refrigerator door alarm trait
  let alarmStarter = starter(
    cabinetPartPath,
    RefrigeratorAlarmTrait.self
  )

  alarmStarter

  // 2. Condition: Ensure the 'doorOpen' alarm remains active for 120 seconds
  condition(for: .seconds(120)) {
    alarmStarter.state.doorOpen.equals(true)
  }

  // 3. Actions: Execute parallel reactions
  parallel {
    // Broadcast warning to household speakers
    action(speaker, SpeakerDeviceType.self) {
      Google.AssistantBroadcastTrait.broadcast(msg: "The refrigerator door has been left open!")
    }

    // Push a notification alerts to home members' mobile devices
    action(structure) {
      Google.NotificationTrait.sendNotifications(
        title: "Fridge Alert",
        body: "The refrigerator door has been open for over 2 min.",
        optInMemberEmailsArray: ["222larabrown@gmail.com"]
      )
    }
  }
}

Ví dụ sau đây chuyển tủ lạnh sang chế độ Tiết kiệm năng lượng khi phát hiện không có ai ở nhà.

import GoogleHomeSDK
import GoogleHomeTypes

typealias AreaPresenceStateTrait = Google.AreaPresenceStateTrait
typealias RefrigeratorAndTemperatureControlledCabinetModeTrait = Matter.RefrigeratorAndTemperatureControlledCabinetModeTrait

let structure = home.structures().list().first()

// Fetch devices using the multipart device model.
let devices = try await self.home.devices(enableMultipartDevices: true).list()

// Obtain a reference to the refrigerator device.
guard let refrigeratorDevice = multipartDevices.first {
    $0.types.contains(TemperatureControlledCabinetDeviceType.self) &&
    $0.traits.contains(Matter.RefrigeratorAndTemperatureControlledCabinetModeTrait.self) &&
    $0.traits.contains(Matter.RefrigeratorAlarmTrait.self)
  }

let refrigeratorDeviceType = await refrigeratorDevice.parts.get(RefrigeratorDeviceType.self)

// Get all temperature-controlled cabinet parts of the refrigerator
let cabinets = refrigeratorDeviceType.parts(type: TemperatureControlledCabinetDeviceType.self)

// Find the cabinet part with the 'refrigerator' semantic tag
let refrigeratorCabinet = cabinets.first {
  $0.metadata.tags.contains(SemanticTag.Refrigerator.refrigerator)
}

let cabinetPartPath = refrigeratorDevice.automationPart(refrigeratorCabinet)

let refrigeratorEcoMode = automation(
  name: "Refrigerator Eco Mode",
  description: "Automatically changes refrigerator to low energy mode when house is vacant."
) {
  // 1. Starter: Monitor household presence changes
  let presenceStarter = starter(structure, AreaPresenceStateTrait.self)

  presenceStarter

  // 2. Condition: Verify presence state transitions to vacant
  condition {
    presenceStarter.presenceState.equals(.presenceStateVacant)
  }
  // 3. Action: Set refrigerator cabinet Mode to 'Low Energy' (commonly option index 1)
  action(cabinetPartPath) {
    RefrigeratorAndTemperatureControlledCabinetModeTrait.changeToMode(newMode: 1)
  }
}