Improve write test
All checks were successful
Midea Merge / Serial Reader (pull_request) Successful in 13m59s
All checks were successful
Midea Merge / Serial Reader (pull_request) Successful in 13m59s
This commit is contained in:
parent
2cf725b5aa
commit
56fe4beb2a
3 changed files with 84 additions and 26 deletions
|
@ -2,6 +2,9 @@ use super::{body::Body, header::Header, Command, MessageType};
|
|||
use std::ops::Deref;
|
||||
|
||||
pub trait RequestSerializer {
|
||||
fn message_type(&self) -> MessageType;
|
||||
fn body_type(&self) -> u8;
|
||||
|
||||
fn serialize(&self) -> Vec<u8>;
|
||||
}
|
||||
|
||||
|
@ -65,6 +68,14 @@ impl RequestSerializer for CommandRequest {
|
|||
|
||||
stream
|
||||
}
|
||||
|
||||
fn message_type(&self) -> MessageType {
|
||||
self.header().message_type()
|
||||
}
|
||||
|
||||
fn body_type(&self) -> u8 {
|
||||
self.body().body_type()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CommandQuerySubtype {
|
||||
|
@ -102,6 +113,14 @@ impl RequestSerializer for CommandQuerySubtype {
|
|||
fn serialize(&self) -> Vec<u8> {
|
||||
self.command.serialize()
|
||||
}
|
||||
|
||||
fn message_type(&self) -> MessageType {
|
||||
self.header().message_type()
|
||||
}
|
||||
|
||||
fn body_type(&self) -> u8 {
|
||||
self.body().body_type()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CommandQueryCustom {
|
||||
|
@ -140,6 +159,14 @@ impl RequestSerializer for CommandQueryCustom {
|
|||
fn serialize(&self) -> Vec<u8> {
|
||||
self.command.serialize()
|
||||
}
|
||||
|
||||
fn message_type(&self) -> MessageType {
|
||||
self.header().message_type()
|
||||
}
|
||||
|
||||
fn body_type(&self) -> u8 {
|
||||
self.body().body_type()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CommandHeartbeat;
|
||||
|
@ -148,4 +175,12 @@ impl RequestSerializer for CommandHeartbeat {
|
|||
fn serialize(&self) -> Vec<u8> {
|
||||
vec![0x00]
|
||||
}
|
||||
|
||||
fn message_type(&self) -> MessageType {
|
||||
MessageType::None
|
||||
}
|
||||
|
||||
fn body_type(&self) -> u8 {
|
||||
0
|
||||
}
|
||||
}
|
||||
|
|
|
@ -124,24 +124,22 @@ impl Device {
|
|||
}
|
||||
|
||||
for cmd in cmds {
|
||||
'sending: loop {
|
||||
self.build_send(cmd.clone())?;
|
||||
self.build_send(cmd.clone())?;
|
||||
|
||||
loop {
|
||||
let mut buf = [0; 512];
|
||||
let bytes_read = match self.socket.lock().unwrap().read(&mut buf) {
|
||||
Ok(b) => b,
|
||||
Err(_) => continue,
|
||||
};
|
||||
loop {
|
||||
let mut buf = [0; 512];
|
||||
let bytes_read = match self.socket.lock().unwrap().read(&mut buf) {
|
||||
Ok(b) => b,
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
}
|
||||
if bytes_read == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
match self.parse_message(&buf[..bytes_read])? {
|
||||
ParseMessage::Success => break 'sending,
|
||||
ParseMessage::Padding => continue,
|
||||
}
|
||||
match self.parse_message(&buf[..bytes_read])? {
|
||||
ParseMessage::Success => break,
|
||||
ParseMessage::Padding => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -226,7 +224,11 @@ impl Device {
|
|||
fn send_message(&self, msg: &[u8]) -> Result<()> {
|
||||
let data = self.security.encode_8370(msg, MsgType::ENCRYPTED_REQUEST)?;
|
||||
|
||||
self.socket.lock().unwrap().write(&data)?;
|
||||
self.socket
|
||||
.lock()
|
||||
.unwrap()
|
||||
.write(&data)
|
||||
.context(format!("{msg:?}"))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -237,15 +239,26 @@ impl Device {
|
|||
}
|
||||
|
||||
fn build_send(&self, cmd: impl RequestSerializer) -> Result<()> {
|
||||
let message_type = cmd.message_type();
|
||||
let body_type = cmd.body_type();
|
||||
|
||||
let data = PacketBuilder::builder(self.info.id, cmd).finalize(1);
|
||||
|
||||
self.send_message(&data)
|
||||
Ok(self.send_message(&data).context(format!(
|
||||
"message_type: {message_type:?}, body_type: {body_type}"
|
||||
))?)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use std::net::{Ipv4Addr, SocketAddr};
|
||||
use std::{
|
||||
net::{Ipv4Addr, SocketAddr},
|
||||
sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
},
|
||||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use futures::future::try_join;
|
||||
|
@ -342,19 +355,29 @@ mod test {
|
|||
addr: SocketAddr::new(std::net::IpAddr::V4(Ipv4Addr::new(192, 168, 178, 94)), 6444),
|
||||
};
|
||||
|
||||
let mut device = Device::connect(device_info, token, key)
|
||||
.await?
|
||||
.register_update(|attributes| {
|
||||
println!("{attributes:?}");
|
||||
let power: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));
|
||||
|
||||
Ok(())
|
||||
let device = Device::connect(device_info, token, key)
|
||||
.await?
|
||||
.register_update({
|
||||
let power = power.clone();
|
||||
|
||||
move |attributes| {
|
||||
println!("{attributes:?}");
|
||||
|
||||
power.store(attributes["power"].bool(), Ordering::SeqCst);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
});
|
||||
|
||||
device
|
||||
.set_attribute("attribute", AttributeValue::Bool(false))
|
||||
.set_attribute("power", AttributeValue::Bool(true))
|
||||
.await?;
|
||||
|
||||
device.refresh_status().await?;
|
||||
device
|
||||
.set_attribute("power", AttributeValue::Bool(false))
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -127,7 +127,7 @@ impl DeviceBackend for E1 {
|
|||
cmd.request()
|
||||
}
|
||||
|
||||
_ => todo!(),
|
||||
_ => todo!("attribute: {attribute}"),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue