json config

This commit is contained in:
Alex Janka 2024-01-17 08:35:05 +11:00
parent 7dd32ea667
commit 29c7dc52b3
6 changed files with 18 additions and 35 deletions

3
Cargo.lock generated
View file

@ -2256,7 +2256,7 @@ dependencies = [
[[package]] [[package]]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.0-beta-3" version = "1.0.0-beta-4-prerelease"
dependencies = [ dependencies = [
"async-channel", "async-channel",
"chrono", "chrono",
@ -2270,6 +2270,7 @@ dependencies = [
"rocket", "rocket",
"ron", "ron",
"serde", "serde",
"serde_json",
"serialport", "serialport",
"termcolor", "termcolor",
"teslatte", "teslatte",

View file

@ -1,6 +1,6 @@
[package] [package]
name = "tesla-charge-controller" name = "tesla-charge-controller"
version = "1.0.0-beta-3" version = "1.0.0-beta-4-prerelease"
edition = "2021" edition = "2021"
license = "MITNFA" license = "MITNFA"
description = "Controls Tesla charge rate based on solar charge data" description = "Controls Tesla charge rate based on solar charge data"
@ -16,6 +16,7 @@ assets = [["target/release/tesla-charge-controller", "usr/bin/", "755"]]
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.0", features = ["derive"] }
ron = "0.8" ron = "0.8"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
tokio = { version = "1.35.1", features = ["full"] } tokio = { version = "1.35.1", features = ["full"] }
teslatte = { path = "./vendored/teslatte" } teslatte = { path = "./vendored/teslatte" }
thiserror = "1.0" thiserror = "1.0"

View file

@ -7,6 +7,7 @@ use std::{
}; };
use teslatte::{ use teslatte::{
auth::{AccessToken, RefreshToken}, auth::{AccessToken, RefreshToken},
error::TeslatteError,
vehicles::{ChargingState, Endpoint, GetVehicleData, SetChargingAmps}, vehicles::{ChargingState, Endpoint, GetVehicleData, SetChargingAmps},
FleetApi, FleetVehicleApi, FleetApi, FleetVehicleApi,
}; };
@ -300,29 +301,18 @@ impl TeslaInterface {
} }
Err(e) => { Err(e) => {
self.metrics.tesla_online.set(0.); self.metrics.tesla_online.set(0.);
if let RequestError::Teslatte(teslatte::error::TeslatteError::DecodeJsonError {
source: _,
request: _,
body,
}) = &e
{
if body.contains("vehicle is offline or asleep") {
return;
}
}
match e { match e {
RequestError::StdIo(e) => log::error!("Stdio error: {e:?}"), teslatte::error::TeslatteError::DecodeJsonError {
RequestError::RonSpanned(e) => log::error!("RON parsing error: {e:?}"),
RequestError::Teslatte(teslatte::error::TeslatteError::DecodeJsonError {
source, source,
request: _, request: _,
body, body,
}) => { } => {
if body.contains("vehicle is offline or asleep") {
return;
}
log::error!("Error {source} getting state: {body}") log::error!("Error {source} getting state: {body}")
} }
RequestError::Teslatte(e) => log::error!("Teslatte error: {e:?}"), _ => log::error!("Teslatte error: {e:?}"),
RequestError::Save(e) => log::error!("Save error: {e:?}"),
} }
} }
} }
@ -347,7 +337,7 @@ impl TeslaInterface {
} }
} }
async fn get_state(&mut self) -> Result<CarState, RequestError> { async fn get_state(&mut self) -> Result<CarState, TeslatteError> {
// Endpoint::VehicleDataCombo or multiple Endpoints in one request // Endpoint::VehicleDataCombo or multiple Endpoints in one request
// doesn't seem to reliably get them all, // doesn't seem to reliably get them all,
// so for each endpoint we do a new request // so for each endpoint we do a new request

View file

@ -9,11 +9,12 @@ static CONFIG: OnceLock<Config> = OnceLock::new();
pub fn access_config<'a>() -> &'a Config { pub fn access_config<'a>() -> &'a Config {
CONFIG.get_or_init(|| { CONFIG.get_or_init(|| {
ron::from_str(&std::fs::read_to_string(CONFIG_PATH.get().unwrap()).unwrap()).unwrap() serde_json::from_str(&std::fs::read_to_string(CONFIG_PATH.get().unwrap()).unwrap()).unwrap()
}) })
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(default)]
pub struct Config { pub struct Config {
pub tesla_update_interval_seconds: u64, pub tesla_update_interval_seconds: u64,
pub tesla_update_interval_while_charging_seconds: u64, pub tesla_update_interval_while_charging_seconds: u64,
@ -28,6 +29,7 @@ pub struct Config {
pub max_rate: i64, pub max_rate: i64,
pub duty_cycle_too_high: f64, pub duty_cycle_too_high: f64,
pub duty_cycle_too_low: f64, pub duty_cycle_too_low: f64,
pub optional_string: Option<String>,
} }
impl Default for Config { impl Default for Config {
@ -49,6 +51,7 @@ impl Default for Config {
max_rate: 10, max_rate: 10,
duty_cycle_too_high: 0.9, duty_cycle_too_high: 0.9,
duty_cycle_too_low: 0.7, duty_cycle_too_low: 0.7,
optional_string: None,
} }
} }
} }

View file

@ -76,18 +76,6 @@ pub enum AuthLoadError {
NoVehicles, NoVehicles,
} }
#[derive(Error, Debug)]
pub enum RequestError {
#[error("stdio error")]
StdIo(#[from] std::io::Error),
#[error("ron - spanned error")]
RonSpanned(#[from] ron::error::SpannedError),
#[error("teslatte error")]
Teslatte(#[from] teslatte::error::TeslatteError),
#[error("save error")]
Save(#[from] SaveError),
}
#[derive(Error, Debug)] #[derive(Error, Debug)]
pub enum PliError { pub enum PliError {
#[error("read error")] #[error("read error")]

View file

@ -54,7 +54,7 @@ async fn main() {
let auth_path = args.config_dir.join("auth"); let auth_path = args.config_dir.join("auth");
let _ = CONFIG_PATH.set(args.config_dir.join("config")); let _ = CONFIG_PATH.set(args.config_dir.join("config.json"));
let _recorder = metrics_prometheus::install(); let _recorder = metrics_prometheus::install();
@ -62,7 +62,7 @@ async fn main() {
Commands::GenerateConfig => { Commands::GenerateConfig => {
println!( println!(
"{}", "{}",
ron::ser::to_string_pretty(&Config::default(), Default::default()).unwrap() serde_json::ser::to_string_pretty(&Config::default()).unwrap(),
); );
} }
Commands::Watch => { Commands::Watch => {