don't panic on failed u32 parsing in get_stats

This commit is contained in:
Данила Горнушко 2024-06-05 12:23:58 +03:00
parent acac35dbe3
commit 139f7485e9

View file

@ -460,24 +460,37 @@ fn handle_error_with_timeout(err_counter: &mut u32) {
fn get_stats(port: &mut Box<dyn SerialPort>, id: u32) -> StatsOptions {
let command = format!("@{} adcstats\n", id);
let response = process_command(port, command).unwrap_or_default();
let response = match process_command(port, command) {
Some(resp) => resp,
None => return StatsOptions::None,
};
let split: Vec<&str> = response.split_whitespace().collect();
let num_parse_err = "Can't parse num in ADC stats!";
match split.len() {
let mut nums: Vec<u32> = Vec::new();
for i in split {
match i.parse() {
Ok(num) => nums.push(num),
Err(_) => return StatsOptions::None,
}
}
match nums.len() {
2 => StatsOptions::Stats(Stats10Min {
hail: split[0].parse().expect(num_parse_err),
noise: split[1].parse().expect(num_parse_err),
hail: nums[0],
noise: nums[1],
}),
4 => StatsOptions::DetectedNew(StatsOnDetect {
hail_since_startup: split[0].parse().expect(num_parse_err),
hail_in_10min: split[1].parse().expect(num_parse_err),
noise_since_startup: split[2].parse().expect(num_parse_err),
noise_in_10min: split[3].parse().expect(num_parse_err),
hail_since_startup: nums[0],
hail_in_10min: nums[1],
noise_since_startup: nums[2],
noise_in_10min: nums[3],
}),
_ => StatsOptions::None,
}
}
fn get_set_param(
port: &mut Box<dyn SerialPort>,
id: u32,