Display device info
This commit is contained in:
@@ -120,6 +120,20 @@ pub enum RPCCommand {
|
||||
RequestDeviceInformation = 0x03,
|
||||
}
|
||||
|
||||
impl TryFrom<&u8> for RPCCommand {
|
||||
type Error= &'static str;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
0x01 => Ok(Self::SendWifiSettings),
|
||||
0x02 => Ok(Self::RequestCurrentState),
|
||||
0x03 => Ok(Self::RequestDeviceInformation),
|
||||
_ => Err("Cannot convert to RPC command"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn calculate_checksum(data: &[u8]) -> u8 {
|
||||
// Pass data as full packet, with header, but without checksum byte
|
||||
|
||||
@@ -318,7 +332,8 @@ impl ImprovDataToPacket for RequestDeviceInformationPacket {
|
||||
}
|
||||
|
||||
pub struct RPCResultPacket {
|
||||
results: Vec<String>,
|
||||
pub command_responded_to: RPCCommand,
|
||||
pub results: Vec<String>,
|
||||
}
|
||||
|
||||
impl ImprovDataPacketType for RPCResultPacket {
|
||||
@@ -333,9 +348,30 @@ impl ImprovDataFromPacket for RPCResultPacket {
|
||||
return Err("Packet is not RPCResult");
|
||||
}
|
||||
|
||||
let results: Vec<String> = Vec::new();
|
||||
let mut results: Vec<String> = Vec::new();
|
||||
|
||||
let mut data_position: usize = 2;
|
||||
loop {
|
||||
if data_position >= raw_packet.data.len() {
|
||||
break;
|
||||
}
|
||||
|
||||
// find string bounds
|
||||
let current_string_len: usize = usize::from(raw_packet.data[data_position]);
|
||||
let current_string_begin = data_position + 1;
|
||||
let current_string_end = data_position + current_string_len;
|
||||
|
||||
// load string and append to results
|
||||
let string_bytes = &raw_packet.data[current_string_begin..current_string_end+1];
|
||||
let string = std::str::from_utf8(string_bytes).unwrap().to_string();
|
||||
results.push(string);
|
||||
|
||||
// next data position
|
||||
data_position = data_position + current_string_len + 1;
|
||||
}
|
||||
|
||||
return Ok(Self {
|
||||
command_responded_to: RPCCommand::try_from(&raw_packet.data[0]).unwrap(),
|
||||
results: results,
|
||||
})
|
||||
}
|
||||
|
Reference in New Issue
Block a user