2018-02-20 20:57:42 +00:00
|
|
|
use librespot::playback::player::PlayerEvent;
|
2020-01-17 17:11:07 +00:00
|
|
|
use log::info;
|
2018-02-26 01:50:41 +00:00
|
|
|
use std::collections::HashMap;
|
2018-10-16 07:24:33 +00:00
|
|
|
use std::io;
|
2018-10-16 09:32:17 +00:00
|
|
|
use std::process::Command;
|
2019-10-08 09:31:18 +00:00
|
|
|
use tokio_process::{Child, CommandExt};
|
2018-02-15 23:16:38 +00:00
|
|
|
|
2018-10-16 07:24:33 +00:00
|
|
|
fn run_program(program: &str, env_vars: HashMap<&str, String>) -> io::Result<Child> {
|
2018-02-15 23:16:38 +00:00
|
|
|
let mut v: Vec<&str> = program.split_whitespace().collect();
|
2018-02-20 22:12:52 +00:00
|
|
|
info!("Running {:?} with environment variables {:?}", v, env_vars);
|
|
|
|
Command::new(&v.remove(0))
|
2018-02-15 23:16:38 +00:00
|
|
|
.args(&v)
|
2018-02-20 21:45:14 +00:00
|
|
|
.envs(env_vars.iter())
|
2018-10-16 09:32:17 +00:00
|
|
|
.spawn_async()
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 07:58:44 +00:00
|
|
|
pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<io::Result<Child>> {
|
2018-02-20 22:09:48 +00:00
|
|
|
let mut env_vars = HashMap::new();
|
|
|
|
match event {
|
2018-02-26 01:50:41 +00:00
|
|
|
PlayerEvent::Changed {
|
|
|
|
old_track_id,
|
|
|
|
new_track_id,
|
|
|
|
} => {
|
2018-02-20 22:09:48 +00:00
|
|
|
env_vars.insert("PLAYER_EVENT", "change".to_string());
|
2018-11-04 17:41:33 +00:00
|
|
|
env_vars.insert("OLD_TRACK_ID", old_track_id.to_base62());
|
|
|
|
env_vars.insert("TRACK_ID", new_track_id.to_base62());
|
2018-02-26 01:50:41 +00:00
|
|
|
}
|
2020-01-31 21:41:11 +00:00
|
|
|
PlayerEvent::Started { track_id, .. } => {
|
2018-02-20 22:09:48 +00:00
|
|
|
env_vars.insert("PLAYER_EVENT", "start".to_string());
|
2018-11-04 17:41:33 +00:00
|
|
|
env_vars.insert("TRACK_ID", track_id.to_base62());
|
2018-02-20 21:45:14 +00:00
|
|
|
}
|
2020-01-31 21:41:11 +00:00
|
|
|
PlayerEvent::Stopped { track_id, .. } => {
|
2018-02-20 22:09:48 +00:00
|
|
|
env_vars.insert("PLAYER_EVENT", "stop".to_string());
|
2018-11-04 17:41:33 +00:00
|
|
|
env_vars.insert("TRACK_ID", track_id.to_base62());
|
2018-02-20 22:09:48 +00:00
|
|
|
}
|
2020-02-03 07:58:44 +00:00
|
|
|
_ => return None,
|
2018-02-20 22:09:48 +00:00
|
|
|
}
|
2020-02-03 07:58:44 +00:00
|
|
|
Some(run_program(onevent, env_vars))
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|