use super::{ButtplugDeviceResultFuture, ButtplugProtocol, ButtplugProtocolCommandHandler};
use crate::{
core::messages::{self, ButtplugDeviceCommandMessageUnion, DeviceMessageAttributesMap},
device::{
protocol::{generic_command_manager::GenericCommandManager, ButtplugProtocolProperties},
DeviceImpl,
DeviceWriteCmd,
Endpoint,
},
};
use std::sync::Arc;
use tokio::sync::Mutex;
#[derive(ButtplugProtocolProperties)]
pub struct Nobra {
name: String,
message_attributes: DeviceMessageAttributesMap,
manager: Arc<Mutex<GenericCommandManager>>,
stop_commands: Vec<ButtplugDeviceCommandMessageUnion>,
}
impl ButtplugProtocol for Nobra {
fn new_protocol(
name: &str,
message_attributes: DeviceMessageAttributesMap,
) -> Box<dyn ButtplugProtocol>
where
Self: Sized,
{
let manager = GenericCommandManager::new(&message_attributes);
Box::new(Self {
name: name.to_owned(),
message_attributes,
stop_commands: manager.get_stop_commands(),
manager: Arc::new(Mutex::new(manager)),
})
}
}
impl ButtplugProtocolCommandHandler for Nobra {
fn handle_vibrate_cmd(
&self,
device: Arc<DeviceImpl>,
message: messages::VibrateCmd,
) -> ButtplugDeviceResultFuture {
let manager = self.manager.clone();
Box::pin(async move {
let result = manager.lock().await.update_vibration(&message, false)?;
let mut fut_vec = vec![];
if let Some(cmds) = result {
for (_, cmd) in cmds.iter().enumerate() {
if let Some(speed) = cmd {
let output_speed = if *speed == 0 {
0x70
} else {
0x60 + speed
};
fut_vec.push(device.write_value(DeviceWriteCmd::new(
Endpoint::Tx,
vec![output_speed as u8],
false,
)));
}
}
} else {
info!("No updates in packet for Nobra protocol");
}
for fut in fut_vec {
fut.await?;
}
Ok(messages::Ok::default().into())
})
}
}