Skip to content

Commit 5eac127

Browse files
committed
Add option to monitor UPS charging
1 parent f3e030d commit 5eac127

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/bot.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct BelaState {
3434
online: bool,
3535
is_streaming: bool,
3636
restart: bool,
37+
notify_ups: Option<bool>,
3738
config: Option<belabox::messages::Config>,
3839
netif: Option<HashMap<String, belabox::messages::Netif>>,
3940
sensors: Option<belabox::messages::Sensors>,
@@ -141,6 +142,41 @@ async fn handle_belabox_messages(
141142
lock.netif = Some(netif);
142143
}
143144
Message::Sensors(sensors) => {
145+
if monitor.ups {
146+
if let Some(voltage) = &sensors.soc_voltage {
147+
let voltage = voltage.split_whitespace().next().unwrap();
148+
let voltage = voltage.parse::<f64>().unwrap();
149+
let plugged_in = (voltage * 10.0).floor() / 10.0 >= 5.1;
150+
151+
let charging = {
152+
let mut lock = bela_state.write().await;
153+
let notify = &mut lock.notify_ups;
154+
155+
let current_notify = match notify {
156+
Some(n) => *n,
157+
None => plugged_in,
158+
};
159+
160+
let charging = match (plugged_in, current_notify) {
161+
(true, false) => Some(true),
162+
(false, true) => Some(false),
163+
_ => None,
164+
};
165+
166+
*notify = Some(plugged_in);
167+
168+
charging
169+
};
170+
171+
if let Some(c) = charging {
172+
let a = if !c { "not" } else { "" };
173+
let msg = format!("BB: UPS {} charging", a);
174+
175+
twitch.send(msg).await;
176+
}
177+
}
178+
}
179+
144180
let mut lock = bela_state.write().await;
145181
lock.sensors = Some(sensors);
146182
}
@@ -293,9 +329,9 @@ async fn handle_twitch_messages(
293329
"Stopping BELABOX".to_string()
294330
}
295331
BotCommand::Stats => {
296-
let netifs = {
332+
let (netifs, ups) = {
297333
let read = bela_state.read().await;
298-
read.netif.to_owned()
334+
(read.netif.to_owned(), read.notify_ups)
299335
};
300336

301337
let mut total_bitrate = 0;
@@ -326,7 +362,14 @@ async fn handle_twitch_messages(
326362
.collect::<Vec<String>>()
327363
.join(", ");
328364

329-
format!("{}, Total: {} kbps", interfaces, total_bitrate)
365+
let mut msg = format!("{}, Total: {} kbps", interfaces, total_bitrate);
366+
367+
if let Some(connected) = ups {
368+
let a = if !connected { "not" } else { "" };
369+
msg += &format!(", UPS: {} charging", a);
370+
}
371+
372+
msg
330373
}
331374
BotCommand::Restart => {
332375
let is_streaming = {

src/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ pub struct Belabox {
3535
pub struct Monitor {
3636
pub modems: bool,
3737
pub notifications: bool,
38+
pub ups: bool,
3839
}
3940

4041
impl Default for Monitor {
4142
fn default() -> Self {
4243
Self {
4344
modems: true,
4445
notifications: true,
46+
ups: false,
4547
}
4648
}
4749
}

0 commit comments

Comments
 (0)