อุปกรณ์แสงสว่างใช้ลักษณะ Matter มาตรฐานเพื่อระบุหลอดไฟ สลับสถานะการเปิด/ปิด และปรับระดับความสว่าง
ประเภทอุปกรณ์หลอดไฟพื้นฐานที่สุดคือ
OnOffLight
ซึ่งใช้กับลักษณะ
OnOff และ
Identify
และอาจรองรับลักษณะ
LevelControl
และลักษณะ
LightEffects
ไฟประเภทอื่นๆ มีความสามารถเพิ่มเติมและใช้งานด้วยลักษณะที่แตกต่างกัน ดังนี้
| ประเภทอุปกรณ์ของ Home API | ลักษณะเฉพาะ | แอปตัวอย่าง Kotlin | กรณีการใช้งาน |
|---|---|---|---|
|
ไฟอุณหภูมิสี
อุปกรณ์ให้แสงสว่างที่สามารถเปิดหรือปิด ปรับความเข้มของแสง และปรับอุณหภูมิสีได้ |
ลักษณะที่จำเป็น matter Identify matter OnOff matter LevelControl matter ColorControl |
เปิดและปิด ความสว่าง |
สว่าง |
|
หลอดไฟที่หรี่แสงได้
อุปกรณ์ให้แสงสว่างที่เปิดหรือปิดได้ ปรับความเข้มของแสงได้ และเปลี่ยนได้โดยใช้เซ็นเซอร์ตรวจจับการมีคนอยู่ซึ่งเชื่อมโยงอยู่ |
ลักษณะที่จำเป็น matter Identify matter OnOff matter LevelControl |
เปิดและปิด ความสว่าง |
สว่าง |
|
ไฟสีแบบขยาย
อุปกรณ์ให้แสงสว่างที่สามารถเปิดหรือปิด ปรับความเข้มของแสง ปรับอุณหภูมิสี และเปลี่ยนได้โดยใช้เซ็นเซอร์ตรวจจับการมีคนอยู่ซึ่งเชื่อมโยงอยู่ |
ลักษณะที่จำเป็น matter Identify matter OnOff matter LevelControl matter ColorControl |
เปิดและปิด ความสว่าง |
สว่าง |
|
เปิด/ปิดไฟ
อุปกรณ์ให้แสงสว่างที่เปิดหรือปิดได้ |
ลักษณะที่จำเป็น matter Identify matter OnOff |
เปิดและปิด ความสว่าง |
สว่าง |
ตรวจสอบการรองรับแอตทริบิวต์และคำสั่งสำหรับอุปกรณ์ก่อนใช้ฟีเจอร์หรือพยายามอัปเดตแอตทริบิวต์เสมอ ดูข้อมูลเพิ่มเติมได้ที่ควบคุมอุปกรณ์ในAndroid
ข้อกำหนดเบื้องต้น
ก่อนที่จะทำงานกับอุปกรณ์แสงสว่างและเส้นโค้งแสงสว่าง โปรดตรวจสอบว่าแอปของคุณเริ่มต้น Home SDK และรวมการนำเข้าที่จำเป็นไว้ด้วย
import com.google.home.HomeDevice import com.google.home.Structure import com.google.home.google.DayOfWeek import com.google.home.google.ExtendedColorControl import com.google.home.google.ExtendedColorControlTrait import com.google.home.google.LightingCurve import com.google.home.google.LightingCurveTrait import com.google.home.matter.standard.ColorControl import com.google.home.matter.standard.ColorControlTrait import com.google.home.matter.standard.ColorTemperatureLightDevice import com.google.home.matter.standard.DimmableLightDevice import com.google.home.matter.standard.ExtendedColorLightDevice import com.google.home.matter.standard.GeneralDiagnostics import com.google.home.matter.standard.Identify import com.google.home.matter.standard.IdentifyTrait import com.google.home.matter.standard.LevelControl import com.google.home.matter.standard.LevelControlTrait import com.google.home.matter.standard.OnOff import com.google.home.matter.standard.OnOffLightDevice import com.google.home.matter.standard.OnOffTrait import com.google.home.matter.standard.RootNodeDevice import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.firstOrNull
ค้นหาอุปกรณ์แสงสว่าง
หากต้องการค้นหาอุปกรณ์แสงสว่างในบ้าน ให้กรองรายการอุปกรณ์ตามประเภทอุปกรณ์ เช่น OnOffLightDevice และ DimmableLightDevice
// Get all On/Off Light devices in the structure val onOffLights: List<HomeDevice> = home.devices().list().filter { it.has(OnOffLightDevice) } // Get all Dimmable Light devices in the structure val dimmableLights: List<HomeDevice> = home.devices().list().filter { it.has(DimmableLightDevice) } // Retrieve a specific device type instance to access its traits val onOffLightDevice: OnOffLightDevice? = onOffLights.firstOrNull()?.type(OnOffLightDevice)?.firstOrNull() val dimmableLightDevice: DimmableLightDevice? = dimmableLights.firstOrNull()?.type(DimmableLightDevice)?.firstOrNull()
นอกจากนี้ คุณยังดึงข้อมูลอุปกรณ์แสงสว่างทั้งหมดในอุปกรณ์แสงสว่างหลายประเภทได้ในตัวกรองเดียว ดังนี้
// Get a list of devices with similar device types (lights) val lightDevices = home.devices().map { devices -> devices.filter { it.has(DimmableLightDevice) || it.has(OnOffLightDevice) || it.has(ColorTemperatureLightDevice) || it.has(ExtendedColorLightDevice) } }
ดูข้อมูลพื้นฐานเกี่ยวกับอุปกรณ์
ติดตั้งใช้งานในแอปตัวอย่างสำหรับ Android
ลักษณะ BasicInformation ประกอบด้วยข้อมูลต่างๆ เช่น ชื่อผู้ให้บริการ รหัสผู้ให้บริการ รหัสสินค้า ชื่อสินค้า (รวมถึงข้อมูลรุ่น) และเวอร์ชันของซอฟต์แวร์สำหรับอุปกรณ์ ดังนี้
// Get device basic information. All general information traits are on the RootNodeDevice type. device.type(RootNodeDevice).first().standardTraits.basicInformation?.let { basicInformation -> println("vendorName ${basicInformation.vendorName}") println("vendorId ${basicInformation.vendorId}") println("productId ${basicInformation.productId}") println("productName ${basicInformation.productName}") println("softwareVersion ${basicInformation.softwareVersion}") }
ระบุอุปกรณ์แสงสว่าง
เมื่อห้องมีหลอดไฟหลายดวง คุณสามารถช่วยผู้ใช้ค้นหาหลอดไฟที่ต้องการได้โดยการทริกเกอร์สถานะการระบุด้วยภาพ เช่น การกะพริบไฟเป็นเวลา 2-3 วินาที อุปกรณ์แสงสว่างทั้ง 4 ประเภทต้องมีลักษณะ Identify
เรียกใช้คำสั่ง identify โดยมีระยะเวลา identifyTime เป็นวินาทีเพื่อเริ่มสถานะการระบุตัวตน การส่ง 0 จะหยุดสถานะการระบุทันที นอกจากนี้ คุณยังเรียกใช้ triggerEffect เพื่อเรียกใช้เอฟเฟกต์ภาพที่เฉพาะเจาะจงได้ เช่น Blink หรือ Breathe
suspend fun identifyLight(lightDevice: DimmableLightDevice) {
val identifyTrait = lightDevice.standardTraits.identify ?: return
// Blink the light for 10 seconds so the user can locate it
if (identifyTrait.supports(Identify.Command.Identify)) {
identifyTrait.identify(identifyTime = 10u)
}
// Or trigger a specific visual identification effect
if (identifyTrait.supports(Identify.Command.TriggerEffect)) {
identifyTrait.triggerEffect(
effectIdentifier = IdentifyTrait.EffectIdentifierEnum.Blink,
effectVariant = IdentifyTrait.EffectVariantEnum.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 มาตรฐานเพื่อระบุหลอดไฟ สลับสถานะพลังงาน และปรับระดับความสว่าง
เปิดหรือปิดไฟ
ใช้ลักษณะ
OnOff
เพื่ออ่านสถานะพลังงานปัจจุบันของ OnOffLight หรือ DimmableLight และเพื่อเปิดหรือปิดไฟ
ก่อนอ่านแอตทริบิวต์ onOff ให้ตรวจสอบว่าอุปกรณ์รองรับแอตทริบิวต์ดังกล่าว อุปกรณ์บางอย่างทำงานเป็นปลายทางแบบคำสั่งเท่านั้นและไม่รายงานสถานะ
// Check if the OnOff trait supports the onOff attribute. if (onOffTrait.supports(OnOff.Attribute.onOff)) { println("onOffTrait supports onOff state") } else { println("onOffTrait is for a command only device!") }
เมื่อonOffเป็นtrue ไฟจะติดสว่าง เมื่อ onOff เป็น false ไฟจะปิด
หากต้องการเปลี่ยนสถานะพลังงานของไฟ ให้ยืนยันว่าลักษณะรองรับคำสั่งที่ต้องการ แล้วเรียกใช้ on(), off() หรือ toggle()
// Check if the OnOff trait supports the toggle command. if (onOffTrait.supports(OnOff.Command.Toggle)) { println("onOffTrait supports toggle command") } else { println("onOffTrait does not support stateful toggle command") }
// Calling a command on a trait. try { onOffTrait.toggle() } catch (e: HomeException) { // Code for handling the exception }
นอกจากนี้ คุณยังเรียกใช้ on() และ off() ได้โดยตรงเพื่อตั้งค่าสถานะที่ชัดเจน ดังนี้
onOffTrait.off() onOffTrait.on()
หากต้องการปิดไฟโดยใช้การเปลี่ยนภาพแบบค่อยเป็นค่อยไป ให้เรียกใช้ offWithEffect() และระบุค่า EffectIdentifierEnum ดังนี้
// Turn off the light using the DyingLight effect. onOffTrait.offWithEffect( effectIdentifier = OnOffTrait.EffectIdentifierEnum.DyingLight, effectVariant = 0u, )
ปรับระดับความสว่าง
DimmableLight ต้องใช้ลักษณะ LevelControl และ OnOffLight สามารถรองรับลักษณะดังกล่าวได้ (ไม่บังคับ) ใช้ลักษณะนี้เพื่ออ่านและควบคุมระดับความสว่างของแสงไฟ
แอตทริบิวต์ currentLevel จะรายงานระดับความสว่างปัจจุบันในระดับตั้งแต่ 0 ถึง 254 นอกจากนี้ คุณยังตรวจสอบ minLevel และ maxLevel เพื่อกำหนดช่วงความสว่างที่ถูกต้องซึ่งฮาร์ดแวร์รองรับได้ด้วย
หากต้องการตั้งค่าความสว่างเป็นค่าที่เฉพาะเจาะจง ให้เรียกใช้ moveToLevel() โปรดทราบว่า
moveToLevel() จะเปลี่ยนเฉพาะระดับปัจจุบันเมื่อไฟเปิดอยู่เท่านั้น
เว้นแต่คุณจะกำหนดค่า optionsOverride ด้วย executeIfOff หากต้องการเปิดไฟโดยอัตโนมัติเมื่อตั้งค่าระดับความสว่างที่ไม่ใช่ศูนย์ (หรือปิดไฟเมื่อตั้งค่าระดับเป็นต่ำสุด) ให้ใช้ moveToLevelWithOnOff()
// Change the brightness of the light to 50% levelControlTrait.moveToLevel( level = 127u.toUByte(), transitionTime = null, optionsMask = LevelControlTrait.OptionsBitmap(), optionsOverride = LevelControlTrait.OptionsBitmap(), )
ตัวอย่างต่อไปนี้จะอ่านระดับความสว่างปัจจุบันและใช้ moveToLevelWithOnOff() เพื่อตั้งค่าไฟที่หรี่แสงได้ให้มีความสว่าง 75% (190u) โดยใช้เวลาเปลี่ยนผ่าน 1 วินาที (10u ในหน่วย 1 ใน 10 ของวินาที)
suspend fun setLightBrightness(lightDevice: DimmableLightDevice) { val levelControl = lightDevice.standardTraits.levelControl ?: return // Read the current brightness level (0 to 254) val currentLevel: UByte? = levelControl.currentLevel println("Current brightness level: $currentLevel") // Move to ~75% brightness (190 out of 254) and turn on the light if it is off if (levelControl.supports(LevelControl.Command.MoveToLevelWithOnOff)) { levelControl.moveToLevelWithOnOff( level = 190u, transitionTime = 10u, optionsMask = LevelControlTrait.OptionsBitmap(), optionsOverride = LevelControlTrait.OptionsBitmap() ) } }
นอกจากนี้ คุณยังเพิ่มหรือลดความสว่างโดยอิงตามระดับปัจจุบันได้ด้วยการเรียกใช้ stepWithOnOff() พร้อม StepModeEnum.Up หรือ StepModeEnum.Down และ stepSize
สีของตัวควบคุม
ลักษณะColorControl พร้อมใช้งานทั้งใน ColorTemperatureLightDevice และ ExtendedColorLightDevice
ลักษณะนี้ช่วยให้คุณปรับอุณหภูมิสีในหน่วยไมโครเรซิโพรคัลดีกรีหรือ Mired ได้
val colorControlTrait = colorTempLightDevice.standardTraits.colorControl ?: 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) val currentMireds = colorControlTrait.colorTemperatureMireds val minMireds = colorControlTrait.colorTempPhysicalMinMireds ?: 153u // ~6500K (coolest) val maxMireds = colorControlTrait.colorTempPhysicalMaxMireds ?: 500u // 2000K (warmest) // Clamp target (370 mireds ≈ 2700K) to the device's physical bounds val targetMireds = 370u.toUShort().coerceIn(minMireds, maxMireds) if ( colorControlTrait.supports( ColorControl.Command.MoveToColorTemperature ) ) { colorControlTrait.moveToColorTemperature( colorTemperatureMireds = targetMireds, transitionTime = 20u, // 2.0 seconds (in 0.1s units) optionsMask = ColorControlTrait.OptionsBitmap(), optionsOverride = ColorControlTrait.OptionsBitmap() ) }
นอกจากนี้ คุณยังตั้งค่าสีโดยใช้พิกัดสี CIE 1931 และค่าสีและค่าความอิ่มตัวของสี 16 บิตได้ด้วย
val colorControlTrait = extendedColorLightDevice.standardTraits.colorControl ?: 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.supports(ColorControl.Command.MoveToColor)) { colorControlTrait.moveToColor( colorX = 20493u, colorY = 21561u, transitionTime = 10u, // 1.0 second (in 0.1s units) optionsMask = ColorControlTrait.OptionsBitmap(), optionsOverride = ColorControlTrait.OptionsBitmap() ) } val currentX = colorControlTrait.currentX val currentY = colorControlTrait.currentY // 2. Move to 16-bit Enhanced Hue (0..65534) and 8-bit Saturation (0..254) if ( colorControlTrait.supports( ColorControl.Command.EnhancedMoveToHueAndSaturation ) ) { colorControlTrait.enhancedMoveToHueAndSaturation( enhancedHue = 43690u, // ~240° (Blue) on the 0..65534 scale saturation = 254u, // Full saturation transitionTime = 15u, // 1.5 seconds (in 0.1s units) optionsMask = ColorControlTrait.OptionsBitmap(), optionsOverride = ColorControlTrait.OptionsBitmap() ) } // 3. Start a hardware color loop across the device's hue range if (colorControlTrait.supports(ColorControl.Command.ColorLoopSet)) { colorControlTrait.colorLoopSet( updateFlags = ColorControlTrait.UpdateFlagsBitmap( updateAction = true, updateDirection = true, updateTime = true, updateStartHue = true ), action = ColorControlTrait.ColorLoopActionEnum .ActivateFromColorLoopStartEnhancedHue, direction = ColorControlTrait.ColorLoopDirectionEnum.Increment, time = 30u, // 30 seconds per full cycle startHue = 0u, optionsMask = ColorControlTrait.OptionsBitmap(), optionsOverride = ColorControlTrait.OptionsBitmap() ) }
ExtendedColorControl
เป็นลักษณะเฉพาะของ Google ที่ไม่บังคับใน
ExtendedColorLightDevice
ซึ่งช่วยให้ควบคุมสีได้ง่ายขึ้นโดยยอมรับชื่อสี ค่า RGB หรือค่า HSV แบบจุดลอยตัวแบบรวมที่รวมสีและความสว่างไว้ในหน่วยเดียว
val extendedColorControlTrait = extendedColorLightDevice.googleTraits.extendedColorControl ?: return // 1. Set color by name if ( extendedColorControlTrait.supports( ExtendedColorControl.Command.MoveToColorName ) ) { extendedColorControlTrait.moveToColorName(colorName = "Lavender") } val currentColorName = extendedColorControlTrait.currentName // 2. Set color using 8-bit RGB values (0..255 per channel) // Automatically coordinates brightness (LevelControl) and color (ColorControl) if ( extendedColorControlTrait.supports( ExtendedColorControl.Command.MoveToColorRgb ) ) { extendedColorControlTrait.moveToColorRgb( red = 255u, green = 128u, blue = 0u ) } val currentRed = extendedColorControlTrait.currentRed val currentGreen = extendedColorControlTrait.currentGreen val currentBlue = extendedColorControlTrait.currentBlue // 3. Set color and brightness together using floating-point HSV // hue: 0.0f..360.0f, saturation: 0.0f..1.0f, value (brightness): 0.0f..1.0f if ( extendedColorControlTrait.supports( ExtendedColorControl.Command.MoveToColorHsv ) ) { extendedColorControlTrait.moveToColorHsv( hue = 210.0f, saturation = 0.85f, value = 0.75f ) } val currentHue = extendedColorControlTrait.currentHue val currentSaturation = extendedColorControlTrait.currentSaturation val currentValue = extendedColorControlTrait.currentValue
จัดการเส้นโค้งแสง
เส้นโค้งแสงจะกำหนดกำหนดเวลาอัตโนมัติที่เปลี่ยนความสว่างและอุณหภูมิสี (ไม่บังคับ) ของไฟเป้าหมายอย่างราบรื่นตลอดทั้งวัน ตัวอย่างเช่น เส้นโค้งของแสงสามารถค่อยๆ เพิ่มความสว่างของแสงในตอนเช้าและหรี่แสงให้เป็นโทนสีอุ่นขึ้นในตอนเย็น ประเภทอุปกรณ์แสงสว่างที่รองรับเฉพาะความสว่าง เช่น หลอดไฟแบบหรี่แสงได้ จะเป็นไปตามเส้นโค้งความสว่างและไม่สนใจอุณหภูมิสี
LightingCurve เป็นลักษณะเฉพาะระดับบ้าน Google Home platform คุณเข้าถึงได้จากอินสแตนซ์ Structure แทนที่จะเข้าถึงจากอุปกรณ์แสงสว่างแต่ละเครื่อง
กำหนดการแต่ละรายการในลักษณะจะแสดงด้วย LightingCurveSetting ซึ่งจับคู่ settingId ที่ไม่ซ้ำกันกับ LightingCurveConfig ตารางต่อไปนี้อธิบายฟิลด์กำหนดค่าของ LightingCurveConfig
| ช่อง | คำอธิบาย |
|---|---|
curvePointsList |
ลำดับของรายการ 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 |
daysOfWeekList |
วันในสัปดาห์ที่เส้นโค้งแสงทำงาน (DayOfWeek) หากรายการว่างเปล่า เส้นโค้งแสงจะทำงานทุกวัน |
adaptive |
บูลีนที่ไม่บังคับซึ่งระบุว่าเส้นโค้งของแสงเป็นแบบปรับอัตโนมัติหรือไม่ ซึ่งหมายความว่าไฟเป้าหมายจะกลับมาทำงานที่ระดับเส้นโค้งที่ใช้งานอยู่เมื่อเปิดในช่วงเวลาที่กำหนดไว้ เมื่อfalse การเปลี่ยนแสงด้วยตนเองจะยกเลิกการเชื่อมต่อแสงจากเส้นโค้งแสงในช่วงเวลาที่เหลือของวัน เมื่อtrue ไฟที่ปิดแล้วเปิดใหม่ในช่วงกรอบเวลานัดหมายที่กำหนดจะกลับมาทำงานตามเส้นโค้งของแสงอีกครั้ง โดยจะปรับให้สอดคล้องกับความสว่างและอุณหภูมิสีปัจจุบันของเส้นโค้ง และทำตามเส้นโค้งนับจากนั้นเป็นต้นไป |
enabled |
บูลีนที่ไม่บังคับซึ่งระบุว่าการตั้งค่าเส้นโค้งแสงทำงานอยู่หรือไม่ ค่าเริ่มต้นจะเป็น true เมื่อสร้างการตั้งค่าใหม่ เมื่อละเว้น enabled จากคำสั่งอัปเดต สถานะของ enabled จะไม่เปลี่ยนแปลง |
อ่านการตั้งค่าเส้นโค้งแสง
หากต้องการตรวจสอบเส้นโค้งของแสงที่กำหนดค่าไว้สำหรับบ้าน ให้ดึงข้อมูลลักษณะ
LightingCurve
จาก Structure และอ่านแอตทริบิวต์
lightingCurveSettingsList
suspend fun readLightingCurves(structure: Structure) {
val lightingCurve = structure.trait(LightingCurve).firstOrNull() ?: return
for (setting in lightingCurve.lightingCurveSettingsList) {
val settingId = setting.settingId
val config = setting.config
println("Setting ID: $settingId, enabled: ${config?.enabled}")
config?.curvePointsList?.forEach { point ->
println(
" Time: ${point.time?.hours}:${point.time?.minutes}, " +
"Brightness: ${point.brightnessPercent}%, " +
"ColorTemp: ${point.colorTemperature}K"
)
}
}
}
สร้างการตั้งค่าเส้นโค้งการจัดแสง
หากต้องการสร้างกำหนดเวลาเส้นโค้งของแสงใหม่ในบ้าน ให้สร้าง LightingCurveConfig ที่มีรายการ CurvePoint อย่างน้อย 2 รายการ หลอดไฟเป้าหมาย (ระบุโดย roomId หรือ devices) และวันในสัปดาห์ที่ใช้งานอยู่ จากนั้นส่งการกำหนดค่าไปยัง createLightingCurveSetting() คำสั่งจะแสดงผล settingId ที่ไม่ซ้ำกันซึ่งกำหนดให้กับการตั้งค่าเส้นโค้งแสงใหม่
ตัวอย่างต่อไปนี้จะสร้างเส้นโค้งของแสงในวันธรรมดาสำหรับห้องหนึ่ง โดยจะเพิ่มจากความสว่าง 30% ที่อุณหภูมิสีแบบอบอุ่น 2700K ในเวลา 07:00 น. เป็น 80% ที่อุณหภูมิสีแบบเย็น 4000K ในเวลา 09:00 น. จากนั้นจะหรี่ลงเหลือ 20% ที่อุณหภูมิสีแบบอบอุ่นมาก 2200K ภายในเวลา 21:00 น.
suspend fun createRoomLightingCurve(
structure: Structure,
targetRoomId: String
): String? {
val lightingCurve = structure.trait(LightingCurve).firstOrNull() ?: return null
val curvePoints = listOf(
LightingCurveTrait.CurvePoint(
time = LightingCurveTrait.CurveTimeOfDay(hours = 7, minutes = 0),
brightnessPercent = 30,
colorTemperature = 2700
),
LightingCurveTrait.CurvePoint(
time = LightingCurveTrait.CurveTimeOfDay(hours = 9, minutes = 0),
brightnessPercent = 80,
colorTemperature = 4000
),
LightingCurveTrait.CurvePoint(
time = LightingCurveTrait.CurveTimeOfDay(hours = 21, minutes = 0),
brightnessPercent = 20,
colorTemperature = 2200
)
)
val config = LightingCurveTrait.LightingCurveConfig(
curvePointsList = curvePoints,
roomId = targetRoomId,
daysOfWeekList = listOf(
DayOfWeek.MONDAY,
DayOfWeek.TUESDAY,
DayOfWeek.WEDNESDAY,
DayOfWeek.THURSDAY,
DayOfWeek.FRIDAY
),
adaptive = true,
enabled = true
)
// Returns the newly created settingId
return lightingCurve.createLightingCurveSetting(config = config)
}
หากต้องการกำหนดเป้าหมายอุปกรณ์แสงสว่างที่เฉพาะเจาะจงแทนทั้งห้อง ให้ระบุพารามิเตอร์ devices โดยใช้ LightingCurveTrait.TargetDevices ดังนี้
val targetDevices = LightingCurveTrait.TargetDevices(
devicesList = listOf(
LightingCurveTrait.TargetDevice(
id = dimmableLight.id.id,
type = DimmableLightDevice.TYPE_ID
)
)
)
val deviceCurveConfig = LightingCurveTrait.LightingCurveConfig(
curvePointsList = curvePoints,
devices = targetDevices,
daysOfWeekList = listOf(DayOfWeek.SATURDAY, DayOfWeek.SUNDAY),
adaptive = true,
enabled = true
)
val settingId = lightingCurve.createLightingCurveSetting(
config = deviceCurveConfig
)
อัปเดตการตั้งค่าเส้นโค้งของแสง
หากต้องการแก้ไขเส้นโค้งของแสงที่มีอยู่ เช่น เปลี่ยนจุดเส้นโค้ง อัปเดตวันในสัปดาห์ที่ใช้งานอยู่ หรือเปิดใช้และปิดใช้กำหนดเวลา ให้เรียกใช้ updateLightingCurveSetting() โดยระบุเป้าหมาย settingId และ LightingCurveConfig ที่อัปเดตแล้ว
suspend fun updateLightingCurve(
structure: Structure,
settingId: String,
updatedConfig: LightingCurveTrait.LightingCurveConfig
) {
val lightingCurve = structure.trait(LightingCurve).firstOrNull() ?: return
lightingCurve.updateLightingCurveSetting(
settingId = settingId,
config = updatedConfig
)
}
ลบการตั้งค่าเส้นโค้งแสง
หากต้องการนำการตั้งค่าเส้นโค้งแสงออกจากบ้าน ให้เรียกใช้ deleteLightingCurveSetting() พร้อมระบุ settingId ของการตั้งค่าที่ต้องการลบ
suspend fun deleteLightingCurve(
structure: Structure,
settingId: String
) {
val lightingCurve = structure.trait(LightingCurve).firstOrNull() ?: return
lightingCurve.deleteLightingCurveSetting(settingId = settingId)
}