basic_example/
basic_example.rs

1use std::time::Duration;
2use reqwest::Url;
3use wled_json_api_library::wled::Wled;
4use wled_json_api_library::structures::{state::State, cfg::Cfg, cfg::cfg_def::Def};
5
6fn main() {
7
8    // create the URL
9    let url: Url = Url::try_from("http://192.168.1.40/").unwrap();
10
11    // create the WLED connection
12    let mut wled: Wled = Wled::try_from_url(&url).unwrap();
13    println!("new wled: {wled:?}");
14
15    // turn off the WLED
16    {
17        // put the desired change in the internal state data member
18        wled.state = Some(State {
19            on: Some(true),
20            bri: None,
21            transition: None,
22            tt: None,
23            ps: None,
24            psave: None,
25            pl: None,
26            nl: None,
27            udpn: None,
28            v: None,
29            rb: None,
30            live: None,
31            lor: None,
32            time: None,
33            mainseg: None,
34            playlist: None,
35            seg: None,
36        });
37
38        // flush and print the server response
39        let response = wled.flush_state().unwrap();
40        println!("turning the thing off {:?}", response.text());
41    }
42
43
44    // fill internal cfg with result from WLED
45    wled.get_cfg_from_wled().unwrap();
46
47    // get the field defining the power on boot default behaviour
48    let turn_on_after_boot = wled.cfg.unwrap().def.unwrap().on.unwrap();
49    // print it
50    println!("received cfg, turn on after boot: {:?}", turn_on_after_boot);
51
52
53    // put the desired change into the config data member
54    wled.cfg = Some(Cfg{
55        rev: None,
56        vid: None,
57        id: None,
58        nw: None,
59        eth: None,
60        ap: None,
61        wifi: None,
62        hw: None,
63        light: None,
64        def: Some(Def{
65            ps: None,
66            on: Some(!turn_on_after_boot),
67            bri: None,
68        }),
69        if_field: None,
70        remote: None,
71        ol: None,
72        timers: None,
73        ota: None,
74        dmx: None,
75        um: None,
76    });
77
78    // print the response.
79    let response = wled.flush_config().unwrap();
80    println!("toggling: {:?}", response.text());
81
82    // wait for WLED to finish making this change.
83    // Around 100 milliseconds should be enough on good hardware,
84    // but this is especially slow because it has to read and write from the internal filesystem
85    // where the config file is stored
86    std::thread::sleep(Duration::from_millis(80));
87
88
89    // get and print the new state from the server
90    wled.get_cfg_from_wled().unwrap();
91    let turn_on_after_boot = wled.cfg.unwrap().def.unwrap().on.unwrap();
92
93    println!("received cfg, turn on after boot: {:?}", turn_on_after_boot);
94
95}