send received messages via mqtt in modem main

This commit is contained in:
Vladan Popovic 2022-07-20 13:32:13 +02:00
parent 23ff182b65
commit 78df516fba
5 changed files with 52 additions and 22 deletions

View file

@ -1,5 +1,6 @@
use crate::command::Command;
use crate::serial::SerialIO;
use crate::types::*;
use anyhow;
use std::{
@ -406,11 +407,6 @@ impl<UART: serial::Uart> std::io::Write for Modem<UART> {
}
}
pub enum Msg {
Location(String),
Movement(String),
}
pub fn main<T: Sync + Send>(
rx: esp_idf_hal::gpio::Gpio26<T>,
tx: esp_idf_hal::gpio::Gpio27<T>,
@ -484,10 +480,28 @@ pub fn main<T: Sync + Send>(
let _ = mdm.mqtt_connect(device_id)?;
println!("entering queue receive loop ...");
while let Ok(Msg::Location(msg)) = receiver.recv() {
println!("received message {} | sending to mqtt ...", msg);
let _ = mdm.mqtt_publish(device_id, &msg)?;
}
let mut err_count = 0;
loop {
match receiver.recv() {
Ok(Msg::Gps(solution)) => {
println!("received GPS solution {:?} | sending to mqtt ...", solution);
let _ = mdm.mqtt_publish(device_id, &format!("{:?}", solution))?;
},
Ok(Msg::Accelerometer(acc)) => {
println!("received message {} | sending to mqtt ...", acc);
let _ = mdm.mqtt_publish(device_id, &format!("{:?}", acc))?;
}
Err(e) => {
println!("received error {} | NOT sending to mqtt ...", e);
if err_count < 10 {
err_count += 1;
}
else {
break
}
}
}
};
let _ = mdm.tcp_close_connection()?;
}