#json-api #json #pixel #protocols #api #wled

wled-json-api-library

使用 WLED ~14.0 编写的 Rust Json API。但应该适用于更多版本

8 个版本

0.1.7 2024年2月16日
0.1.6 2023年11月20日

#453 in 网络编程

每月 48 次下载

MIT 许可证

125KB
2K SLoC

wled-json-api-library

通过他们的 JSON API 简单地控制 WLED。

文档

此库为每个字段的意义提供了相当多的文档,因此即使您正在创建自己的库或无法使用 Rust,这也是我找到的最佳位置,以了解各个字段的意义。大多数内容不在 JSON API 页面上,其余的大部分甚至在 WLED 源代码中也没有注释。

如果您是那位愿意将此 WLED-docs 文档放入的善良之人,并且不能阅读 Rust,这里有一些提示

  • Option 是一个可以包含值 (Some(x)) 或无 (None) 的枚举
  • 以下示例字段
    /// On/Off state of the light                        // The documentation for the feild
    #[serde(skip_serializing_if = "Option::is_none")]    // Says not to convert to text when sending the root object if it is "None"
    #[serde(default = "none_function")]                  // Says to set this to None if it can't find this feild in the input text.
    pub on: Option<bool>,                                // The field. in this case "on" is the key, and the data type is a bool
// If the field has a line that says "serde(rename = "something")"
// it is likely because the name of the field in WLED is a reserved rust keyword,
// so it has to be somthing else and the actual key string is in the "rename" line

兼容性

我使用 WLED 14.0 创建并测试了这个库,但它的目的是尽可能支持更多构建和旧版本。未来的版本可能会添加,但现有的字段应该仍然有效。

流式传输颜色

虽然 JSON API 中确实有流式传输颜色的方法,但它很糟糕且速度慢。如果您想要这样做,请使用 DDP 协议。我决定不在本库中实现此功能,但如果您希望添加它,WLED 文档 中准确记录了该功能。

示例

use std::time::Duration;
use reqwest::Url;
use wled_json_api_library::wled::Wled;
use wled_json_api_library::structures::{state::State, cfg::Cfg, cfg::cfg_def::Def};

fn main() {

    // create the URL
    let url: Url = Url::try_from("http://192.168.1.40/").unwrap();

    // create the WLED connection
    let mut wled: Wled = Wled::try_from_url(&url).unwrap();
    println!("new wled: {wled:?}");

    // turn off the WLED
    {
        // put the desired change in the internal state data member
        wled.state = Some(State {
            on: Some(true),
            bri: None,
            transition: None,
            tt: None,
            ps: None,
            psave: None,
            pl: None,
            nl: None,
            udpn: None,
            v: None,
            rb: None,
            live: None,
            lor: None,
            time: None,
            mainseg: None,
            playlist: None,
            seg: None,
        });

        // flush and print the server response
        let response = wled.flush_state().unwrap();
        println!("turning the thing off {:?}", response.text());
    }


    // fill internal cfg with result from WLED
    wled.get_cfg_from_wled().unwrap();

    // get the field defining the power on boot default behaviour
    let turn_on_after_boot = wled.cfg.unwrap().def.unwrap().on.unwrap();
    // print it
    println!("received cfg, turn on after boot: {:?}", turn_on_after_boot);


    // put the desired change into the config data member
    wled.cfg = Some(Cfg{
        rev: None,
        vid: None,
        id: None,
        nw: None,
        eth: None,
        ap: None,
        wifi: None,
        hw: None,
        light: None,
        def: Some(Def{
            ps: None,
            on: Some(!turn_on_after_boot),
            bri: None,
        }),
        if_field: None,
        remote: None,
        ol: None,
        timers: None,
        ota: None,
        dmx: None,
        um: None,
    });

    // print the response.
    let response = wled.flush_config().unwrap();
    println!("toggling: {:?}", response.text());

    // wait for WLED to finish making this change.
    // Around 100 milliseconds should be enough on good hardware,
    // but this is especially slow because it has to read and write from the internal filesystem
    // where the config file is stored
    std::thread::sleep(Duration::from_millis(80));


    // get and print the new state from the server
    wled.get_cfg_from_wled().unwrap();
    let turn_on_after_boot = wled.cfg.unwrap().def.unwrap().on.unwrap();

    println!("received cfg, turn on after boot: {:?}", turn_on_after_boot);

}

依赖项

~4–15MB
~234K SLoC