some_or_print_with context

This commit is contained in:
Alex Janka 2024-01-15 10:13:46 +11:00
parent ba1214c219
commit 27c8697899
6 changed files with 20 additions and 10 deletions

2
Cargo.lock generated
View file

@ -2256,7 +2256,7 @@ dependencies = [
[[package]]
name = "tesla-charge-controller"
version = "0.2.3"
version = "0.2.4"
dependencies = [
"async-channel",
"chrono",

View file

@ -1,6 +1,6 @@
[package]
name = "tesla-charge-controller"
version = "0.2.3"
version = "0.2.4"
edition = "2021"
license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data"

View file

@ -231,9 +231,15 @@ impl TeslaInterface {
// refresh our Tesla (the company's web servers, not the car) access token
if Instant::now().duration_since(self.last_refresh) >= KEY_REFRESH_INTERVAL {
log::warn!("refreshing keys...");
if self.api.refresh().await.some_or_print().is_some() {
if self
.api
.refresh()
.await
.some_or_print_with("refreshing key")
.is_some()
{
let now = Instant::now();
if self.save_key().some_or_print().is_some() {
if self.save_key().some_or_print_with("saving key").is_some() {
self.last_refresh = now;
}
}

View file

@ -178,7 +178,7 @@ impl Pli {
}
pub fn refresh(&mut self) {
if let Some(new_state) = self.read_state().some_or_print() {
if let Some(new_state) = self.read_state().some_or_print_with("reading pl state") {
self.battery_voltage.set(new_state.battery_voltage);
self.target_voltage.set(new_state.target_voltage);
self.duty_cycle.set(new_state.duty_cycle);
@ -196,7 +196,7 @@ impl Pli {
pub fn process_request(&mut self, message: PliRequest) {
match message {
PliRequest::ReadRam(address) => {
if let Some(data) = self.read_ram(address).some_or_print() {
if let Some(data) = self.read_ram(address).some_or_print_with("reading pl ram") {
let mut stdout =
termcolor::StandardStream::stdout(termcolor::ColorChoice::Always);
let _ = stdout.set_color(

View file

@ -5,7 +5,8 @@ use thiserror::Error;
pub trait PrintErrors {
type Inner;
fn some_or_print(self) -> Option<Self::Inner>;
fn some_or_print_with(self, context: &str) -> Option<Self::Inner>;
}
impl<T, E> PrintErrors for Result<T, E>
@ -14,11 +15,11 @@ where
{
type Inner = T;
fn some_or_print(self) -> Option<Self::Inner> {
fn some_or_print_with(self, context: &str) -> Option<Self::Inner> {
match self {
Ok(val) => Some(val),
Err(e) => {
log::error!("{e:?}");
log::error!("{context}: {e:?}");
None
}
}

View file

@ -55,7 +55,10 @@ async fn main() {
);
}
Commands::Watch => {
if let Some(mut interface) = TeslaInterface::load(auth_path).await.some_or_print() {
if let Some(mut interface) = TeslaInterface::load(auth_path)
.await
.some_or_print_with("loading tesla interface")
{
let config = access_config();
// build the channel that takes messages from the webserver thread to the api thread
let (api_requests, api_receiver) = async_channel::unbounded();