From 1c9d36c6adf4e87539d1e8c7b68ea38456656249 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Sat, 11 Jan 2025 18:35:40 +1100 Subject: [PATCH] temp: config v2 --- charge-controller-supervisor/src/config.rs | 16 +++++- .../src/config/outdated.rs | 56 +++++++++++++++++-- 2 files changed, 65 insertions(+), 7 deletions(-) diff --git a/charge-controller-supervisor/src/config.rs b/charge-controller-supervisor/src/config.rs index 0a2caf0..5d10328 100644 --- a/charge-controller-supervisor/src/config.rs +++ b/charge-controller-supervisor/src/config.rs @@ -124,7 +124,9 @@ pub enum ConfigStorage { #[serde(rename = "1")] V1(outdated::ConfigV1), #[serde(rename = "2")] - V2(Config), + V2(outdated::ConfigV2), + #[serde(rename = "3")] + V3(Config), } mod outdated; @@ -137,13 +139,14 @@ impl Default for ConfigStorage { impl ConfigStorage { pub const fn from_latest(config: Config) -> Self { - Self::V2(config) + Self::V3(config) } pub fn into_latest(self) -> Config { match self { ConfigStorage::V1(v1) => v1.into(), - ConfigStorage::V2(config) => config, + ConfigStorage::V2(v2) => v2.into(), + ConfigStorage::V3(config) => config, } } @@ -184,6 +187,13 @@ impl Config { } } +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct ConnectionConfig { + transport: Transport, + variant: ChargeControllerVariant, + charge_controllers: Vec, +} + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct ChargeControllerConfig { pub name: String, diff --git a/charge-controller-supervisor/src/config/outdated.rs b/charge-controller-supervisor/src/config/outdated.rs index 320cf72..207ae17 100644 --- a/charge-controller-supervisor/src/config/outdated.rs +++ b/charge-controller-supervisor/src/config/outdated.rs @@ -1,4 +1,46 @@ pub use v1::ConfigV1; +pub use v2::ConfigV2; + +mod v2 { + use serde::{Deserialize, Serialize}; + + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Default)] + #[serde(default)] + pub struct ConfigV2 { + pub primary_charge_controller: String, + pub enable_secondary_control: bool, + pub charge_controllers: Vec, + } + + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] + pub struct ChargeControllerConfigV2 { + pub name: String, + pub watch_interval_seconds: u64, + pub variant: ChargeControllerVariantV2, + #[serde(default)] + pub follow_primary: bool, + pub transport: TransportV2, + } + + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] + pub enum ChargeControllerVariantV2 { + Tristar, + Pl { timeout_milliseconds: u64 }, + } + + #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] + #[serde(rename_all = "lowercase")] + pub enum TransportV2 { + Serial { port: String, baud_rate: u32 }, + Tcp { ip: std::net::IpAddr, port: u16 }, + } + + impl From for crate::config::Config { + fn from(value: ConfigV2) -> Self { + todo!() + } + } +} mod v1 { use serde::{Deserialize, Serialize}; @@ -28,11 +70,11 @@ mod v1 { Pl { timeout_milliseconds: u64 }, } - impl From for crate::config::ChargeControllerConfig { + impl From for super::v2::ChargeControllerConfigV2 { fn from(value: ChargeControllerConfigV1) -> Self { Self { name: value.name, - transport: crate::config::Transport::Serial { + transport: super::v2::TransportV2::Serial { port: value.serial_port, baud_rate: value.baud_rate, }, @@ -43,7 +85,7 @@ mod v1 { } } - impl From for crate::config::ChargeControllerVariant { + impl From for super::v2::ChargeControllerVariantV2 { fn from(value: ChargeControllerVariantV1) -> Self { match value { ChargeControllerVariantV1::Tristar => Self::Tristar, @@ -56,7 +98,7 @@ mod v1 { } } - impl From for crate::config::Config { + impl From for super::v2::ConfigV2 { fn from(value: ConfigV1) -> Self { Self { primary_charge_controller: value.primary_charge_controller, @@ -69,4 +111,10 @@ mod v1 { } } } + + impl From for crate::config::Config { + fn from(value: ConfigV1) -> Self { + super::v2::ConfigV2::from(value).into() + } + } }