main refactor: refine command handler
This commit is contained in:
parent
6473b27539
commit
31f7e7eead
|
@ -1,4 +1,4 @@
|
|||
use crate::{CHANNEL_CONFIG_KEY, ad7172, channels::{self, CHANNELS}, command_parser::PwmPin, config::ChannelConfig, dfu, flash_store::{FlashStore}, session::Session};
|
||||
use crate::{CHANNEL_CONFIG_KEY, ad7172, channels::{self, CHANNELS}, command_parser::{CenterPoint, PidParameter, PwmPin, ShParameter}, config::ChannelConfig, dfu, flash_store::{FlashStore}, session::Session};
|
||||
use channels::Channels;
|
||||
use smoltcp::socket::TcpSocket;
|
||||
use log::{error, warn};
|
||||
|
@ -34,7 +34,7 @@ pub enum Handler {
|
|||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Error {
|
||||
ParseFloat,
|
||||
|
||||
}
|
||||
|
||||
fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
||||
|
@ -65,22 +65,17 @@ fn send_line(socket: &mut TcpSocket, data: &[u8]) -> bool {
|
|||
|
||||
impl Handler {
|
||||
|
||||
pub fn handle_command (command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, leds: &mut Leds, store: &mut FlashStore, ipv4_config: &mut Ipv4Config) -> Result<Self, Error> {
|
||||
match command {
|
||||
Command::Quit =>
|
||||
// socket.close(),
|
||||
Ok(Handler::CloseSocket),
|
||||
|
||||
Command::Reporting(_reporting) => {
|
||||
// handled by session
|
||||
fn reporting(socket: &mut TcpSocket) -> Result<Handler, Error> {
|
||||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::Reporting) => {
|
||||
|
||||
fn show_report_mode(socket: &mut TcpSocket, session: &Session) -> Result<Handler, Error> {
|
||||
let _ = writeln!(socket, "{{ \"report\": {:?} }}", session.reporting());
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::Input) => {
|
||||
|
||||
fn show_report(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||
match channels.reports_json() {
|
||||
Ok(buf) => {
|
||||
send_line(socket, &buf[..]);
|
||||
|
@ -93,7 +88,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::Pid) => {
|
||||
|
||||
fn show_pid(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||
match channels.pid_summaries_json() {
|
||||
Ok(buf) => {
|
||||
send_line(socket, &buf);
|
||||
|
@ -105,7 +101,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::Pwm) => {
|
||||
|
||||
fn show_pwm(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||
match channels.pwm_summaries_json() {
|
||||
Ok(buf) => {
|
||||
send_line(socket, &buf);
|
||||
|
@ -117,7 +114,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::SteinhartHart) => {
|
||||
|
||||
fn show_steinhart_hart(socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||
match channels.steinhart_hart_summaries_json() {
|
||||
Ok(buf) => {
|
||||
send_line(socket, &buf);
|
||||
|
@ -129,7 +127,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::PostFilter) => {
|
||||
|
||||
fn show_post_filter (socket: &mut TcpSocket, channels: &mut Channels) -> Result<Handler, Error> {
|
||||
match channels.postfilter_summaries_json() {
|
||||
Ok(buf) => {
|
||||
send_line(socket, &buf);
|
||||
|
@ -141,20 +140,23 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Show(ShowCommand::Ipv4) => {
|
||||
|
||||
fn show_ipv4 (socket: &mut TcpSocket, ipv4_config: &mut Ipv4Config) -> Result<Handler, Error> {
|
||||
let (cidr, gateway) = net::split_ipv4_config(ipv4_config.clone());
|
||||
let _ = write!(socket, "{{\"addr\":\"{}\"", cidr);
|
||||
gateway.map(|gateway| write!(socket, ",\"gateway\":\"{}\"", gateway));
|
||||
let _ = writeln!(socket, "}}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::PwmPid { channel } => {
|
||||
|
||||
fn engage_pid (channels: &mut Channels, channel: usize, socket: &mut TcpSocket, leds: &mut Leds) -> Result<Handler, Error> {
|
||||
channels.channel_state(channel).pid_engaged = true;
|
||||
leds.g3.on();
|
||||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Pwm { channel, pin, value } => {
|
||||
|
||||
fn set_pwm (channels: &mut Channels, channel: usize, pin: PwmPin, value: f64, socket: &mut TcpSocket, leds: &mut Leds) -> Result<Handler, Error> {
|
||||
match pin {
|
||||
PwmPin::ISet => {
|
||||
channels.channel_state(channel).pid_engaged = false;
|
||||
|
@ -179,7 +181,8 @@ impl Handler {
|
|||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::CenterPoint { channel, center } => {
|
||||
|
||||
fn set_center_point(channels: &mut Channels, socket: &mut TcpSocket, channel: usize, center: CenterPoint) -> Result<Handler, Error> {
|
||||
let i_tec = channels.get_i(channel);
|
||||
let state = channels.channel_state(channel);
|
||||
state.center = center;
|
||||
|
@ -189,7 +192,8 @@ impl Handler {
|
|||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Pid { channel, parameter, value } => {
|
||||
|
||||
fn set_pid (channels: &mut Channels, socket: &mut TcpSocket, channel: usize, parameter: PidParameter, value: f64) -> Result<Handler, Error> {
|
||||
let pid = &mut channels.channel_state(channel).pid;
|
||||
use command_parser::PidParameter::*;
|
||||
match parameter {
|
||||
|
@ -213,7 +217,8 @@ impl Handler {
|
|||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::SteinhartHart { channel, parameter, value } => {
|
||||
|
||||
fn set_steinhart_hart (channels: &mut Channels, socket: &mut TcpSocket, channel: usize, parameter: ShParameter, value: f64) -> Result<Handler, Error> {
|
||||
let sh = &mut channels.channel_state(channel).sh;
|
||||
use command_parser::ShParameter::*;
|
||||
match parameter {
|
||||
|
@ -224,12 +229,14 @@ impl Handler {
|
|||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::PostFilter { channel, rate: None } => {
|
||||
|
||||
fn reset_post_filter (channels: &mut Channels, socket: &mut TcpSocket, channel: usize) -> Result<Handler, Error> {
|
||||
channels.adc.set_postfilter(channel as u8, None).unwrap();
|
||||
send_line(socket, b"{}");
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::PostFilter { channel, rate: Some(rate) } => {
|
||||
|
||||
fn set_post_filter (channels: &mut Channels, socket: &mut TcpSocket, channel: usize, rate: f32) -> Result<Handler, Error> {
|
||||
let filter = ad7172::PostFilter::closest(rate);
|
||||
match filter {
|
||||
Some(filter) => {
|
||||
|
@ -243,7 +250,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Load { channel } => {
|
||||
|
||||
fn load_channel (channels: &mut Channels, channel: Option<usize>, socket: &mut TcpSocket, store: &mut FlashStore) -> Result<Handler, Error> {
|
||||
for c in 0..CHANNELS {
|
||||
if channel.is_none() || channel == Some(c) {
|
||||
match store.read_value::<ChannelConfig>(CHANNEL_CONFIG_KEY[c]) {
|
||||
|
@ -264,7 +272,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Save { channel } => {
|
||||
|
||||
fn save_channel (channels: &mut Channels, channel: Option<usize>, socket: &mut TcpSocket, store: &mut FlashStore) -> Result<Handler, Error> {
|
||||
for c in 0..CHANNELS {
|
||||
let mut store_value_buf = [0u8; 256];
|
||||
if channel.is_none() || channel == Some(c) {
|
||||
|
@ -282,7 +291,8 @@ impl Handler {
|
|||
}
|
||||
Ok(Handler::Handled)
|
||||
}
|
||||
Command::Ipv4(config) => {
|
||||
|
||||
fn set_ipv4 (socket: &mut TcpSocket, config: Ipv4Config, store: &mut FlashStore) -> Result<Handler, Error> {
|
||||
let _ = store
|
||||
.write_value("ipv4", &config, [0; 16])
|
||||
.map_err(|e| error!("unable to save ipv4 config to flash: {:?}", e));
|
||||
|
@ -290,14 +300,16 @@ impl Handler {
|
|||
send_line(socket, b"{}");
|
||||
Ok(Handler::NewIPV4(new_ipv4_config.unwrap()))
|
||||
}
|
||||
Command::Reset => {
|
||||
|
||||
fn reset (channels: &mut Channels) -> Result<Handler, Error> {
|
||||
for i in 0..CHANNELS {
|
||||
channels.power_down(i);
|
||||
}
|
||||
// should_reset = true;
|
||||
Ok(Handler::Reset)
|
||||
}
|
||||
Command::Dfu => {
|
||||
|
||||
fn dfu (channels: &mut Channels) -> Result<Handler, Error> {
|
||||
for i in 0..CHANNELS {
|
||||
channels.power_down(i);
|
||||
}
|
||||
|
@ -307,6 +319,30 @@ impl Handler {
|
|||
// should_reset = true;
|
||||
Ok(Handler::Reset)
|
||||
}
|
||||
|
||||
pub fn handle_command (command: Command, socket: &mut TcpSocket, channels: &mut Channels, session: &Session, leds: &mut Leds, store: &mut FlashStore, ipv4_config: &mut Ipv4Config) -> Result<Self, Error> {
|
||||
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),
|
||||
Command::Show(ShowCommand::SteinhartHart) => Handler::show_steinhart_hart(socket, channels),
|
||||
Command::Show(ShowCommand::PostFilter) => Handler::show_post_filter(socket, channels),
|
||||
Command::Show(ShowCommand::Ipv4) => Handler::show_ipv4(socket, ipv4_config),
|
||||
Command::PwmPid { channel } => Handler::engage_pid(channels, channel, socket, leds),
|
||||
Command::Pwm { channel, pin, value } => Handler::set_pwm(channels, channel, pin, value, socket, leds),
|
||||
Command::CenterPoint { channel, center } => Handler::set_center_point(channels, socket, channel, center),
|
||||
Command::Pid { channel, parameter, value } => Handler::set_pid(channels, socket, channel, parameter, value),
|
||||
Command::SteinhartHart { channel, parameter, value } => Handler::set_steinhart_hart(channels, socket, channel, parameter, value),
|
||||
Command::PostFilter { channel, rate: None } => Handler::reset_post_filter(channels, socket, channel),
|
||||
Command::PostFilter { channel, rate: Some(rate) } => Handler::set_post_filter(channels, socket, channel, rate),
|
||||
Command::Load { channel } => Handler::load_channel(channels, channel, socket, store),
|
||||
Command::Save { channel } => Handler::save_channel(channels, channel, socket, store),
|
||||
Command::Ipv4(config) => Handler::set_ipv4(socket, config, store),
|
||||
Command::Reset => Handler::reset(channels),
|
||||
Command::Dfu => Handler::dfu(channels)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -208,7 +208,6 @@ fn main() -> ! {
|
|||
// Do nothing and feed more data to the line reader in the next loop cycle.
|
||||
Ok(SessionInput::Nothing) => {}
|
||||
Ok(SessionInput::Command(command)) => {
|
||||
// let handler = Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, store, ipv4_config);
|
||||
match Handler::handle_command(command, &mut socket, &mut channels, session, &mut leds, &mut store, &mut ipv4_config) {
|
||||
Ok(Handler::NewIPV4(ip)) => {
|
||||
new_ipv4_config = Some(ip);
|
||||
|
|
Loading…
Reference in New Issue