Implement public set_attribute interface

This commit is contained in:
hodasemi 2023-10-04 14:50:24 +02:00
parent 5e996cd47d
commit 2b3fa02fd1
6 changed files with 37 additions and 30 deletions

View file

@ -48,6 +48,7 @@ impl Header {
MessageType::from_u8(self.0[9]).unwrap() MessageType::from_u8(self.0[9]).unwrap()
} }
#[allow(unused)]
pub fn device_type(&self) -> u8 { pub fn device_type(&self) -> u8 {
self.0[2] self.0[2]
} }

View file

@ -39,10 +39,12 @@ impl CommandRequest {
} }
} }
#[allow(unused)]
pub fn header(&self) -> &Header { pub fn header(&self) -> &Header {
&self.command.header &self.command.header
} }
#[allow(unused)]
pub fn body(&self) -> &Body { pub fn body(&self) -> &Body {
&self.command.body &self.command.body
} }
@ -119,6 +121,7 @@ impl CommandQueryCustom {
} }
} }
#[allow(unused)]
pub fn request(self) -> CommandRequest { pub fn request(self) -> CommandRequest {
self.command self.command
} }

View file

@ -11,8 +11,8 @@ use anyhow::{bail, Context, Error, Result};
use crate::{ use crate::{
command::{ command::{
CommandHeartbeat, CommandQuerySubtype, CommandSubtypeResponse, MessageType, Body, CommandHeartbeat, CommandQueryCustom, CommandQuerySubtype, CommandSubtypeResponse,
RequestSerializer, MessageType, RequestSerializer,
}, },
devices::{e1::E1, AttributeValue, DeviceBackend}, devices::{e1::E1, AttributeValue, DeviceBackend},
hex, hex,
@ -148,7 +148,15 @@ impl Device {
} }
pub async fn set_attribute(&self, attribute: &str, value: AttributeValue) -> Result<()> { pub async fn set_attribute(&self, attribute: &str, value: AttributeValue) -> Result<()> {
todo!() self.build_send(self.device_backend.set_attribute(attribute, value))
}
pub async fn send_command(&self, message_type: MessageType, body: &[u8]) -> Result<()> {
self.build_send(CommandQueryCustom::new(
self.info.device_type,
message_type,
Body::from(body),
))
} }
pub fn register_update<F>(mut self, f: F) -> Self pub fn register_update<F>(mut self, f: F) -> Self
@ -221,7 +229,7 @@ impl Device {
Ok(()) Ok(())
} }
fn send_heartbeat(&self) -> Result<()> { pub async fn send_heartbeat(&self) -> Result<()> {
let msg = PacketBuilder::builder(self.info.id, CommandHeartbeat).finalize(0); let msg = PacketBuilder::builder(self.info.id, CommandHeartbeat).finalize(0);
self.send_message(&msg) self.send_message(&msg)
} }

View file

@ -106,13 +106,28 @@ impl DeviceBackend for E1 {
); );
} }
fn set_attribute(&self, attribute: &str, value: &str) -> () { fn set_attribute(&self, attribute: &str, value: AttributeValue) -> CommandRequest {
match attribute { match attribute {
"power" => (), "power" => {
"child_lock" => (), let mut cmd = CommandPower::new(self.device_protocol_version);
"storage" => (), cmd.set_power(value.bool());
_ => (), cmd.request()
}
"child_lock" => {
let mut cmd = CommandLock::new(self.device_protocol_version);
cmd.set_lock(value.bool());
cmd.request()
}
"storage" => {
let mut cmd = CommandStorage::new(self.device_protocol_version);
cmd.set_storage(value.bool());
cmd.request()
}
_ => todo!(),
} }
} }
@ -169,10 +184,6 @@ impl CommandPower {
} }
} }
pub fn power(&self) -> bool {
self.body()[1] == 0x01
}
pub fn set_power(&mut self, power: bool) { pub fn set_power(&mut self, power: bool) {
self.command.body_mut()[1] = if power { 0x01 } else { 0x00 }; self.command.body_mut()[1] = if power { 0x01 } else { 0x00 };
} }
@ -207,10 +218,6 @@ impl CommandLock {
} }
} }
pub fn lock(&self) -> bool {
self.body()[1] == 0x03
}
pub fn set_lock(&mut self, lock: bool) { pub fn set_lock(&mut self, lock: bool) {
self.command.body_mut()[1] = if lock { 0x03 } else { 0x04 } self.command.body_mut()[1] = if lock { 0x03 } else { 0x04 }
} }
@ -250,10 +257,6 @@ impl CommandStorage {
} }
} }
pub fn storage(&self) -> bool {
self.body()[4] == 0x01
}
pub fn set_storage(&mut self, storage: bool) { pub fn set_storage(&mut self, storage: bool) {
self.command.body_mut()[4] = if storage { 0x01 } else { 0x00 } self.command.body_mut()[4] = if storage { 0x01 } else { 0x00 }
} }

View file

@ -141,5 +141,5 @@ pub trait DeviceBackend: Send + Sync {
fn build_query(&self) -> CommandRequest; fn build_query(&self) -> CommandRequest;
fn process_message(&mut self, msg: &[u8]); fn process_message(&mut self, msg: &[u8]);
fn attributes(&self) -> &HashMap<&'static str, AttributeValue>; fn attributes(&self) -> &HashMap<&'static str, AttributeValue>;
fn set_attribute(&self, attribute: &str, value: &str) -> (); fn set_attribute(&self, attribute: &str, value: AttributeValue) -> CommandRequest;
} }

View file

@ -205,15 +205,7 @@ impl Security {
|| msgtype == MsgType::ENCRYPTED_REQUEST as u8 || msgtype == MsgType::ENCRYPTED_REQUEST as u8
{ {
let sign = data[(data.len() - 32)..].to_vec(); let sign = data[(data.len() - 32)..].to_vec();
// let sign_ref = b"\xf5\x1c\x93\x18\xa3\xb9HR.X\x9aFU\xb1\xc4N\xe8J\x1a\x04\x0f\xd4\x90\xc4\x132\xeby\xa6\x83\x06e";
// assert_eq!(sign, sign_ref);
data = data[..(data.len() - 32)].to_vec(); data = data[..(data.len() - 32)].to_vec();
let data_ref = b"\xb2\x174\xbb\xf4\xf1\x8fWg\xc4\x97h%\x06\x9aCIL\xf6tf\x86\x8b\x83\xac\xa9@\xb1r\xb3\xa0>>\xd0_#\x03\x93\xcdZ[\x17\xde\xd8\xaeT\x1a\xd0\x0f\xa5\x8fk\xebJF\x0b\xfd\xf8\\o_\x06\xe9\xdd\xda45\x0b\x90\xf1\x82*\x9f\xa4\x1a\xc3\xb3\xde]\x04\x9e\x9c\xafy\xcfq\x02\xa34\xa4\x15\x0b\xcb\t\x96V\xd3f\x14\xd6\xf5X]\xc3'\xbc\x1b\xc1\x10\x08\x9c\x8d";
// assert_eq!(data, data_ref);
data = Self::aes_cbc_decrypt(&data, &self.tcp_key.unwrap()).to_vec(); data = Self::aes_cbc_decrypt(&data, &self.tcp_key.unwrap()).to_vec();
let compare_sign: Vec<u8> = Sha256::digest(&[header.to_vec(), data.clone()].concat()) let compare_sign: Vec<u8> = Sha256::digest(&[header.to_vec(), data.clone()].concat())