อุปกรณ์แสงสว่างใช้ลักษณะ Matter มาตรฐานเพื่อระบุหลอดไฟ สลับสถานะการเปิด/ปิด และปรับระดับความสว่าง
ประเภทอุปกรณ์หลอดไฟพื้นฐานที่สุดคือ
OnOffLightDeviceType
ซึ่งใช้กับลักษณะ
OnOffTrait และ
IdentifyTrait
และอาจรองรับลักษณะ
LevelControlTrait
และลักษณะ
LightEffectsTrait
ไฟประเภทอื่นๆ มีความสามารถเพิ่มเติมและใช้งานด้วยลักษณะที่แตกต่างกัน ดังนี้
| ประเภทอุปกรณ์ของ Home API | ลักษณะเฉพาะ | แอปตัวอย่าง Swift | กรณีการใช้งาน |
|---|---|---|---|
|
ไฟอุณหภูมิสี
อุปกรณ์ให้แสงสว่างที่สามารถเปิดหรือปิด ปรับความเข้มของแสง และปรับอุณหภูมิสีได้ |
ลักษณะที่จำเป็น matter IdentifyTrait matter OnOffTrait matter LevelControlTrait matter ColorControlTrait |
สว่าง | |
|
หลอดไฟที่หรี่แสงได้
อุปกรณ์ให้แสงสว่างที่เปิดหรือปิดได้ ปรับความเข้มของแสงได้ และเปลี่ยนได้โดยใช้เซ็นเซอร์ตรวจจับการมีคนอยู่ซึ่งเชื่อมโยงอยู่ |
ลักษณะที่จำเป็น matter IdentifyTrait matter OnOffTrait matter LevelControlTrait |
สว่าง | |
|
ไฟสีแบบขยาย
อุปกรณ์ให้แสงสว่างที่สามารถเปิดหรือปิด ปรับความเข้มของแสง ปรับอุณหภูมิสี และเปลี่ยนได้โดยใช้เซ็นเซอร์ตรวจจับการมีคนอยู่ซึ่งเชื่อมโยงอยู่ |
ลักษณะที่จำเป็น matter IdentifyTrait matter OnOffTrait matter LevelControlTrait matter ColorControlTrait |
สว่าง | |
|
เปิด/ปิดไฟ
อุปกรณ์ให้แสงสว่างที่เปิดหรือปิดได้ |
ลักษณะที่จำเป็น matter IdentifyTrait matter OnOffTrait |
สว่าง |
ตรวจสอบการรองรับแอตทริบิวต์และคำสั่งสำหรับอุปกรณ์ก่อนใช้ฟีเจอร์หรือพยายามอัปเดตแอตทริบิวต์เสมอ ดูข้อมูลเพิ่มเติมได้ที่ควบคุมอุปกรณ์ในiOS
ข้อกำหนดเบื้องต้น
ก่อนที่จะทำงานกับอุปกรณ์แสงสว่างและเส้นโค้งแสงสว่าง โปรดตรวจสอบว่าแอปของคุณเริ่มต้น Home SDK และรวมการนำเข้าที่จำเป็นไว้ด้วย
import Foundation import GoogleHomeSDK import GoogleHomeTypes import Network
ค้นหาอุปกรณ์แสงสว่าง
หากต้องการค้นหาอุปกรณ์แสงสว่างในบ้าน ให้กรองรายการอุปกรณ์ตามประเภทอุปกรณ์ เช่น OnOffLightDeviceType และ DimmableLightDeviceType
let allDevices = try await home.devices().list() // Get all On/Off Light devices in the structure let onOffLights = allDevices.filter { $0.types.contains(OnOffLightDeviceType.self) } // Get all Dimmable Light devices in the structure let dimmableLights = allDevices.filter { $0.types.contains(DimmableLightDeviceType.self) } // Retrieve a specific device type instance to access its traits let onOffLightDeviceType = await onOffLights.first?.types.get(OnOffLightDeviceType.self) let dimmableLightDeviceType = await dimmableLights.first?.types.get(DimmableLightDeviceType.self)
นอกจากนี้ คุณยังดึงข้อมูลอุปกรณ์แสงสว่างทั้งหมดในอุปกรณ์แสงสว่างหลายประเภทได้ในตัวกรองเดียว ดังนี้
// Get a list of devices with similar device types (lights) let lightDevices = try await self.home.devices().list().compactMap { $0.types.contains(DimmableLightDeviceType.self) || $0.types.contains(OnOffLightDeviceType.self) || $0.types.contains(ColorTemperatureLightDeviceType.self) || $0.types.contains(ExtendedColorLightDeviceType.self) }
ดูข้อมูลพื้นฐานเกี่ยวกับอุปกรณ์
ติดตั้งใช้งานในแอปตัวอย่างสำหรับ iOS
ลักษณะ BasicInformation ประกอบด้วยข้อมูลต่างๆ เช่น ชื่อผู้ให้บริการ รหัสผู้ให้บริการ รหัสสินค้า ชื่อสินค้า (รวมถึงข้อมูลรุ่น) และเวอร์ชันของซอฟต์แวร์สำหรับอุปกรณ์ ดังนี้
let vendorName = basicInfoTrait.attributes.vendorName! let vendorID = basicInfoTrait.attributes.vendorID! let productID = basicInfoTrait.attributes.productID! let productName = basicInfoTrait.attributes.productName! let softwareVersion = basicInfoTrait.attributes.softwareVersion!
ระบุอุปกรณ์แสงสว่าง
เมื่อห้องมีหลอดไฟหลายดวง คุณสามารถช่วยผู้ใช้ค้นหาหลอดไฟที่ต้องการได้โดยการทริกเกอร์สถานะการระบุด้วยภาพ เช่น การกะพริบไฟเป็นเวลา 2-3 วินาที อุปกรณ์แสงสว่างทั้ง 4 ประเภทต้องมีลักษณะ Matter.IdentifyTrait
เรียกใช้คำสั่ง identify โดยมีระยะเวลา identifyTime เป็นวินาทีเพื่อเริ่มสถานะการระบุตัวตน การส่ง 0 จะหยุดสถานะการระบุทันที นอกจากนี้ คุณยังเรียกใช้ triggerEffect เพื่อเรียกใช้เอฟเฟกต์ภาพที่เฉพาะเจาะจงได้ เช่น Blink หรือ Breathe
func identifyLight(lightDeviceType: DimmableLightDeviceType) async throws {
guard let identifyTrait = lightDeviceType.matterTraits.identifyTrait else {
return
}
// Blink the light for 10 seconds so the user can locate it
if identifyTrait.supportsIdentifyCommand {
try await identifyTrait.identify(identifyTime: 10)
}
// Or trigger a specific visual identification effect
if identifyTrait.supportsTriggerEffectCommand {
try await identifyTrait.triggerEffect(
effectIdentifier: .blink,
effectVariant: .default
)
}
}
ตรวจสอบการเชื่อมต่อของอุปกรณ์แสงสว่าง
ระบบจะตรวจสอบการเชื่อมต่อของอุปกรณ์ที่ระดับประเภทอุปกรณ์ เนื่องจากอุปกรณ์บางเครื่องรองรับอุปกรณ์หลายประเภท สถานะที่แสดงผลคือชุดค่าผสมของสถานะการเชื่อมต่อสำหรับลักษณะทั้งหมดในอุปกรณ์นั้น
let lightConnectivity = dimmableLightDeviceType.metadata.sourceConnectivity .connectivityState
คุณอาจเห็นสถานะ partiallyOnline ในกรณีที่มีอุปกรณ์หลายประเภทและไม่มีการเชื่อมต่ออินเทอร์เน็ต ลักษณะมาตรฐานของ Matter อาจยังออนไลน์อยู่เนื่องจากการกำหนดเส้นทางในเครื่อง แต่ลักษณะที่อยู่ในระบบคลาวด์จะออฟไลน์
ดูที่อยู่ IP ของอุปกรณ์
หากต้องการค้นหาที่อยู่ IP ของอุปกรณ์ ให้ใช้แอตทริบิวต์ networkInterfaces ของ GeneralDiagnosticsTrait
ระบบจะแสดงผลที่อยู่เป็นออบเจ็กต์ Data ซึ่งคุณสามารถจัดรูปแบบเป็นสตริง IPv4 หรือ IPv6 มาตรฐานได้โดยใช้เฟรมเวิร์ก Network ดังนี้
func getIpAddresses(trait: Matter.GeneralDiagnosticsTrait) -> [String] {
let interfaces = trait.attributes.networkInterfaces ?? []
var ipAddresses: [String] = []
for interface in interfaces {
for data in interface.iPv4Addresses {
if let ipv4 = IPv4Address(data) {
ipAddresses.append(String(describing: ipv4))
}
}
for data in interface.iPv6Addresses {
if let ipv6 = IPv6Address(data) {
ipAddresses.append(String(describing: ipv6))
}
}
}
return ipAddresses
}
ควบคุมอุปกรณ์แสงสว่าง
ประเภทอุปกรณ์ OnOffLight และ DimmableLight ใช้ลักษณะ Matter มาตรฐานเพื่อระบุหลอดไฟ สลับสถานะพลังงาน และปรับระดับความสว่าง
เปิดหรือปิดไฟ
ใช้ลักษณะ
OnOffTrait
เพื่ออ่านสถานะพลังงานปัจจุบันของ OnOffLight หรือ DimmableLight และเพื่อเปิดหรือปิดไฟ
ก่อนอ่านแอตทริบิวต์ onOff ให้ตรวจสอบว่าอุปกรณ์รองรับแอตทริบิวต์ดังกล่าว อุปกรณ์บางอย่างทำงานเป็นปลายทางแบบคำสั่งเท่านั้นและไม่รายงานสถานะ
// Check if the OnOff trait supports the onOff attribute. if onOffTrait.attributes.$onOff.isSupported { print("onOffTrait supports onOff state") } else { print("onOffTrait is for a command only device!") }
เมื่อonOffเป็นtrue ไฟจะติดสว่าง เมื่อ onOff เป็น false ไฟจะปิด
หากต้องการเปลี่ยนสถานะพลังงานของไฟ ให้ยืนยันว่าลักษณะรองรับคำสั่งที่ต้องการ แล้วเรียกใช้ on(), off() หรือ toggle()
// Check if the OnOff trait supports the toggle command. if onOffTraitTest.supportsToggleCommand { print("onOffTrait supports toggle command") } else { print("onOffTrait does not support stateful toggle command") }
// Calling a command on a trait. do { try await onOffTraitTest.toggle() } catch let _ as HomeError { // Code for handling the exception }
นอกจากนี้ คุณยังเรียกใช้ on() และ off() ได้โดยตรงเพื่อตั้งค่าสถานะที่ชัดเจน ดังนี้
do { try await onOffTraitTest.off() try await onOffTraitTest.on() } catch let _ as HomeError { // Code for handling the exception }
หากต้องการปิดไฟโดยใช้การเปลี่ยนภาพแบบค่อยเป็นค่อยไป ให้เรียกใช้ offWithEffect() และระบุค่า EffectIdentifierEnum ดังนี้
// Turn off the light using the DyingLight effect. do { try await onOffTraitTest.offWithEffect( effectIdentifier: Matter.OnOffTrait.EffectIdentifierEnum.dyingLight, effectVariant: 0 ) } catch let _ as HomeError { // Code for handling the exception }
ปรับระดับความสว่าง
DimmableLight ต้องใช้ลักษณะ LevelControlTrait และ OnOffLight สามารถเลือกที่จะรองรับลักษณะนี้ได้ ใช้ลักษณะนี้เพื่ออ่านและควบคุมระดับความสว่างของแสงไฟ
แอตทริบิวต์ currentLevel จะรายงานระดับความสว่างปัจจุบันในระดับตั้งแต่ 0 ถึง 254 นอกจากนี้ คุณยังตรวจสอบ minLevel และ maxLevel เพื่อกำหนดช่วงความสว่างที่ถูกต้องซึ่งฮาร์ดแวร์รองรับได้ด้วย
หากต้องการตั้งค่าความสว่างเป็นค่าที่เฉพาะเจาะจง ให้เรียกใช้ moveToLevel() โปรดทราบว่า
moveToLevel() จะเปลี่ยนเฉพาะระดับปัจจุบันเมื่อไฟเปิดอยู่เท่านั้น
เว้นแต่คุณจะกำหนดค่า optionsOverride ด้วย executeIfOff หากต้องการเปิดไฟโดยอัตโนมัติเมื่อตั้งค่าระดับความสว่างที่ไม่ใช่ศูนย์ (หรือปิดไฟเมื่อตั้งค่าระดับเป็นต่ำสุด) ให้ใช้ moveToLevelWithOnOff()
// Change the brightness of the light to 50% do { try await levelControlTraitTest.moveToLevel( level: UInt8(127), transitionTime: 0, optionsMask: Matter.LevelControlTrait.OptionsBitmap(), optionsOverride: Matter.LevelControlTrait.OptionsBitmap() ) } catch let _ as HomeError { // Code for handling the exception }
ตัวอย่างต่อไปนี้จะอ่านระดับความสว่างปัจจุบันและใช้ moveToLevelWithOnOff() เพื่อตั้งค่าไฟที่หรี่แสงได้ให้มีความสว่าง 75% (190) โดยใช้เวลาเปลี่ยนผ่าน 1 วินาที (10 ในหน่วย 1 ใน 10 ของวินาที)
func setLightBrightness(lightDeviceType: DimmableLightDeviceType) async throws { guard let levelControl = lightDeviceType.matterTraits.levelControlTrait else { return } // Read the current brightness level (0 to 254) let currentLevel: UInt8? = levelControl.attributes.currentLevel print("Current brightness level: \(String(describing: currentLevel))") // Move to ~75% brightness (190 out of 254) and turn on the light if it is off if levelControl.supportsMoveToLevelWithOnOffCommand { try await levelControl.moveToLevelWithOnOff( level: UInt8(190), transitionTime: 10, optionsMask: Matter.LevelControlTrait.OptionsBitmap(), optionsOverride: Matter.LevelControlTrait.OptionsBitmap() ) } }
นอกจากนี้ คุณยังเพิ่มหรือลดความสว่างโดยสัมพันธ์กับระดับปัจจุบันได้ด้วยการเรียกใช้ stepWithOnOff() พร้อม .up หรือ .down และ stepSize
สีของตัวควบคุม
ลักษณะColorControlTrait พร้อมใช้งานทั้งใน ColorTemperatureLightDeviceType และ ExtendedColorLightDeviceType
ลักษณะนี้ช่วยให้คุณปรับอุณหภูมิสีในหน่วยไมโครเรซิโพรคัลดีกรีหรือ Mired ได้
guard let colorControlTrait = colorTempLightDeviceType.matterTraits.colorControlTrait else { return } // Read the current color temperature and hardware limits in mireds // (Mireds = 1,000,000 / Kelvin; e.g., 370 mireds ≈ 2700K warm white, 250 mireds = 4000K cool white) let currentMireds = colorControlTrait.attributes.colorTemperatureMireds let minMireds = colorControlTrait.attributes.colorTempPhysicalMinMireds ?? 153 let maxMireds = colorControlTrait.attributes.colorTempPhysicalMaxMireds ?? 500 // Clamp target (370 mireds ≈ 2700K) to the device's physical bounds let targetMireds = min(max(UInt16(370), minMireds), maxMireds) if colorControlTrait.supportsMoveToColorTemperatureCommand { try await colorControlTrait.moveToColorTemperature( colorTemperatureMireds: targetMireds, transitionTime: 20, // 2.0 seconds (in 0.1s units) optionsMask: Matter.ColorControlTrait.OptionsBitmap(), optionsOverride: Matter.ColorControlTrait.OptionsBitmap() ) }
นอกจากนี้ คุณยังตั้งค่าสีโดยใช้พิกัดสี CIE 1931 และค่าสีและค่าความอิ่มตัวของสี 16 บิตได้ด้วย
guard let colorControlTrait = extendedColorLightDeviceType.matterTraits.colorControlTrait else { return } // 1. Move to CIE 1931 xy chromaticity coordinates (scaled 0..65279) // Example: x = 0.3127 (20493), y = 0.3290 (21561) for D65 white point if colorControlTrait.supportsMoveToColorCommand { try await colorControlTrait.moveToColor( colorX: 20493, colorY: 21561, transitionTime: 10, // 1.0 second (in 0.1s units) optionsMask: Matter.ColorControlTrait.OptionsBitmap(), optionsOverride: Matter.ColorControlTrait.OptionsBitmap() ) } let currentX = colorControlTrait.attributes.currentX let currentY = colorControlTrait.attributes.currentY // 2. Move to 16-bit Enhanced Hue (0..65534) and 8-bit Saturation (0..254) if colorControlTrait.supportsEnhancedMoveToHueAndSaturationCommand { try await colorControlTrait.enhancedMoveToHueAndSaturation( enhancedHue: 43690, // ~240° (Blue) on the 0..65534 scale saturation: 254, // Full saturation transitionTime: 15, // 1.5 seconds (in 0.1s units) optionsMask: Matter.ColorControlTrait.OptionsBitmap(), optionsOverride: Matter.ColorControlTrait.OptionsBitmap() ) } // 3. Start a hardware color loop across the device's hue range if colorControlTrait.supportsColorLoopSetCommand { try await colorControlTrait.colorLoopSet( updateFlags: [ .updateAction, .updateDirection, .updateTime, .updateStartHue ], action: .activateFromColorLoopStartEnhancedHue, direction: .increment, time: 30, // 30 seconds per full cycle startHue: 0, optionsMask: Matter.ColorControlTrait.OptionsBitmap(), optionsOverride: Matter.ColorControlTrait.OptionsBitmap() ) }
Google.ExtendedColorControlTrait
เป็นลักษณะเฉพาะของ Google ที่ไม่บังคับใน
ExtendedColorLightDevice
ซึ่งช่วยให้ควบคุมสีได้ง่ายขึ้นโดยยอมรับชื่อสี ค่า RGB หรือค่า HSV แบบจุดลอยตัวแบบรวมที่รวมสีและความสว่างไว้ในหน่วยเดียว
guard let extendedColorControlTrait = extendedColorLightDeviceType.googleTraits.extendedColorControlTrait else { return } // 1. Set color by name if extendedColorControlTrait.supportsMoveToColorNameCommand { try await extendedColorControlTrait.moveToColorName(colorName: "Lavender") } let currentColorName = extendedColorControlTrait.attributes.currentName // 2. Set color using 8-bit RGB values (0..255 per channel) // Automatically coordinates brightness (LevelControl) and color (ColorControl) if extendedColorControlTrait.supportsMoveToColorRGBCommand { try await extendedColorControlTrait.moveToColorRGB( red: 255, green: 128, blue: 0 ) } let currentRed = extendedColorControlTrait.attributes.currentRed let currentGreen = extendedColorControlTrait.attributes.currentGreen let currentBlue = extendedColorControlTrait.attributes.currentBlue // 3. Set color and brightness together using floating-point HSV // hue: 0.0...360.0, saturation: 0.0...1.0, value (brightness): 0.0...1.0 if extendedColorControlTrait.supportsMoveToColorHSVCommand { try await extendedColorControlTrait.moveToColorHSV( hue: 210.0, saturation: 0.85, value: 0.75 ) } let currentHue = extendedColorControlTrait.attributes.currentHue let currentSaturation = extendedColorControlTrait.attributes.currentSaturation let currentValue = extendedColorControlTrait.attributes.currentValue
จัดการเส้นโค้งแสง
เส้นโค้งแสงจะกำหนดกำหนดเวลาอัตโนมัติที่เปลี่ยนความสว่างและอุณหภูมิสี (ไม่บังคับ) ของไฟเป้าหมายอย่างราบรื่นตลอดทั้งวัน ตัวอย่างเช่น เส้นโค้งของแสงสามารถค่อยๆ เพิ่มความสว่างของแสงในตอนเช้าและหรี่แสงให้เป็นโทนสีอุ่นขึ้นในตอนเย็น ประเภทอุปกรณ์แสงสว่างที่รองรับเฉพาะความสว่าง เช่น หลอดไฟแบบหรี่แสงได้ จะเป็นไปตามเส้นโค้งความสว่างและไม่สนใจอุณหภูมิสี
Google.LightingCurveTrait เป็นลักษณะเฉพาะระดับโครงสร้าง Google Home platform (GHP) คุณเข้าถึงได้จากอินสแตนซ์ Structure แทนที่จะเข้าถึงจากอุปกรณ์แสงสว่างแต่ละเครื่อง
กำหนดการแต่ละรายการในลักษณะจะแสดงด้วย LightingCurveSetting ซึ่งจับคู่ settingId ที่ไม่ซ้ำกันกับ LightingCurveConfig ตารางต่อไปนี้อธิบายฟิลด์กำหนดค่าของ LightingCurveConfig
| ช่อง | คำอธิบาย |
|---|---|
curvePointsArray |
ลำดับของรายการ CurvePoint อย่างน้อย 2 รายการที่มีเวลาต่างกัน แต่ละจุดจะระบุ time (CurveTimeOfDay ที่มี hours ตั้งแต่ 0 ถึง 23 และ minutes ตั้งแต่ 0 ถึง 59), brightnessPercent (0 ถึง 100) และ colorTemperature (ไม่บังคับ) ในหน่วยเคลวิน หากระบุ colorTemperature สำหรับจุดใดๆ ค่าดังกล่าวต้องเป็นค่าบวกสำหรับทุกจุด |
roomId |
รหัสของห้องเป้าหมาย (เช่น room@{uuid}) ใช้เส้นโค้งการจัดแสงกับไฟที่หรี่แสงได้และไฟอุณหภูมิสีที่เข้ากันได้ในห้องนั้น ระบุ roomId หรือ devices |
devices |
ออบเจ็กต์ TargetDevices ที่มีรายการ TargetDevice ซึ่งแต่ละรายการมีidอุปกรณ์และรหัสtypeอุปกรณ์ (เช่น home.matter.0000.types.0101 สำหรับ DimmableLight หรือ home.matter.0000.types.010c สำหรับ ColorTemperatureLight) ระบุ devices หรือ roomId |
daysOfWeekArray |
วันในสัปดาห์ที่เส้นโค้งแสงทำงาน (Locale.Weekday) หากรายการว่างเปล่า เส้นโค้งแสงจะทำงานทุกวัน |
adaptive |
บูลีนที่ไม่บังคับซึ่งระบุว่าเส้นโค้งของแสงเป็นแบบปรับอัตโนมัติหรือไม่ ซึ่งหมายความว่าไฟเป้าหมายจะกลับมาทำงานที่ระดับเส้นโค้งที่ใช้งานอยู่เมื่อเปิดในช่วงเวลาที่กำหนดไว้ เมื่อfalse การเปลี่ยนแสงด้วยตนเองจะยกเลิกการเชื่อมต่อแสงจากเส้นโค้งแสงในช่วงเวลาที่เหลือของวัน เมื่อtrue ไฟที่ปิดแล้วเปิดใหม่ในช่วงกรอบเวลานัดหมายที่กำหนดจะกลับมาทำงานตามเส้นโค้งของแสงอีกครั้ง โดยจะปรับให้สอดคล้องกับความสว่างและอุณหภูมิสีปัจจุบันของเส้นโค้ง และทำตามเส้นโค้งนับจากนั้นเป็นต้นไป |
enabled |
บูลีนที่ไม่บังคับซึ่งระบุว่าการตั้งค่าเส้นโค้งแสงทำงานอยู่หรือไม่ ค่าเริ่มต้นจะเป็น true เมื่อสร้างการตั้งค่าใหม่ เมื่อละเว้น enabled จากคำสั่งอัปเดต สถานะของ enabled จะไม่เปลี่ยนแปลง |
อ่านการตั้งค่าเส้นโค้งแสง
หากต้องการตรวจสอบเส้นโค้งของแสงที่กำหนดค่าไว้สำหรับบ้าน ให้ดึงข้อมูลลักษณะ Google.LightingCurveTrait จาก Structure และอ่านแอตทริบิวต์ lightingCurveSettingsArray
func readLightingCurves(structure: Structure) async {
guard let lightingCurve = await structure.traits.get(
Google.LightingCurveTrait.self
) else {
return
}
for setting in lightingCurve.attributes.lightingCurveSettingsArray {
let settingId = setting.settingId
let config = setting.config
print(
"Setting ID: \(String(describing: settingId)), " +
"enabled: \(String(describing: config?.enabled))"
)
for point in config?.curvePointsArray ?? [] {
print(
" Time: \(point.time?.hours ?? 0):\(point.time?.minutes ?? 0), " +
"Brightness: \(point.brightnessPercent ?? 0)%, " +
"ColorTemp: \(point.colorTemperature ?? 0)K"
)
}
}
}
สร้างการตั้งค่าเส้นโค้งการจัดแสง
หากต้องการสร้างกำหนดเวลาเส้นโค้งของแสงใหม่ในบ้าน ให้สร้าง LightingCurveConfig ที่มีรายการ CurvePoint อย่างน้อย 2 รายการ หลอดไฟเป้าหมาย (ระบุโดย roomId หรือ devices) และวันในสัปดาห์ที่ใช้งานอยู่ จากนั้นส่งการกำหนดค่าไปยัง createLightingCurveSetting() คำสั่งจะแสดงผล settingId ที่ไม่ซ้ำกันซึ่งกำหนดให้กับการตั้งค่าเส้นโค้งแสงใหม่
ตัวอย่างต่อไปนี้จะสร้างเส้นโค้งของแสงในวันธรรมดาสำหรับห้องที่เพิ่มความสว่างจาก 30% เวลา 07:00 น. เป็น 80% เวลา 09:00 น. และหรี่แสงลงเหลือ 20% เวลา 21:00 น.
func createRoomLightingCurve(
structure: Structure,
targetRoomId: String
) async throws -> String? {
guard let lightingCurve = await structure.traits.get(
Google.LightingCurveTrait.self
) else {
return nil
}
let curvePoints = [
Google.LightingCurveTrait.CurvePoint(
time: Google.LightingCurveTrait.CurveTimeOfDay(hours: 7, minutes: 0),
brightnessPercent: 30,
colorTemperature: 2700
),
Google.LightingCurveTrait.CurvePoint(
time: Google.LightingCurveTrait.CurveTimeOfDay(hours: 9, minutes: 0),
brightnessPercent: 80,
colorTemperature: 4000
),
Google.LightingCurveTrait.CurvePoint(
time: Google.LightingCurveTrait.CurveTimeOfDay(hours: 21, minutes: 0),
brightnessPercent: 20,
colorTemperature: 2200
),
]
let config = Google.LightingCurveTrait.LightingCurveConfig(
curvePointsArray: curvePoints,
roomId: targetRoomId,
daysOfWeekArray: [
.monday,
.tuesday,
.wednesday,
.thursday,
.friday,
],
adaptive: true,
enabled: true
)
let response = try await lightingCurve.createLightingCurveSetting(
config: config
)
return response.settingId
}
หากต้องการกำหนดเป้าหมายอุปกรณ์แสงสว่างที่เฉพาะเจาะจงแทนทั้งห้อง ให้ระบุพารามิเตอร์ devices โดยใช้ Google.LightingCurveTrait.TargetDevices ดังนี้
let targetDevices = Google.LightingCurveTrait.TargetDevices(
devicesArray: [
Google.LightingCurveTrait.TargetDevice(
id: dimmableLight.id,
type: DimmableLightDeviceType.identifier
)
]
)
let deviceCurveConfig = Google.LightingCurveTrait.LightingCurveConfig(
curvePointsArray: curvePoints,
devices: targetDevices,
daysOfWeekArray: [.saturday, .sunday],
adaptive: true,
enabled: true
)
let response = try await lightingCurve.createLightingCurveSetting(
config: deviceCurveConfig
)
let settingId = response.settingId
อัปเดตการตั้งค่าเส้นโค้งของแสง
หากต้องการแก้ไขเส้นโค้งของแสงที่มีอยู่ เช่น เปลี่ยนจุดเส้นโค้ง อัปเดตวันในสัปดาห์ที่ใช้งานอยู่ หรือเปิดใช้และปิดใช้กำหนดเวลา ให้เรียกใช้ updateLightingCurveSetting() โดยระบุเป้าหมาย settingId และ LightingCurveConfig ที่อัปเดตแล้ว
func updateLightingCurve(
structure: Structure,
settingId: String,
updatedConfig: Google.LightingCurveTrait.LightingCurveConfig
) async throws {
guard let lightingCurve = await structure.traits.get(
Google.LightingCurveTrait.self
) else {
return
}
_ = try await lightingCurve.updateLightingCurveSetting(
settingId: settingId,
config: updatedConfig
)
}
ลบการตั้งค่าเส้นโค้งแสง
หากต้องการนำการตั้งค่าเส้นโค้งแสงออกจากบ้าน ให้เรียกใช้ deleteLightingCurveSetting() พร้อมระบุ settingId ของการตั้งค่าที่ต้องการลบ
func deleteLightingCurve(
structure: Structure,
settingId: String
) async throws {
guard let lightingCurve = await structure.traits.get(
Google.LightingCurveTrait.self
) else {
return
}
_ = try await lightingCurve.deleteLightingCurveSetting(settingId: settingId)
}