refactor sending data to remote server

works as expected now, but far from perfect
This commit is contained in:
Vladan Popovic 2022-06-20 00:12:34 +02:00
parent 4fefd547d1
commit 84ab94df46

View file

@ -178,21 +178,24 @@ impl<UART: serial::Uart> Modem<UART> {
self.read_response(cmd.contains, cmd.timeout) self.read_response(cmd.contains, cmd.timeout)
} }
fn send_command_data(&mut self, cmd: Command, payload: &str) -> Result<String> { fn send_data(&mut self, payload: &str) -> Result<String> {
println!("-----------------------------------------------------------"); let _ = self.send_bytes("AT+CIPSEND".as_bytes())?;
println!("Sending {} ...", cmd.text); for c in self.rx.reset(Duration::from_millis(2000)).map(char::from) {
let _ = self.send_bytes(cmd.text.as_bytes())?; println!("{}", c);
if c == '>' {
let prompt: String = self.rx.reset(cmd.timeout) for b in payload.as_bytes() {
.map(|b| char::from(b)) self.send(*b)?;
.take_while(|c| *c != ' ') }
.collect(); self.send(26)?;
return self.read_response(Some("DATA ACCEPT".to_string()), Duration::from_millis(3000));
if prompt != ">".to_string() { }
println!("invalid prompt: {}", prompt);
} }
let _ = self.send_bytes(payload.as_bytes())?; self.send_command(Command {
self.read_response(cmd.contains, cmd.timeout) text: "AT+CIPACK".to_string(),
contains: Some("OK".to_string()),
timeout: Duration::from_millis(3000),
})?;
Ok("OK".to_string())
} }
pub fn get_ip_addr(&mut self) -> Result<String> { pub fn get_ip_addr(&mut self) -> Result<String> {
@ -259,8 +262,7 @@ impl<UART: serial::Uart> Modem<UART> {
} }
pub fn tcp_send(&mut self, payload: &str) -> Result<()> { pub fn tcp_send(&mut self, payload: &str) -> Result<()> {
self.send_command(Command::tcp_send_size(payload.len()))?; self.send_data(payload)?;
self.send_command_data(Command::tcp_send(), payload)?;
Ok(()) Ok(())
} }
} }