mqtt_mux: elaborate process error

pull/4/head
occheung 2020-09-30 15:57:33 +08:00
parent b577c8b715
commit 430fc12f54
1 changed files with 7 additions and 21 deletions

View File

@ -35,7 +35,7 @@ pub enum MqttTopic {
// Such that Urukul accepts the enum directly // Such that Urukul accepts the enum directly
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum MqttCommand { pub enum MqttCommand {
ProcessError, ProcessError(&'static str),
Reset, Reset,
Switch(u8, bool), Switch(u8, bool),
Attenuation(u8, f32), Attenuation(u8, f32),
@ -74,19 +74,19 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
let topic = match self.parse_topic(topic) { let topic = match self.parse_topic(topic) {
Ok(t) => t, Ok(t) => t,
Err(_) => { Err(_) => {
self.yet_to_respond = Some(MqttCommand::ProcessError); self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot prase MQTT topic"));
return; return;
} }
}; };
let command = match self.parse_message(topic, message) { let command = match self.parse_message(topic, message) {
Ok((_, cmd)) => cmd, Ok((_, cmd)) => cmd,
Err(_) => { Err(_) => {
self.yet_to_respond = Some(MqttCommand::ProcessError); self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot parse MQTT message"));
return; return;
} }
}; };
self.yet_to_respond = match self.execute(command.clone()) { self.yet_to_respond = match self.execute(command.clone()) {
Err(_) => Some(MqttCommand::ProcessError), Err(_) => Some(MqttCommand::ProcessError("Cannot execute MQTT command")),
Ok(()) => Some(command) Ok(()) => Some(command)
}; };
} }
@ -102,7 +102,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
match prev_cmd { match prev_cmd {
Some(cmd) => match cmd { Some(cmd) => match cmd {
MqttCommand::ProcessError => { MqttCommand::ProcessError(e_str) => {
vec.push(( vec.push((
{ {
let mut topic_string = String::from(self.name); let mut topic_string = String::from(self.name);
@ -110,7 +110,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
.map_err(|_| Error::StringOutOfSpace)?; .map_err(|_| Error::StringOutOfSpace)?;
topic_string topic_string
}, },
String::from("Cannot parse the previous command.") String::from(e_str)
)).map_err(|_| Error::VectorOutOfSpace)?; )).map_err(|_| Error::VectorOutOfSpace)?;
Ok(vec) Ok(vec)
} }
@ -273,20 +273,6 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
)).map_err(|_| Error::VectorOutOfSpace)?; )).map_err(|_| Error::VectorOutOfSpace)?;
Ok(vec) Ok(vec)
} }
// _ => {
// vec.push((
// {
// let mut topic_string = String::from(self.name);
// topic_string.push_str("/Feedback/Unimplemented")
// .map_err(|_| Error::StringOutOfSpace)?;
// topic_string
// },
// String::from("test")
// ))
// .map_err(|_| Error::VectorOutOfSpace)?;
// Ok(vec)
// }
}, },
None => Ok(vec), None => Ok(vec),
} }
@ -494,7 +480,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
fn execute(&mut self, command: MqttCommand) -> Result<(), Error<E>> { fn execute(&mut self, command: MqttCommand) -> Result<(), Error<E>> {
match command { match command {
MqttCommand::ProcessError => Ok(()), MqttCommand::ProcessError(_) => Ok(()),
MqttCommand::Reset => self.urukul.reset(), MqttCommand::Reset => self.urukul.reset(),
MqttCommand::Switch(ch, state) => self.urukul.set_channel_switch(ch.into(), state), MqttCommand::Switch(ch, state) => self.urukul.set_channel_switch(ch.into(), state),
MqttCommand::Attenuation(ch, ampl) => self.urukul.set_channel_attenuation(ch, ampl), MqttCommand::Attenuation(ch, ampl) => self.urukul.set_channel_attenuation(ch, ampl),