From 5521563c919f8d11630b68f709c7ef51e74adfef Mon Sep 17 00:00:00 2001 From: Astro Date: Wed, 30 Sep 2020 20:06:47 +0200 Subject: [PATCH] pid: export summary as json --- src/main.rs | 44 ++++++++++++++++++++++---------------------- src/pid.rs | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 23 deletions(-) diff --git a/src/main.rs b/src/main.rs index 643576e..3b1a08a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -96,7 +96,6 @@ fn report_to(channel: usize, channels: &mut Channels, socket: &mut TcpSocket) -> let _ = socket.send_slice(b"\n"); // success return true - // TODO: session.mark_report_sent(channel); } Ok(sent) => warn!("sent only {}/{} bytes of report", sent, buf.len()), @@ -208,29 +207,30 @@ fn main() -> ! { } Command::Show(ShowCommand::Pid) => { for channel in 0..CHANNELS { - let state = channels.channel_state(channel); - let _ = writeln!(socket, "PID settings for channel {}", channel); - let pid = &state.pid; - let _ = writeln!(socket, "- target={:.4} °C", pid.target); - macro_rules! show_pid_parameter { - ($p: tt) => { - let _ = writeln!( - socket, "- {}={:.4}", - stringify!($p), pid.parameters.$p + let send_free = socket.send_capacity() - socket.send_queue(); + match channels.channel_state(channel).pid.summary(channel).to_json() { + Ok(buf) if buf.len() > send_free + 1 => { + // Not enough buffer space, skip report for now + warn!( + "TCP socket has only {}/{} needed {}", + send_free + 1, socket.send_capacity(), buf.len(), ); - }; + } + Ok(buf) => { + match socket.send_slice(&buf) { + Ok(sent) if sent == buf.len() => { + let _ = socket.send_slice(b"\n"); + // success + } + Ok(sent) => + warn!("sent only {}/{} bytes of summary", sent, buf.len()), + Err(e) => + error!("error sending summary: {:?}", e), + } + } + Err(e) => + error!("unable to serialize pid summary: {:?}", e), } - show_pid_parameter!(kp); - show_pid_parameter!(ki); - show_pid_parameter!(kd); - show_pid_parameter!(integral_min); - show_pid_parameter!(integral_max); - show_pid_parameter!(output_min); - show_pid_parameter!(output_max); - if let Some(last_output) = pid.last_output { - let _ = writeln!(socket, "- last_output={:.3} A", last_output); - } - let _ = writeln!(socket, ""); } } Command::Show(ShowCommand::Pwm) => { diff --git a/src/pid.rs b/src/pid.rs index e7babc3..5ec8d56 100644 --- a/src/pid.rs +++ b/src/pid.rs @@ -81,11 +81,35 @@ impl Controller { output } - #[allow(dead_code)] pub fn reset(&mut self) { self.integral = 0.0; self.last_input = None; } + + pub fn summary(&self, channel: usize) -> Summary { + Summary { + channel, + parameters: self.parameters.clone(), + target: self.target, + integral: self.integral, + } + } +} + +type JsonBuffer = heapless::Vec; + +#[derive(Clone, Serialize, Deserialize)] +pub struct Summary { + channel: usize, + parameters: Parameters, + target: f64, + integral: f64, +} + +impl Summary { + pub fn to_json(&self) -> Result { + serde_json_core::to_vec(self) + } } #[cfg(test)] @@ -126,4 +150,13 @@ mod test { total_t += 1; } } + + #[test] + fn summary_to_json() { + let mut pid = Controller::new(PARAMETERS.clone()); + pid.target = 30.0 / 1.1; + let buf = pid.summary(0).to_json().unwrap(); + assert_eq!(buf[0], b'{'); + assert_eq!(buf[buf.len() - 1], b'}'); + } }