|
|
|
@ -1,14 +1,24 @@ |
|
|
|
pub struct Protocol { |
|
|
|
segments: Vec<bool>, |
|
|
|
running: bool, |
|
|
|
} |
|
|
|
|
|
|
|
impl Protocol { |
|
|
|
pub fn new() -> Protocol { |
|
|
|
Protocol { |
|
|
|
segments: [false, false, false, false, false, false, false, false].to_vec(), |
|
|
|
segments: [false, false, false, false, false, false, false, false].to_vec(), |
|
|
|
running: false, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn start(&mut self) { |
|
|
|
self.running = true; |
|
|
|
} |
|
|
|
|
|
|
|
pub fn is_running(&self) -> bool{ |
|
|
|
self.running |
|
|
|
} |
|
|
|
|
|
|
|
pub fn get_segment(&self, index: usize) -> Result<bool, &'static str> { |
|
|
|
if index < 8 && index >= 0 { |
|
|
|
Ok(self.segments[index]) |
|
|
|
@ -20,32 +30,40 @@ impl Protocol { |
|
|
|
pub fn parse(&mut self, input : &str) -> Result<&'static str, &'static str> { |
|
|
|
let arguments:Vec<&str> = input.split_whitespace().collect(); |
|
|
|
if arguments.len() < 1 { |
|
|
|
self.running = false; |
|
|
|
return Err("No argument found!"); |
|
|
|
} |
|
|
|
match arguments[0] { |
|
|
|
"setsegment" => { |
|
|
|
if arguments.len() != 3 { |
|
|
|
self.running = false; |
|
|
|
return Err("Wrong number of arguments!"); |
|
|
|
} |
|
|
|
let index = arguments[1].parse::<usize>().unwrap(); |
|
|
|
let value = match arguments[2] { |
|
|
|
"on" => true, |
|
|
|
"off" => false, |
|
|
|
_ => self.segments[index], |
|
|
|
}; |
|
|
|
self.segments[index] = value; |
|
|
|
} |
|
|
|
"display" => { |
|
|
|
//show the saved state on the display
|
|
|
|
println!("Showing state on display"); |
|
|
|
print!("Showing state on display: "); |
|
|
|
for seg in self.segments.clone() { |
|
|
|
print!("{}, ", seg); |
|
|
|
} |
|
|
|
println!(); |
|
|
|
} |
|
|
|
"exit" => { |
|
|
|
//close connection
|
|
|
|
println!("Closing connection."); |
|
|
|
} |
|
|
|
"shutdown" => { |
|
|
|
//exit program
|
|
|
|
println!("Closing connection and shut down server"); |
|
|
|
self.running = false; |
|
|
|
} |
|
|
|
_ => { |
|
|
|
println!("Err: Command not found"); |
|
|
|
return Err("Command not found\n"); |
|
|
|
println!("Err: Command not found, closing connection!"); |
|
|
|
self.running = false; |
|
|
|
return Err("Command not found!"); |
|
|
|
} |
|
|
|
} |
|
|
|
Ok("OK") |
|
|
|
|