Returning the client name.
This commit is contained in:
parent
574a7ee5c1
commit
f28057352f
@ -49,9 +49,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
sleep(Duration::from_secs(1)).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(())
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ impl MpvCtl {
|
||||
loop {
|
||||
match framed_read.read_frame().await {
|
||||
Ok(Some(frame)) => {
|
||||
println!("Read {}", frame);
|
||||
// get the request id and push the result into the channel
|
||||
let reqid = {
|
||||
let obj = match &frame {
|
||||
@ -94,7 +95,7 @@ impl MpvCtl {
|
||||
cmd: Command,
|
||||
) -> Result<oneshot::Receiver<serde_json::Value>, Box<dyn std::error::Error>> {
|
||||
let serialized = serde_json::to_string(&cmd)? + "\n";
|
||||
println!("Sending {}", serialized);
|
||||
print!("Sending {}", serialized);
|
||||
let (response_tx, response_rx) = oneshot::channel::<serde_json::Value>();
|
||||
match cmd.get_request_id() {
|
||||
Some(reqid) => {
|
||||
@ -111,12 +112,21 @@ impl MpvCtl {
|
||||
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 cmd = Command::new(vec!["client_name".to_string()], Some(request_id));
|
||||
let cmd_result = self.send_command(cmd).await?;
|
||||
let result = cmd_result.await?;
|
||||
println!("Read the following result: {}", result);
|
||||
Ok(())
|
||||
if let serde_json::Value::Object(obj) = result {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user