-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tapo-py: Improve the concurrency of device handlers
- Loading branch information
1 parent
945b41b
commit ff30406
Showing
8 changed files
with
228 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,64 @@ | ||
use std::ops::{Deref, DerefMut}; | ||
use std::sync::Arc; | ||
|
||
use pyo3::prelude::*; | ||
use pyo3::types::PyDict; | ||
use tapo::responses::DeviceInfoGenericResult; | ||
use tapo::GenericDeviceHandler; | ||
use tokio::sync::Mutex; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::call_handler_method; | ||
use crate::errors::ErrorWrapper; | ||
|
||
#[derive(Clone)] | ||
#[pyclass(name = "GenericDeviceHandler")] | ||
pub struct PyGenericDeviceHandler { | ||
handler: Arc<Mutex<GenericDeviceHandler>>, | ||
handler: Arc<RwLock<GenericDeviceHandler>>, | ||
} | ||
|
||
impl PyGenericDeviceHandler { | ||
pub fn new(handler: GenericDeviceHandler) -> Self { | ||
Self { | ||
handler: Arc::new(Mutex::new(handler)), | ||
handler: Arc::new(RwLock::new(handler)), | ||
} | ||
} | ||
} | ||
|
||
#[pymethods] | ||
impl PyGenericDeviceHandler { | ||
pub async fn refresh_session(&self) -> PyResult<()> { | ||
call_handler_method!(self, GenericDeviceHandler::refresh_session, discard_result) | ||
let handler = self.handler.clone(); | ||
call_handler_method!( | ||
handler.write().await.deref_mut(), | ||
GenericDeviceHandler::refresh_session, | ||
discard_result | ||
) | ||
} | ||
|
||
pub async fn on(&self) -> PyResult<()> { | ||
call_handler_method!(self, GenericDeviceHandler::on) | ||
let handler = self.handler.clone(); | ||
call_handler_method!(handler.read().await.deref(), GenericDeviceHandler::on) | ||
} | ||
|
||
pub async fn off(&self) -> PyResult<()> { | ||
call_handler_method!(self, GenericDeviceHandler::off) | ||
let handler = self.handler.clone(); | ||
call_handler_method!(handler.read().await.deref(), GenericDeviceHandler::off) | ||
} | ||
|
||
pub async fn get_device_info(&self) -> PyResult<DeviceInfoGenericResult> { | ||
call_handler_method!(self, GenericDeviceHandler::get_device_info) | ||
let handler = self.handler.clone(); | ||
call_handler_method!( | ||
handler.read().await.deref(), | ||
GenericDeviceHandler::get_device_info | ||
) | ||
} | ||
|
||
pub async fn get_device_info_json(&self) -> PyResult<Py<PyDict>> { | ||
let result = call_handler_method!(self, GenericDeviceHandler::get_device_info_json)?; | ||
let handler = self.handler.clone(); | ||
let result = call_handler_method!( | ||
handler.read().await.deref(), | ||
GenericDeviceHandler::get_device_info_json, | ||
)?; | ||
Python::with_gil(|py| tapo::python::serde_object_to_py_dict(py, &result)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.