Better error catching when failing after connect

This commit is contained in:
JP Stringham
2026-02-05 09:21:27 -05:00
parent 49785e9707
commit c60e0d2713
2 changed files with 27 additions and 10 deletions

View File

@@ -1,7 +1,9 @@
#[derive(Debug, PartialEq, Eq)] #[derive(Debug, PartialEq, Eq)]
pub enum RemotePicoError { pub enum RemotePicoError {
ConnectionRefused, ConnectionFailed,
CantSetWriteTimeout,
StreamWriteError, StreamWriteError,
CantSetReadTimeout,
StreamReadError, StreamReadError,
CorruptReadError, CorruptReadError,
} }

View File

@@ -4,33 +4,39 @@ use std::{
fs, fs,
io::{Read, Write}, io::{Read, Write},
net::{SocketAddr, TcpStream}, net::{SocketAddr, TcpStream},
time::{self, Duration, Instant}, time::{Duration, Instant},
}; };
use chrono::Utc; 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() { fn main() {
let cur = Utc::now().format("%s"); let cur = Utc::now().format("%s");
let mut log_file = fs::File::create(format!("logs/log_{}.csv", cur)).unwrap(); let mut log_file = fs::File::create(format!("logs/log_{}.csv", cur)).unwrap();
writeln!(log_file, "Unix TS, Temp, Read, Hi, Lo").unwrap(); writeln!(log_file, "Unix TS, Temp, Read, Hi, Lo").unwrap();
let sleep_time = time::Duration::from_millis(100);
loop { loop {
let last_check = Instant::now(); let last_check = Instant::now();
while last_check.elapsed().as_secs() < 5 { while last_check.elapsed() < CHECK_INTERVAL {
std::thread::sleep(sleep_time); std::thread::sleep(SLEEP_INTERVAL);
} }
write!(log_file, "{}, ", Utc::now().format("%s")).unwrap();
match get_pico_update() { match get_pico_update() {
Ok(vals) => { Ok(vals) => {
let vals_str: Vec<String> = vals.iter().map(|v| format!("{v}")).collect(); 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(); writeln!(log_file, "{}", vals_str.join(", ")).unwrap();
} }
Err(e) => { Err(e) => {
println!("Error connecting to pico: {:?}", 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> { fn get_pico_update() -> Result<[u16; 4], err::RemotePicoError> {
let socket_addr: SocketAddr = "192.168.1.201:1234".parse().unwrap(); 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)) let Ok(mut stream) = TcpStream::connect_timeout(&socket_addr, CONNECT_TIMEOUT) else {
else { return Err(err::RemotePicoError::ConnectionFailed);
return Err(err::RemotePicoError::ConnectionRefused);
}; };
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()) { match stream.write("Hello Pico!".as_bytes()) {
Err(_) => return Err(err::RemotePicoError::StreamWriteError), Err(_) => return Err(err::RemotePicoError::StreamWriteError),
_ => (), _ => (),