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