mqtt_mux: elaborate process error

This commit is contained in:
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
#[derive(Debug, Clone)]
pub enum MqttCommand {
ProcessError,
ProcessError(&'static str),
Reset,
Switch(u8, bool),
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) {
Ok(t) => t,
Err(_) => {
self.yet_to_respond = Some(MqttCommand::ProcessError);
self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot prase MQTT topic"));
return;
}
};
let command = match self.parse_message(topic, message) {
Ok((_, cmd)) => cmd,
Err(_) => {
self.yet_to_respond = Some(MqttCommand::ProcessError);
self.yet_to_respond = Some(MqttCommand::ProcessError("Cannot parse MQTT message"));
return;
}
};
self.yet_to_respond = match self.execute(command.clone()) {
Err(_) => Some(MqttCommand::ProcessError),
Err(_) => Some(MqttCommand::ProcessError("Cannot execute MQTT command")),
Ok(()) => Some(command)
};
}
@ -102,7 +102,7 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
match prev_cmd {
Some(cmd) => match cmd {
MqttCommand::ProcessError => {
MqttCommand::ProcessError(e_str) => {
vec.push((
{
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)?;
topic_string
},
String::from("Cannot parse the previous command.")
String::from(e_str)
)).map_err(|_| Error::VectorOutOfSpace)?;
Ok(vec)
}
@ -273,20 +273,6 @@ impl<'s, SPI, E> MqttMux<'s, SPI> where SPI: Transfer<u8, Error = E> {
)).map_err(|_| Error::VectorOutOfSpace)?;
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),
}
@ -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>> {
match command {
MqttCommand::ProcessError => Ok(()),
MqttCommand::ProcessError(_) => Ok(()),
MqttCommand::Reset => self.urukul.reset(),
MqttCommand::Switch(ch, state) => self.urukul.set_channel_switch(ch.into(), state),
MqttCommand::Attenuation(ch, ampl) => self.urukul.set_channel_attenuation(ch, ampl),