aboutsummaryrefslogtreecommitdiff
path: root/src/run.rs
blob: 02a6b2594df69f7edf4241f0cf68be7d144e2f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
use crate::blockmanager::*;
use crate::config::CONFIG;
use crate::types::*;
use crate::utils::*;
use async_std::channel::{unbounded, Sender};
use async_std::task;
use async_std::task::sleep;
use futures::future;
use futures::stream::StreamExt;
use std::future::Future;
use std::time::Duration;

async fn init_block<F, Fut>(tx: Sender<ThreadsData>, block: F, delay: f64)
where
    F: Fn() -> Fut,
    Fut: Future<Output = ThreadsData>,
{
    loop {
        let _ = tx.send(block().await).await;
        sleep(Duration::from_secs_f64(delay)).await;
    }
}

pub async fn run(mut blocks: BlockManager) {
    let (tx, rx) = unbounded();

    // loadavrage task
    if CONFIG.loadavg.enabled {
        let b = init_block(tx.clone(), load_average::get_load_avg, CONFIG.loadavg.delay);
        task::spawn(b);
    }
    // public ip task
    if CONFIG.pub_ip.enabled {
        let b = init_block(tx.clone(), pub_ip::get_pub_ip, CONFIG.pub_ip.delay);
        task::spawn(b);
    }

    // local ip task
    if CONFIG.local_ip.enabled {
        let b = init_block(tx.clone(), local_ip::get_local_ip, CONFIG.local_ip.delay);
        task::spawn(b);
    }

    // spotify task
    if CONFIG.spotify.enabled {
        let b = init_block(tx.clone(), spotify::get_spotify, CONFIG.spotify.delay);
        task::spawn(b);
    }

    // mpd task
    if CONFIG.mpd.enabled {
        let b = init_block(tx.clone(), mpd::get_mpd_current, CONFIG.mpd.delay);
        task::spawn(b);
    }

    // volume task
    if CONFIG.volume.enabled {
        let b = init_block(tx.clone(), volume::get_volume, CONFIG.volume.delay);
        task::spawn(b);
    }

    // Disk task
    if CONFIG.disk.enabled {
        let b = init_block(tx.clone(), disk::get_disk, CONFIG.disk.delay);
        task::spawn(b);
    }

    // Memory task
    if CONFIG.memory.enabled {
        let b = init_block(tx.clone(), memory::get_memory, CONFIG.memory.delay);
        task::spawn(b);
    }

    // Weather task
    if CONFIG.weather.enabled {
        let b = init_block(tx.clone(), weather::get_weather, CONFIG.weather.delay);
        task::spawn(b);
    }

    // Battery task
    if CONFIG.battery.enabled {
        let b = init_block(tx.clone(), battery::get_battery, CONFIG.battery.delay);
        task::spawn(b);
    }

    // Cpu temperature task
    if CONFIG.cpu_temperature.enabled {
        let b = init_block(tx.clone(), cpu::get_cpu_temp, CONFIG.cpu_temperature.delay);
        task::spawn(b);
    }

    // Uptime task
    if CONFIG.uptime.enabled {
        let b = init_block(tx.clone(), uptime::get_uptime, CONFIG.uptime.delay);
        task::spawn(b);
    }

    // brightness task
    if CONFIG.brightness.enabled {
        let b = init_block(
            tx.clone(),
            brightness::get_brightness,
            CONFIG.brightness.delay,
        );
        task::spawn(b);
    }

    // BTC task
    if CONFIG.bitcoins.enabled {
        let b = init_block(tx.clone(), bitcoins::get_price, CONFIG.bitcoins.delay);
        task::spawn(b);
    }

    // net speed task
    if CONFIG.netspeed.enabled {
        let b = init_block(tx.clone(), netspeed::get_netspeed, 0.);
        task::spawn(b);
    }

    // Time task
    let b = init_block(tx, time::get_time, CONFIG.time.delay);
    task::spawn(b);

    rx.for_each(|data| {
        blocks.update(data);
        future::ready(())
    })
    .await;
}