C#.NET标准库控制LIFX Smart LED灯泡在云上
该项目是一个包装库,包含官方LIFX API https://api.developer.lifx.com/reference/introduction的功能
通过提供Tokenapikey和LED灯泡的标签名称作为参数,从LifxBulbs类创建一个实例。
using LifxLibrary ;
LifxBulbs bedroom = new LifxBulbs ( "tokenKey" , "Bedroom" ) ;
//synchronous
bedroom . LightToggle ( 3 ) ; //toggle with 3 seconds duration
//async
await bedroom . LightToggleAsync ( 3 ) ; //async toggle with 3 seconds duration使用Sweeptoggle方法时,不强迫在类构造函数中指定标签名称。这些方法将在所有连接的设备上执行一般的切换操作。
using LifxLibrary ;
LifxBulbs devices = new LifxBulbs ( "tokenKey" ) ;
//synchronous
devices . SweepToggle ( ) ;
//async
await devices . SweepToggleAsync ( ) ;PutPower方法使您可以更改灯泡的状态,将它们设置为on或off ,并提供从0到100秒的可选持续时间参数。
using LifxLibrary ;
LifxBulbs bedroom = new LifxBulbs ( "tokenKey" , "Bedroom" ) ;
//synchronous
bedroom . PutPower ( "on" ) ;
bedroom . PutPower ( "off" ) ;
bedroom . PutPower ( "on" , 5 ) ; //with 5 seconds duration time
//async
await bedroom . PutPowerAsync ( "off" ) ;PutBrightness方法采用一个范围从0到100的整数参数,以设置亮度的强度。
using LifxLibrary ;
LifxBulbs bedroom = new LifxBulbs ( "tokenKey" , "Bedroom" ) ;
//synchronous
bedroom . PutBrightness ( 50 ) ; //set the brightness at 50%
//async
await bedroom . PutBrightnessAsync ( 50 ) ;PutColor方法可以接收一系列的字符串值作为参数,以定义颜色,亮度,饱和度和其他属性。
using LifxLibrary ;
LifxBulbs bedroom = new LifxBulbs ( "tokenKey" , "Bedroom" ) ;
//hexadecimal
bedroom . PutColor ( "#0000FF" ) ; //blue color
//RGB
bedroom . PutColor ( "rgb:255,0,0" ) ; //red color
//plain text
bedroom . PutColor ( "white" ) ; //white color
//async
await bedroom . PutColorAsync ( "hue:120 saturation:1.0 brightness:0.5" ) ; //Deep green 50% brightness有关定义颜色的更详细的指南,请访问https://api.developer.lifx.com/reference/colors/colors的官方LIFX API文档。
多用途方法接受一组可选参数,以在LED灯泡上执行各种操作,例如更改color , brightness , power , duration time和激活fast mode 。
快速模式可以快速执行查询,而无需初始状态检查,并且等待没有结果。
using LifxLibrary ;
LifxBulbs bedroom = new LifxBulbs ( "tokenKey" , "Bedroom" ) ;
//synchronous
bedroom . MultiUse ( "on" , "blue" , 50 , 6 , true ) ; //power on, color blue, 50% brightness, 6 seconds duration with fast mode activated
//other ways to use it
bedroom . MultiUse ( power : "on" , color : "blue" , brightness : 100 , duration : 6 , fast : true ) ;
//duration time is 0 by default and fast mode is false by default
bedroom . MultiUse ( power : "on" , color : "white" , brightness : 80 ) ;
//async
await bedroom . MultiUseAsync ( "off" ) ;
await bedroom . MultiUseAsync ( "on" , "orange" ) ; LightSearcher类是一个静态类,包含静态异步方法,有助于发现LED灯泡并检索其属性,例如LED bulb name , power status , connection status , brightness level , saturation level 。
ShowConnectedDevicesAsync方法返回一个带有所有连接设备的标签名称的字符串对象列表。
using LifxLibrary ;
//set the tokenkey
LightSearcher . SetTokenKey ( "tokenkey" ) ;
var devices = await LightSearcher . ShowConnectedDevicesAsync ( ) ;
foreach ( var device in devices )
{
Console . WriteLine ( device ) ;
} GetNamesAsync方法返回一个字符串对象列表,其中所有设备的标签名称链接到您的帐户,无论其连接状态或电源状态如何。
using LifxLibrary ;
//set the tokenkey
LightSearcher . SetTokenKey ( "tokenkey" ) ;
var devices = await LightSearcher . GetNamesAsync ( ) ;
foreach ( var device in devices )
{
Console . WriteLine ( device ) ;
} ShowStateAsync方法返回具有power status , connection status , LED label name , brightness level , saturation level , UUID , ID和HUE级别的BulbState对象。
using LifxLibrary ;
//set the tokenkey
LightSearcher . SetTokenKey ( "tokenkey" ) ;
BulbState bedroom = await LightSearcher . ShowStateAsync ( "Bedroom" ) ;
Console . WriteLine ( bedroom . Power ) ; //the response would be off or on
Console . WriteLine ( bedroom . Connected ) ; //the response would be true or false
Console . WriteLine ( bedroom . Brightness ) ; //brightness level
Console . WriteLine ( bedroom . Saturation ) ; //saturation level
Console . WriteLine ( bedroom . Id ) ; //bulb ID LightGroup类包含控制一组属于特定组的LED灯泡的方法。
LightGroup类中可用的异步和同步方法。
Toggle methods Brightness methods Color methods MultiUse methods
using LifxLibrary ;
LightGroup bulbs = new LightGroup ( "tokenKey" , "Kitchen" ) ; //tokenKey and group name
bulbs . SweepToggle ( ) ;
await bulbs . PutColorAsync ( "blue" ) ;