diff --git a/Cargo.toml b/Cargo.toml index 15912b0..43bce0a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,5 @@ walkdir = "2.3.2" sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite", "migrate" ] } tokio = { version = "1.16.1", features = ["full"] } dirs = "4.0.0" +serde_json = "1.0.79" +serde = { version = "1.0.136", features= ["derive"] } diff --git a/src/main.rs b/src/main.rs index f2496d8..6015ffc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use tokio::process::Command; use walkdir::DirEntry; use walkdir::WalkDir; mod mpvctl; +use crate::mpvctl::MpvCtl; +use tokio::time::{sleep, Duration}; #[tokio::main] async fn main() -> Result<(), Box> { @@ -43,6 +45,13 @@ async fn main() -> Result<(), Box> { } launch_mpv().await?; + sleep(Duration::from_secs(10)).await; + + let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?; + mpvctl.get_client_name().await?; + + println!("done"); + Ok(()) } diff --git a/src/mpvctl/command.rs b/src/mpvctl/command.rs index 5e0ca67..10428a9 100644 --- a/src/mpvctl/command.rs +++ b/src/mpvctl/command.rs @@ -1,9 +1,35 @@ +use serde::ser::SerializeMap; +use serde::{Deserialize, Serialize, Serializer}; + pub struct Command { command: Vec, + request_id: Option, + asynchronous: bool, } impl Command { - pub fn new(command: Vec) -> Self { - Command { command: command } + pub fn new(command: Vec, request_id: Option) -> Self { + Command { + command, + request_id, + asynchronous: false, + } + } +} + +impl Serialize for Command { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut map = serializer.serialize_map(None)?; + map.serialize_entry("command", &self.command)?; + self.request_id + .map(|reqid| map.serialize_entry("request_id", &reqid)) + .map_or(Ok(None), |x| x.map(Some))?; + if self.asynchronous { + map.serialize_entry("async", &self.asynchronous)?; + } + map.end() } } diff --git a/src/mpvctl/mpvctl.rs b/src/mpvctl/mpvctl.rs index d40119c..8556d47 100644 --- a/src/mpvctl/mpvctl.rs +++ b/src/mpvctl/mpvctl.rs @@ -1,5 +1,8 @@ use std::collections::HashMap; +use std::io::Read; +use std::os::unix::prelude::OsStrExt; use std::{collections::BTreeMap, path::Path}; +use tokio::io::AsyncWriteExt; use tokio::net::UnixStream; use super::command::Command; @@ -26,14 +29,17 @@ impl MpvCtl { ret } - async fn send_command(&self, cmd: Command) { - // - // let serialized = + async fn send_command(&mut self, cmd: Command) -> Result<(), Box> { + let serialized = serde_json::to_string(&cmd)?; + println!("Sending {}", serialized); + self.socket.write_all(serialized.as_bytes()).await?; + Ok(()) } pub async fn get_client_name(&mut self) -> Result<(), Box> { let request_id = self.grab_request_id(); - let cmd = Command::new(vec!["client_name".to_string()]); + let cmd = Command::new(vec!["client_name".to_string()], Some(request_id)); + self.send_command(cmd).await?; Ok(()) } }