battery: Give up if both power-profiles-daemon and system76-power connections fail

This commit is contained in:
Lily Foster 2024-04-18 15:31:20 -04:00 committed by Ashley Wulber
parent 019a981ffe
commit e4a6f94723

View file

@ -34,14 +34,28 @@ pub enum BackendType {
} }
impl BackendType { impl BackendType {
fn next(self) -> Self { fn next(self) -> Option<Self> {
match self { match self {
Self::S76PowerDaemon => Self::PowerProfilesDaemon, Self::S76PowerDaemon => Some(Self::PowerProfilesDaemon),
Self::PowerProfilesDaemon => Self::S76PowerDaemon, Self::PowerProfilesDaemon => None,
} }
} }
} }
pub async fn get_power_backend<'a>(
conn: &'a Connection,
backend_type: &BackendType,
) -> Result<Backend<'a>> {
match backend_type {
BackendType::S76PowerDaemon => PowerDaemonProxy::new(conn)
.await
.map(Backend::S76PowerDaemon),
BackendType::PowerProfilesDaemon => PowerProfilesProxy::new(conn)
.await
.map(Backend::PowerProfilesDaemon),
}
}
pub async fn get_power_profile(daemon: Backend<'_>) -> Result<Power> { pub async fn get_power_profile(daemon: Backend<'_>) -> Result<Power> {
match daemon { match daemon {
Backend::S76PowerDaemon(p) => { Backend::S76PowerDaemon(p) => {
@ -121,30 +135,18 @@ async fn start_listening(
return State::Finished; return State::Finished;
} }
}; };
let backend = match backend_type { let backend = match get_power_backend(&conn, &backend_type)
BackendType::S76PowerDaemon => { .await
match PowerDaemonProxy::new(&conn) .map_err(|e| e.to_string())
.await {
.map_err(|e| e.to_string()) Ok(b) => b,
{ Err(e) => {
Ok(p) => Backend::S76PowerDaemon(p), _ = output.send(PowerProfileUpdate::Error(e)).await;
Err(e) => { if let Some(next_type) = backend_type.next() {
_ = output.send(PowerProfileUpdate::Error(e)).await; return State::Connecting(next_type);
return State::Connecting(backend_type.next()); } else {
} return State::Finished;
} };
}
BackendType::PowerProfilesDaemon => {
match PowerProfilesProxy::new(&conn)
.await
.map_err(|e| e.to_string())
{
Ok(p) => Backend::PowerProfilesDaemon(p),
Err(e) => {
_ = output.send(PowerProfileUpdate::Error(e)).await;
return State::Connecting(backend_type.next());
}
}
} }
}; };
// Successful connection // Successful connection
@ -152,7 +154,11 @@ async fn start_listening(
Ok(p) => p, Ok(p) => p,
Err(e) => { Err(e) => {
_ = output.send(PowerProfileUpdate::Error(e)).await; _ = output.send(PowerProfileUpdate::Error(e)).await;
return State::Connecting(backend_type.next()); if let Some(next_type) = backend_type.next() {
return State::Connecting(next_type);
} else {
return State::Finished;
};
} }
}; };
let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
@ -160,30 +166,14 @@ async fn start_listening(
State::Waiting(conn, rx, backend_type) State::Waiting(conn, rx, backend_type)
} }
State::Waiting(conn, mut rx, backend_type) => { State::Waiting(conn, mut rx, backend_type) => {
let backend = match backend_type { let backend = match get_power_backend(&conn, &backend_type)
BackendType::S76PowerDaemon => { .await
match PowerDaemonProxy::new(&conn) .map_err(|e| e.to_string())
.await {
.map_err(|e| e.to_string()) Ok(b) => b,
{ Err(e) => {
Ok(p) => Backend::S76PowerDaemon(p), _ = output.send(PowerProfileUpdate::Error(e)).await;
Err(e) => { return State::Connecting(backend_type);
_ = output.send(PowerProfileUpdate::Error(e)).await;
return State::Connecting(backend_type);
}
}
}
BackendType::PowerProfilesDaemon => {
match PowerProfilesProxy::new(&conn)
.await
.map_err(|e| e.to_string())
{
Ok(p) => Backend::PowerProfilesDaemon(p),
Err(e) => {
_ = output.send(PowerProfileUpdate::Error(e)).await;
return State::Connecting(backend_type);
}
}
} }
}; };