mod command;
mod controller;
mod group;
mod led;
mod mode;
mod plugin;
mod segment;
mod zone;
pub use {command::*, controller::*, group::*, led::*, mode::*, segment::*, zone::*};
use tokio::net::ToSocketAddrs;
use crate::{
DEFAULT_PROTOCOL, OpenRgbError,
client::plugin::OpenRgbPlugin,
data::DeviceType,
error::OpenRgbResult,
protocol::{DEFAULT_ADDR, OpenRgbProtocol},
};
pub struct OpenRgbClient {
proto: OpenRgbProtocol,
}
impl OpenRgbClient {
pub async fn connect() -> OpenRgbResult<Self> {
Self::connect_to(DEFAULT_ADDR, DEFAULT_PROTOCOL).await
}
pub async fn connect_to(
addr: impl ToSocketAddrs + std::fmt::Debug + Copy,
protocol_version: u32,
) -> OpenRgbResult<Self> {
let client = OpenRgbProtocol::connect_to(addr, protocol_version).await?;
Ok(Self { proto: client })
}
}
impl OpenRgbClient {
pub async fn get_all_controllers(&self) -> OpenRgbResult<ControllerGroup> {
let count = self.proto.get_controller_count().await? as usize;
let mut controllers = Vec::with_capacity(count);
for id in 0..count {
let controller = self.get_controller(id).await?;
controllers.push(controller);
}
Ok(ControllerGroup::new(controllers))
}
pub async fn get_controllers_of_type(
&self,
device_type: DeviceType,
) -> OpenRgbResult<ControllerGroup> {
let group = self.get_all_controllers().await?;
group.split_per_type().remove(&device_type).ok_or_else(|| {
OpenRgbError::CommandError(format!("No controllers of type {device_type:?} found"))
})
}
pub async fn get_controller(&self, i: usize) -> OpenRgbResult<Controller> {
let c_data = self.proto.get_controller(i as u32).await?;
Ok(Controller::new(i, self.proto.clone(), c_data))
}
}
impl OpenRgbClient {
pub fn get_protocol_version(&mut self) -> u32 {
self.proto.get_protocol_version()
}
pub async fn set_name(&mut self, name: impl Into<String>) -> OpenRgbResult<()> {
self.proto.set_name(name).await
}
pub async fn get_profiles(&self) -> OpenRgbResult<Vec<String>> {
self.proto.get_profiles().await
}
pub async fn save_profile(&self, name: impl Into<String>) -> OpenRgbResult<()> {
self.proto.save_profile(name).await
}
pub async fn load_profile(&self, name: impl Into<String>) -> OpenRgbResult<()> {
self.proto.load_profile(name).await
}
pub async fn delete_profile(&self, name: impl Into<String>) -> OpenRgbResult<()> {
self.proto.delete_profile(name).await
}
pub async fn get_controller_count(&mut self) -> OpenRgbResult<u32> {
self.proto.get_controller_count().await
}
pub async fn rescan_devices(&self) -> OpenRgbResult<()> {
self.proto.rescan_devices().await
}
pub async fn get_plugins(&self) -> OpenRgbResult<Vec<OpenRgbPlugin>> {
let plugins_raw = self.proto.get_plugins().await?;
let plugins = plugins_raw.into_iter().map(OpenRgbPlugin::from).collect();
Ok(plugins)
}
}