Returning the client name.

This commit is contained in:
Tom Alexander 2022-02-20 21:21:56 -05:00
parent 574a7ee5c1
commit f28057352f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 16 additions and 6 deletions

View File

@ -49,9 +49,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
sleep(Duration::from_secs(1)).await; sleep(Duration::from_secs(1)).await;
let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?; let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?;
mpvctl.get_client_name().await?; let client_name = mpvctl.get_client_name().await?;
println!("done"); println!("done {}", client_name);
Ok(()) Ok(())
} }

View File

@ -35,6 +35,7 @@ impl MpvCtl {
loop { loop {
match framed_read.read_frame().await { match framed_read.read_frame().await {
Ok(Some(frame)) => { Ok(Some(frame)) => {
println!("Read {}", frame);
// get the request id and push the result into the channel // get the request id and push the result into the channel
let reqid = { let reqid = {
let obj = match &frame { let obj = match &frame {
@ -94,7 +95,7 @@ impl MpvCtl {
cmd: Command, cmd: Command,
) -> Result<oneshot::Receiver<serde_json::Value>, Box<dyn std::error::Error>> { ) -> Result<oneshot::Receiver<serde_json::Value>, Box<dyn std::error::Error>> {
let serialized = serde_json::to_string(&cmd)? + "\n"; let serialized = serde_json::to_string(&cmd)? + "\n";
println!("Sending {}", serialized); print!("Sending {}", serialized);
let (response_tx, response_rx) = oneshot::channel::<serde_json::Value>(); let (response_tx, response_rx) = oneshot::channel::<serde_json::Value>();
match cmd.get_request_id() { match cmd.get_request_id() {
Some(reqid) => { Some(reqid) => {
@ -111,12 +112,21 @@ impl MpvCtl {
Ok(response_rx) Ok(response_rx)
} }
pub async fn get_client_name(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub async fn get_client_name(&mut self) -> Result<String, Box<dyn std::error::Error>> {
let request_id = self.grab_request_id(); let request_id = self.grab_request_id();
let cmd = Command::new(vec!["client_name".to_string()], Some(request_id)); let cmd = Command::new(vec!["client_name".to_string()], Some(request_id));
let cmd_result = self.send_command(cmd).await?; let cmd_result = self.send_command(cmd).await?;
let result = cmd_result.await?; let result = cmd_result.await?;
println!("Read the following result: {}", result); if let serde_json::Value::Object(obj) = result {
Ok(()) let client_name = match obj.get("data") {
Some(serde_json::Value::String(client_name)) => client_name,
_ => {
return Err("Failed to get client name".into());
}
};
return Ok(client_name.to_owned());
} else {
return Err("Did not get back an object".into());
}
} }
} }