Better error catching when failing after connect
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum RemotePicoError {
|
||||
ConnectionRefused,
|
||||
ConnectionFailed,
|
||||
CantSetWriteTimeout,
|
||||
StreamWriteError,
|
||||
CantSetReadTimeout,
|
||||
StreamReadError,
|
||||
CorruptReadError,
|
||||
}
|
||||
|
||||
33
src/main.rs
33
src/main.rs
@@ -4,33 +4,39 @@ use std::{
|
||||
fs,
|
||||
io::{Read, Write},
|
||||
net::{SocketAddr, TcpStream},
|
||||
time::{self, Duration, Instant},
|
||||
time::{Duration, Instant},
|
||||
};
|
||||
|
||||
use chrono::Utc;
|
||||
|
||||
const CONNECT_TIMEOUT: Duration = Duration::from_millis(2500);
|
||||
const WRITE_TIMEOUT: Duration = Duration::from_millis(1000);
|
||||
const READ_TIMEOUT: Duration = Duration::from_millis(2000);
|
||||
|
||||
const CHECK_INTERVAL: Duration = Duration::from_secs(8);
|
||||
const SLEEP_INTERVAL: Duration = Duration::from_millis(200);
|
||||
|
||||
fn main() {
|
||||
let cur = Utc::now().format("%s");
|
||||
let mut log_file = fs::File::create(format!("logs/log_{}.csv", cur)).unwrap();
|
||||
writeln!(log_file, "Unix TS, Temp, Read, Hi, Lo").unwrap();
|
||||
let sleep_time = time::Duration::from_millis(100);
|
||||
|
||||
loop {
|
||||
let last_check = Instant::now();
|
||||
while last_check.elapsed().as_secs() < 5 {
|
||||
std::thread::sleep(sleep_time);
|
||||
while last_check.elapsed() < CHECK_INTERVAL {
|
||||
std::thread::sleep(SLEEP_INTERVAL);
|
||||
}
|
||||
|
||||
write!(log_file, "{}, ", Utc::now().format("%s")).unwrap();
|
||||
match get_pico_update() {
|
||||
Ok(vals) => {
|
||||
let vals_str: Vec<String> = vals.iter().map(|v| format!("{v}")).collect();
|
||||
|
||||
write!(log_file, "{}, ", Utc::now().format("%s")).unwrap();
|
||||
writeln!(log_file, "{}", vals_str.join(", ")).unwrap();
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error connecting to pico: {:?}", e);
|
||||
writeln!(log_file, "E, E, E, E").unwrap();
|
||||
writeln!(log_file, "{:?}, E, E, E", e).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,11 +44,20 @@ fn main() {
|
||||
|
||||
fn get_pico_update() -> Result<[u16; 4], err::RemotePicoError> {
|
||||
let socket_addr: SocketAddr = "192.168.1.201:1234".parse().unwrap();
|
||||
let Ok(mut stream) = TcpStream::connect_timeout(&socket_addr, Duration::from_secs_f32(1.5))
|
||||
else {
|
||||
return Err(err::RemotePicoError::ConnectionRefused);
|
||||
let Ok(mut stream) = TcpStream::connect_timeout(&socket_addr, CONNECT_TIMEOUT) else {
|
||||
return Err(err::RemotePicoError::ConnectionFailed);
|
||||
};
|
||||
|
||||
match stream.set_write_timeout(Some(WRITE_TIMEOUT)) {
|
||||
Err(_) => return Err(err::RemotePicoError::CantSetWriteTimeout),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match stream.set_read_timeout(Some(READ_TIMEOUT)) {
|
||||
Err(_) => return Err(err::RemotePicoError::CantSetReadTimeout),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match stream.write("Hello Pico!".as_bytes()) {
|
||||
Err(_) => return Err(err::RemotePicoError::StreamWriteError),
|
||||
_ => (),
|
||||
|
||||
Reference in New Issue
Block a user