Compare commits
48 commits
v1.9.9-pre
...
main
Author | SHA1 | Date | |
---|---|---|---|
7ffdfd1fc3 | |||
9d12bce452 | |||
b549ceebab | |||
b28026820c | |||
ab3cd28f83 | |||
cec26d8cfb | |||
186d8fc71a | |||
05edfe6b84 | |||
c4c99eff1d | |||
76c33534be | |||
9bd1243129 | |||
667f51d375 | |||
9a87b4f820 | |||
37c7412df1 | |||
03d6278ba9 | |||
7508459414 | |||
b28f39667d | |||
d0018b7953 | |||
b5121ce7f4 | |||
8b2ef6513f | |||
12bc89ede6 | |||
ae9091c95e | |||
21f3a176ab | |||
ade01e4a91 | |||
edfc0373db | |||
f4f5e4bd3e | |||
8c8aba919d | |||
b8c2a8c114 | |||
4f2534b5a0 | |||
4905a89b33 | |||
a908490bb0 | |||
ed82c3444e | |||
88144693a8 | |||
0b53d347dc | |||
9e19f669c5 | |||
2fbae49297 | |||
e914f2fd10 | |||
f41e763743 | |||
23fe7acbb0 | |||
813f26500c | |||
8bc7c8e17c | |||
47e711f111 | |||
93aeff17cb | |||
7e664848dd | |||
cc2f493401 | |||
7ceaf73037 | |||
8b26a8d6bb | |||
9c87f12a7d |
24 changed files with 2416 additions and 954 deletions
42
Cargo.lock
generated
42
Cargo.lock
generated
|
@ -185,9 +185,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bitflags"
|
name = "bitflags"
|
||||||
version = "2.6.0"
|
version = "2.7.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de"
|
checksum = "1be3f42a67d6d345ecd59f675f3f012d6974981560836e938c22b424b85ce1be"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
@ -239,8 +239,9 @@ checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "charge-controller-supervisor"
|
name = "charge-controller-supervisor"
|
||||||
version = "1.9.9-pre-13"
|
version = "1.9.9-pre-30"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
|
"bitflags 2.7.0",
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
"env_logger",
|
"env_logger",
|
||||||
|
@ -254,6 +255,7 @@ dependencies = [
|
||||||
"rocket",
|
"rocket",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"thiserror 2.0.11",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-modbus",
|
"tokio-modbus",
|
||||||
"tokio-serial",
|
"tokio-serial",
|
||||||
|
@ -381,7 +383,7 @@ version = "0.4.2"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b035a542cf7abf01f2e3c4d5a7acbaebfefe120ae4efc7bde3df98186e4b8af7"
|
checksum = "b035a542cf7abf01f2e3c4d5a7acbaebfefe120ae4efc7bde3df98186e4b8af7"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"proc-macro2-diagnostics",
|
"proc-macro2-diagnostics",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -1169,7 +1171,7 @@ version = "0.1.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
|
checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
"libc",
|
"libc",
|
||||||
"redox_syscall",
|
"redox_syscall",
|
||||||
]
|
]
|
||||||
|
@ -1360,7 +1362,7 @@ version = "7.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c533b4c39709f9ba5005d8002048266593c1cfaf3c5f0739d5b8ab0c6c504009"
|
checksum = "c533b4c39709f9ba5005d8002048266593c1cfaf3c5f0739d5b8ab0c6c504009"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
"filetime",
|
"filetime",
|
||||||
"fsevent-sys",
|
"fsevent-sys",
|
||||||
"inotify",
|
"inotify",
|
||||||
|
@ -1591,7 +1593,7 @@ dependencies = [
|
||||||
"rustc-hash",
|
"rustc-hash",
|
||||||
"rustls",
|
"rustls",
|
||||||
"socket2",
|
"socket2",
|
||||||
"thiserror 2.0.9",
|
"thiserror 2.0.11",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tracing",
|
"tracing",
|
||||||
]
|
]
|
||||||
|
@ -1610,7 +1612,7 @@ dependencies = [
|
||||||
"rustls",
|
"rustls",
|
||||||
"rustls-pki-types",
|
"rustls-pki-types",
|
||||||
"slab",
|
"slab",
|
||||||
"thiserror 2.0.9",
|
"thiserror 2.0.11",
|
||||||
"tinyvec",
|
"tinyvec",
|
||||||
"tracing",
|
"tracing",
|
||||||
"web-time",
|
"web-time",
|
||||||
|
@ -1675,7 +1677,7 @@ version = "0.5.8"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
|
checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -1888,7 +1890,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
|
checksum = "b91f7eff05f748767f183df4320a63d6936e9c6107d97c9e6bdd9784f4289c94"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"base64 0.21.7",
|
"base64 0.21.7",
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
@ -1911,7 +1913,7 @@ version = "0.38.42"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
|
checksum = "f93dc38ecbab2eb790ff964bb77fa94faf256fd3e73285fd7ba0903b76bedb85"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
"errno",
|
"errno",
|
||||||
"libc",
|
"libc",
|
||||||
"linux-raw-sys",
|
"linux-raw-sys",
|
||||||
|
@ -2053,7 +2055,7 @@ version = "4.6.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "779e2977f0cc2ff39708fef48f96f3768ac8ddd8c6caaaab82e83bd240ef99b2"
|
checksum = "779e2977f0cc2ff39708fef48f96f3768ac8ddd8c6caaaab82e83bd240ef99b2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.6.0",
|
"bitflags 2.7.0",
|
||||||
"cfg-if",
|
"cfg-if",
|
||||||
"core-foundation",
|
"core-foundation",
|
||||||
"core-foundation-sys",
|
"core-foundation-sys",
|
||||||
|
@ -2203,7 +2205,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tesla-charge-controller"
|
name = "tesla-charge-controller"
|
||||||
version = "1.9.9-pre-13"
|
version = "1.9.9-pre-30"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"chrono",
|
"chrono",
|
||||||
"clap",
|
"clap",
|
||||||
|
@ -2222,7 +2224,7 @@ dependencies = [
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serialport",
|
"serialport",
|
||||||
"thiserror 2.0.9",
|
"thiserror 2.0.11",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-modbus",
|
"tokio-modbus",
|
||||||
"tokio-serial",
|
"tokio-serial",
|
||||||
|
@ -2239,11 +2241,11 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "2.0.9"
|
version = "2.0.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "f072643fd0190df67a8bab670c20ef5d8737177d6ac6b2e9a236cb096206b2cc"
|
checksum = "d452f284b73e6d76dd36758a0c8684b1d5be31f92b89d07fd5822175732206fc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"thiserror-impl 2.0.9",
|
"thiserror-impl 2.0.11",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -2259,9 +2261,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror-impl"
|
name = "thiserror-impl"
|
||||||
version = "2.0.9"
|
version = "2.0.11"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "7b50fa271071aae2e6ee85f842e2e28ba8cd2c5fb67f11fcb1fd70b276f9e7d4"
|
checksum = "26afc1baea8a989337eeb52b6e72a039780ce45c3edfcc9c5b9d112feeb173c2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -2376,7 +2378,7 @@ dependencies = [
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"log",
|
"log",
|
||||||
"smallvec",
|
"smallvec",
|
||||||
"thiserror 2.0.9",
|
"thiserror 2.0.11",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-util",
|
"tokio-util",
|
||||||
]
|
]
|
||||||
|
|
18
Cargo.toml
18
Cargo.toml
|
@ -4,16 +4,32 @@ default-members = ["charge-controller-supervisor"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "1.9.9-pre-13"
|
version = "1.9.9-pre-30"
|
||||||
|
|
||||||
[workspace.lints.clippy]
|
[workspace.lints.clippy]
|
||||||
pedantic = "warn"
|
pedantic = "warn"
|
||||||
|
|
||||||
|
branches_sharing_code = "warn"
|
||||||
|
derive_partial_eq_without_eq = "warn"
|
||||||
|
equatable_if_let = "warn"
|
||||||
|
fallible_impl_from = "warn"
|
||||||
|
large_stack_frames = "warn"
|
||||||
|
missing_const_for_fn = "warn"
|
||||||
|
needless_collect = "warn"
|
||||||
|
needless_pass_by_ref_mut = "warn"
|
||||||
|
or_fun_call = "warn"
|
||||||
|
redundant_clone = "warn"
|
||||||
|
significant_drop_in_scrutinee = "warn"
|
||||||
|
significant_drop_tightening = "warn"
|
||||||
|
too_long_first_doc_paragraph = "warn"
|
||||||
|
trait_duplication_in_bounds = "warn"
|
||||||
|
|
||||||
cast-possible-truncation = { level = "allow", priority = 1 }
|
cast-possible-truncation = { level = "allow", priority = 1 }
|
||||||
cast-precision-loss = { level = "allow", priority = 1 }
|
cast-precision-loss = { level = "allow", priority = 1 }
|
||||||
default-trait-access = { level = "allow", priority = 1 }
|
default-trait-access = { level = "allow", priority = 1 }
|
||||||
missing-errors-doc = { level = "allow", priority = 1 }
|
missing-errors-doc = { level = "allow", priority = 1 }
|
||||||
missing-panics-doc = { level = "allow", priority = 1 }
|
missing-panics-doc = { level = "allow", priority = 1 }
|
||||||
module-name-repetitions = { level = "allow", priority = 1 }
|
module-name-repetitions = { level = "allow", priority = 1 }
|
||||||
|
similar-names = { level = "allow", priority = 1 }
|
||||||
struct-excessive-bools = { level = "allow", priority = 1 }
|
struct-excessive-bools = { level = "allow", priority = 1 }
|
||||||
too-many-lines = { level = "allow", priority = 1 }
|
too-many-lines = { level = "allow", priority = 1 }
|
||||||
|
|
|
@ -12,6 +12,7 @@ systemd-units = { enable = false }
|
||||||
depends = ""
|
depends = ""
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
bitflags = { version = "2.7.0", features = ["serde"] }
|
||||||
chrono = "0.4.39"
|
chrono = "0.4.39"
|
||||||
clap = { version = "4.5.23", features = ["derive"] }
|
clap = { version = "4.5.23", features = ["derive"] }
|
||||||
env_logger = "0.11.6"
|
env_logger = "0.11.6"
|
||||||
|
@ -25,6 +26,7 @@ prometheus = "0.13.4"
|
||||||
rocket = { version = "0.5.1", features = ["json"] }
|
rocket = { version = "0.5.1", features = ["json"] }
|
||||||
serde = { version = "1.0.216", features = ["derive"] }
|
serde = { version = "1.0.216", features = ["derive"] }
|
||||||
serde_json = "1.0.134"
|
serde_json = "1.0.134"
|
||||||
|
thiserror = "2.0.11"
|
||||||
tokio = { version = "1.42.0", features = ["full"] }
|
tokio = { version = "1.42.0", features = ["full"] }
|
||||||
tokio-modbus = "0.16.1"
|
tokio-modbus = "0.16.1"
|
||||||
tokio-serial = "5.4.4"
|
tokio-serial = "5.4.4"
|
||||||
|
|
|
@ -14,11 +14,14 @@ pub(super) struct ConfigWatcher {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_config(path: impl AsRef<std::path::Path>) -> Option<ConfigWatcher> {
|
pub fn init_config(path: impl AsRef<std::path::Path>) -> Option<ConfigWatcher> {
|
||||||
|
let _ = CONFIG_PATH.get_or_init(|| path.as_ref().to_path_buf());
|
||||||
log::trace!("loading config...");
|
log::trace!("loading config...");
|
||||||
let config = Config::load(&path);
|
let config = Config::load(&path).unwrap();
|
||||||
|
if let Err(e) = config.save() {
|
||||||
|
log::warn!("couldn't save updated config: {e:?}");
|
||||||
|
}
|
||||||
log::trace!("watching config for changes...");
|
log::trace!("watching config for changes...");
|
||||||
let config_watcher = ConfigWatcher::new(&path);
|
let config_watcher = ConfigWatcher::new(&path);
|
||||||
let _ = CONFIG_PATH.get_or_init(|| path.as_ref().to_path_buf());
|
|
||||||
CONFIG.set(tokio::sync::RwLock::new(config)).unwrap();
|
CONFIG.set(tokio::sync::RwLock::new(config)).unwrap();
|
||||||
|
|
||||||
config_watcher
|
config_watcher
|
||||||
|
@ -48,7 +51,7 @@ impl ConfigWatcher {
|
||||||
loop {
|
loop {
|
||||||
match rx.recv().await {
|
match rx.recv().await {
|
||||||
Some(Ok(_event)) => {
|
Some(Ok(_event)) => {
|
||||||
let config = Config::load(&config_path);
|
let config = Config::load(&config_path).unwrap();
|
||||||
|
|
||||||
if let Err(e) = overwrite_config(config).await {
|
if let Err(e) = overwrite_config(config).await {
|
||||||
log::error!("{e:?}");
|
log::error!("{e:?}");
|
||||||
|
@ -70,7 +73,7 @@ impl ConfigWatcher {
|
||||||
async fn overwrite_config(config: Config) -> eyre::Result<()> {
|
async fn overwrite_config(config: Config) -> eyre::Result<()> {
|
||||||
let mut h = CONFIG
|
let mut h = CONFIG
|
||||||
.get()
|
.get()
|
||||||
.ok_or(eyre::eyre!("could not get config"))?
|
.ok_or_else(|| eyre::eyre!("could not get config"))?
|
||||||
.write()
|
.write()
|
||||||
.await;
|
.await;
|
||||||
if h.charge_controllers != config.charge_controllers
|
if h.charge_controllers != config.charge_controllers
|
||||||
|
@ -79,6 +82,7 @@ async fn overwrite_config(config: Config) -> eyre::Result<()> {
|
||||||
log::warn!("charge controller configuration changed on disk; please restart");
|
log::warn!("charge controller configuration changed on disk; please restart");
|
||||||
}
|
}
|
||||||
*h = config;
|
*h = config;
|
||||||
|
drop(h);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,17 +118,37 @@ pub async fn write_to_config<'a>() -> ConfigHandle<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||||
#[serde(default)]
|
#[serde(tag = "version")]
|
||||||
pub struct Config {
|
pub enum ConfigStorage {
|
||||||
pub primary_charge_controller: String,
|
#[serde(rename = "1")]
|
||||||
pub enable_secondary_control: bool,
|
V1(outdated::ConfigV1),
|
||||||
pub charge_controllers: Vec<ChargeControllerConfig>,
|
#[serde(rename = "2")]
|
||||||
|
V2(Config),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
mod outdated;
|
||||||
fn load(path: impl AsRef<std::path::Path>) -> Self {
|
|
||||||
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap()
|
impl Default for ConfigStorage {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::from_latest(Default::default())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConfigStorage {
|
||||||
|
pub const fn from_latest(config: Config) -> Self {
|
||||||
|
Self::V2(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_latest(self) -> Config {
|
||||||
|
match self {
|
||||||
|
ConfigStorage::V1(v1) => v1.into(),
|
||||||
|
ConfigStorage::V2(config) => config,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn load(path: impl AsRef<std::path::Path>) -> eyre::Result<Self> {
|
||||||
|
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_to(&self, path: impl AsRef<std::path::Path>) -> eyre::Result<()> {
|
fn save_to(&self, path: impl AsRef<std::path::Path>) -> eyre::Result<()> {
|
||||||
|
@ -139,19 +163,46 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
pub struct Config {
|
||||||
|
pub primary_charge_controller: String,
|
||||||
|
pub enable_secondary_control: bool,
|
||||||
|
pub charge_controllers: Vec<ChargeControllerConfig>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
fn load(path: impl AsRef<std::path::Path>) -> eyre::Result<Self> {
|
||||||
|
let storage = ConfigStorage::load(path)?;
|
||||||
|
Ok(storage.into_latest())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save(&self) -> eyre::Result<()> {
|
||||||
|
let as_storage = ConfigStorage::from_latest(self.clone());
|
||||||
|
as_storage.save()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct ChargeControllerConfig {
|
pub struct ChargeControllerConfig {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub serial_port: String,
|
|
||||||
pub baud_rate: u32,
|
|
||||||
pub watch_interval_seconds: u64,
|
pub watch_interval_seconds: u64,
|
||||||
pub variant: ChargeControllerVariant,
|
pub variant: ChargeControllerVariant,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub follow_primary: bool,
|
pub follow_primary: bool,
|
||||||
|
pub transport: Transport,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
||||||
pub enum ChargeControllerVariant {
|
pub enum ChargeControllerVariant {
|
||||||
Tristar,
|
Tristar,
|
||||||
Pl { timeout_milliseconds: u64 },
|
Pl { timeout_milliseconds: u64 },
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
||||||
|
#[serde(rename_all = "lowercase")]
|
||||||
|
pub enum Transport {
|
||||||
|
Serial { port: String, baud_rate: u32 },
|
||||||
|
Tcp { ip: std::net::IpAddr, port: u16 },
|
||||||
|
}
|
||||||
|
|
72
charge-controller-supervisor/src/config/outdated.rs
Normal file
72
charge-controller-supervisor/src/config/outdated.rs
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
pub use v1::ConfigV1;
|
||||||
|
|
||||||
|
mod v1 {
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Default)]
|
||||||
|
#[serde(default)]
|
||||||
|
pub struct ConfigV1 {
|
||||||
|
primary_charge_controller: String,
|
||||||
|
enable_secondary_control: bool,
|
||||||
|
charge_controllers: Vec<ChargeControllerConfigV1>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||||
|
struct ChargeControllerConfigV1 {
|
||||||
|
name: String,
|
||||||
|
serial_port: String,
|
||||||
|
baud_rate: u32,
|
||||||
|
watch_interval_seconds: u64,
|
||||||
|
variant: ChargeControllerVariantV1,
|
||||||
|
#[serde(default)]
|
||||||
|
follow_primary: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
|
||||||
|
enum ChargeControllerVariantV1 {
|
||||||
|
Tristar,
|
||||||
|
Pl { timeout_milliseconds: u64 },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ChargeControllerConfigV1> for crate::config::ChargeControllerConfig {
|
||||||
|
fn from(value: ChargeControllerConfigV1) -> Self {
|
||||||
|
Self {
|
||||||
|
name: value.name,
|
||||||
|
transport: crate::config::Transport::Serial {
|
||||||
|
port: value.serial_port,
|
||||||
|
baud_rate: value.baud_rate,
|
||||||
|
},
|
||||||
|
watch_interval_seconds: value.watch_interval_seconds,
|
||||||
|
variant: value.variant.into(),
|
||||||
|
follow_primary: value.follow_primary,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ChargeControllerVariantV1> for crate::config::ChargeControllerVariant {
|
||||||
|
fn from(value: ChargeControllerVariantV1) -> Self {
|
||||||
|
match value {
|
||||||
|
ChargeControllerVariantV1::Tristar => Self::Tristar,
|
||||||
|
ChargeControllerVariantV1::Pl {
|
||||||
|
timeout_milliseconds,
|
||||||
|
} => Self::Pl {
|
||||||
|
timeout_milliseconds,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ConfigV1> for crate::config::Config {
|
||||||
|
fn from(value: ConfigV1) -> Self {
|
||||||
|
Self {
|
||||||
|
primary_charge_controller: value.primary_charge_controller,
|
||||||
|
enable_secondary_control: value.enable_secondary_control,
|
||||||
|
charge_controllers: value
|
||||||
|
.charge_controllers
|
||||||
|
.into_iter()
|
||||||
|
.map(Into::into)
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,17 @@
|
||||||
|
use crate::storage::ControllerData;
|
||||||
|
|
||||||
|
mod pl;
|
||||||
|
mod tristar;
|
||||||
|
|
||||||
pub struct Controller {
|
pub struct Controller {
|
||||||
name: String,
|
name: String,
|
||||||
interval: std::time::Duration,
|
interval: std::time::Duration,
|
||||||
inner: ControllerInner,
|
inner: ControllerInner,
|
||||||
data: std::sync::Arc<tokio::sync::RwLock<CommonData>>,
|
data: std::sync::Arc<ControllerData>,
|
||||||
voltage_rx: Option<tokio::sync::mpsc::UnboundedReceiver<VoltageCommand>>,
|
follow_voltage: bool,
|
||||||
voltage_tx: Option<MultiTx>,
|
voltage_rx: tokio::sync::watch::Receiver<VoltageCommand>,
|
||||||
|
voltage_tx: Option<tokio::sync::watch::Sender<VoltageCommand>>,
|
||||||
|
settings_last_read: Option<std::time::Instant>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, serde::Serialize, Clone)]
|
#[derive(Default, serde::Serialize, Clone)]
|
||||||
|
@ -14,57 +21,79 @@ pub struct CommonData {
|
||||||
pub battery_temp: f64,
|
pub battery_temp: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Clone)]
|
||||||
|
#[serde(tag = "model")]
|
||||||
|
pub enum ControllerState {
|
||||||
|
Pl(pl::PlState),
|
||||||
|
Tristar(tristar::TristarState),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControllerState {
|
||||||
|
pub fn common(&self) -> CommonData {
|
||||||
|
match self {
|
||||||
|
Self::Pl(pl_state) => crate::controller::CommonData {
|
||||||
|
battery_voltage: pl_state.battery_voltage,
|
||||||
|
target_voltage: pl_state.target_voltage,
|
||||||
|
battery_temp: pl_state.battery_temp,
|
||||||
|
},
|
||||||
|
Self::Tristar(tristar_state) => crate::controller::CommonData {
|
||||||
|
battery_voltage: tristar_state.battery_voltage,
|
||||||
|
target_voltage: tristar_state.target_voltage,
|
||||||
|
battery_temp: f64::from(tristar_state.battery_temp),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(serde::Serialize, Clone)]
|
||||||
|
#[serde(tag = "model")]
|
||||||
|
pub enum ControllerSettings {
|
||||||
|
Pl(pl::PlSettings),
|
||||||
|
Tristar(tristar::TristarSettings),
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub enum VoltageCommand {
|
pub enum VoltageCommand {
|
||||||
|
None,
|
||||||
Set(f64),
|
Set(f64),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Controller {
|
impl Controller {
|
||||||
pub fn new(
|
pub async fn new(
|
||||||
config: crate::config::ChargeControllerConfig,
|
config: crate::config::ChargeControllerConfig,
|
||||||
) -> eyre::Result<(
|
voltage_rx: tokio::sync::watch::Receiver<VoltageCommand>,
|
||||||
Self,
|
) -> eyre::Result<Self> {
|
||||||
Option<tokio::sync::mpsc::UnboundedSender<VoltageCommand>>,
|
|
||||||
)> {
|
|
||||||
let inner = match config.variant {
|
let inner = match config.variant {
|
||||||
crate::config::ChargeControllerVariant::Tristar => ControllerInner::Tristar(
|
crate::config::ChargeControllerVariant::Tristar => ControllerInner::Tristar(
|
||||||
crate::tristar::Tristar::new(&config.serial_port, &config.name, config.baud_rate)?,
|
tristar::Tristar::new(&config.name, &config.transport).await?,
|
||||||
),
|
),
|
||||||
crate::config::ChargeControllerVariant::Pl {
|
crate::config::ChargeControllerVariant::Pl {
|
||||||
timeout_milliseconds,
|
timeout_milliseconds,
|
||||||
} => ControllerInner::Pl(crate::pl::Pli::new(
|
} => match &config.transport {
|
||||||
&config.serial_port,
|
crate::config::Transport::Serial { port, baud_rate } => ControllerInner::Pl(
|
||||||
&config.name,
|
pl::Pli::new(port, &config.name, *baud_rate, timeout_milliseconds)?,
|
||||||
config.baud_rate,
|
),
|
||||||
timeout_milliseconds,
|
crate::config::Transport::Tcp { ip: _, port: _ } => {
|
||||||
)?),
|
return Err(eyre::eyre!("pl doesn't support tcp"))
|
||||||
};
|
}
|
||||||
|
|
||||||
let data = CommonData::default();
|
|
||||||
|
|
||||||
let data = std::sync::Arc::new(tokio::sync::RwLock::new(data));
|
|
||||||
|
|
||||||
let (voltage_tx, voltage_rx) = if config.follow_primary {
|
|
||||||
let (a, b) = tokio::sync::mpsc::unbounded_channel();
|
|
||||||
(Some(a), Some(b))
|
|
||||||
} else {
|
|
||||||
(None, None)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok((
|
|
||||||
Self {
|
|
||||||
name: config.name,
|
|
||||||
interval: std::time::Duration::from_secs(config.watch_interval_seconds),
|
|
||||||
inner,
|
|
||||||
data,
|
|
||||||
voltage_rx,
|
|
||||||
voltage_tx: None,
|
|
||||||
},
|
},
|
||||||
voltage_tx,
|
};
|
||||||
))
|
|
||||||
|
let data = std::sync::Arc::new(ControllerData::new());
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
name: config.name,
|
||||||
|
interval: std::time::Duration::from_secs(config.watch_interval_seconds),
|
||||||
|
inner,
|
||||||
|
data,
|
||||||
|
voltage_rx,
|
||||||
|
voltage_tx: None,
|
||||||
|
settings_last_read: None,
|
||||||
|
follow_voltage: config.follow_primary,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_data_ptr(&self) -> std::sync::Arc<tokio::sync::RwLock<CommonData>> {
|
pub fn get_data_ptr(&self) -> std::sync::Arc<ControllerData> {
|
||||||
self.data.clone()
|
self.data.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,32 +105,44 @@ impl Controller {
|
||||||
.await
|
.await
|
||||||
.enable_secondary_control
|
.enable_secondary_control
|
||||||
{
|
{
|
||||||
|
let target = data.common().target_voltage;
|
||||||
log::debug!(
|
log::debug!(
|
||||||
"tristar {}: primary: sending target voltage {}",
|
"tristar {}: primary: sending target voltage {}",
|
||||||
self.name,
|
self.name,
|
||||||
data.target_voltage
|
target
|
||||||
);
|
);
|
||||||
|
|
||||||
tx.send_to_all(VoltageCommand::Set(data.target_voltage));
|
tx.send(VoltageCommand::Set(target))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*self.data.write().await = data;
|
*self.data.write_state().await = Some(data);
|
||||||
|
if self.needs_new_settings() {
|
||||||
|
match self.inner.get_settings().await {
|
||||||
|
Ok(s) => {
|
||||||
|
*self.data.write_settings().await = Some(s);
|
||||||
|
self.settings_last_read = Some(std::time::Instant::now());
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::error!("couldn't read config from {}: {e:?}", self.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn timeout_interval(&self) -> std::time::Duration {
|
pub const fn timeout_interval(&self) -> std::time::Duration {
|
||||||
self.interval
|
self.interval
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &str {
|
pub fn name(&self) -> &str {
|
||||||
&self.name
|
self.name.as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tx_to_secondary(&mut self, tx: MultiTx) {
|
pub fn set_tx_to_secondary(&mut self, tx: tokio::sync::watch::Sender<VoltageCommand>) {
|
||||||
assert!(
|
assert!(
|
||||||
self.voltage_rx.is_none(),
|
!self.follow_voltage,
|
||||||
"trying to set {} as primary when it is also a secondary!",
|
"trying to set {} as primary when it is also a secondary!",
|
||||||
self.name
|
self.name
|
||||||
);
|
);
|
||||||
|
@ -109,47 +150,64 @@ impl Controller {
|
||||||
self.voltage_tx = Some(tx);
|
self.voltage_tx = Some(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_rx(&mut self) -> Option<&mut tokio::sync::mpsc::UnboundedReceiver<VoltageCommand>> {
|
pub fn get_rx(&mut self) -> &mut tokio::sync::watch::Receiver<VoltageCommand> {
|
||||||
self.voltage_rx.as_mut()
|
&mut self.voltage_rx
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn process_command(&mut self, command: VoltageCommand) -> eyre::Result<()> {
|
pub async fn process_command(&mut self, command: VoltageCommand) -> eyre::Result<()> {
|
||||||
match command {
|
match command {
|
||||||
VoltageCommand::Set(target_voltage) => {
|
VoltageCommand::Set(target_voltage) => {
|
||||||
self.inner.set_target_voltage(target_voltage).await
|
if self.follow_voltage {
|
||||||
|
self.inner.set_target_voltage(target_voltage).await
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
VoltageCommand::None => {
|
||||||
|
// todo: disable voltage control
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub fn needs_new_settings(&self) -> bool {
|
||||||
pub struct MultiTx(pub Vec<tokio::sync::mpsc::UnboundedSender<VoltageCommand>>);
|
self.settings_last_read.is_none_or(|t| {
|
||||||
|
std::time::Instant::now().duration_since(t) >= std::time::Duration::from_secs(60 * 60)
|
||||||
impl MultiTx {
|
})
|
||||||
pub fn send_to_all(&self, command: VoltageCommand) {
|
|
||||||
for sender in &self.0 {
|
|
||||||
if let Err(e) = sender.send(command) {
|
|
||||||
log::error!("failed to send command {command:?}: {e:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[expect(clippy::large_enum_variant)]
|
||||||
pub enum ControllerInner {
|
pub enum ControllerInner {
|
||||||
Pl(crate::pl::Pli),
|
Pl(pl::Pli),
|
||||||
Tristar(crate::tristar::Tristar),
|
Tristar(tristar::Tristar),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ControllerInner {
|
impl ControllerInner {
|
||||||
pub async fn refresh(&mut self) -> eyre::Result<CommonData> {
|
pub async fn refresh(&mut self) -> eyre::Result<ControllerState> {
|
||||||
match self {
|
match self {
|
||||||
ControllerInner::Pl(pli) => {
|
ControllerInner::Pl(pli) => {
|
||||||
let pl_data = pli.refresh().await?;
|
let pl_data = pli.refresh().await?;
|
||||||
Ok(pl_data)
|
|
||||||
|
Ok(ControllerState::Pl(pl_data))
|
||||||
}
|
}
|
||||||
ControllerInner::Tristar(tristar) => {
|
ControllerInner::Tristar(tristar) => {
|
||||||
let tristar_data = tristar.refresh().await?;
|
let tristar_data = tristar.refresh().await?;
|
||||||
Ok(tristar_data)
|
|
||||||
|
Ok(ControllerState::Tristar(tristar_data))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_settings(&mut self) -> eyre::Result<ControllerSettings> {
|
||||||
|
match self {
|
||||||
|
ControllerInner::Pl(pl) => {
|
||||||
|
let settings = pl.read_settings().await?;
|
||||||
|
Ok(ControllerSettings::Pl(settings))
|
||||||
|
}
|
||||||
|
ControllerInner::Tristar(tristar) => {
|
||||||
|
let settings = tristar.read_settings().await?;
|
||||||
|
Ok(ControllerSettings::Tristar(settings))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
671
charge-controller-supervisor/src/controller/pl.rs
Normal file
671
charge-controller-supervisor/src/controller/pl.rs
Normal file
|
@ -0,0 +1,671 @@
|
||||||
|
use chrono::Timelike;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
use tokio_serial::{SerialPort, SerialPortBuilderExt};
|
||||||
|
|
||||||
|
use crate::gauges::{
|
||||||
|
BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, INPUT_CURRENT, PL_DUTY_CYCLE, PL_LOAD_CURRENT,
|
||||||
|
TARGET_VOLTAGE,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct Pli {
|
||||||
|
friendly_name: String,
|
||||||
|
port: tokio_serial::SerialStream,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
|
||||||
|
pub struct PlState {
|
||||||
|
pub battery_voltage: f64,
|
||||||
|
pub target_voltage: f64,
|
||||||
|
pub duty_cycle: f64,
|
||||||
|
pub internal_charge_current: f64,
|
||||||
|
pub internal_load_current: f64,
|
||||||
|
pub battery_temp: f64,
|
||||||
|
pub regulator_state: RegulatorState,
|
||||||
|
// pub internal_charge_ah_accumulator: u16,
|
||||||
|
// pub external_charge_ah_accumulator: u16,
|
||||||
|
// pub internal_load_ah_accumulator: u16,
|
||||||
|
// pub external_load_ah_accumulator: u16,
|
||||||
|
// pub internal_charge_ah: u16,
|
||||||
|
// pub external_charge_ah: u16,
|
||||||
|
// pub internal_load_ah: u16,
|
||||||
|
// pub external_load_ah: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||||
|
pub enum RegulatorState {
|
||||||
|
Boost,
|
||||||
|
Equalise,
|
||||||
|
Absorption,
|
||||||
|
Float,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<u8> for RegulatorState {
|
||||||
|
fn from(value: u8) -> Self {
|
||||||
|
match value & 0b11 {
|
||||||
|
0b00 => Self::Boost,
|
||||||
|
0b01 => Self::Equalise,
|
||||||
|
0b10 => Self::Absorption,
|
||||||
|
0b11 => Self::Float,
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<RegulatorState> for u8 {
|
||||||
|
fn from(value: RegulatorState) -> Self {
|
||||||
|
match value {
|
||||||
|
RegulatorState::Boost => 0b00,
|
||||||
|
RegulatorState::Equalise => 0b01,
|
||||||
|
RegulatorState::Absorption => 0b10,
|
||||||
|
RegulatorState::Float => 0b11,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for RegulatorState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::Absorption
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_regulator_gauges(state: RegulatorState, label: &str) {
|
||||||
|
let boost = CHARGE_STATE.with_label_values(&[label, "boost"]);
|
||||||
|
let equalise = CHARGE_STATE.with_label_values(&[label, "equalise"]);
|
||||||
|
let absorption = CHARGE_STATE.with_label_values(&[label, "absorption"]);
|
||||||
|
let float = CHARGE_STATE.with_label_values(&[label, "float"]);
|
||||||
|
|
||||||
|
match state {
|
||||||
|
RegulatorState::Boost => {
|
||||||
|
boost.set(1);
|
||||||
|
equalise.set(0);
|
||||||
|
absorption.set(0);
|
||||||
|
float.set(0);
|
||||||
|
}
|
||||||
|
RegulatorState::Equalise => {
|
||||||
|
boost.set(0);
|
||||||
|
equalise.set(1);
|
||||||
|
absorption.set(0);
|
||||||
|
float.set(0);
|
||||||
|
}
|
||||||
|
RegulatorState::Absorption => {
|
||||||
|
boost.set(0);
|
||||||
|
equalise.set(0);
|
||||||
|
absorption.set(1);
|
||||||
|
float.set(0);
|
||||||
|
}
|
||||||
|
RegulatorState::Float => {
|
||||||
|
boost.set(0);
|
||||||
|
equalise.set(0);
|
||||||
|
absorption.set(0);
|
||||||
|
float.set(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub struct PlSettings {
|
||||||
|
load_disconnect_voltage: f64,
|
||||||
|
load_reconnect_voltage: f64,
|
||||||
|
delay_before_disconnect_minutes: u8,
|
||||||
|
days_between_boost: u8,
|
||||||
|
absorption_time: u8,
|
||||||
|
hysteresis: u8,
|
||||||
|
boost_return_voltage: f64,
|
||||||
|
charge_current_limit: u8,
|
||||||
|
battery_2_regulation_voltage: f64,
|
||||||
|
days_between_equalization: u8,
|
||||||
|
equalization_length: u8,
|
||||||
|
absorption_voltage: f64,
|
||||||
|
equalization_voltage: f64,
|
||||||
|
float_voltage: f64,
|
||||||
|
boost_voltage: f64,
|
||||||
|
program: Program,
|
||||||
|
system_voltage: SystemVoltage,
|
||||||
|
battery_capacity_ah: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub enum Program {
|
||||||
|
Prog0,
|
||||||
|
Prog1,
|
||||||
|
Prog2,
|
||||||
|
Prog3,
|
||||||
|
Prog4,
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
|
pub enum SystemVoltage {
|
||||||
|
V12,
|
||||||
|
V24,
|
||||||
|
V32,
|
||||||
|
V36,
|
||||||
|
V48,
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code, reason = "writing settings is not yet implemented")]
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub enum PliRequest {
|
||||||
|
ReadRam(u8),
|
||||||
|
ReadEeprom(u8),
|
||||||
|
SyncTime,
|
||||||
|
SetRegulatorState(RegulatorState),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Pli {
|
||||||
|
pub fn new(
|
||||||
|
serial_port: &str,
|
||||||
|
friendly_name: &str,
|
||||||
|
baud_rate: u32,
|
||||||
|
timeout: u64,
|
||||||
|
) -> Result<Self, tokio_serial::Error> {
|
||||||
|
let port = tokio_serial::new(serial_port, baud_rate)
|
||||||
|
.timeout(std::time::Duration::from_millis(timeout))
|
||||||
|
.open_native_async()?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
friendly_name: friendly_name.to_owned(),
|
||||||
|
port,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn refresh(&mut self) -> eyre::Result<PlState> {
|
||||||
|
let new_state = self.read_state().await?;
|
||||||
|
BATTERY_VOLTAGE
|
||||||
|
.with_label_values(&[&self.friendly_name])
|
||||||
|
.set(new_state.battery_voltage);
|
||||||
|
TARGET_VOLTAGE
|
||||||
|
.with_label_values(&[&self.friendly_name])
|
||||||
|
.set(new_state.target_voltage);
|
||||||
|
PL_DUTY_CYCLE
|
||||||
|
.with_label_values(&[&self.friendly_name])
|
||||||
|
.set(new_state.duty_cycle);
|
||||||
|
INPUT_CURRENT
|
||||||
|
.with_label_values(&[&self.friendly_name])
|
||||||
|
.set(new_state.internal_charge_current);
|
||||||
|
PL_LOAD_CURRENT
|
||||||
|
.with_label_values(&[&self.friendly_name])
|
||||||
|
.set(new_state.internal_load_current);
|
||||||
|
BATTERY_TEMP
|
||||||
|
.with_label_values(&[&self.friendly_name])
|
||||||
|
.set(new_state.battery_temp);
|
||||||
|
|
||||||
|
set_regulator_gauges(new_state.regulator_state, &self.friendly_name);
|
||||||
|
|
||||||
|
Ok(new_state)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code, reason = "writing settings is not yet implemented")]
|
||||||
|
pub async fn process_request(&mut self, message: PliRequest) -> eyre::Result<()> {
|
||||||
|
match message {
|
||||||
|
PliRequest::ReadRam(address) => {
|
||||||
|
let data = self.read_ram_with_retires(address).await?;
|
||||||
|
log::warn!("Read RAM at {address}: data {data}");
|
||||||
|
}
|
||||||
|
PliRequest::ReadEeprom(address) => {
|
||||||
|
let data = self.read_eeprom(address).await?;
|
||||||
|
log::warn!("Read EEPROM at {address}: data {data}");
|
||||||
|
}
|
||||||
|
PliRequest::SyncTime => {
|
||||||
|
let now = chrono::Local::now();
|
||||||
|
let timestamp = (((now.hour() * 10) + (now.minute() / 6)) & 0xFF) as u8;
|
||||||
|
let min = (now.minute() % 6) as u8;
|
||||||
|
let sec = (now.second() / 2).min(29) as u8;
|
||||||
|
self.write_ram(PlRamAddress::Hour, timestamp).await?;
|
||||||
|
self.write_ram(PlRamAddress::Min, min).await?;
|
||||||
|
self.write_ram(PlRamAddress::Sec, sec).await?;
|
||||||
|
log::warn!(
|
||||||
|
"Set time: {now} corresponds to {timestamp} + minutes {min} + seconds {sec}"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
PliRequest::SetRegulatorState(state) => {
|
||||||
|
log::warn!("Setting regulator state to {state:?}");
|
||||||
|
self.write_ram(PlRamAddress::Rstate, state.into()).await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_settings(&mut self) -> eyre::Result<PlSettings> {
|
||||||
|
let load_disconnect_voltage =
|
||||||
|
f64::from(self.read_eeprom(PlEepromAddress::LOff).await?) * (4. / 10.);
|
||||||
|
let load_reconnect_voltage =
|
||||||
|
f64::from(self.read_eeprom(PlEepromAddress::LOn).await?) * (4. / 10.);
|
||||||
|
let delay_before_disconnect_minutes = self.read_eeprom(PlEepromAddress::LDel).await?;
|
||||||
|
let days_between_boost = self.read_eeprom(PlEepromAddress::BstFreq).await?;
|
||||||
|
let absorption_time = self.read_eeprom(PlEepromAddress::ATim).await?;
|
||||||
|
let hysteresis = self.read_eeprom(PlEepromAddress::Hyst).await?;
|
||||||
|
let boost_return_voltage =
|
||||||
|
f64::from(self.read_eeprom(PlEepromAddress::BRet).await?) * (4. / 10.);
|
||||||
|
let charge_current_limit = self.read_eeprom(PlEepromAddress::CurLim).await?;
|
||||||
|
let battery_2_regulation_voltage =
|
||||||
|
f64::from(self.read_eeprom(PlEepromAddress::Bat2).await?) * (4. / 10.);
|
||||||
|
let days_between_equalization = self.read_eeprom(PlEepromAddress::EqFreq).await?;
|
||||||
|
let equalization_length = self.read_eeprom(PlEepromAddress::ETim).await?;
|
||||||
|
let absorption_voltage =
|
||||||
|
f64::from(self.read_eeprom(PlEepromAddress::AbsV).await?) * (4. / 10.);
|
||||||
|
let equalization_voltage =
|
||||||
|
f64::from(self.read_eeprom(PlEepromAddress::EMax).await?) * (4. / 10.);
|
||||||
|
let float_voltage = f64::from(self.read_eeprom(PlEepromAddress::FltV).await?) * (4. / 10.);
|
||||||
|
let boost_voltage = f64::from(self.read_eeprom(PlEepromAddress::BMax).await?) * (4. / 10.);
|
||||||
|
let volt = self.read_eeprom(PlEepromAddress::Volt).await?;
|
||||||
|
let program = match volt >> 4 {
|
||||||
|
0 => Program::Prog0,
|
||||||
|
1 => Program::Prog1,
|
||||||
|
2 => Program::Prog2,
|
||||||
|
3 => Program::Prog3,
|
||||||
|
4 => Program::Prog4,
|
||||||
|
_ => Program::Unknown,
|
||||||
|
};
|
||||||
|
let system_voltage = match volt & 0xF {
|
||||||
|
0 => SystemVoltage::V12,
|
||||||
|
1 => SystemVoltage::V24,
|
||||||
|
2 => SystemVoltage::V32,
|
||||||
|
3 => SystemVoltage::V36,
|
||||||
|
4 => SystemVoltage::V48,
|
||||||
|
_ => SystemVoltage::Unknown,
|
||||||
|
};
|
||||||
|
let battery_cap = usize::from(self.read_eeprom(PlEepromAddress::BCap).await?);
|
||||||
|
|
||||||
|
let battery_capacity_ah = if battery_cap > 50 {
|
||||||
|
(50 * 20) + ((battery_cap - 50) * 100)
|
||||||
|
} else {
|
||||||
|
battery_cap * 20
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(PlSettings {
|
||||||
|
load_disconnect_voltage,
|
||||||
|
load_reconnect_voltage,
|
||||||
|
delay_before_disconnect_minutes,
|
||||||
|
days_between_boost,
|
||||||
|
absorption_time,
|
||||||
|
hysteresis,
|
||||||
|
boost_return_voltage,
|
||||||
|
charge_current_limit,
|
||||||
|
battery_2_regulation_voltage,
|
||||||
|
days_between_equalization,
|
||||||
|
equalization_length,
|
||||||
|
absorption_voltage,
|
||||||
|
equalization_voltage,
|
||||||
|
float_voltage,
|
||||||
|
boost_voltage,
|
||||||
|
program,
|
||||||
|
system_voltage,
|
||||||
|
battery_capacity_ah,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_state(&mut self) -> eyre::Result<PlState> {
|
||||||
|
// let int_charge_acc_low = self.read_ram(PlRamAddress::Ciacc1).await?;
|
||||||
|
// let int_charge_acc = self.read_ram(PlRamAddress::Ciacc2).await?;
|
||||||
|
// let int_charge_acc_high = self.read_ram(PlRamAddress::Ciacc3).await?;
|
||||||
|
// let mut internal_charge_ah_accumulator = u16::from(int_charge_acc_high) << 9;
|
||||||
|
// internal_charge_ah_accumulator |= u16::from(int_charge_acc) << 1;
|
||||||
|
// internal_charge_ah_accumulator |= u16::from(int_charge_acc_low & 0b1);
|
||||||
|
// let int_charge_low = self.read_ram(PlRamAddress::Ciahl).await?;
|
||||||
|
// let int_charge_high = self.read_ram(PlRamAddress::Ciahh).await?;
|
||||||
|
// let internal_charge_ah = u16::from_le_bytes([int_charge_low, int_charge_high]);
|
||||||
|
|
||||||
|
// let int_load_acc_low = self.read_ram(PlRamAddress::Liacc1).await?;
|
||||||
|
// let int_load_acc = self.read_ram(PlRamAddress::Liacc2).await?;
|
||||||
|
// let int_load_acc_high = self.read_ram(PlRamAddress::Liacc3).await?;
|
||||||
|
// let mut internal_load_ah_accumulator = u16::from(int_load_acc_high) << 9;
|
||||||
|
// internal_load_ah_accumulator |= u16::from(int_load_acc) << 1;
|
||||||
|
// internal_load_ah_accumulator |= u16::from(int_load_acc_low & 0b1);
|
||||||
|
// let int_load_low = self.read_ram(PlRamAddress::Liahl).await?;
|
||||||
|
// let int_load_high = self.read_ram(PlRamAddress::Liahh).await?;
|
||||||
|
// let internal_load_ah = u16::from_le_bytes([int_load_low, int_load_high]);
|
||||||
|
|
||||||
|
// let ext_charge_acc_low = self.read_ram(PlRamAddress::Ceacc1).await?;
|
||||||
|
// let ext_charge_acc = self.read_ram(PlRamAddress::Ceacc2).await?;
|
||||||
|
// let ext_charge_acc_high = self.read_ram(PlRamAddress::Ceacc3).await?;
|
||||||
|
// let mut external_charge_ah_accumulator = u16::from(ext_charge_acc_high) << 9;
|
||||||
|
// external_charge_ah_accumulator |= u16::from(ext_charge_acc) << 1;
|
||||||
|
// external_charge_ah_accumulator |= u16::from(ext_charge_acc_low & 0b1);
|
||||||
|
// let ext_charge_low = self.read_ram(PlRamAddress::Ceahl).await?;
|
||||||
|
// let ext_charge_high = self.read_ram(PlRamAddress::Ceahh).await?;
|
||||||
|
// let external_charge_ah = u16::from_le_bytes([ext_charge_low, ext_charge_high]);
|
||||||
|
|
||||||
|
// let ext_load_acc_low = self.read_ram(PlRamAddress::Leacc1).await?;
|
||||||
|
// let ext_load_acc = self.read_ram(PlRamAddress::Leacc2).await?;
|
||||||
|
// let ext_load_acc_high = self.read_ram(PlRamAddress::Leacc3).await?;
|
||||||
|
// let mut external_load_ah_accumulator = u16::from(ext_load_acc_high) << 9;
|
||||||
|
// external_load_ah_accumulator |= u16::from(ext_load_acc) << 1;
|
||||||
|
// external_load_ah_accumulator |= u16::from(ext_load_acc_low & 0b1);
|
||||||
|
// let ext_load_low = self.read_ram(PlRamAddress::Leahl).await?;
|
||||||
|
// let ext_load_high = self.read_ram(PlRamAddress::Leahh).await?;
|
||||||
|
// let external_load_ah = u16::from_le_bytes([ext_load_low, ext_load_high]);
|
||||||
|
|
||||||
|
Ok(PlState {
|
||||||
|
battery_voltage: f64::from(self.read_ram_with_retires(PlRamAddress::Batv).await?)
|
||||||
|
* (4. / 10.),
|
||||||
|
target_voltage: f64::from(self.read_ram_with_retires(PlRamAddress::Vreg).await?)
|
||||||
|
* (4. / 10.),
|
||||||
|
duty_cycle: f64::from(self.read_ram_with_retires(PlRamAddress::Dutycyc).await?) / 255.,
|
||||||
|
internal_charge_current: f64::from(
|
||||||
|
self.read_ram_with_retires(PlRamAddress::Cint).await?,
|
||||||
|
) * (4. / 10.),
|
||||||
|
internal_load_current: f64::from(self.read_ram_with_retires(PlRamAddress::Lint).await?)
|
||||||
|
* (2. / 10.),
|
||||||
|
battery_temp: f64::from(self.read_ram_with_retires(PlRamAddress::Battemp).await?),
|
||||||
|
regulator_state: self
|
||||||
|
.read_ram_with_retires(PlRamAddress::Rstate)
|
||||||
|
.await?
|
||||||
|
.into(),
|
||||||
|
// internal_charge_ah_accumulator,
|
||||||
|
// external_charge_ah_accumulator,
|
||||||
|
// internal_load_ah_accumulator,
|
||||||
|
// external_load_ah_accumulator,
|
||||||
|
// internal_charge_ah,
|
||||||
|
// external_charge_ah,
|
||||||
|
// internal_load_ah,
|
||||||
|
// external_load_ah,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn send_command(&mut self, req: [u8; 4]) -> eyre::Result<()> {
|
||||||
|
self.flush()?;
|
||||||
|
self.port.write_all(&req).await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn flush(&self) -> eyre::Result<()> {
|
||||||
|
self.port.clear(tokio_serial::ClearBuffer::All)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn receive<const LENGTH: usize>(&mut self) -> eyre::Result<[u8; LENGTH]> {
|
||||||
|
let mut buf = [0; LENGTH];
|
||||||
|
self.port.read_exact(&mut buf).await?;
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_ram_with_retires<T>(&mut self, address: T) -> eyre::Result<u8>
|
||||||
|
where
|
||||||
|
T: Into<u8>,
|
||||||
|
{
|
||||||
|
const READ_TRIES: usize = 3;
|
||||||
|
let address: u8 = address.into();
|
||||||
|
|
||||||
|
let mut last_err = None;
|
||||||
|
for _ in 0..READ_TRIES {
|
||||||
|
match self.read_ram_single(address).await {
|
||||||
|
Ok(v) => return Ok(v),
|
||||||
|
Err(e) => last_err = Some(e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_err.unwrap_or_else(|| {
|
||||||
|
eyre::eyre!(
|
||||||
|
"no error was stored in read_ram_with_retries: this should be unreachable??"
|
||||||
|
)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_ram_single<T>(&mut self, address: T) -> eyre::Result<u8>
|
||||||
|
where
|
||||||
|
T: Into<u8>,
|
||||||
|
{
|
||||||
|
self.send_command(command(20, address.into(), 0)).await?;
|
||||||
|
let response = self.get_response().await?;
|
||||||
|
match response {
|
||||||
|
Ok(()) => Ok(self.receive::<1>().await?[0]),
|
||||||
|
Err(e) => Err(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn write_ram<T>(&mut self, address: T, data: u8) -> eyre::Result<()>
|
||||||
|
where
|
||||||
|
T: Into<u8>,
|
||||||
|
{
|
||||||
|
self.send_command(command(152, address.into(), data))
|
||||||
|
.await?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn read_eeprom<T>(&mut self, address: T) -> eyre::Result<u8>
|
||||||
|
where
|
||||||
|
T: Into<u8>,
|
||||||
|
{
|
||||||
|
self.send_command(command(72, address.into(), 0)).await?;
|
||||||
|
|
||||||
|
let response = self.get_response().await?;
|
||||||
|
match response {
|
||||||
|
Ok(()) => Ok(self.receive::<1>().await?[0]),
|
||||||
|
Err(e) => Err(e.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_response(&mut self) -> eyre::Result<Result<(), PlError>> {
|
||||||
|
let res = self.receive::<1>().await?[0];
|
||||||
|
Ok(PlError::parse(res))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(thiserror::Error, Debug)]
|
||||||
|
enum PlError {
|
||||||
|
#[error("No comms or corrupt comms")]
|
||||||
|
NoComms,
|
||||||
|
#[error("Loopback response code")]
|
||||||
|
Loopback,
|
||||||
|
#[error("Timeout error")]
|
||||||
|
Timeout,
|
||||||
|
#[error("Checksum error in PLI receive data.")]
|
||||||
|
Checksum,
|
||||||
|
#[error("Command received by PLI is not recognised.")]
|
||||||
|
CommandNotRecognised,
|
||||||
|
#[error("Unused - or could be returning PL40 version!")]
|
||||||
|
Unused1,
|
||||||
|
#[error("Processor did not receive a reply to request.")]
|
||||||
|
NoReply,
|
||||||
|
#[error("Error in reply from PL.")]
|
||||||
|
ErrorFromPl,
|
||||||
|
#[error("<not used> #2")]
|
||||||
|
Unused2,
|
||||||
|
#[error("<not used> #3")]
|
||||||
|
Unused3,
|
||||||
|
#[error("Unknown: {0:#X?}")]
|
||||||
|
Unknown(u8),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PlError {
|
||||||
|
const fn parse(value: u8) -> Result<(), Self> {
|
||||||
|
match value {
|
||||||
|
200 => Ok(()),
|
||||||
|
5 => Err(Self::NoComms),
|
||||||
|
128 => Err(Self::Loopback),
|
||||||
|
129 => Err(Self::Timeout),
|
||||||
|
130 => Err(Self::Checksum),
|
||||||
|
131 => Err(Self::CommandNotRecognised),
|
||||||
|
132 => Err(Self::Unused1),
|
||||||
|
133 => Err(Self::NoReply),
|
||||||
|
134 => Err(Self::ErrorFromPl),
|
||||||
|
135 => Err(Self::Unused2),
|
||||||
|
136 => Err(Self::Unused3),
|
||||||
|
v => Err(Self::Unknown(v)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
enum PlRamAddress {
|
||||||
|
Dutycyc,
|
||||||
|
Sec,
|
||||||
|
Min,
|
||||||
|
Hour,
|
||||||
|
Batv,
|
||||||
|
Battemp,
|
||||||
|
Rstate,
|
||||||
|
Vreg,
|
||||||
|
Cint,
|
||||||
|
Lint,
|
||||||
|
Ciacc1,
|
||||||
|
Ciacc2,
|
||||||
|
Ciacc3,
|
||||||
|
Ciahl,
|
||||||
|
Ciahh,
|
||||||
|
Ceacc1,
|
||||||
|
Ceacc2,
|
||||||
|
Ceacc3,
|
||||||
|
Ceahl,
|
||||||
|
Ceahh,
|
||||||
|
Liacc1,
|
||||||
|
Liacc2,
|
||||||
|
Liacc3,
|
||||||
|
Liahl,
|
||||||
|
Liahh,
|
||||||
|
Leacc1,
|
||||||
|
Leacc2,
|
||||||
|
Leacc3,
|
||||||
|
Leahl,
|
||||||
|
Leahh,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PlRamAddress> for u8 {
|
||||||
|
fn from(value: PlRamAddress) -> Self {
|
||||||
|
match value {
|
||||||
|
PlRamAddress::Dutycyc => 39,
|
||||||
|
PlRamAddress::Sec => 46,
|
||||||
|
PlRamAddress::Min => 47,
|
||||||
|
PlRamAddress::Hour => 48,
|
||||||
|
PlRamAddress::Batv => 50,
|
||||||
|
PlRamAddress::Battemp => 52,
|
||||||
|
PlRamAddress::Rstate => 101,
|
||||||
|
PlRamAddress::Vreg => 105,
|
||||||
|
PlRamAddress::Cint => 213,
|
||||||
|
PlRamAddress::Lint => 217,
|
||||||
|
PlRamAddress::Ciacc1 => 0xB9,
|
||||||
|
PlRamAddress::Ciacc2 => 0xBA,
|
||||||
|
PlRamAddress::Ciacc3 => 0xBB,
|
||||||
|
PlRamAddress::Ciahl => 0xBC,
|
||||||
|
PlRamAddress::Ciahh => 0xBD,
|
||||||
|
PlRamAddress::Ceacc1 => 0xBE,
|
||||||
|
PlRamAddress::Ceacc2 => 0xBF,
|
||||||
|
PlRamAddress::Ceacc3 => 0xC0,
|
||||||
|
PlRamAddress::Ceahl => 0xC1,
|
||||||
|
PlRamAddress::Ceahh => 0xC2,
|
||||||
|
PlRamAddress::Liacc1 => 0xC3,
|
||||||
|
PlRamAddress::Liacc2 => 0xC4,
|
||||||
|
PlRamAddress::Liacc3 => 0xC5,
|
||||||
|
PlRamAddress::Liahl => 0xC6,
|
||||||
|
PlRamAddress::Liahh => 0xC7,
|
||||||
|
PlRamAddress::Leacc1 => 0xC8,
|
||||||
|
PlRamAddress::Leacc2 => 0xC9,
|
||||||
|
PlRamAddress::Leacc3 => 0xCA,
|
||||||
|
PlRamAddress::Leahl => 0xCB,
|
||||||
|
PlRamAddress::Leahh => 0xCC,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
enum PlEepromAddress {
|
||||||
|
// calibration
|
||||||
|
BCals,
|
||||||
|
BCal12,
|
||||||
|
BCal24,
|
||||||
|
BCal48,
|
||||||
|
ChargeOffset,
|
||||||
|
ChargeGain,
|
||||||
|
LoadOffset,
|
||||||
|
LoadGain,
|
||||||
|
BatTmpOffset,
|
||||||
|
BatTmpGain,
|
||||||
|
SolarOffset,
|
||||||
|
SolarGain,
|
||||||
|
BatsenOffset,
|
||||||
|
BatsenGain,
|
||||||
|
|
||||||
|
// settings
|
||||||
|
GOn,
|
||||||
|
GOff,
|
||||||
|
GDel,
|
||||||
|
GExF,
|
||||||
|
GRun,
|
||||||
|
LOff,
|
||||||
|
LOn,
|
||||||
|
LDel,
|
||||||
|
ASet,
|
||||||
|
BstFreq,
|
||||||
|
ATim,
|
||||||
|
Hyst,
|
||||||
|
BRet,
|
||||||
|
CurLim,
|
||||||
|
Bat2,
|
||||||
|
ESet1,
|
||||||
|
ESet2,
|
||||||
|
ESet3,
|
||||||
|
EqFreq,
|
||||||
|
ETim,
|
||||||
|
AbsV,
|
||||||
|
EMax,
|
||||||
|
FltV,
|
||||||
|
BMax,
|
||||||
|
LGSet,
|
||||||
|
PwmE,
|
||||||
|
SStop,
|
||||||
|
EtMod,
|
||||||
|
GMod,
|
||||||
|
Volt,
|
||||||
|
BCap,
|
||||||
|
HistoryDataPtr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<PlEepromAddress> for u8 {
|
||||||
|
fn from(value: PlEepromAddress) -> Self {
|
||||||
|
match value {
|
||||||
|
PlEepromAddress::BCals => 0x00,
|
||||||
|
PlEepromAddress::BCal12 => 0x01,
|
||||||
|
PlEepromAddress::BCal24 => 0x02,
|
||||||
|
PlEepromAddress::BCal48 => 0x03,
|
||||||
|
PlEepromAddress::ChargeOffset => 0x04,
|
||||||
|
PlEepromAddress::ChargeGain => 0x05,
|
||||||
|
PlEepromAddress::LoadOffset => 0x06,
|
||||||
|
PlEepromAddress::LoadGain => 0x07,
|
||||||
|
PlEepromAddress::BatTmpOffset => 0x08,
|
||||||
|
PlEepromAddress::BatTmpGain => 0x09,
|
||||||
|
PlEepromAddress::SolarOffset => 0x0A,
|
||||||
|
PlEepromAddress::SolarGain => 0x0B,
|
||||||
|
PlEepromAddress::BatsenOffset => 0x0C,
|
||||||
|
PlEepromAddress::BatsenGain => 0x0D,
|
||||||
|
PlEepromAddress::GOn => 0x0E,
|
||||||
|
PlEepromAddress::GOff => 0x0F,
|
||||||
|
PlEepromAddress::GDel => 0x10,
|
||||||
|
PlEepromAddress::GExF => 0x11,
|
||||||
|
PlEepromAddress::GRun => 0x12,
|
||||||
|
PlEepromAddress::LOff => 0x13,
|
||||||
|
PlEepromAddress::LOn => 0x14,
|
||||||
|
PlEepromAddress::LDel => 0x15,
|
||||||
|
PlEepromAddress::ASet => 0x16,
|
||||||
|
PlEepromAddress::BstFreq => 0x17,
|
||||||
|
PlEepromAddress::ATim => 0x18,
|
||||||
|
PlEepromAddress::Hyst => 0x19,
|
||||||
|
PlEepromAddress::BRet => 0x1A,
|
||||||
|
PlEepromAddress::CurLim => 0x1B,
|
||||||
|
PlEepromAddress::Bat2 => 0x1C,
|
||||||
|
PlEepromAddress::ESet1 => 0x1D,
|
||||||
|
PlEepromAddress::ESet2 => 0x1E,
|
||||||
|
PlEepromAddress::ESet3 => 0x1F,
|
||||||
|
PlEepromAddress::EqFreq => 0x20,
|
||||||
|
PlEepromAddress::ETim => 0x21,
|
||||||
|
PlEepromAddress::AbsV => 0x22,
|
||||||
|
PlEepromAddress::EMax => 0x23,
|
||||||
|
PlEepromAddress::FltV => 0x24,
|
||||||
|
PlEepromAddress::BMax => 0x25,
|
||||||
|
PlEepromAddress::LGSet => 0x26,
|
||||||
|
PlEepromAddress::PwmE => 0x27,
|
||||||
|
PlEepromAddress::SStop => 0x28,
|
||||||
|
PlEepromAddress::EtMod => 0x29,
|
||||||
|
PlEepromAddress::GMod => 0x2A,
|
||||||
|
PlEepromAddress::Volt => 0x2B,
|
||||||
|
PlEepromAddress::BCap => 0x2c,
|
||||||
|
PlEepromAddress::HistoryDataPtr => 0x2d,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn command(operation: u8, address: u8, data: u8) -> [u8; 4] {
|
||||||
|
[operation, address, data, !operation]
|
||||||
|
}
|
1001
charge-controller-supervisor/src/controller/tristar.rs
Normal file
1001
charge-controller-supervisor/src/controller/tristar.rs
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,192 @@
|
||||||
|
pub struct ModbusTimeout {
|
||||||
|
context: Option<tokio_modbus::client::Context>,
|
||||||
|
transport_settings: crate::config::Transport,
|
||||||
|
counters: Counters,
|
||||||
|
}
|
||||||
|
|
||||||
|
const MODBUS_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(3);
|
||||||
|
|
||||||
|
async fn connect(
|
||||||
|
transport_settings: &crate::config::Transport,
|
||||||
|
) -> eyre::Result<tokio_modbus::client::Context> {
|
||||||
|
let slave = tokio_modbus::Slave(super::DEVICE_ID);
|
||||||
|
|
||||||
|
let modbus = match transport_settings {
|
||||||
|
crate::config::Transport::Serial { port, baud_rate } => {
|
||||||
|
let modbus_serial =
|
||||||
|
tokio_serial::SerialStream::open(&tokio_serial::new(port, *baud_rate))?;
|
||||||
|
tokio_modbus::client::rtu::attach_slave(modbus_serial, slave)
|
||||||
|
}
|
||||||
|
crate::config::Transport::Tcp { ip, port } => {
|
||||||
|
let modbus_tcp = tokio::net::TcpStream::connect((*ip, *port)).await?;
|
||||||
|
tokio_modbus::client::tcp::attach_slave(modbus_tcp, slave)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(modbus)
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModbusDeviceResult<T> = Result<T, tokio_modbus::ExceptionCode>;
|
||||||
|
type ModbusResult<T> = Result<ModbusDeviceResult<T>, tokio_modbus::Error>;
|
||||||
|
|
||||||
|
type ContextFuture<'a, R> = dyn std::future::Future<Output = ModbusResult<R>> + Send + 'a;
|
||||||
|
type ContextFn<R, D> =
|
||||||
|
fn(&mut tokio_modbus::client::Context, D) -> std::pin::Pin<Box<ContextFuture<'_, R>>>;
|
||||||
|
|
||||||
|
trait TryInsert {
|
||||||
|
type T;
|
||||||
|
async fn get_or_try_insert_with<F, R, Fut>(&mut self, f: F) -> Result<&mut Self::T, R>
|
||||||
|
where
|
||||||
|
Fut: std::future::Future<Output = Result<Self::T, R>>,
|
||||||
|
F: FnOnce() -> Fut;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> TryInsert for Option<T> {
|
||||||
|
type T = T;
|
||||||
|
|
||||||
|
async fn get_or_try_insert_with<F, R, Fut>(&mut self, f: F) -> Result<&mut Self::T, R>
|
||||||
|
where
|
||||||
|
Fut: std::future::Future<Output = Result<Self::T, R>>,
|
||||||
|
F: FnOnce() -> Fut,
|
||||||
|
{
|
||||||
|
if self.is_none() {
|
||||||
|
let got = f().await?;
|
||||||
|
*self = Some(got);
|
||||||
|
}
|
||||||
|
|
||||||
|
// a `None` variant for `self` would have been replaced by a `Some` variant
|
||||||
|
// in the code above, or the ? would have caused an early return
|
||||||
|
Ok(self.as_mut().unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
struct Counters {
|
||||||
|
gateway: usize,
|
||||||
|
timeout: usize,
|
||||||
|
protocol: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAX_ERRORS: usize = 2;
|
||||||
|
|
||||||
|
impl Counters {
|
||||||
|
fn reset(&mut self) {
|
||||||
|
*self = Self::default();
|
||||||
|
}
|
||||||
|
|
||||||
|
const fn any_above_max(&self) -> bool {
|
||||||
|
self.gateway > MAX_ERRORS || self.timeout > MAX_ERRORS || self.protocol > MAX_ERRORS
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const NUM_TRIES: usize = 3;
|
||||||
|
|
||||||
|
impl ModbusTimeout {
|
||||||
|
pub async fn new(transport_settings: crate::config::Transport) -> eyre::Result<Self> {
|
||||||
|
let context = Some(connect(&transport_settings).await?);
|
||||||
|
Ok(Self {
|
||||||
|
context,
|
||||||
|
transport_settings,
|
||||||
|
counters: Counters::default(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn with_context<R, D: Copy>(
|
||||||
|
&mut self,
|
||||||
|
f: ContextFn<R, D>,
|
||||||
|
data: D,
|
||||||
|
) -> eyre::Result<Result<R, tokio_modbus::ExceptionCode>> {
|
||||||
|
let mut last_err = None;
|
||||||
|
for _ in 0..NUM_TRIES {
|
||||||
|
if let Ok(context) = self
|
||||||
|
.context
|
||||||
|
.get_or_try_insert_with(async || connect(&self.transport_settings).await)
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
let res = tokio::time::timeout(MODBUS_TIMEOUT, f(context, data)).await;
|
||||||
|
match res {
|
||||||
|
Ok(Ok(Err(e)))
|
||||||
|
if e == tokio_modbus::ExceptionCode::GatewayTargetDevice
|
||||||
|
|| e == tokio_modbus::ExceptionCode::GatewayPathUnavailable =>
|
||||||
|
{
|
||||||
|
log::warn!("gateway error: {e:?}");
|
||||||
|
last_err = Some(e.into());
|
||||||
|
self.counters.gateway += 1;
|
||||||
|
}
|
||||||
|
Ok(Ok(v)) => {
|
||||||
|
self.counters.reset();
|
||||||
|
return Ok(v);
|
||||||
|
}
|
||||||
|
Ok(Err(tokio_modbus::Error::Protocol(e))) => {
|
||||||
|
// protocol error
|
||||||
|
log::warn!("protocol error: {e:?}");
|
||||||
|
last_err = Some(e.into());
|
||||||
|
self.counters.protocol += 1;
|
||||||
|
}
|
||||||
|
Ok(Err(tokio_modbus::Error::Transport(e))) => {
|
||||||
|
// transport error
|
||||||
|
log::warn!("reconnecting due to transport error: {e:?}");
|
||||||
|
last_err = Some(e.into());
|
||||||
|
self.context = None;
|
||||||
|
}
|
||||||
|
Err(_) => {
|
||||||
|
// timeout
|
||||||
|
last_err = Some(eyre::eyre!("timeout"));
|
||||||
|
self.counters.timeout += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if self.counters.any_above_max() {
|
||||||
|
self.context = None;
|
||||||
|
log::warn!(
|
||||||
|
"reconnecting due to multiple errors without a successful operation: {:?}",
|
||||||
|
self.counters
|
||||||
|
);
|
||||||
|
self.counters.reset();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// failed to reconnect
|
||||||
|
return Err(eyre::eyre!("failed to reconnect to controller"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(last_err.unwrap_or_else(|| eyre::eyre!("unknown last error????")))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn write_single_register(
|
||||||
|
&mut self,
|
||||||
|
addr: tokio_modbus::Address,
|
||||||
|
word: u16,
|
||||||
|
) -> eyre::Result<ModbusDeviceResult<()>> {
|
||||||
|
async fn write(
|
||||||
|
context: &mut tokio_modbus::client::Context,
|
||||||
|
addr: tokio_modbus::Address,
|
||||||
|
word: u16,
|
||||||
|
) -> ModbusResult<()> {
|
||||||
|
use tokio_modbus::client::Writer;
|
||||||
|
context.write_single_register(addr, word).await
|
||||||
|
}
|
||||||
|
|
||||||
|
let fut: ContextFn<(), _> = |context, (addr, word)| Box::pin(write(context, addr, word));
|
||||||
|
let r = self.with_context(fut, (addr, word)).await?;
|
||||||
|
Ok(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_holding_registers(
|
||||||
|
&mut self,
|
||||||
|
addr: tokio_modbus::Address,
|
||||||
|
cnt: tokio_modbus::Quantity,
|
||||||
|
) -> eyre::Result<ModbusDeviceResult<Vec<u16>>> {
|
||||||
|
async fn read(
|
||||||
|
context: &mut tokio_modbus::client::Context,
|
||||||
|
addr: tokio_modbus::Address,
|
||||||
|
cnt: tokio_modbus::Quantity,
|
||||||
|
) -> ModbusResult<Vec<u16>> {
|
||||||
|
use tokio_modbus::client::Reader;
|
||||||
|
context.read_holding_registers(addr, cnt).await
|
||||||
|
}
|
||||||
|
|
||||||
|
let fut: ContextFn<_, _> = |context, (addr, cnt)| Box::pin(read(context, addr, cnt));
|
||||||
|
let res = self.with_context(fut, (addr, cnt)).await?;
|
||||||
|
Ok(res)
|
||||||
|
}
|
||||||
|
}
|
|
@ -25,8 +25,8 @@ enum Commands {
|
||||||
|
|
||||||
mod controller;
|
mod controller;
|
||||||
mod gauges;
|
mod gauges;
|
||||||
mod pl;
|
|
||||||
mod tristar;
|
mod storage;
|
||||||
|
|
||||||
mod web;
|
mod web;
|
||||||
|
|
||||||
|
@ -59,7 +59,32 @@ async fn run() -> eyre::Result<()> {
|
||||||
match args.command {
|
match args.command {
|
||||||
Commands::Watch => watch(args).await,
|
Commands::Watch => watch(args).await,
|
||||||
Commands::GenerateConfig => {
|
Commands::GenerateConfig => {
|
||||||
let config = config::Config::default();
|
let mut config = config::Config::default();
|
||||||
|
config
|
||||||
|
.charge_controllers
|
||||||
|
.push(config::ChargeControllerConfig {
|
||||||
|
name: String::from("tcp"),
|
||||||
|
transport: config::Transport::Tcp {
|
||||||
|
ip: std::net::IpAddr::V4(std::net::Ipv4Addr::new(192, 168, 1, 102)),
|
||||||
|
port: 420,
|
||||||
|
},
|
||||||
|
watch_interval_seconds: 0,
|
||||||
|
variant: config::ChargeControllerVariant::Tristar,
|
||||||
|
follow_primary: false,
|
||||||
|
});
|
||||||
|
config
|
||||||
|
.charge_controllers
|
||||||
|
.push(config::ChargeControllerConfig {
|
||||||
|
name: String::from("serial"),
|
||||||
|
transport: config::Transport::Serial {
|
||||||
|
port: "/dev/someport".to_string(),
|
||||||
|
baud_rate: 69,
|
||||||
|
},
|
||||||
|
watch_interval_seconds: 0,
|
||||||
|
variant: config::ChargeControllerVariant::Tristar,
|
||||||
|
follow_primary: false,
|
||||||
|
});
|
||||||
|
let config = config::ConfigStorage::from_latest(config);
|
||||||
let json = serde_json::to_string_pretty(&config)?;
|
let json = serde_json::to_string_pretty(&config)?;
|
||||||
println!("{json}");
|
println!("{json}");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -69,7 +94,7 @@ async fn run() -> eyre::Result<()> {
|
||||||
|
|
||||||
async fn watch(args: Args) -> eyre::Result<()> {
|
async fn watch(args: Args) -> eyre::Result<()> {
|
||||||
let _w = config::init_config(&args.config);
|
let _w = config::init_config(&args.config);
|
||||||
let (map, follow_voltage_tx, mut controller_tasks) = {
|
let (storage, follow_voltage_tx, mut controller_tasks) = {
|
||||||
let config = config::access_config().await;
|
let config = config::access_config().await;
|
||||||
if config
|
if config
|
||||||
.charge_controllers
|
.charge_controllers
|
||||||
|
@ -83,45 +108,49 @@ async fn watch(args: Args) -> eyre::Result<()> {
|
||||||
|
|
||||||
let mut controllers = Vec::new();
|
let mut controllers = Vec::new();
|
||||||
let mut map = std::collections::HashMap::new();
|
let mut map = std::collections::HashMap::new();
|
||||||
let mut follow_voltage_tx = Vec::new();
|
|
||||||
|
let (voltage_tx, voltage_rx) =
|
||||||
|
tokio::sync::watch::channel(controller::VoltageCommand::None);
|
||||||
|
|
||||||
for config in &config.charge_controllers {
|
for config in &config.charge_controllers {
|
||||||
let n = config.name.clone();
|
let n = config.name.clone();
|
||||||
match controller::Controller::new(config.clone()) {
|
match controller::Controller::new(config.clone(), voltage_rx.clone()).await {
|
||||||
Ok((v, voltage_tx)) => {
|
Ok(v) => {
|
||||||
map.insert(n, v.get_data_ptr());
|
map.insert(n, v.get_data_ptr());
|
||||||
controllers.push(v);
|
controllers.push(v);
|
||||||
if let Some(voltage_tx) = voltage_tx {
|
|
||||||
follow_voltage_tx.push(voltage_tx);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(e) => log::error!("couldn't connect to {}: {e:?}", n),
|
Err(e) => log::error!("couldn't connect to {}: {e:?}", n),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let follow_voltage_tx = controller::MultiTx(follow_voltage_tx);
|
|
||||||
|
|
||||||
if let Some(primary) = controllers
|
if let Some(primary) = controllers
|
||||||
.iter_mut()
|
.iter_mut()
|
||||||
.find(|c| c.name() == config.primary_charge_controller)
|
.find(|c| c.name() == config.primary_charge_controller)
|
||||||
{
|
{
|
||||||
primary.set_tx_to_secondary(follow_voltage_tx.clone());
|
primary.set_tx_to_secondary(voltage_tx.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drop(config);
|
||||||
|
|
||||||
let controller_tasks = futures::stream::FuturesUnordered::new();
|
let controller_tasks = futures::stream::FuturesUnordered::new();
|
||||||
for controller in controllers {
|
for controller in controllers {
|
||||||
controller_tasks.push(run_loop(controller));
|
controller_tasks.push(run_loop(controller));
|
||||||
}
|
}
|
||||||
|
|
||||||
(map, follow_voltage_tx, controller_tasks)
|
(
|
||||||
|
storage::AllControllers::new(map),
|
||||||
|
voltage_tx,
|
||||||
|
controller_tasks,
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let server = web::rocket(web::ServerState::new(
|
let server = web::rocket(web::ServerState::new(
|
||||||
&config::access_config().await.primary_charge_controller,
|
&config::access_config().await.primary_charge_controller,
|
||||||
map,
|
storage,
|
||||||
follow_voltage_tx,
|
follow_voltage_tx,
|
||||||
));
|
));
|
||||||
let server_task = tokio::task::spawn(server.launch());
|
let server_task = tokio::task::spawn(server.launch());
|
||||||
|
log::warn!("...started!");
|
||||||
|
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
v = controller_tasks.next() => {
|
v = controller_tasks.next() => {
|
||||||
|
@ -149,22 +178,20 @@ async fn watch(args: Args) -> eyre::Result<()> {
|
||||||
|
|
||||||
async fn run_loop(mut controller: controller::Controller) -> eyre::Result<()> {
|
async fn run_loop(mut controller: controller::Controller) -> eyre::Result<()> {
|
||||||
let mut timeout = tokio::time::interval(controller.timeout_interval());
|
let mut timeout = tokio::time::interval(controller.timeout_interval());
|
||||||
|
timeout.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
if let Some(rx) = controller.get_rx() {
|
let rx = controller.get_rx();
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = timeout.tick() => {
|
_ = timeout.tick() => {
|
||||||
do_refresh(&mut controller).await;
|
do_refresh(&mut controller).await;
|
||||||
}
|
}
|
||||||
Some(command) = rx.recv() => {
|
Ok(()) = rx.changed() => {
|
||||||
if let Err(e) = controller.process_command(command).await {
|
let command = *rx.borrow();
|
||||||
log::error!("controller {} failed to process command: {e}", controller.name());
|
if let Err(e) = controller.process_command(command).await {
|
||||||
}
|
log::error!("controller {} failed to process command: {e}", controller.name());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
timeout.tick().await;
|
|
||||||
do_refresh(&mut controller).await;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,354 +0,0 @@
|
||||||
use chrono::Timelike;
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
||||||
use tokio_serial::{SerialPort, SerialPortBuilderExt};
|
|
||||||
|
|
||||||
use crate::gauges::{
|
|
||||||
BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, INPUT_CURRENT, PL_DUTY_CYCLE, PL_LOAD_CURRENT,
|
|
||||||
TARGET_VOLTAGE,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub struct Pli {
|
|
||||||
friendly_name: String,
|
|
||||||
port: tokio_serial::SerialStream,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
|
|
||||||
pub struct PlState {
|
|
||||||
pub battery_voltage: f64,
|
|
||||||
pub target_voltage: f64,
|
|
||||||
pub duty_cycle: f64,
|
|
||||||
pub internal_charge_current: f64,
|
|
||||||
pub internal_load_current: f64,
|
|
||||||
pub battery_temp: f64,
|
|
||||||
pub regulator_state: RegulatorState,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
||||||
pub enum RegulatorState {
|
|
||||||
Boost,
|
|
||||||
Equalise,
|
|
||||||
Absorption,
|
|
||||||
Float,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<u8> for RegulatorState {
|
|
||||||
fn from(value: u8) -> Self {
|
|
||||||
match value & 0b11 {
|
|
||||||
0b00 => Self::Boost,
|
|
||||||
0b01 => Self::Equalise,
|
|
||||||
0b10 => Self::Absorption,
|
|
||||||
0b11 => Self::Float,
|
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<RegulatorState> for u8 {
|
|
||||||
fn from(value: RegulatorState) -> Self {
|
|
||||||
match value {
|
|
||||||
RegulatorState::Boost => 0b00,
|
|
||||||
RegulatorState::Equalise => 0b01,
|
|
||||||
RegulatorState::Absorption => 0b10,
|
|
||||||
RegulatorState::Float => 0b11,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for RegulatorState {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Absorption
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_regulator_gauges(state: RegulatorState, label: &str) {
|
|
||||||
let boost = CHARGE_STATE.with_label_values(&[label, "boost"]);
|
|
||||||
let equalise = CHARGE_STATE.with_label_values(&[label, "equalise"]);
|
|
||||||
let absorption = CHARGE_STATE.with_label_values(&[label, "absorption"]);
|
|
||||||
let float = CHARGE_STATE.with_label_values(&[label, "float"]);
|
|
||||||
|
|
||||||
match state {
|
|
||||||
RegulatorState::Boost => {
|
|
||||||
boost.set(1);
|
|
||||||
equalise.set(0);
|
|
||||||
absorption.set(0);
|
|
||||||
float.set(0);
|
|
||||||
}
|
|
||||||
RegulatorState::Equalise => {
|
|
||||||
boost.set(0);
|
|
||||||
equalise.set(1);
|
|
||||||
absorption.set(0);
|
|
||||||
float.set(0);
|
|
||||||
}
|
|
||||||
RegulatorState::Absorption => {
|
|
||||||
boost.set(0);
|
|
||||||
equalise.set(0);
|
|
||||||
absorption.set(1);
|
|
||||||
float.set(0);
|
|
||||||
}
|
|
||||||
RegulatorState::Float => {
|
|
||||||
boost.set(0);
|
|
||||||
equalise.set(0);
|
|
||||||
absorption.set(0);
|
|
||||||
float.set(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[expect(dead_code, reason = "writing settings is not yet implemented")]
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub enum PliRequest {
|
|
||||||
ReadRam(u8),
|
|
||||||
ReadEeprom(u8),
|
|
||||||
SyncTime,
|
|
||||||
SetRegulatorState(RegulatorState),
|
|
||||||
}
|
|
||||||
|
|
||||||
static SHOULD_SHOW_ACCUMULATORS: std::sync::LazyLock<bool> =
|
|
||||||
std::sync::LazyLock::new(|| std::env::var("SHOULD_SHOW_ACCUMULATORS").is_ok());
|
|
||||||
|
|
||||||
impl Pli {
|
|
||||||
pub fn new(
|
|
||||||
serial_port: &str,
|
|
||||||
friendly_name: &str,
|
|
||||||
baud_rate: u32,
|
|
||||||
timeout: u64,
|
|
||||||
) -> Result<Self, tokio_serial::Error> {
|
|
||||||
let port = tokio_serial::new(serial_port, baud_rate)
|
|
||||||
.timeout(std::time::Duration::from_millis(timeout))
|
|
||||||
.open_native_async()?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
friendly_name: friendly_name.to_owned(),
|
|
||||||
port,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn refresh(&mut self) -> eyre::Result<crate::controller::CommonData> {
|
|
||||||
let new_state = self.read_state().await?;
|
|
||||||
BATTERY_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.battery_voltage);
|
|
||||||
TARGET_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.target_voltage);
|
|
||||||
PL_DUTY_CYCLE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.duty_cycle);
|
|
||||||
INPUT_CURRENT
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.internal_charge_current);
|
|
||||||
PL_LOAD_CURRENT
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.internal_load_current);
|
|
||||||
BATTERY_TEMP
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.battery_temp);
|
|
||||||
|
|
||||||
set_regulator_gauges(new_state.regulator_state, &self.friendly_name);
|
|
||||||
|
|
||||||
Ok(crate::controller::CommonData {
|
|
||||||
battery_voltage: new_state.battery_voltage,
|
|
||||||
target_voltage: new_state.target_voltage,
|
|
||||||
battery_temp: new_state.battery_temp,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
#[expect(dead_code, reason = "writing settings is not yet implemented")]
|
|
||||||
pub async fn process_request(&mut self, message: PliRequest) -> eyre::Result<()> {
|
|
||||||
match message {
|
|
||||||
PliRequest::ReadRam(address) => {
|
|
||||||
let data = self.read_ram(address).await?;
|
|
||||||
log::warn!("Read RAM at {address}: data {data}");
|
|
||||||
}
|
|
||||||
PliRequest::ReadEeprom(address) => {
|
|
||||||
let data = self.read_eeprom(address).await?;
|
|
||||||
log::warn!("Read EEPROM at {address}: data {data}");
|
|
||||||
}
|
|
||||||
PliRequest::SyncTime => {
|
|
||||||
let now = chrono::Local::now();
|
|
||||||
let timestamp = (((now.hour() * 10) + (now.minute() / 6)) & 0xFF) as u8;
|
|
||||||
let min = (now.minute() % 6) as u8;
|
|
||||||
let sec = (now.second() / 2).min(29) as u8;
|
|
||||||
self.write_ram(PlRamAddress::Hour, timestamp).await?;
|
|
||||||
self.write_ram(PlRamAddress::Min, min).await?;
|
|
||||||
self.write_ram(PlRamAddress::Sec, sec).await?;
|
|
||||||
log::warn!(
|
|
||||||
"Set time: {now} corresponds to {timestamp} + minutes {min} + seconds {sec}"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
PliRequest::SetRegulatorState(state) => {
|
|
||||||
log::warn!("Setting regulator state to {state:?}");
|
|
||||||
self.write_ram(PlRamAddress::Rstate, state.into()).await?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn read_state(&mut self) -> eyre::Result<PlState> {
|
|
||||||
if *SHOULD_SHOW_ACCUMULATORS {
|
|
||||||
let int_acc_lsb = self.read_ram(PlRamAddress::Ciacc1).await?;
|
|
||||||
let int_acc = self.read_ram(PlRamAddress::Ciacc2).await?;
|
|
||||||
let int_acc_msb = self.read_ram(PlRamAddress::Ciacc3).await?;
|
|
||||||
println!("internal charge ah accumulator: lsb {int_acc_lsb:#X?}, middle: {int_acc:#X?}, msb: {int_acc_msb:#X?}");
|
|
||||||
let mut internal_charge_ah_accumulator = u16::from(int_acc_msb) << 9;
|
|
||||||
internal_charge_ah_accumulator |= u16::from(int_acc) << 1;
|
|
||||||
internal_charge_ah_accumulator |= u16::from(int_acc_lsb & 0b1);
|
|
||||||
println!("\t\t-->which is: {internal_charge_ah_accumulator:#X?}");
|
|
||||||
let int_charge_low = self.read_ram(PlRamAddress::Ciahl).await?;
|
|
||||||
let int_charge_high = self.read_ram(PlRamAddress::Ciahh).await?;
|
|
||||||
let int_charge = u16::from_le_bytes([int_charge_low, int_charge_high]);
|
|
||||||
println!("internal charge ah: low {int_charge_low:#X?}, high {int_charge_high:#X?}, total: {int_charge}Ah");
|
|
||||||
|
|
||||||
let int_load_acc_lsb = self.read_ram(PlRamAddress::Liacc1).await?;
|
|
||||||
let int_load_acc = self.read_ram(PlRamAddress::Liacc2).await?;
|
|
||||||
let int_load_acc_msb = self.read_ram(PlRamAddress::Liacc3).await?;
|
|
||||||
println!("internal charge ah accumulator: lsb {int_load_acc_lsb:#X?}, middle: {int_load_acc:#X?}, msb: {int_load_acc_msb:#X?}");
|
|
||||||
let mut internal_load_ah_accumulator = u16::from(int_load_acc_msb) << 9;
|
|
||||||
internal_load_ah_accumulator |= u16::from(int_load_acc) << 1;
|
|
||||||
internal_load_ah_accumulator |= u16::from(int_load_acc_lsb & 0b1);
|
|
||||||
println!("\t\t-->which is: {internal_load_ah_accumulator:#X?}");
|
|
||||||
let int_load_low = self.read_ram(PlRamAddress::Liahl).await?;
|
|
||||||
let int_load_high = self.read_ram(PlRamAddress::Liahh).await?;
|
|
||||||
let int_load = u16::from_le_bytes([int_load_low, int_load_high]);
|
|
||||||
println!("internal charge ah: low {int_load_low:#X?}, high {int_load_high:#X?}, total: {int_load}Ah");
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(PlState {
|
|
||||||
battery_voltage: f64::from(self.read_ram(PlRamAddress::Batv).await?) * (4. / 10.),
|
|
||||||
target_voltage: f64::from(self.read_ram(PlRamAddress::Vreg).await?) * (4. / 10.),
|
|
||||||
duty_cycle: f64::from(self.read_ram(PlRamAddress::Dutycyc).await?) / 255.,
|
|
||||||
internal_charge_current: f64::from(self.read_ram(PlRamAddress::Cint).await?)
|
|
||||||
* (4. / 10.),
|
|
||||||
internal_load_current: f64::from(self.read_ram(PlRamAddress::Lint).await?) * (2. / 10.),
|
|
||||||
battery_temp: f64::from(self.read_ram(PlRamAddress::Battemp).await?),
|
|
||||||
regulator_state: self.read_ram(PlRamAddress::Rstate).await?.into(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn send_command(&mut self, req: [u8; 4]) -> eyre::Result<()> {
|
|
||||||
self.flush()?;
|
|
||||||
self.port.write_all(&req).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn flush(&mut self) -> eyre::Result<()> {
|
|
||||||
self.port.clear(tokio_serial::ClearBuffer::All)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn receive<const LENGTH: usize>(&mut self) -> eyre::Result<[u8; LENGTH]> {
|
|
||||||
let mut buf = [0; LENGTH];
|
|
||||||
self.port.read_exact(&mut buf).await?;
|
|
||||||
Ok(buf)
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn read_ram<T>(&mut self, address: T) -> eyre::Result<u8>
|
|
||||||
where
|
|
||||||
T: Into<u8>,
|
|
||||||
{
|
|
||||||
self.send_command(command(20, address.into(), 0)).await?;
|
|
||||||
let buf = self.receive::<1>().await?;
|
|
||||||
if buf[0] == 200 {
|
|
||||||
Ok(self.receive::<1>().await?[0])
|
|
||||||
} else {
|
|
||||||
Err(eyre::eyre!("read error: result is {}", buf[0]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn write_ram<T>(&mut self, address: T, data: u8) -> eyre::Result<()>
|
|
||||||
where
|
|
||||||
T: Into<u8>,
|
|
||||||
{
|
|
||||||
self.send_command(command(152, address.into(), data))
|
|
||||||
.await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn read_eeprom<T>(&mut self, address: T) -> eyre::Result<u8>
|
|
||||||
where
|
|
||||||
T: Into<u8>,
|
|
||||||
{
|
|
||||||
self.send_command(command(72, address.into(), 0)).await?;
|
|
||||||
let buf = self.receive::<1>().await?;
|
|
||||||
if buf[0] == 200 {
|
|
||||||
Ok(self.receive::<1>().await?[0])
|
|
||||||
} else {
|
|
||||||
Err(eyre::eyre!("read error: result is {}", buf[0]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
enum PlRamAddress {
|
|
||||||
Dutycyc,
|
|
||||||
Sec,
|
|
||||||
Min,
|
|
||||||
Hour,
|
|
||||||
Batv,
|
|
||||||
Battemp,
|
|
||||||
Rstate,
|
|
||||||
Vreg,
|
|
||||||
Cint,
|
|
||||||
Lint,
|
|
||||||
Ciacc1,
|
|
||||||
Ciacc2,
|
|
||||||
Ciacc3,
|
|
||||||
Ciahl,
|
|
||||||
Ciahh,
|
|
||||||
Ceacc1,
|
|
||||||
Ceacc2,
|
|
||||||
Ceacc3,
|
|
||||||
Ceahl,
|
|
||||||
Ceahh,
|
|
||||||
Liacc1,
|
|
||||||
Liacc2,
|
|
||||||
Liacc3,
|
|
||||||
Liahl,
|
|
||||||
Liahh,
|
|
||||||
Leacc1,
|
|
||||||
Leacc2,
|
|
||||||
Leacc3,
|
|
||||||
Leahl,
|
|
||||||
Leahh,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<PlRamAddress> for u8 {
|
|
||||||
fn from(value: PlRamAddress) -> Self {
|
|
||||||
match value {
|
|
||||||
PlRamAddress::Dutycyc => 39,
|
|
||||||
PlRamAddress::Sec => 46,
|
|
||||||
PlRamAddress::Min => 47,
|
|
||||||
PlRamAddress::Hour => 48,
|
|
||||||
PlRamAddress::Batv => 50,
|
|
||||||
PlRamAddress::Battemp => 52,
|
|
||||||
PlRamAddress::Rstate => 101,
|
|
||||||
PlRamAddress::Vreg => 105,
|
|
||||||
PlRamAddress::Cint => 213,
|
|
||||||
PlRamAddress::Lint => 217,
|
|
||||||
PlRamAddress::Ciacc1 => 0xB9,
|
|
||||||
PlRamAddress::Ciacc2 => 0xBA,
|
|
||||||
PlRamAddress::Ciacc3 => 0xBB,
|
|
||||||
PlRamAddress::Ciahl => 0xBC,
|
|
||||||
PlRamAddress::Ciahh => 0xBD,
|
|
||||||
PlRamAddress::Ceacc1 => 0xBE,
|
|
||||||
PlRamAddress::Ceacc2 => 0xBF,
|
|
||||||
PlRamAddress::Ceacc3 => 0xC0,
|
|
||||||
PlRamAddress::Ceahl => 0xC1,
|
|
||||||
PlRamAddress::Ceahh => 0xC2,
|
|
||||||
PlRamAddress::Liacc1 => 0xC3,
|
|
||||||
PlRamAddress::Liacc2 => 0xC4,
|
|
||||||
PlRamAddress::Liacc3 => 0xC5,
|
|
||||||
PlRamAddress::Liahl => 0xC6,
|
|
||||||
PlRamAddress::Liahh => 0xC7,
|
|
||||||
PlRamAddress::Leacc1 => 0xC8,
|
|
||||||
PlRamAddress::Leacc2 => 0xC9,
|
|
||||||
PlRamAddress::Leacc3 => 0xCA,
|
|
||||||
PlRamAddress::Leahl => 0xCB,
|
|
||||||
PlRamAddress::Leahh => 0xCC,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn command(operation: u8, address: u8, data: u8) -> [u8; 4] {
|
|
||||||
[operation, address, data, !operation]
|
|
||||||
}
|
|
61
charge-controller-supervisor/src/storage.rs
Normal file
61
charge-controller-supervisor/src/storage.rs
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
pub struct AllControllers {
|
||||||
|
map: std::collections::HashMap<String, std::sync::Arc<ControllerData>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AllControllers {
|
||||||
|
pub const fn new(
|
||||||
|
map: std::collections::HashMap<String, std::sync::Arc<ControllerData>>,
|
||||||
|
) -> Self {
|
||||||
|
Self { map }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn controller_names(&self) -> impl Iterator<Item = &String> {
|
||||||
|
self.map.keys()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, name: &str) -> Option<&std::sync::Arc<ControllerData>> {
|
||||||
|
self.map.get(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn all_data(&self) -> impl Iterator<Item = (&String, &std::sync::Arc<ControllerData>)> {
|
||||||
|
self.map.iter()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct ControllerData {
|
||||||
|
state: tokio::sync::RwLock<Option<crate::controller::ControllerState>>,
|
||||||
|
settings: tokio::sync::RwLock<Option<crate::controller::ControllerSettings>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ControllerData {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
state: tokio::sync::RwLock::const_new(None),
|
||||||
|
settings: tokio::sync::RwLock::const_new(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn write_state(
|
||||||
|
&self,
|
||||||
|
) -> tokio::sync::RwLockWriteGuard<Option<crate::controller::ControllerState>> {
|
||||||
|
self.state.write().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_state(
|
||||||
|
&self,
|
||||||
|
) -> tokio::sync::RwLockReadGuard<Option<crate::controller::ControllerState>> {
|
||||||
|
self.state.read().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn write_settings(
|
||||||
|
&self,
|
||||||
|
) -> tokio::sync::RwLockWriteGuard<Option<crate::controller::ControllerSettings>> {
|
||||||
|
self.settings.write().await
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_settings(
|
||||||
|
&self,
|
||||||
|
) -> tokio::sync::RwLockReadGuard<Option<crate::controller::ControllerSettings>> {
|
||||||
|
self.settings.read().await
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,426 +0,0 @@
|
||||||
use prometheus::core::{AtomicI64, GenericGauge};
|
|
||||||
use tokio_modbus::client::{Reader, Writer};
|
|
||||||
|
|
||||||
use crate::gauges::{
|
|
||||||
BATTERY_TEMP, BATTERY_VOLTAGE, CHARGE_STATE, HEATSINK_TEMP, INPUT_CURRENT, TARGET_VOLTAGE,
|
|
||||||
TRISTAR_CHARGE_CURRENT, TRISTAR_INPUT_VOLTAGE, TRISTAR_MAX_ARRAY_POWER,
|
|
||||||
TRISTAR_MAX_ARRAY_VOLTAGE, TRISTAR_OPEN_CIRCUIT_VOLTAGE, TRISTAR_POWER_IN, TRISTAR_POWER_OUT,
|
|
||||||
TRISTAR_TOTAL_AH_CHARGE_DAILY, TRISTAR_TOTAL_WH_CHARGE_DAILY,
|
|
||||||
};
|
|
||||||
|
|
||||||
const DEVICE_ID: u8 = 0x01;
|
|
||||||
const RAM_DATA_SIZE: u16 = 0x005B;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
pub struct Scaling {
|
|
||||||
pub v_scale: f64,
|
|
||||||
pub i_scale: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Scaling {
|
|
||||||
fn from(data: &[u16]) -> Self {
|
|
||||||
Self::from_internal(data[0], data[1], data[2], data[3])
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_internal(v_pu_hi: u16, v_pu_lo: u16, i_pu_hi: u16, i_pu_lo: u16) -> Self {
|
|
||||||
Self {
|
|
||||||
v_scale: f64::from(v_pu_hi) + (f64::from(v_pu_lo) / f64::powf(2., 16.)),
|
|
||||||
i_scale: f64::from(i_pu_hi) + (f64::from(i_pu_lo) / f64::powf(2., 16.)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_voltage(&self, data: u16) -> f64 {
|
|
||||||
f64::from(data) * self.v_scale * f64::powf(2., -15.)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_current(&self, data: u16) -> f64 {
|
|
||||||
f64::from(data) * self.i_scale * f64::powf(2., -15.)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_power(&self, data: u16) -> f64 {
|
|
||||||
f64::from(data) * self.v_scale * self.i_scale * f64::powf(2., -17.)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[expect(clippy::cast_sign_loss)]
|
|
||||||
fn inverse_voltage(&self, voltage: f64) -> u16 {
|
|
||||||
(voltage / (self.v_scale * f64::powf(2., -15.))) as u16
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Tristar {
|
|
||||||
friendly_name: String,
|
|
||||||
modbus: tokio_modbus::client::Context,
|
|
||||||
charge_state_gauges: ChargeStateGauges,
|
|
||||||
consecutive_errors: usize,
|
|
||||||
scaling: Option<Scaling>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone, Copy)]
|
|
||||||
pub struct TristarState {
|
|
||||||
scaling: Option<Scaling>,
|
|
||||||
battery_voltage: f64,
|
|
||||||
target_voltage: f64,
|
|
||||||
input_current: f64,
|
|
||||||
battery_temp: i16,
|
|
||||||
heatsink_temp: i16,
|
|
||||||
charge_state: ChargeState,
|
|
||||||
tristar_input_voltage: f64,
|
|
||||||
tristar_charge_current: f64,
|
|
||||||
tristar_power_out: f64,
|
|
||||||
tristar_power_in: f64,
|
|
||||||
tristar_max_array_power: f64,
|
|
||||||
tristar_max_array_voltage: f64,
|
|
||||||
tristar_open_circuit_voltage: f64,
|
|
||||||
daily_charge_amp_hours: f64,
|
|
||||||
daily_charge_watt_hours: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn signed(val: u16) -> i16 {
|
|
||||||
match i16::try_from(val) {
|
|
||||||
Ok(v) => v,
|
|
||||||
Err(_) => -i16::try_from(val % 128).unwrap(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TristarState {
|
|
||||||
fn from_ram(ram: &[u16]) -> Self {
|
|
||||||
let scaling = Scaling::from(ram);
|
|
||||||
Self {
|
|
||||||
scaling: Some(scaling),
|
|
||||||
battery_voltage: scaling.get_voltage(ram[TristarRamAddress::AdcVbFMed]),
|
|
||||||
target_voltage: scaling.get_voltage(ram[TristarRamAddress::VbRef]),
|
|
||||||
input_current: scaling.get_current(ram[TristarRamAddress::AdcIaFShadow]),
|
|
||||||
battery_temp: signed(ram[TristarRamAddress::Tbatt]),
|
|
||||||
heatsink_temp: signed(ram[TristarRamAddress::THs]),
|
|
||||||
charge_state: ChargeState::from(ram[TristarRamAddress::ChargeState]),
|
|
||||||
tristar_input_voltage: scaling.get_voltage(ram[TristarRamAddress::AdcVaF]),
|
|
||||||
tristar_charge_current: scaling.get_current(ram[TristarRamAddress::AdcIbFShadow]),
|
|
||||||
tristar_power_out: scaling.get_power(ram[TristarRamAddress::PowerOutShadow]),
|
|
||||||
tristar_power_in: scaling.get_power(ram[TristarRamAddress::PowerInShadow]),
|
|
||||||
tristar_max_array_power: scaling.get_power(ram[TristarRamAddress::SweepPinMax]),
|
|
||||||
tristar_max_array_voltage: scaling.get_voltage(ram[TristarRamAddress::SweepVmp]),
|
|
||||||
tristar_open_circuit_voltage: scaling.get_voltage(ram[TristarRamAddress::SweepVoc]),
|
|
||||||
daily_charge_amp_hours: f64::from(ram[TristarRamAddress::AhcDaily]) * 0.1,
|
|
||||||
daily_charge_watt_hours: f64::from(ram[TristarRamAddress::WhcDaily]),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
|
||||||
enum ChargeState {
|
|
||||||
Start,
|
|
||||||
NightCheck,
|
|
||||||
Disconnect,
|
|
||||||
Night,
|
|
||||||
Fault,
|
|
||||||
Mppt,
|
|
||||||
Absorption,
|
|
||||||
Float,
|
|
||||||
Equalize,
|
|
||||||
Slave,
|
|
||||||
Unknown,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for ChargeState {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::Unknown
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<u16> for ChargeState {
|
|
||||||
fn from(value: u16) -> Self {
|
|
||||||
match value {
|
|
||||||
0 => Self::Start,
|
|
||||||
1 => Self::NightCheck,
|
|
||||||
2 => Self::Disconnect,
|
|
||||||
3 => Self::Night,
|
|
||||||
4 => Self::Fault,
|
|
||||||
5 => Self::Mppt,
|
|
||||||
6 => Self::Absorption,
|
|
||||||
7 => Self::Float,
|
|
||||||
8 => Self::Equalize,
|
|
||||||
9 => Self::Slave,
|
|
||||||
_ => {
|
|
||||||
log::warn!("Unknown chargestate value: {value}");
|
|
||||||
Self::Unknown
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
struct ChargeStateGauges {
|
|
||||||
start: GenericGauge<AtomicI64>,
|
|
||||||
night_check: GenericGauge<AtomicI64>,
|
|
||||||
disconnect: GenericGauge<AtomicI64>,
|
|
||||||
night: GenericGauge<AtomicI64>,
|
|
||||||
fault: GenericGauge<AtomicI64>,
|
|
||||||
mppt: GenericGauge<AtomicI64>,
|
|
||||||
absorption: GenericGauge<AtomicI64>,
|
|
||||||
float: GenericGauge<AtomicI64>,
|
|
||||||
equalize: GenericGauge<AtomicI64>,
|
|
||||||
slave: GenericGauge<AtomicI64>,
|
|
||||||
unknown: GenericGauge<AtomicI64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ChargeStateGauges {
|
|
||||||
fn new(label: &str) -> Self {
|
|
||||||
let start = CHARGE_STATE.with_label_values(&[label, "start"]);
|
|
||||||
let night_check = CHARGE_STATE.with_label_values(&[label, "night_check"]);
|
|
||||||
let disconnect = CHARGE_STATE.with_label_values(&[label, "disconnect"]);
|
|
||||||
let night = CHARGE_STATE.with_label_values(&[label, "night"]);
|
|
||||||
let fault = CHARGE_STATE.with_label_values(&[label, "fault"]);
|
|
||||||
let mppt = CHARGE_STATE.with_label_values(&[label, "mppt"]);
|
|
||||||
let absorption = CHARGE_STATE.with_label_values(&[label, "absorption"]);
|
|
||||||
let float = CHARGE_STATE.with_label_values(&[label, "float"]);
|
|
||||||
let equalize = CHARGE_STATE.with_label_values(&[label, "equalize"]);
|
|
||||||
let slave = CHARGE_STATE.with_label_values(&[label, "slave"]);
|
|
||||||
let unknown = CHARGE_STATE.with_label_values(&[label, "unknown"]);
|
|
||||||
Self {
|
|
||||||
start,
|
|
||||||
night_check,
|
|
||||||
disconnect,
|
|
||||||
night,
|
|
||||||
fault,
|
|
||||||
mppt,
|
|
||||||
absorption,
|
|
||||||
float,
|
|
||||||
equalize,
|
|
||||||
slave,
|
|
||||||
unknown,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn zero_all(&mut self) {
|
|
||||||
self.start.set(0);
|
|
||||||
self.night_check.set(0);
|
|
||||||
self.disconnect.set(0);
|
|
||||||
self.night.set(0);
|
|
||||||
self.fault.set(0);
|
|
||||||
self.mppt.set(0);
|
|
||||||
self.absorption.set(0);
|
|
||||||
self.float.set(0);
|
|
||||||
self.equalize.set(0);
|
|
||||||
self.slave.set(0);
|
|
||||||
self.unknown.set(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set(&mut self, state: ChargeState) {
|
|
||||||
match state {
|
|
||||||
ChargeState::Start => {
|
|
||||||
self.zero_all();
|
|
||||||
self.start.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::NightCheck => {
|
|
||||||
self.zero_all();
|
|
||||||
self.night_check.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Disconnect => {
|
|
||||||
self.zero_all();
|
|
||||||
self.disconnect.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Night => {
|
|
||||||
self.zero_all();
|
|
||||||
self.night.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Fault => {
|
|
||||||
self.zero_all();
|
|
||||||
self.fault.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Mppt => {
|
|
||||||
self.zero_all();
|
|
||||||
self.mppt.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Absorption => {
|
|
||||||
self.zero_all();
|
|
||||||
self.absorption.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Float => {
|
|
||||||
self.zero_all();
|
|
||||||
self.float.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Equalize => {
|
|
||||||
self.zero_all();
|
|
||||||
self.equalize.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Slave => {
|
|
||||||
self.zero_all();
|
|
||||||
self.slave.set(1);
|
|
||||||
}
|
|
||||||
ChargeState::Unknown => {
|
|
||||||
self.zero_all();
|
|
||||||
self.unknown.set(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Tristar {
|
|
||||||
pub fn new(serial_port: &str, friendly_name: &str, baud_rate: u32) -> eyre::Result<Self> {
|
|
||||||
let modbus_serial =
|
|
||||||
tokio_serial::SerialStream::open(&tokio_serial::new(serial_port, baud_rate))?;
|
|
||||||
let slave = tokio_modbus::Slave(DEVICE_ID);
|
|
||||||
let modbus = tokio_modbus::client::rtu::attach_slave(modbus_serial, slave);
|
|
||||||
let charge_state_gauges = ChargeStateGauges::new(friendly_name);
|
|
||||||
Ok(Self {
|
|
||||||
friendly_name: friendly_name.to_owned(),
|
|
||||||
modbus,
|
|
||||||
charge_state_gauges,
|
|
||||||
consecutive_errors: 0,
|
|
||||||
scaling: None,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn refresh(&mut self) -> eyre::Result<crate::controller::CommonData> {
|
|
||||||
let new_state = self.get_data().await?;
|
|
||||||
|
|
||||||
self.scaling = new_state.scaling;
|
|
||||||
self.consecutive_errors = 0;
|
|
||||||
BATTERY_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.battery_voltage);
|
|
||||||
TARGET_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.target_voltage);
|
|
||||||
INPUT_CURRENT
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.input_current);
|
|
||||||
BATTERY_TEMP
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.battery_temp.into());
|
|
||||||
HEATSINK_TEMP
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.heatsink_temp.into());
|
|
||||||
TRISTAR_INPUT_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_input_voltage);
|
|
||||||
TRISTAR_CHARGE_CURRENT
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_charge_current);
|
|
||||||
TRISTAR_POWER_OUT
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_power_out);
|
|
||||||
TRISTAR_POWER_IN
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_power_in);
|
|
||||||
TRISTAR_MAX_ARRAY_POWER
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_max_array_power);
|
|
||||||
TRISTAR_MAX_ARRAY_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_max_array_voltage);
|
|
||||||
TRISTAR_OPEN_CIRCUIT_VOLTAGE
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.tristar_open_circuit_voltage);
|
|
||||||
TRISTAR_TOTAL_AH_CHARGE_DAILY
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.daily_charge_amp_hours);
|
|
||||||
TRISTAR_TOTAL_WH_CHARGE_DAILY
|
|
||||||
.with_label_values(&[&self.friendly_name])
|
|
||||||
.set(new_state.daily_charge_watt_hours);
|
|
||||||
|
|
||||||
self.charge_state_gauges.set(new_state.charge_state);
|
|
||||||
|
|
||||||
Ok(crate::controller::CommonData {
|
|
||||||
battery_voltage: new_state.battery_voltage,
|
|
||||||
target_voltage: new_state.target_voltage,
|
|
||||||
battery_temp: f64::from(new_state.battery_temp),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn set_target_voltage(&mut self, target_voltage: f64) -> eyre::Result<()> {
|
|
||||||
let scaled_voltage: u16 = self.scale_voltage(target_voltage)?;
|
|
||||||
self.modbus
|
|
||||||
.write_single_register(TristarRamAddress::VbRefSlave as u16, scaled_voltage)
|
|
||||||
.await??;
|
|
||||||
|
|
||||||
log::debug!(
|
|
||||||
"tristar {} being set to voltage {target_voltage} (scaled: {scaled_voltage:#X?})",
|
|
||||||
self.friendly_name
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn scale_voltage(&self, voltage: f64) -> eyre::Result<u16> {
|
|
||||||
let Some(scaling) = &self.scaling else {
|
|
||||||
return Err(eyre::eyre!("no scaling data present"));
|
|
||||||
};
|
|
||||||
Ok(scaling.inverse_voltage(voltage))
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn get_data(&mut self) -> eyre::Result<TristarState> {
|
|
||||||
let data = self
|
|
||||||
.modbus
|
|
||||||
.read_holding_registers(0x0000, RAM_DATA_SIZE + 1)
|
|
||||||
.await??;
|
|
||||||
Ok(TristarState::from_ram(&data))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum TristarRamAddress {
|
|
||||||
AdcVbFMed = 0x0018,
|
|
||||||
AdcVbtermF = 0x0019,
|
|
||||||
AdcVbsF = 0x001A,
|
|
||||||
AdcVaF = 0x001B,
|
|
||||||
AdcIbFShadow = 0x001C,
|
|
||||||
AdcIaFShadow = 0x001D,
|
|
||||||
AdcP12F = 0x001E,
|
|
||||||
AdcP3F = 0x001F,
|
|
||||||
AdcPmeterF = 0x0020,
|
|
||||||
AdcP18F = 0x0021,
|
|
||||||
AdcVRef = 0x0022,
|
|
||||||
THs = 0x0023,
|
|
||||||
TRts = 0x0024,
|
|
||||||
Tbatt = 0x0025,
|
|
||||||
AdcVbF1m = 0x0026,
|
|
||||||
AdcIbF1m = 0x0027,
|
|
||||||
VbMin = 0x0028,
|
|
||||||
VbMax = 0x0029,
|
|
||||||
HourmeterHi = 0x002A,
|
|
||||||
HourmeterLo = 0x002B,
|
|
||||||
Fault = 0x002C,
|
|
||||||
AlarmHi = 0x002E,
|
|
||||||
AlarmLo = 0x002F,
|
|
||||||
Dip = 0x0030,
|
|
||||||
Led = 0x0031,
|
|
||||||
ChargeState = 0x0032,
|
|
||||||
VbRef = 0x0033,
|
|
||||||
AhcRHi = 0x0034,
|
|
||||||
AhcRLo = 0x0035,
|
|
||||||
AhcTHi = 0x0036,
|
|
||||||
AhcTLo = 0x0037,
|
|
||||||
KwhcR = 0x0038,
|
|
||||||
KwhcT = 0x0039,
|
|
||||||
PowerOutShadow = 0x003A,
|
|
||||||
PowerInShadow = 0x003B,
|
|
||||||
SweepPinMax = 0x003C,
|
|
||||||
SweepVmp = 0x003D,
|
|
||||||
SweepVoc = 0x003E,
|
|
||||||
VbMinDaily = 0x0040,
|
|
||||||
VbMaxDaily = 0x0041,
|
|
||||||
VaMaxDaily = 0x0042,
|
|
||||||
AhcDaily = 0x0043,
|
|
||||||
WhcDaily = 0x0044,
|
|
||||||
FlagsDaily = 0x0045,
|
|
||||||
PoutMaxDaily = 0x0046,
|
|
||||||
TbMinDaily = 0x0047,
|
|
||||||
TbMaxDaily = 0x0048,
|
|
||||||
FaultDaily = 0x0049,
|
|
||||||
AlarmDailyHi = 0x004B,
|
|
||||||
AlarmDailyLo = 0x004C,
|
|
||||||
TimeAbDaily = 0x004D,
|
|
||||||
TimeEqDaily = 0x004E,
|
|
||||||
TimeFlDaily = 0x004F,
|
|
||||||
IbRefSlave = 0x0058,
|
|
||||||
VbRefSlave = 0x0059,
|
|
||||||
VaRefFixed = 0x005A,
|
|
||||||
VaRefFixedPc = 0x005B,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl std::ops::Index<TristarRamAddress> for [u16] {
|
|
||||||
type Output = u16;
|
|
||||||
|
|
||||||
fn index(&self, index: TristarRamAddress) -> &Self::Output {
|
|
||||||
&self[index as usize]
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,29 +1,25 @@
|
||||||
use rocket::{get, post, routes, serde::json::Json, State};
|
use rocket::{get, post, routes, serde::json::Json, State};
|
||||||
|
|
||||||
|
use crate::storage::AllControllers;
|
||||||
|
|
||||||
mod static_handler;
|
mod static_handler;
|
||||||
|
|
||||||
pub struct ServerState {
|
pub struct ServerState {
|
||||||
primary_name: String,
|
primary_name: String,
|
||||||
map: std::collections::HashMap<
|
data: AllControllers,
|
||||||
String,
|
tx_to_controllers: tokio::sync::watch::Sender<crate::controller::VoltageCommand>,
|
||||||
std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>,
|
|
||||||
>,
|
|
||||||
tx_to_controllers: crate::controller::MultiTx,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerState {
|
impl ServerState {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
primary_name: &impl ToString,
|
primary_name: &impl ToString,
|
||||||
map: std::collections::HashMap<
|
data: AllControllers,
|
||||||
String,
|
tx_to_controllers: tokio::sync::watch::Sender<crate::controller::VoltageCommand>,
|
||||||
std::sync::Arc<tokio::sync::RwLock<crate::controller::CommonData>>,
|
|
||||||
>,
|
|
||||||
tx_to_controllers: crate::controller::MultiTx,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let primary_name = primary_name.to_string();
|
let primary_name = primary_name.to_string();
|
||||||
Self {
|
Self {
|
||||||
primary_name,
|
primary_name,
|
||||||
map,
|
data,
|
||||||
tx_to_controllers,
|
tx_to_controllers,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -52,8 +48,12 @@ pub fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
|
||||||
metrics,
|
metrics,
|
||||||
interfaces,
|
interfaces,
|
||||||
all_interfaces,
|
all_interfaces,
|
||||||
|
all_interfaces_full,
|
||||||
|
all_interfaces_settings,
|
||||||
primary_interface,
|
primary_interface,
|
||||||
interface,
|
interface,
|
||||||
|
interface_full,
|
||||||
|
interface_settings,
|
||||||
get_control,
|
get_control,
|
||||||
enable_control,
|
enable_control,
|
||||||
disable_control
|
disable_control
|
||||||
|
@ -63,7 +63,7 @@ pub fn rocket(state: ServerState) -> rocket::Rocket<rocket::Build> {
|
||||||
|
|
||||||
#[get("/interfaces")]
|
#[get("/interfaces")]
|
||||||
fn interfaces(state: &State<ServerState>) -> Json<Vec<String>> {
|
fn interfaces(state: &State<ServerState>) -> Json<Vec<String>> {
|
||||||
Json(state.map.keys().cloned().collect())
|
Json(state.data.controller_names().cloned().collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/interfaces/primary")]
|
#[get("/interfaces/primary")]
|
||||||
|
@ -71,14 +71,13 @@ async fn primary_interface(
|
||||||
state: &State<ServerState>,
|
state: &State<ServerState>,
|
||||||
) -> Result<Json<crate::controller::CommonData>, ServerError> {
|
) -> Result<Json<crate::controller::CommonData>, ServerError> {
|
||||||
let s = state
|
let s = state
|
||||||
.map
|
.data
|
||||||
.get(&state.primary_name)
|
.get(&state.primary_name)
|
||||||
.ok_or(ServerError::InvalidPrimaryName)?
|
.ok_or(ServerError::InvalidPrimaryName)?
|
||||||
.read()
|
.read_state()
|
||||||
.await
|
.await;
|
||||||
.clone();
|
|
||||||
|
|
||||||
Ok(Json(s))
|
Ok(Json(s.as_ref().ok_or(ServerError::NoData)?.common()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/interfaces/data")]
|
#[get("/interfaces/data")]
|
||||||
|
@ -87,8 +86,43 @@ async fn all_interfaces(
|
||||||
) -> Json<Vec<(String, crate::controller::CommonData)>> {
|
) -> Json<Vec<(String, crate::controller::CommonData)>> {
|
||||||
let mut data = Vec::new();
|
let mut data = Vec::new();
|
||||||
|
|
||||||
for (k, v) in &state.map {
|
for (k, v) in state.data.all_data() {
|
||||||
data.push((k.clone(), v.read().await.clone()));
|
let v = v.read_state().await;
|
||||||
|
if let Some(v) = v.as_ref() {
|
||||||
|
data.push((k.clone(), v.common().clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Json(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/interfaces/data/full")]
|
||||||
|
async fn all_interfaces_full(
|
||||||
|
state: &State<ServerState>,
|
||||||
|
) -> Json<Vec<(String, crate::controller::ControllerState)>> {
|
||||||
|
let mut data = Vec::new();
|
||||||
|
|
||||||
|
for (k, v) in state.data.all_data() {
|
||||||
|
let v = v.read_state().await;
|
||||||
|
if let Some(v) = v.as_ref() {
|
||||||
|
data.push((k.clone(), v.clone()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Json(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/interfaces/settings")]
|
||||||
|
async fn all_interfaces_settings(
|
||||||
|
state: &State<ServerState>,
|
||||||
|
) -> Json<Vec<(String, crate::controller::ControllerSettings)>> {
|
||||||
|
let mut data = Vec::new();
|
||||||
|
|
||||||
|
for (k, v) in state.data.all_data() {
|
||||||
|
let v = v.read_settings().await;
|
||||||
|
if let Some(v) = v.as_ref() {
|
||||||
|
data.push((k.clone(), v.clone()));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Json(data)
|
Json(data)
|
||||||
|
@ -100,13 +134,40 @@ async fn interface(
|
||||||
state: &State<ServerState>,
|
state: &State<ServerState>,
|
||||||
) -> Result<Json<crate::controller::CommonData>, ServerError> {
|
) -> Result<Json<crate::controller::CommonData>, ServerError> {
|
||||||
let data = state
|
let data = state
|
||||||
.map
|
.data
|
||||||
.get(name)
|
.get(name)
|
||||||
.ok_or(ServerError::NotFound)?
|
.ok_or(ServerError::NotFound)?
|
||||||
.read()
|
.read_state()
|
||||||
.await
|
.await;
|
||||||
.clone();
|
Ok(Json(data.as_ref().ok_or(ServerError::NoData)?.common()))
|
||||||
Ok(Json(data))
|
}
|
||||||
|
|
||||||
|
#[get("/interface/<name>/full")]
|
||||||
|
async fn interface_full(
|
||||||
|
name: &str,
|
||||||
|
state: &State<ServerState>,
|
||||||
|
) -> Result<Json<crate::controller::ControllerState>, ServerError> {
|
||||||
|
let data = state
|
||||||
|
.data
|
||||||
|
.get(name)
|
||||||
|
.ok_or(ServerError::NotFound)?
|
||||||
|
.read_state()
|
||||||
|
.await;
|
||||||
|
Ok(Json(data.as_ref().ok_or(ServerError::NoData)?.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[get("/interface/<name>/settings")]
|
||||||
|
async fn interface_settings(
|
||||||
|
name: &str,
|
||||||
|
state: &State<ServerState>,
|
||||||
|
) -> Result<Json<crate::controller::ControllerSettings>, ServerError> {
|
||||||
|
let data = state
|
||||||
|
.data
|
||||||
|
.get(name)
|
||||||
|
.ok_or(ServerError::NotFound)?
|
||||||
|
.read_settings()
|
||||||
|
.await;
|
||||||
|
Ok(Json(data.as_ref().ok_or(ServerError::NoData)?.clone()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/metrics")]
|
#[get("/metrics")]
|
||||||
|
@ -139,19 +200,23 @@ async fn enable_control() {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/control/disable")]
|
#[post("/control/disable")]
|
||||||
async fn disable_control(state: &State<ServerState>) {
|
async fn disable_control(state: &State<ServerState>) -> Result<(), ServerError> {
|
||||||
log::warn!("disabling control");
|
log::warn!("disabling control");
|
||||||
crate::config::write_to_config()
|
crate::config::write_to_config()
|
||||||
.await
|
.await
|
||||||
.enable_secondary_control = false;
|
.enable_secondary_control = false;
|
||||||
state
|
state
|
||||||
.tx_to_controllers
|
.tx_to_controllers
|
||||||
.send_to_all(crate::controller::VoltageCommand::Set(-1.0));
|
.send(crate::controller::VoltageCommand::None)?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ServerError {
|
enum ServerError {
|
||||||
Prometheus,
|
Prometheus,
|
||||||
NotFound,
|
NotFound,
|
||||||
InvalidPrimaryName,
|
InvalidPrimaryName,
|
||||||
|
NoData,
|
||||||
|
ControllerTx,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<prometheus::Error> for ServerError {
|
impl From<prometheus::Error> for ServerError {
|
||||||
|
@ -160,12 +225,20 @@ impl From<prometheus::Error> for ServerError {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> From<tokio::sync::watch::error::SendError<T>> for ServerError {
|
||||||
|
fn from(_: tokio::sync::watch::error::SendError<T>) -> Self {
|
||||||
|
Self::ControllerTx
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> rocket::response::Responder<'a, 'a> for ServerError {
|
impl<'a> rocket::response::Responder<'a, 'a> for ServerError {
|
||||||
fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> {
|
fn respond_to(self, _: &'a rocket::Request<'_>) -> rocket::response::Result<'a> {
|
||||||
Err(match self {
|
Err(match self {
|
||||||
Self::Prometheus => rocket::http::Status::InternalServerError,
|
|
||||||
Self::NotFound => rocket::http::Status::NotFound,
|
Self::NotFound => rocket::http::Status::NotFound,
|
||||||
Self::InvalidPrimaryName => rocket::http::Status::ServiceUnavailable,
|
Self::InvalidPrimaryName => rocket::http::Status::ServiceUnavailable,
|
||||||
|
Self::ControllerTx | Self::NoData | Self::Prometheus => {
|
||||||
|
rocket::http::Status::InternalServerError
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,10 +51,12 @@ impl Handler for UiStatic {
|
||||||
data: v.contents().to_vec(),
|
data: v.contents().to_vec(),
|
||||||
name: p,
|
name: p,
|
||||||
})
|
})
|
||||||
.or(UI_DIR_FILES.get_file(&plus_index).map(|v| RawHtml {
|
.or_else(|| {
|
||||||
data: v.contents().to_vec(),
|
UI_DIR_FILES.get_file(&plus_index).map(|v| RawHtml {
|
||||||
name: plus_index,
|
data: v.contents().to_vec(),
|
||||||
}));
|
name: plus_index,
|
||||||
|
})
|
||||||
|
});
|
||||||
file.respond_to(req).or_forward((data, Status::NotFound))
|
file.respond_to(req).or_forward((data, Status::NotFound))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
[toolchain]
|
[toolchain]
|
||||||
channel = "nightly"
|
channel = "nightly-2025-01-16"
|
||||||
|
targets = ["aarch64-unknown-linux-musl"]
|
||||||
|
|
|
@ -95,6 +95,7 @@ impl Vehicle {
|
||||||
Ok(state.charge_state)
|
Ok(state.charge_state)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code, reason = "active charge control not yet implemented")]
|
||||||
pub async fn set_charging_amps(&self, charging_amps: i64) -> eyre::Result<()> {
|
pub async fn set_charging_amps(&self, charging_amps: i64) -> eyre::Result<()> {
|
||||||
self.client
|
self.client
|
||||||
.post(format!(
|
.post(format!(
|
||||||
|
|
|
@ -42,11 +42,11 @@ impl Car {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn vehicle(&self) -> &http::Vehicle {
|
pub const fn vehicle(&self) -> &http::Vehicle {
|
||||||
&self.vehicle
|
&self.vehicle
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn state(&self) -> &tokio::sync::RwLock<CarState> {
|
pub const fn state(&self) -> &tokio::sync::RwLock<CarState> {
|
||||||
&self.state
|
&self.state
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -153,7 +153,7 @@ impl ChargeState {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
|
||||||
pub enum ChargingState {
|
pub enum ChargingState {
|
||||||
Charging,
|
Charging,
|
||||||
Stopped,
|
Stopped,
|
||||||
|
|
|
@ -60,7 +60,7 @@ impl ConfigWatcher {
|
||||||
async fn overwrite_config(config: Config) -> eyre::Result<()> {
|
async fn overwrite_config(config: Config) -> eyre::Result<()> {
|
||||||
*CONFIG
|
*CONFIG
|
||||||
.get()
|
.get()
|
||||||
.ok_or(eyre::eyre!("could not get config"))?
|
.ok_or_else(|| eyre::eyre!("could not get config"))?
|
||||||
.write()
|
.write()
|
||||||
.await = config;
|
.await = config;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -4,6 +4,7 @@ pub struct VehicleController {
|
||||||
control_state: ChargeRateControllerState,
|
control_state: ChargeRateControllerState,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[expect(dead_code, reason = "not all states are currently in use")]
|
||||||
pub enum ChargeRateControllerState {
|
pub enum ChargeRateControllerState {
|
||||||
Inactive,
|
Inactive,
|
||||||
Charging { rate_amps: i64 },
|
Charging { rate_amps: i64 },
|
||||||
|
@ -14,7 +15,7 @@ pub enum InterfaceRequest {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VehicleController {
|
impl VehicleController {
|
||||||
pub fn new(
|
pub const fn new(
|
||||||
car: std::sync::Arc<crate::api::Car>,
|
car: std::sync::Arc<crate::api::Car>,
|
||||||
requests: tokio::sync::mpsc::UnboundedReceiver<InterfaceRequest>,
|
requests: tokio::sync::mpsc::UnboundedReceiver<InterfaceRequest>,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
|
@ -50,7 +51,10 @@ impl VehicleController {
|
||||||
}
|
}
|
||||||
match self.control_state {
|
match self.control_state {
|
||||||
ChargeRateControllerState::Inactive => {
|
ChargeRateControllerState::Inactive => {
|
||||||
if let Some(state) = self.car.state().read().await.charge_state().await {
|
let car_state = self.car.state().read().await;
|
||||||
|
let state = car_state.charge_state().await;
|
||||||
|
|
||||||
|
if let Some(state) = state {
|
||||||
if state.is_charging() {
|
if state.is_charging() {
|
||||||
self.control_state = ChargeRateControllerState::Charging {
|
self.control_state = ChargeRateControllerState::Charging {
|
||||||
rate_amps: state.charge_amps,
|
rate_amps: state.charge_amps,
|
||||||
|
@ -58,10 +62,14 @@ impl VehicleController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ChargeRateControllerState::Charging { rate_amps } => todo!(),
|
ChargeRateControllerState::Charging { rate_amps: _ } => todo!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[expect(
|
||||||
|
clippy::needless_pass_by_ref_mut,
|
||||||
|
reason = "this will eventually need to mutate self"
|
||||||
|
)]
|
||||||
pub async fn process_requests(&mut self, req: InterfaceRequest) {
|
pub async fn process_requests(&mut self, req: InterfaceRequest) {
|
||||||
if let Err(e) = match req {
|
if let Err(e) = match req {
|
||||||
InterfaceRequest::FlashLights => self.car.vehicle().flash_lights().await,
|
InterfaceRequest::FlashLights => self.car.vehicle().flash_lights().await,
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![allow(clippy::significant_drop_tightening)]
|
||||||
|
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
|
@ -27,7 +27,7 @@ pub struct ServerState {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerState {
|
impl ServerState {
|
||||||
pub fn new(car: Arc<Car>, api_requests: UnboundedSender<InterfaceRequest>) -> Self {
|
pub const fn new(car: Arc<Car>, api_requests: UnboundedSender<InterfaceRequest>) -> Self {
|
||||||
Self { car, api_requests }
|
Self { car, api_requests }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -52,10 +52,12 @@ impl Handler for UiStatic {
|
||||||
data: v.contents().to_vec(),
|
data: v.contents().to_vec(),
|
||||||
name: p,
|
name: p,
|
||||||
})
|
})
|
||||||
.or(UI_DIR_FILES.get_file(&plus_index).map(|v| RawHtml {
|
.or_else(|| {
|
||||||
data: v.contents().to_vec(),
|
UI_DIR_FILES.get_file(&plus_index).map(|v| RawHtml {
|
||||||
name: plus_index,
|
data: v.contents().to_vec(),
|
||||||
}));
|
name: plus_index,
|
||||||
|
})
|
||||||
|
});
|
||||||
file.respond_to(req).or_forward((data, Status::NotFound))
|
file.respond_to(req).or_forward((data, Status::NotFound))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue