diff --git a/src/main.rs b/src/main.rs index 62f23f9..effe89d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -30,6 +30,8 @@ use tokio_serial; #[derive(Subcommand, Clone)] enum DeviceCommands { + /// Request current state + State, /// Set wifi credentials SetWifi { /// SSID of the network @@ -39,6 +41,12 @@ enum DeviceCommands { }, } +impl Default for DeviceCommands { + fn default() -> Self { + return Self::State; + } +} + #[derive(Subcommand, Clone)] enum Commands { /// List available serial devices @@ -120,41 +128,48 @@ async fn main() -> Result<()>{ .fold(String::new(), |a, b| a + &b + &String::from("\n"))); }, Commands::Device {path, baud_rate, device_command} => { - let request_current_state_packet = (RequestCurrentStateCommand {}).to_raw_packet(); + match &device_command.clone().unwrap_or_default() { + DeviceCommands::State => { + let request_current_state_packet = (RequestCurrentStateCommand {}).to_raw_packet(); - println!("{}", hex::encode(&request_current_state_packet.to_bytes())); - println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes())); + println!("{}", hex::encode(&request_current_state_packet.to_bytes())); + println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes())); - let mut serial_interface = tokio_serial::new(path, *baud_rate).open().unwrap(); + let mut serial_interface = tokio_serial::new(path, *baud_rate).open().unwrap(); - serial_interface.write(&request_current_state_packet.to_bytes()).unwrap(); + serial_interface.write(&request_current_state_packet.to_bytes()).unwrap(); - let mut buffer: Vec = Vec::new(); - serial_interface.read_to_end(&mut buffer); + let mut buffer: Vec = Vec::new(); + serial_interface.read_to_end(&mut buffer); - println!("{}", hex::encode(&buffer)); - println!("{}", to_ascii_debug(&buffer)); - println!("{}", std::str::from_utf8(&buffer).unwrap_or("")); + println!("{}", hex::encode(&buffer)); + println!("{}", to_ascii_debug(&buffer)); + println!("{}", std::str::from_utf8(&buffer).unwrap_or("")); - let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap(); - println!("{}", improv_packet_offset); + let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap(); + println!("{}", improv_packet_offset); - let improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]); - let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); + let improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]); + let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); - // version - println!("Version: {}", &raw_packet.version); + // version + println!("Version: {}", &raw_packet.version); - // type - println!("Type: {}", &raw_packet.r#type); + // type + println!("Type: {}", &raw_packet.r#type); - if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { - println!("Current state: {}", ¤t_state_response.current_state); - } + if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { + println!("Current state: {}", ¤t_state_response.current_state); + } - if let ImprovPacket::ErrorState(error_state) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { - println!("Error state: {}", &error_state.error_state); - } + if let ImprovPacket::ErrorState(error_state) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { + println!("Error state: {}", &error_state.error_state); + } + }, + DeviceCommands::SetWifi {ssid, password} => { + println!("Not implemented"); + } + }; }, };