#async-api #synchronous #light #effect #offline #communicating #lifx-api

lifx-rs

A synchronous + asynchronous library for communicating with the official LIFX-API and the unoffical offline API

8 releases

0.1.30 May 10, 2023
0.1.29 Oct 24, 2022
0.1.28 Jun 25, 2022
0.1.22 Feb 3, 2022

#862 in Web programming

Download history 7/week @ 2024-09-19 5/week @ 2024-09-26 1/week @ 2024-10-10 1/week @ 2024-10-17

68 downloads per month
Used in lifx-api-server

MIT/Apache

230KB
3K SLoC

lifx-rs

Description

A synchronous + asynchronous library for communicating with the official LIFX-API and the unoffical offline API.

LIFX-API Supported Methods:

  • List Lights
  • Set State
  • Set States
  • State Delta
  • Toggle Power
  • Breathe Effect
  • Move Effect
  • Morph Effect
  • Flame Effect
  • Pulse Effect
  • Effects Off
  • Clean (HEV)
  • List Scenes
  • Validate Color

Un-Official Offline API Supported Methods:

  • List Lights
  • Set State
  • Set States

To use offline use the Un-Official API Server:

lifx-api-server

How to use library

Add the following line to your cargo.toml:

lifx-rs = "0.1.30"

Example:

extern crate lifx_rs as lifx;

fn main() {

    let key = "xxx".to_string();
    
    let mut api_endpoints: Vec<String> = Vec::new();
    
    // Official API
    api_endpoints.push(format!("https://api.lifx.com"));

    // lifx-server-api (Un-Official)
    api_endpoints.push(format!("http://localhost:8089"));

    let config = lifx::LifxConfig{
        access_token: key.clone(),
        api_endpoints: api_endpoints
    };

    // Build an "OffState" to set
    let mut off_state = lifx::State::new();
    off_state.power = Some(format!("off"));

    // Turn off all lights
    lifx::Light::set_state_by_selector(config.clone(), format!("all"), off_state);


    let all_lights = lifx::Light::list_all(config.clone());
    match all_lights {
        Ok(lights) => {
            println!("{:?}",lights.clone());

            let mut state = lifx::State::new();
            state.power = Some(format!("on"));
            state.brightness = Some(1.0);
        
            for light in lights {
                let results = light.set_state(config.clone(), state.clone());
                println!("{:?}",results);
            }
        },
        Err(e) => println!("{}",e)
    }

}

Async Example:

extern crate lifx_rs as lifx;

#[tokio::main]
async fn main() {

    let key = "xxx".to_string();
    
    let mut api_endpoints: Vec<String> = Vec::new();
    
    // Official API
    api_endpoints.push(format!("https://api.lifx.com"));

    // lifx-server-api (Un-Official)
    api_endpoints.push(format!("http://localhost:8089"));

    let config = lifx::LifxConfig{
        access_token: key.clone(),
        api_endpoints: api_endpoints
    };

    // Build "OffState" to set
    let mut off_state = lifx::State::new();
    off_state.power = Some(format!("off"));
    
    // Turn off all lights
    lifx::Light::async_set_state_by_selector(config.clone(), format!("all"), off_state).await;
}

License

Released under Apache 2.0 or MIT.

Support and follow my work by:

Buying my dope NTFs:

Checking out my Github:

Following my facebook page:

Subscribing to my Patreon:

Or donating crypto:

  • ADA: addr1qyp299a45tgvveh83tcxlf7ds3yaeh969yt3v882lvxfkkv4e0f46qvr4wzj8ty5c05jyffzq8a9pfwz9dl6m0raac7s4rac48
  • ALGO: VQ5EK4GA3IUTGSPNGV64UANBUVFAIVBXVL5UUCNZSDH544XIMF7BAHEDM4
  • ATOM: cosmos1wm7lummcealk0fxn3x9tm8hg7xsyuz06ul5fw9
  • BTC: bc1qh5p3rff4vxnv23vg0hw8pf3gmz3qgc029cekxz
  • ETH: 0x7A66beaebF7D0d17598d37525e63f524CfD23452
  • ERC20: 0x7A66beaebF7D0d17598d37525e63f524CfD23452
  • XLM: GCJAUMCO2L7PTYMXELQ6GHBTF25MCQKEBNSND2C4QMUPTSVCPEN3LCOG
  • XTZ: tz1SgJppPn56whprsDDGcqR4fxqCr2PXvg1R

Dependencies

~11–25MB
~398K SLoC