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" ) ;