adding a basic obs implementation

This commit is contained in:
Chris Cochrun 2023-11-02 06:30:16 -05:00
parent 499567a4ed
commit de0c26271f
3 changed files with 79 additions and 25 deletions

View file

@ -1,7 +1,8 @@
use core::fmt;
use std::error::Error;
use obws::Client;
use obws::responses::scenes::Scenes;
use obws::Client;
use tracing::debug;
pub struct Obs {
@ -9,16 +10,43 @@ pub struct Obs {
client: Client,
}
impl fmt::Debug for Obs {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Client")
.field("host", &"localhost")
.field("port", &4455)
.finish()
}
}
impl Clone for Obs {
fn clone(&self) -> Self {
Self {
scenes: self.scenes.clone(),
client: make_client(),
}
}
}
impl Obs {
pub async fn new() -> Result<Self, Box<dyn Error>> {
let client = Client::connect("localhost", 4455, Some("")).await?;
let client =
Client::connect("localhost", 4455, Some("")).await?;
let scene_list = client.scenes().list().await?;
debug!(?scene_list);
Ok(Self{scenes: scene_list, client})
Ok(Self {
scenes: scene_list,
client,
})
}
pub fn get_list(self) -> Result<Vec<String>, Box<dyn Error>> {
let scenes = self.scenes.scenes.iter().map(|x| x.name).collect::<Vec<String>>();
let scenes = self
.scenes
.scenes
.iter()
.map(|x| x.name.clone())
.collect::<Vec<String>>();
if scenes.len() > 0 {
Ok(scenes)
} else {
@ -26,12 +54,30 @@ impl Obs {
}
}
pub fn set_scene(self, scene: String) -> Result<(), Box<dyn Error>> {
if let Some(scene) = self.scenes.scenes.iter().filter(|x| x.name == scene).next() {
self.client.scenes().set_current_program_scene(scene.name.as_str());
pub fn set_scene(
self,
scene: String,
) -> Result<(), Box<dyn Error>> {
if let Some(scene) = self
.scenes
.scenes
.iter()
.filter(|x| x.name == scene)
.next()
{
self.client
.scenes()
.set_current_program_scene(scene.name.as_str());
Ok(())
} else {
Err("Couldn't set the scene".to_owned())?
}
}
}
fn make_client() -> Client {
let runtime = tokio::runtime::Runtime::new().unwrap();
let future = Client::connect("localhost", 4455, Some(""));
let client = runtime.block_on(future).unwrap();
client
}