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 => {}