add "print-json" command

This commit is contained in:
Alex Janka 2022-12-18 13:19:48 +11:00
parent 41a607c8c2
commit 87ee197f01
5 changed files with 55 additions and 9 deletions

24
Cargo.lock generated
View file

@ -302,6 +302,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "itoa"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]] [[package]]
name = "kernel32-sys" name = "kernel32-sys"
version = "0.2.2" version = "0.2.2"
@ -423,6 +429,7 @@ dependencies = [
"clap 4.0.29", "clap 4.0.29",
"libmodbus-rs", "libmodbus-rs",
"serde", "serde",
"serde_json",
] ]
[[package]] [[package]]
@ -594,6 +601,12 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "ryu"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.151" version = "1.0.151"
@ -614,6 +627,17 @@ dependencies = [
"syn", "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]] [[package]]
name = "strsim" name = "strsim"
version = "0.8.0" version = "0.8.0"

View file

@ -14,3 +14,4 @@ libmodbus-rs = "0.8.3"
clap = { version = "4.0", features = ["derive"] } clap = { version = "4.0", features = ["derive"] }
bincode = "1.3" bincode = "1.3"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

View file

@ -1,4 +1,5 @@
use crate::INFO_SCALE; use crate::INFO_SCALE;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug}; use std::fmt::{self, Debug};
pub trait DataPoint { 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 { pub struct Voltage {
data: u16, data: u16,
} }
@ -59,7 +60,7 @@ impl DataPoint for Voltage {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Current { pub struct Current {
data: u16, data: u16,
} }
@ -96,7 +97,7 @@ impl DataPoint for Current {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct VoltagePercentage { pub struct VoltagePercentage {
data: u16, data: u16,
} }
@ -131,7 +132,7 @@ impl DataPoint for VoltagePercentage {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Tempcomp { pub struct Tempcomp {
data: u16, data: u16,
} }
@ -168,7 +169,7 @@ impl DataPoint for Tempcomp {
} }
} }
#[derive(Debug, Clone, Copy)] #[derive(Serialize, Deserialize, Debug, Clone, Copy)]
pub struct Raw { pub struct Raw {
data: u16, data: u16,
} }

View file

@ -4,7 +4,7 @@ mod datatypes;
mod mppt_structs; mod mppt_structs;
mod offsets; mod offsets;
use crate::datatypes::*; use crate::datatypes::*;
use crate::mppt_structs::{MpptEeprom, MpptRam}; use crate::mppt_structs::{MpptData, MpptEeprom, MpptRam};
use crate::offsets::{OffsetsEeprom, OffsetsRam}; use crate::offsets::{OffsetsEeprom, OffsetsRam};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use libmodbus_rs::{Modbus, ModbusClient, ModbusRTU}; use libmodbus_rs::{Modbus, ModbusClient, ModbusRTU};
@ -58,6 +58,9 @@ enum Commands {
/// Get all RAM values /// Get all RAM values
GetRam, GetRam,
/// Print RAM and EEPROM values to JSON
PrintJSON,
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -164,6 +167,16 @@ fn main() {
Some(Commands::GetRam) => { Some(Commands::GetRam) => {
println!("ram: {:#?}", ram_data); 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 => { None => {
println!("{eeprom_data}"); println!("{eeprom_data}");
} }

View file

@ -1,7 +1,8 @@
use crate::datatypes::*; use crate::datatypes::*;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display}; use std::fmt::{self, Debug, Display};
#[derive(Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct MpptRam { pub struct MpptRam {
// scaling values // scaling values
pub v_pu: f32, pub v_pu: f32,
@ -140,7 +141,7 @@ impl MpptRam {
} }
} }
#[derive(Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct MpptEeprom { pub struct MpptEeprom {
pub ev_absorp: Voltage, pub ev_absorp: Voltage,
pub ev_float: 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,
}