From b336c4f9935903f6b4229212517e781085500fcd Mon Sep 17 00:00:00 2001 From: atse Date: Mon, 16 Sep 2024 11:52:15 +0800 Subject: [PATCH] Remove report mode As the report mode command breaks the send-receive architecture model of the command interface, and is deemed an anti-feature, making clients difficult to write, remove it from the firmware entirely. --- README.md | 9 ++------- pytec/pytec/client.py | 5 +++-- src/command_handler.rs | 15 +------------- src/command_parser.rs | 46 ++---------------------------------------- src/main.rs | 20 ++---------------- src/session.rs | 41 ------------------------------------- 6 files changed, 10 insertions(+), 126 deletions(-) diff --git a/README.md b/README.md index bf6cff8..e4558a3 100644 --- a/README.md +++ b/README.md @@ -84,9 +84,7 @@ invalidate the first line of input. ### Reading ADC input -Set report mode to `on` for a continuous stream of input data. - -The scope of this setting is per TCP session. +ADC input data is provided in reports. Query for the latest report with the command `report`. See the *Reports* section below. ### TCP commands @@ -97,8 +95,6 @@ formatted as line-delimited JSON. | Syntax | Function | |----------------------------------|-------------------------------------------------------------------------------| | `report` | Show current input | -| `report mode` | Show current report mode | -| `report mode ` | Set report mode | | `pwm` | Show current PWM settings | | `pwm <0/1> max_i_pos ` | Set maximum positive output current | | `pwm <0/1> max_i_neg ` | Set maximum negative output current | @@ -251,8 +247,7 @@ pwm 0 pid ## Reports -Use the bare `report` command to obtain a single report. Enable -continuous reporting with `report mode on`. Reports are JSON objects +Use the bare `report` command to obtain a single report. Reports are JSON objects with the following keys. | Key | Unit | Description | diff --git a/pytec/pytec/client.py b/pytec/pytec/client.py index 062d8dc..5ac2095 100644 --- a/pytec/pytec/client.py +++ b/pytec/pytec/client.py @@ -1,6 +1,7 @@ import socket import json import logging +import time class CommandError(Exception): pass @@ -126,9 +127,8 @@ class Client: 'tec_u_meas': 2.5340000000000003, 'pid_output': 2.067581958092247} """ - self._command("report mode", "on") - while True: + self._socket.sendall("report\n".encode('utf-8')) line = self._read_line() if not line: break @@ -136,6 +136,7 @@ class Client: yield json.loads(line) except json.decoder.JSONDecodeError: pass + time.sleep(0.05) def set_param(self, topic, channel, field="", value=""): """Set configuration parameters diff --git a/src/command_handler.rs b/src/command_handler.rs index 3f30083..3cf3b6e 100644 --- a/src/command_handler.rs +++ b/src/command_handler.rs @@ -22,7 +22,6 @@ use super::{ config::ChannelConfig, dfu, flash_store::FlashStore, - session::Session, FanCtrl, hw_rev::HWRev, }; @@ -87,16 +86,6 @@ fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool { impl Handler { - fn reporting(socket: &mut TcpSocket) -> Result { - send_line(socket, b"{}"); - Ok(Handler::Handled) - } - - fn show_report_mode(socket: &mut TcpSocket, session: &Session) -> Result { - let _ = writeln!(socket, "{{ \"report\": {:?} }}", session.reporting()); - Ok(Handler::Handled) - } - fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result { match channels.reports_json() { Ok(buf) => { @@ -412,11 +401,9 @@ impl Handler { } } - pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result { + pub fn handle_command(command: Command, socket: &mut TcpSocket, channels: &mut Channels, store: &mut FlashStore, ipv4_config: &mut Ipv4Config, fan_ctrl: &mut FanCtrl, hwrev: HWRev) -> Result { match command { Command::Quit => Ok(Handler::CloseSocket), - Command::Reporting(_reporting) => Handler::reporting(socket), - Command::Show(ShowCommand::Reporting) => Handler::show_report_mode(socket, session), Command::Show(ShowCommand::Input) => Handler::show_report(socket, channels), Command::Show(ShowCommand::Pid) => Handler::show_pid(socket, channels), Command::Show(ShowCommand::Pwm) => Handler::show_pwm(socket, channels), diff --git a/src/command_parser.rs b/src/command_parser.rs index b9e70bb..56a593e 100644 --- a/src/command_parser.rs +++ b/src/command_parser.rs @@ -96,7 +96,6 @@ pub struct Ipv4Config { #[derive(Debug, Clone, PartialEq)] pub enum ShowCommand { Input, - Reporting, Pwm, Pid, SteinhartHart, @@ -148,7 +147,6 @@ pub enum Command { Reset, Ipv4(Ipv4Config), Show(ShowCommand), - Reporting(bool), /// PWM parameter setting Pwm { channel: usize, @@ -233,12 +231,6 @@ fn float(input: &[u8]) -> IResult<&[u8], Result> { Ok((input, result)) } -fn off_on(input: &[u8]) -> IResult<&[u8], bool> { - alt((value(false, tag("off")), - value(true, tag("on")) - ))(input) -} - fn channel(input: &[u8]) -> IResult<&[u8], usize> { map(one_of("01"), |c| (c as usize) - ('0' as usize))(input) } @@ -246,24 +238,8 @@ fn channel(input: &[u8]) -> IResult<&[u8], usize> { fn report(input: &[u8]) -> IResult<&[u8], Command> { preceded( tag("report"), - alt(( - preceded( - whitespace, - preceded( - tag("mode"), - alt(( - preceded( - whitespace, - // `report mode ` - Switch repoting mode - map(off_on, Command::Reporting) - ), - // `report mode` - Show current reporting state - value(Command::Show(ShowCommand::Reporting), end) - )) - )), - // `report` - Report once - value(Command::Show(ShowCommand::Input), end) - )) + // `report` - Report once + value(Command::Show(ShowCommand::Input), end) )(input) } @@ -682,24 +658,6 @@ mod test { assert_eq!(command, Ok(Command::Show(ShowCommand::Input))); } - #[test] - fn parse_report_mode() { - let command = Command::parse(b"report mode"); - assert_eq!(command, Ok(Command::Show(ShowCommand::Reporting))); - } - - #[test] - fn parse_report_mode_on() { - let command = Command::parse(b"report mode on"); - assert_eq!(command, Ok(Command::Reporting(true))); - } - - #[test] - fn parse_report_mode_off() { - let command = Command::parse(b"report mode off"); - assert_eq!(command, Ok(Command::Reporting(false))); - } - #[test] fn parse_pwm_i_set() { let command = Command::parse(b"pwm 1 i_set 16383"); diff --git a/src/main.rs b/src/main.rs index 353a161..7c874e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -180,10 +180,7 @@ fn main() -> ! { loop { let mut new_ipv4_config = None; let instant = Instant::from_millis(i64::from(timer::now())); - let updated_channel = channels.poll_adc(instant); - if let Some(channel) = updated_channel { - server.for_each(|_, session| session.set_report_pending(channel.into())); - } + channels.poll_adc(instant); fan_ctrl.cycle(channels.current_abs_max_tec_i()); @@ -216,7 +213,7 @@ fn main() -> ! { // Do nothing and feed more data to the line reader in the next loop cycle. Ok(SessionInput::Nothing) => {} Ok(SessionInput::Command(command)) => { - match Handler::handle_command(command, &mut socket, &mut channels, session, &mut store, &mut ipv4_config, &mut fan_ctrl, hwrev) { + match Handler::handle_command(command, &mut socket, &mut channels, &mut store, &mut ipv4_config, &mut fan_ctrl, hwrev) { Ok(Handler::NewIPV4(ip)) => new_ipv4_config = Some(ip), Ok(Handler::Handled) => {}, Ok(Handler::CloseSocket) => socket.close(), @@ -231,19 +228,6 @@ fn main() -> ! { Err(_) => socket.close(), } - } else if socket.can_send() { - if let Some(channel) = session.is_report_pending() { - match channels.reports_json() { - Ok(buf) => { - send_line(&mut socket, &buf[..]); - session.mark_report_sent(channel); - } - Err(e) => { - error!("unable to serialize report: {:?}", e); - - } - } - } } }); } else { diff --git a/src/session.rs b/src/session.rs index f9e25b0..38b5471 100644 --- a/src/session.rs +++ b/src/session.rs @@ -1,5 +1,4 @@ use super::command_parser::{Command, Error as ParserError}; -use super::channels::CHANNELS; const MAX_LINE_LEN: usize = 64; @@ -53,8 +52,6 @@ impl From> for SessionInput { pub struct Session { reader: LineReader, - reporting: bool, - report_pending: [bool; CHANNELS], } impl Default for Session { @@ -67,43 +64,11 @@ impl Session { pub fn new() -> Self { Session { reader: LineReader::new(), - reporting: false, - report_pending: [false; CHANNELS], } } pub fn reset(&mut self) { self.reader = LineReader::new(); - self.reporting = false; - self.report_pending = [false; CHANNELS]; - } - - pub fn reporting(&self) -> bool { - self.reporting - } - - pub fn set_report_pending(&mut self, channel: usize) { - if self.reporting { - self.report_pending[channel] = true; - } - } - - pub fn is_report_pending(&self) -> Option { - if ! self.reporting { - None - } else { - self.report_pending.iter() - .enumerate() - .fold(None, |result, (channel, report_pending)| { - result.or_else(|| { - if *report_pending { Some(channel) } else { None } - }) - }) - } - } - - pub fn mark_report_sent(&mut self, channel: usize) { - self.report_pending[channel] = false; } pub fn feed(&mut self, buf: &[u8]) -> (usize, SessionInput) { @@ -114,12 +79,6 @@ impl Session { match line { Some(line) => { let command = Command::parse(&line); - match command { - Ok(Command::Reporting(reporting)) => { - self.reporting = reporting; - } - _ => {} - } return (buf_bytes, command.into()); } None => {}