C# .NET Pustaka Standar untuk Mengontrol LIFX Smart LED LED di atas cloud
Proyek ini adalah perpustakaan pembungkus yang berisi fungsionalitas dari LIFX API resmi https://api.developer.lifx.com/reference/introduction
Buat instance dari kelas LifxBulbs dengan menyediakan tokenapikey dan nama label bohlam LED sebagai parameter.
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 durationTidak wajib menentukan nama label di konstruktor kelas saat menggunakan metode Sweeptoggle. Metode ini akan menjalankan operasi sakelar umum di semua perangkat yang terhubung.
using LifxLibrary ;
LifxBulbs devices = new LifxBulbs ( "tokenKey" ) ;
//synchronous
devices . SweepToggle ( ) ;
//async
await devices . SweepToggleAsync ( ) ; Metode PutPower memungkinkan Anda untuk mengubah status umbi, mengaturnya untuk on atau off , dan memberikan parameter waktu durasi opsional mulai dari 0 hingga 100 detik.
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" ) ; Metode PutBrightness mengambil parameter integer mulai dari 0 hingga 100 untuk mengatur intensitas kecerahan.
using LifxLibrary ;
LifxBulbs bedroom = new LifxBulbs ( "tokenKey" , "Bedroom" ) ;
//synchronous
bedroom . PutBrightness ( 50 ) ; //set the brightness at 50%
//async
await bedroom . PutBrightnessAsync ( 50 ) ; Metode PutColor dapat menerima serangkaian nilai string sebagai parameter untuk menentukan warna, kecerahan, saturasi, dan atribut lainnya.
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% brightnessUntuk panduan yang lebih rinci tentang menentukan warna, silakan kunjungi dokumentasi API LIFX resmi di https://api.developer.lifx.com/reference/colors.
Metode multiuse menerima serangkaian parameter opsional untuk melakukan beragam tindakan pada lampu LED, seperti mengubah color , brightness , power , duration time , dan mengaktifkan fast mode .
Mode cepat menjalankan kueri dengan cepat, tanpa cek keadaan awal dan tidak menunggu hasilnya.
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" ) ; Kelas LightSearcher adalah kelas statis yang berisi metode async statis yang membantu menemukan lampu LED dan mengambil sifatnya seperti LED bulb name , power status , connection status , brightness level , saturation level dan banyak lagi.
Metode ShowConnectedDevicesAsync mengembalikan daftar objek String dengan nama label semua perangkat yang terhubung.
using LifxLibrary ;
//set the tokenkey
LightSearcher . SetTokenKey ( "tokenkey" ) ;
var devices = await LightSearcher . ShowConnectedDevicesAsync ( ) ;
foreach ( var device in devices )
{
Console . WriteLine ( device ) ;
} Metode GetNamesAsync mengembalikan daftar objek string dengan nama label semua perangkat yang ditautkan ke akun Anda, terlepas dari status koneksi atau status daya mereka.
using LifxLibrary ;
//set the tokenkey
LightSearcher . SetTokenKey ( "tokenkey" ) ;
var devices = await LightSearcher . GetNamesAsync ( ) ;
foreach ( var device in devices )
{
Console . WriteLine ( device ) ;
} Metode ShowStateAsync mengembalikan objek BulbState dengan power status , connection status , LED label name , brightness level , saturation level , UUID , ID , dan level HUE .
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 Kelas LightGroup berisi metode untuk mengontrol satu set lampu LED yang termasuk dalam kelompok tertentu.
Metode async dan sinkron tersedia di kelas 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" ) ;