diff --git a/Cargo.lock b/Cargo.lock index 57fa1c6..38d404f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -302,6 +302,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itoa" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440" + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -423,6 +429,7 @@ dependencies = [ "clap 4.0.29", "libmodbus-rs", "serde", + "serde_json", ] [[package]] @@ -594,6 +601,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "ryu" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde" + [[package]] name = "serde" version = "1.0.151" @@ -614,6 +627,17 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.90" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8778cc0b528968fe72abec38b5db5a20a70d148116cd9325d2bc5f5180ca3faf" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "strsim" version = "0.8.0" diff --git a/Cargo.toml b/Cargo.toml index 0018b2d..5f7ae98 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ authors = ["Alex Janka "] libmodbus-rs = "0.8.3" clap = { version = "4.0", features = ["derive"] } bincode = "1.3" -serde = { version = "1.0", features = ["derive"] } \ No newline at end of file +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/src/datatypes.rs b/src/datatypes.rs index bf31b0a..dfc9688 100644 --- a/src/datatypes.rs +++ b/src/datatypes.rs @@ -1,4 +1,5 @@ use crate::INFO_SCALE; +use serde::{Deserialize, Serialize}; use std::fmt::{self, Debug}; pub trait DataPoint { @@ -22,7 +23,7 @@ impl fmt::Debug for dyn DataPoint { } } -#[derive(Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub struct Voltage { data: u16, } @@ -59,7 +60,7 @@ impl DataPoint for Voltage { } } -#[derive(Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub struct Current { data: u16, } @@ -96,7 +97,7 @@ impl DataPoint for Current { } } -#[derive(Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub struct VoltagePercentage { data: u16, } @@ -131,7 +132,7 @@ impl DataPoint for VoltagePercentage { } } -#[derive(Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub struct Tempcomp { data: u16, } @@ -168,7 +169,7 @@ impl DataPoint for Tempcomp { } } -#[derive(Debug, Clone, Copy)] +#[derive(Serialize, Deserialize, Debug, Clone, Copy)] pub struct Raw { data: u16, } diff --git a/src/main.rs b/src/main.rs index 8dc3754..e346710 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ mod datatypes; mod mppt_structs; mod offsets; use crate::datatypes::*; -use crate::mppt_structs::{MpptEeprom, MpptRam}; +use crate::mppt_structs::{MpptData, MpptEeprom, MpptRam}; use crate::offsets::{OffsetsEeprom, OffsetsRam}; use clap::{Parser, Subcommand}; use libmodbus_rs::{Modbus, ModbusClient, ModbusRTU}; @@ -58,6 +58,9 @@ enum Commands { /// Get all RAM values GetRam, + + /// Print RAM and EEPROM values to JSON + PrintJSON, } #[derive(Debug, Clone)] @@ -164,6 +167,16 @@ fn main() { Some(Commands::GetRam) => { println!("ram: {:#?}", ram_data); } + Some(Commands::PrintJSON) => { + println!( + "{}", + serde_json::to_string(&MpptData { + ram: ram_data, + eeprom: eeprom_data + }) + .expect("Could not format data as JSON!") + ); + } None => { println!("{eeprom_data}"); } diff --git a/src/mppt_structs.rs b/src/mppt_structs.rs index ee7ef06..b3d05bb 100644 --- a/src/mppt_structs.rs +++ b/src/mppt_structs.rs @@ -1,7 +1,8 @@ use crate::datatypes::*; +use serde::{Deserialize, Serialize}; use std::fmt::{self, Debug, Display}; -#[derive(Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct MpptRam { // scaling values pub v_pu: f32, @@ -140,7 +141,7 @@ impl MpptRam { } } -#[derive(Debug)] +#[derive(Serialize, Deserialize, Debug)] pub struct MpptEeprom { pub ev_absorp: Voltage, pub ev_float: Voltage, @@ -266,3 +267,9 @@ eva_ref_fixed_pct_init: {}", ) } } + +#[derive(Serialize, Deserialize)] +pub struct MpptData { + pub ram: MpptRam, + pub eeprom: MpptEeprom, +}