C#.NET標準ライブラリクラウド上のLIFXスマート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 durationSweeptoggleメソッドを使用する場合、クラスコンストラクターにラベル名を指定することは義務的ではありません。これらのメソッドは、すべての接続されたデバイスで一般的なトグル操作を実行します。
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の公式Lifx APIドキュメントをご覧ください。
多目的メソッドは、 color 、 brightness 、 power 、 duration time 、およびfast modeアクティブ化など、LED電球で多様なアクションを実行するための一連のオプションパラメーターを受け入れます。
FASTモードは、最初の状態チェックなしでクエリを高速に実行し、結果がないのを待ちます。
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" ) ;