use crate::{app::InstallOptions, app::UninstallOptions, file::FileTransferOptions, Result};
pub struct HdcClient {
runtime: tokio::runtime::Runtime,
inner: crate::HdcClient,
}
impl HdcClient {
pub fn connect(addr: &str) -> Result<Self> {
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(crate::HdcError::Io)?;
let inner = runtime.block_on(crate::HdcClient::connect(addr))?;
Ok(Self { runtime, inner })
}
pub fn list_targets(&mut self) -> Result<Vec<String>> {
self.runtime.block_on(self.inner.list_targets())
}
pub fn connect_device(&mut self, device_id: &str) -> Result<()> {
self.runtime.block_on(self.inner.connect_device(device_id))
}
pub fn shell(&mut self, command: &str) -> Result<String> {
self.runtime.block_on(self.inner.shell(command))
}
pub fn fport(
&mut self,
local: crate::forward::ForwardNode,
remote: crate::forward::ForwardNode,
) -> Result<String> {
self.runtime.block_on(self.inner.fport(local, remote))
}
pub fn rport(
&mut self,
remote: crate::forward::ForwardNode,
local: crate::forward::ForwardNode,
) -> Result<String> {
self.runtime.block_on(self.inner.rport(remote, local))
}
pub fn fport_remove(&mut self, task_str: &str) -> Result<String> {
self.runtime.block_on(self.inner.fport_remove(task_str))
}
pub fn install(&mut self, packages: &[&str], options: InstallOptions) -> Result<String> {
self.runtime.block_on(self.inner.install(packages, options))
}
pub fn uninstall(&mut self, package: &str, options: UninstallOptions) -> Result<String> {
self.runtime
.block_on(self.inner.uninstall(package, options))
}
pub fn file_send(
&mut self,
local_path: &str,
remote_path: &str,
options: FileTransferOptions,
) -> Result<String> {
self.runtime
.block_on(self.inner.file_send(local_path, remote_path, options))
}
pub fn file_recv(
&mut self,
remote_path: &str,
local_path: &str,
options: FileTransferOptions,
) -> Result<String> {
self.runtime
.block_on(self.inner.file_recv(remote_path, local_path, options))
}
pub fn hilog(&mut self, args: Option<&str>) -> Result<String> {
self.runtime.block_on(self.inner.hilog(args))
}
pub fn wait_for_device(&mut self) -> Result<String> {
self.runtime.block_on(self.inner.wait_for_device())
}
pub fn hilog_stream<F>(&mut self, args: Option<&str>, callback: F) -> Result<()>
where
F: FnMut(&str) -> bool,
{
self.runtime
.block_on(self.inner.hilog_stream(args, callback))
}
pub fn monitor_devices<F>(&mut self, interval_secs: u64, callback: F) -> Result<()>
where
F: FnMut(&[String]) -> bool,
{
let interval = std::time::Duration::from_secs(interval_secs);
self.runtime
.block_on(self.inner.monitor_devices(interval, callback))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore] fn test_blocking_client_creation() {
let result = HdcClient::connect("127.0.0.1:8710");
assert!(result.is_ok());
}
#[test]
#[ignore] fn test_blocking_list_targets() {
let mut client = HdcClient::connect("127.0.0.1:8710").unwrap();
let result = client.list_targets();
assert!(result.is_ok());
}
}