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
use crate::poll;
use crate::EventType;
use crate::{plugin::Plugin, ChargerBase, GaugeBase};
use std::sync::mpsc::channel;
use std::thread;
pub struct BM {
gauge: Box<dyn GaugeBase>,
charger: Box<dyn ChargerBase>,
plugins: Vec<Box<dyn Plugin>>,
}
impl BM {
pub fn new(g: Box<dyn GaugeBase>, c: Box<dyn ChargerBase>) -> BM {
Self {
gauge: g,
charger: c,
plugins: Vec::new(),
}
}
pub fn add_plugin<T>(&mut self, plugin: T) -> &mut Self
where
T: Plugin,
{
println!("added plugin: {}", plugin.name());
self.plugins.push(Box::new(plugin));
self
}
pub fn run(&self) {
let (tx, rx) = channel::<EventType>();
thread::spawn(move || {
let socket = udev::MonitorBuilder::new()?
.match_subsystem("power_supply")?
.listen()?;
poll::poll(socket, tx)
});
loop {
let t = rx.recv().unwrap();
for p in &self.plugins {
p.process(t, self.gauge.as_ref(), self.charger.as_ref());
}
}
}
}