Update init and project name
All checks were successful
Basic Cargo Checks / docker-build (push) Successful in 1m24s
Basic Cargo Checks / docker-check (push) Successful in 3m32s

Project is no longer Proof Of Concept.
Modified Device init to be more consistent.
Started explicit typing of variables.
This commit is contained in:
Blizzard Finnegan 2023-06-16 09:53:46 -04:00
parent c2ea32f2c9
commit 823543ff03
Signed by: blizzardfinnegan
GPG key ID: 61C1E13067E0018E
5 changed files with 58 additions and 26 deletions

2
Cargo.lock generated
View file

@ -638,7 +638,7 @@ dependencies = [
] ]
[[package]] [[package]]
name = "seymour_poc_rust" name = "seymour_life_rust"
version = "2.2.0" version = "2.2.0"
dependencies = [ dependencies = [
"chrono", "chrono",

View file

@ -1,7 +1,7 @@
#cargo-features = ["per-package-target"] #cargo-features = ["per-package-target"]
[package] [package]
name = "seymour_poc_rust" name = "seymour_life_rust"
version = "2.2.0" version = "2.2.0"
edition = "2021" edition = "2021"

View file

@ -40,10 +40,10 @@ impl Device{
_ = fs::create_dir(&OUTPUT_FOLDER); _ = fs::create_dir(&OUTPUT_FOLDER);
}; };
log::debug!("{:?}",&self.serial); log::debug!("{:?}",&self.serial);
let output_path = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt"; let output_path:String = OUTPUT_FOLDER.to_owned() + &self.serial + ".txt";
if ! Path::new(&output_path).exists(){ if ! Path::new(&output_path).exists(){
log::debug!("Creating file {}",&output_path); log::debug!("Creating file {}",&output_path);
let temp = fs::File::create(&output_path); let temp:Result<File, std::io::Error> = fs::File::create(&output_path);
match temp{ match temp{
Ok(file) => { Ok(file) => {
self.output_file = Some(file); self.output_file = Some(file);
@ -55,17 +55,17 @@ impl Device{
} }
} }
else { else {
let temp = std::fs::read_to_string(output_path); let temp:Result<String, std::io::Error> = std::fs::read_to_string(output_path);
match temp{ match temp{
Ok(file_contents) =>{ Ok(file_contents) =>{
let file_lines = file_contents.split("\n"); let file_lines:Vec<&str> = file_contents.split("\n").collect();
log::trace!("{:?}",file_contents); log::trace!("{:?}",file_contents);
for line in file_lines { for line in file_lines {
if line.len() > 0{ if line.len() > 0{
log::trace!("{:?}",line); log::trace!("{:?}",line);
let section_and_data:Vec<&str> = line.split(": ").collect(); let section_and_data:Vec<&str> = line.split(": ").collect();
let section = section_and_data[0]; let section:&str = section_and_data[0];
let possible_value = section_and_data[1].trim().parse::<u64>(); let possible_value:Result<u64, std::num::ParseIntError> = section_and_data[1].trim().parse::<u64>();
match possible_value{ match possible_value{
Ok(value) => { Ok(value) => {
log::trace!("{:?} value: [{:?}]",section,value); log::trace!("{:?} value: [{:?}]",section,value);
@ -83,7 +83,7 @@ impl Device{
}; };
} }
Err(_) => { Err(_) => {
log::warn!("Unable to parse value [{}] into integer",section_and_data[1]); log::warn!("Unable to parse value [{:?}] into integer",section_and_data);
} }
} }
}; };
@ -107,14 +107,32 @@ impl Device{
_ = usb_port.read_from_device(None); _ = usb_port.read_from_device(None);
initial_state = State::LoginPrompt; initial_state = State::LoginPrompt;
}, },
//Response::Empty parsing here is potentially in bad faith
Response::Other | Response::Empty | Response::ShellPrompt | Response::Other | Response::Empty | Response::ShellPrompt |
Response::LoginPrompt | Response::ShuttingDown | Response::Rebooting => Response::LoginPrompt | Response::ShuttingDown | Response::Rebooting =>
initial_state = State::LoginPrompt, initial_state = State::LoginPrompt,
Response::BPOn | Response::BPOff | Response::TempCount(_) => Response::BPOn | Response::BPOff | Response::TempCount(_) |
initial_state = State::LifecycleMenu, Response::DebugMenuReady | Response::DebugMenuWithContinuedMessage=>{
Response::DebugMenuReady | Response::DebugMenuWithContinuedMessage=> usb_port.write_to_device(Command::Quit);
initial_state = State::DebugMenu, _ = usb_port.read_from_device(None);
} usb_port.write_to_device(Command::Newline);
match usb_port.read_from_device(None){
Response::Rebooting => {
while usb_port.read_from_device(None) != Response::LoginPrompt {}
initial_state = State::LoginPrompt;
},
Response::ShellPrompt => {
usb_port.write_to_device(Command::Shutdown);
while usb_port.read_from_device(None) != Response::LoginPrompt {}
initial_state = State::LoginPrompt;
},
_ => {
log::error!("Unknown state for TTY {:?}!!! Consult logs immediately.",usb_port);
return Err("Failed TTY init. Unknown state, cannot trust.".to_string());
}
};
},
};
}, },
None => initial_state = State::LoginPrompt None => initial_state = State::LoginPrompt
}; };
@ -259,8 +277,8 @@ impl Device{
output_data.push_str(&self.bps.to_string()); output_data.push_str(&self.bps.to_string());
output_data.push_str("\n"); output_data.push_str("\n");
output_data.push_str(TEMP_SECTION); output_data.push_str(TEMP_SECTION);
log::trace!("Current temps: {}",self.temps); log::trace!("Current temps: [{}]",self.temps);
log::trace!("Initial temps: {}",self.init_temps); log::trace!("Initial temps: [{}]",self.init_temps);
let saved_temps = self.temps - self.init_temps; let saved_temps = self.temps - self.init_temps;
output_data.push_str(&saved_temps.to_string()); output_data.push_str(&saved_temps.to_string());
output_data.push_str("\n"); output_data.push_str("\n");
@ -359,6 +377,7 @@ impl Device{
match self.usb_tty.read_from_device(None){ match self.usb_tty.read_from_device(None){
Response::TempCount(count) => { Response::TempCount(count) => {
log::trace!("Count for device {} updated to {}",self.serial,count); log::trace!("Count for device {} updated to {}",self.serial,count);
self.temps = count;
return count return count
}, },
_ => {}, _ => {},
@ -369,6 +388,7 @@ impl Device{
match self.usb_tty.read_from_device(None){ match self.usb_tty.read_from_device(None){
Response::TempCount(count) => { Response::TempCount(count) => {
log::trace!("Count for device {} updated to {}",self.serial,count); log::trace!("Count for device {} updated to {}",self.serial,count);
self.temps = count;
return count return count
}, },
_ => {}, _ => {},
@ -420,16 +440,18 @@ impl Device{
pub fn reboot(&mut self) -> () { pub fn reboot(&mut self) -> () {
self.usb_tty.write_to_device(Command::Quit); self.usb_tty.write_to_device(Command::Quit);
let mut successful_reboot:bool = false; let mut successful_reboot:bool = false;
let mut exited_menu:bool = false;
loop{ loop{
match self.usb_tty.read_from_device(None){ match self.usb_tty.read_from_device(None){
Response::LoginPrompt => break, Response::LoginPrompt => break,
Response::Rebooting => { Response::Rebooting => {
log::trace!("Successful reboot detected for device {}.",self.serial); log::trace!("Successful reboot detected for device {}.",self.serial);
successful_reboot = true; successful_reboot = true;
if !exited_menu { log::info!("Unusual reboot detected for device {}. Please check logs.",self.serial); }
}, },
Response::ShuttingDown => { Response::ShuttingDown => {
log::warn!("Failed reboot on device {}!",self.serial); log::trace!("Exiting debug menu on device {}.",self.serial);
successful_reboot = false; exited_menu = true;
}, },
_ => {} _ => {}
} }

View file

@ -1,6 +1,6 @@
use seymour_poc_rust::{device::Device, use seymour_life_rust::{device::Device,
tty::{self,TTY,Response}, tty::{self,TTY,Response},
gpio_facade::GpioPins}; gpio_facade::GpioPins};
use std::{io::{stdin,stdout,Write}, use std::{io::{stdin,stdout,Write},
thread::{self, JoinHandle}, thread::{self, JoinHandle},
path::Path, path::Path,

View file

@ -23,6 +23,7 @@ pub enum Command{
Login, Login,
DebugMenu, DebugMenu,
Newline, Newline,
Shutdown,
} }
#[derive(Clone,Eq,Derivative,Debug)] #[derive(Clone,Eq,Derivative,Debug)]
@ -57,6 +58,7 @@ const COMMAND_MAP:Lazy<HashMap<Command,&str>> = Lazy::new(||HashMap::from([
(Command::RedrawMenu,"?"), (Command::RedrawMenu,"?"),
(Command::DebugMenu," python3 -m debugmenu; shutdown -r now\n"), (Command::DebugMenu," python3 -m debugmenu; shutdown -r now\n"),
(Command::Newline,"\n"), (Command::Newline,"\n"),
(Command::Shutdown,"shutdown -r now\n"),
])); ]));
const RESPONSES:[(&str,Response);10] = [ const RESPONSES:[(&str,Response);10] = [
@ -135,10 +137,18 @@ impl TTY{
let trimmed_line = single_line.trim(); let trimmed_line = single_line.trim();
match trimmed_line.rsplit_once(' '){ match trimmed_line.rsplit_once(' '){
None => return enum_value, None => return enum_value,
Some((header,temp_count)) => { Some((_header,temp_count)) => {
log::trace!("Header: {}",header); match temp_count.trim().parse::<u64>(){
log::trace!("Temp count: {}",temp_count); Err(_) => {
return Response::TempCount(temp_count.parse().unwrap_or(0)) log::error!("String {} from device {} unable to be parsed!",temp_count,self.tty.name().unwrap_or("unknown shell".to_string()));
return Response::TempCount(0)
},
Ok(parsed_temp_count) => {
//log::trace!("Header: {}",header);
log::trace!("parsed temp count for device {}: {}",self.tty.name().unwrap_or("unknown shell".to_string()),temp_count);
return Response::TempCount(parsed_temp_count)
}
}
} }
} }
} }