2022-02-14 11:15:19 +00:00
|
|
|
use log::info;
|
|
|
|
|
2022-01-09 15:04:53 +00:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
2022-02-14 11:15:19 +00:00
|
|
|
io::{Error, ErrorKind, Result},
|
2022-01-09 15:04:53 +00:00
|
|
|
process::{Command, ExitStatus},
|
|
|
|
};
|
|
|
|
|
2021-03-01 02:37:22 +00:00
|
|
|
use tokio::process::{Child as AsyncChild, Command as AsyncCommand};
|
|
|
|
|
2022-01-09 15:04:53 +00:00
|
|
|
use librespot::playback::player::{PlayerEvent, SinkStatus};
|
2018-02-15 23:16:38 +00:00
|
|
|
|
2022-02-14 11:15:19 +00:00
|
|
|
pub fn run_program_on_events(event: PlayerEvent, onevent: &str) -> Option<Result<AsyncChild>> {
|
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,
|
2022-02-14 11:15:19 +00:00
|
|
|
} => match old_track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Changed: Invalid old track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(old_id) => match new_track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Changed: Invalid old track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(new_id) => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "changed".to_string());
|
|
|
|
env_vars.insert("OLD_TRACK_ID", old_id);
|
|
|
|
env_vars.insert("TRACK_ID", new_id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
PlayerEvent::Started { track_id, .. } => match track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Started: Invalid track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(id) => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "started".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", id);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PlayerEvent::Stopped { track_id, .. } => match track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Stopped: Invalid track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(id) => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "stopped".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", id);
|
|
|
|
}
|
|
|
|
},
|
2020-03-12 12:01:45 +00:00
|
|
|
PlayerEvent::Playing {
|
2020-03-20 06:31:18 +00:00
|
|
|
track_id,
|
|
|
|
duration_ms,
|
2020-03-12 12:01:45 +00:00
|
|
|
position_ms,
|
|
|
|
..
|
2022-02-14 11:15:19 +00:00
|
|
|
} => match track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Playing: Invalid track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(id) => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "playing".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", id);
|
|
|
|
env_vars.insert("DURATION_MS", duration_ms.to_string());
|
|
|
|
env_vars.insert("POSITION_MS", position_ms.to_string());
|
|
|
|
}
|
|
|
|
},
|
2020-03-12 12:01:45 +00:00
|
|
|
PlayerEvent::Paused {
|
2020-03-20 06:31:18 +00:00
|
|
|
track_id,
|
|
|
|
duration_ms,
|
2020-03-12 12:01:45 +00:00
|
|
|
position_ms,
|
|
|
|
..
|
2022-02-14 11:15:19 +00:00
|
|
|
} => match track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Paused: Invalid track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(id) => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "paused".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", id);
|
|
|
|
env_vars.insert("DURATION_MS", duration_ms.to_string());
|
|
|
|
env_vars.insert("POSITION_MS", position_ms.to_string());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
PlayerEvent::Preloading { track_id, .. } => match track_id.to_base62() {
|
2022-01-23 18:02:04 +00:00
|
|
|
Err(e) => {
|
2022-02-14 11:15:19 +00:00
|
|
|
return Some(Err(Error::new(
|
|
|
|
ErrorKind::InvalidData,
|
2022-07-27 21:31:11 +00:00
|
|
|
format!("PlayerEvent::Preloading: Invalid track id: {}", e),
|
2022-02-14 11:15:19 +00:00
|
|
|
)))
|
|
|
|
}
|
|
|
|
Ok(id) => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "preloading".to_string());
|
|
|
|
env_vars.insert("TRACK_ID", id);
|
|
|
|
}
|
|
|
|
},
|
2020-03-12 12:01:45 +00:00
|
|
|
PlayerEvent::VolumeSet { volume } => {
|
|
|
|
env_vars.insert("PLAYER_EVENT", "volume_set".to_string());
|
|
|
|
env_vars.insert("VOLUME", volume.to_string());
|
|
|
|
}
|
2020-02-03 07:58:44 +00:00
|
|
|
_ => return None,
|
2018-02-20 22:09:48 +00:00
|
|
|
}
|
2021-02-21 10:08:34 +00:00
|
|
|
|
|
|
|
let mut v: Vec<&str> = onevent.split_whitespace().collect();
|
|
|
|
info!("Running {:?} with environment variables {:?}", v, env_vars);
|
|
|
|
Some(
|
|
|
|
AsyncCommand::new(&v.remove(0))
|
|
|
|
.args(&v)
|
|
|
|
.envs(env_vars.iter())
|
|
|
|
.spawn(),
|
|
|
|
)
|
2018-02-15 23:16:38 +00:00
|
|
|
}
|
2020-03-10 12:26:01 +00:00
|
|
|
|
2022-02-14 11:15:19 +00:00
|
|
|
pub fn emit_sink_event(sink_status: SinkStatus, onevent: &str) -> Result<ExitStatus> {
|
2020-03-10 12:26:01 +00:00
|
|
|
let mut env_vars = HashMap::new();
|
|
|
|
env_vars.insert("PLAYER_EVENT", "sink".to_string());
|
|
|
|
let sink_status = match sink_status {
|
|
|
|
SinkStatus::Running => "running",
|
|
|
|
SinkStatus::TemporarilyClosed => "temporarily_closed",
|
|
|
|
SinkStatus::Closed => "closed",
|
|
|
|
};
|
|
|
|
env_vars.insert("SINK_STATUS", sink_status.to_string());
|
2021-02-21 10:08:34 +00:00
|
|
|
let mut v: Vec<&str> = onevent.split_whitespace().collect();
|
|
|
|
info!("Running {:?} with environment variables {:?}", v, env_vars);
|
2020-03-10 12:26:01 +00:00
|
|
|
|
2021-02-21 10:08:34 +00:00
|
|
|
Command::new(&v.remove(0))
|
|
|
|
.args(&v)
|
|
|
|
.envs(env_vars.iter())
|
|
|
|
.spawn()?
|
|
|
|
.wait()
|
2020-03-10 12:26:01 +00:00
|
|
|
}
|