Handle device commands
This commit is contained in:
parent
cd3282a69d
commit
945c8386ad
63
src/main.rs
63
src/main.rs
@ -30,6 +30,8 @@ use tokio_serial;
|
|||||||
|
|
||||||
#[derive(Subcommand, Clone)]
|
#[derive(Subcommand, Clone)]
|
||||||
enum DeviceCommands {
|
enum DeviceCommands {
|
||||||
|
/// Request current state
|
||||||
|
State,
|
||||||
/// Set wifi credentials
|
/// Set wifi credentials
|
||||||
SetWifi {
|
SetWifi {
|
||||||
/// SSID of the network
|
/// SSID of the network
|
||||||
@ -39,6 +41,12 @@ enum DeviceCommands {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for DeviceCommands {
|
||||||
|
fn default() -> Self {
|
||||||
|
return Self::State;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Subcommand, Clone)]
|
#[derive(Subcommand, Clone)]
|
||||||
enum Commands {
|
enum Commands {
|
||||||
/// List available serial devices
|
/// List available serial devices
|
||||||
@ -120,41 +128,48 @@ async fn main() -> Result<()>{
|
|||||||
.fold(String::new(), |a, b| a + &b + &String::from("\n")));
|
.fold(String::new(), |a, b| a + &b + &String::from("\n")));
|
||||||
},
|
},
|
||||||
Commands::Device {path, baud_rate, device_command} => {
|
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!("{}", hex::encode(&request_current_state_packet.to_bytes()));
|
||||||
println!("{}", to_ascii_debug(&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<u8> = Vec::new();
|
let mut buffer: Vec<u8> = Vec::new();
|
||||||
serial_interface.read_to_end(&mut buffer);
|
serial_interface.read_to_end(&mut buffer);
|
||||||
|
|
||||||
println!("{}", hex::encode(&buffer));
|
println!("{}", hex::encode(&buffer));
|
||||||
println!("{}", to_ascii_debug(&buffer));
|
println!("{}", to_ascii_debug(&buffer));
|
||||||
println!("{}", std::str::from_utf8(&buffer).unwrap_or(""));
|
println!("{}", std::str::from_utf8(&buffer).unwrap_or(""));
|
||||||
|
|
||||||
let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap();
|
let improv_packet_offset = find_begin_of_improv_packet(&buffer).unwrap();
|
||||||
println!("{}", improv_packet_offset);
|
println!("{}", improv_packet_offset);
|
||||||
|
|
||||||
let improv_packet_end = improv_packet_offset + 10 + <u8 as Into<usize>>::into(buffer[improv_packet_offset+8]);
|
let improv_packet_end = improv_packet_offset + 10 + <u8 as Into<usize>>::into(buffer[improv_packet_offset+8]);
|
||||||
let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap();
|
let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap();
|
||||||
|
|
||||||
// version
|
// version
|
||||||
println!("Version: {}", &raw_packet.version);
|
println!("Version: {}", &raw_packet.version);
|
||||||
|
|
||||||
// type
|
// type
|
||||||
println!("Type: {}", &raw_packet.r#type);
|
println!("Type: {}", &raw_packet.r#type);
|
||||||
|
|
||||||
if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
|
if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
|
||||||
println!("Current state: {}", ¤t_state_response.current_state);
|
println!("Current state: {}", ¤t_state_response.current_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
if let ImprovPacket::ErrorState(error_state) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
|
if let ImprovPacket::ErrorState(error_state) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
|
||||||
println!("Error state: {}", &error_state.error_state);
|
println!("Error state: {}", &error_state.error_state);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
DeviceCommands::SetWifi {ssid, password} => {
|
||||||
|
println!("Not implemented");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user