use alloc::boxed::Box;
use crate::{
Error,
Identifier,
source::Source,
message::{Configuration, Flux},
};
pub trait Fixture: Send + Sync {
fn identifier(&self) -> Identifier;
fn display(&mut self, config: Configuration, target_flux: Flux) -> Result<(Configuration, Flux), Error>;
fn sources(&self) -> &[Box<dyn Source>];
}
#[cfg(feature = "remote")]
pub mod remote {
use crate::{
Identifier,
source::remote::RemoteSource,
message::{
Command, CommandMessage, Configuration, Event, Flux,
FixtureCommand, FixtureEvent, FixtureInfo, SourceInfo,
},
runtime::remote::RemoteRuntime,
};
#[derive(Clone, Debug)]
pub struct RemoteFixture {
info: FixtureInfo,
remote: RemoteRuntime,
}
impl RemoteFixture {
pub fn new(info: FixtureInfo, remote: RemoteRuntime) -> Self {
Self { info, remote }
}
pub async fn source_count(&self) -> Result<u32, crate::Error> {
let command = Command::Fixture(FixtureCommand::SourceCount);
let command_message = CommandMessage::root(command, Some(self.identifier()));
let event_message = self.remote.execute_command(command_message).await?;
match event_message.event {
Event::Fixture(FixtureEvent::SourceCount(count)) => Ok(count),
_ => Err(crate::Error::UnexpectedResponse),
}
}
pub fn identifier(&self) -> Identifier {
self.info.identifier
}
async fn source_info(&self, index: u32) -> Result<SourceInfo, crate::Error> {
let command = Command::Fixture(FixtureCommand::SourceInfo(index));
let command_message = CommandMessage::root(command, Some(self.identifier()));
let event_message = self.remote.execute_command(command_message).await?;
match event_message.event {
Event::Fixture(FixtureEvent::SourceInfo(info)) => Ok(info),
_ => Err(crate::Error::UnexpectedResponse),
}
}
pub async fn sources(&self) -> Result<Vec<RemoteSource>, crate::Error> {
let count = self.source_count().await?;
let mut sources = Vec::with_capacity(count as usize);
for i in 0..count {
let info = self.source_info(i).await?;
let source = RemoteSource::new(
info,
self.remote.clone(),
);
sources.push(source);
}
Ok(sources)
}
pub async fn display(&self, config: Configuration, target_flux: Flux) -> Result<(Configuration, Flux), crate::Error> {
let command = Command::Fixture(FixtureCommand::Display(config, target_flux));
let command_message = CommandMessage::root(command, Some(self.identifier()));
let event_message = self.remote.execute_command(command_message).await?;
match event_message.event {
Event::Fixture(FixtureEvent::Display(config, flux)) => Ok((config, flux)),
_ => Err(crate::Error::UnexpectedResponse),
}
}
}
}