Created
April 3, 2020 14:51
-
-
Save dbl0null/742990f35375df3491cb9ee1daced2f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extern crate http; | |
#[macro_use] | |
extern crate json; | |
extern crate lili_agent; | |
#[macro_use] | |
extern crate log; | |
extern crate simple_server; | |
// | |
//use std::borrow::Borrow; | |
//use std::ops::Sub; | |
use http::header; | |
//use http::request::Builder; | |
//use json::number::Number; | |
use json::JsonValue; | |
//use simple_server::Response; | |
use simple_server::Server; | |
use lili_agent::utils; | |
use lili_agent::utils::GetObjectId; | |
fn main() { | |
env_logger::init(); | |
info!("starting"); | |
let host = "localhost"; | |
let port = "7171"; | |
let _default_input: u32 = utils::get_default_device(&utils::Scope::Input) | |
.unwrap() | |
.get_id(); | |
let _default_output: u32 = utils::get_default_device(&utils::Scope::Output) | |
.unwrap() | |
.get_id(); | |
let server = Server::new(|request, mut response| { | |
info!("Request received. {} {}", request.method(), request.uri()); | |
response.header(header::ACCESS_CONTROL_ALLOW_ORIGIN, "*".as_bytes()); | |
let mut body: Vec<u8> = "".as_bytes().to_vec(); | |
match request.uri().path() { | |
"/" => Ok(response.body( | |
json::stringify_pretty(get_devices_info(), 2) | |
.as_bytes() | |
.to_vec(), | |
)?), | |
"/input" => { | |
let scope = &utils::Scope::Input; | |
if request.method().as_str() == "GET" { | |
let id: u32 = utils::get_default_device(scope).unwrap().get_id(); | |
body = id.to_string().as_bytes().to_vec() | |
} else if request.method().as_str() == "POST" { | |
change_default_device(scope, request.uri().query()); | |
body = "OK".as_bytes().to_vec() | |
} | |
Ok(response.body(body)?) | |
} | |
"/output" => { | |
let scope = &utils::Scope::Output; | |
if request.method().as_str() == "GET" { | |
let id: u32 = utils::get_default_device(scope).unwrap().get_id(); | |
body = id.to_string().as_bytes().to_vec() | |
} else if request.method().as_str() == "POST" { | |
change_default_device(scope, request.uri().query()); | |
body = "OK".as_bytes().to_vec() | |
} | |
Ok(response.body(body)?) | |
} | |
_ => Ok(response.body("Wut?".as_bytes().to_vec())?), | |
} | |
}); | |
server.listen(host, port); | |
} | |
fn get_devices_info() -> Option<JsonValue> { | |
let mut inputs: Vec<JsonValue> = Vec::new(); | |
let mut outputs: Vec<JsonValue> = Vec::new(); | |
for output_device in utils::get_devices(&utils::Scope::Output).unwrap() { | |
outputs.push(get_device_info(&output_device, &utils::Scope::Output).unwrap()) | |
} | |
for input_device in utils::get_devices(&utils::Scope::Input).unwrap() { | |
inputs.push(get_device_info(&input_device, &utils::Scope::Input).unwrap()) | |
} | |
Some(object! { | |
"input" => inputs, | |
"output" => outputs | |
}) | |
} | |
fn get_device_info(device: &utils::AudioObject, scope: &utils::Scope) -> Option<JsonValue> { | |
if !device.in_scope(scope).unwrap() { | |
return None; | |
} | |
let result = object! { | |
"id" => utils::GetObjectId::get_id(device), | |
"label" => device.get_label(scope).unwrap().to_string(), | |
"uid" => device.get_uid().unwrap().to_string(), | |
"manufacturer" => device.get_manufacturer().unwrap().to_string(), | |
"channels" => device.get_channel_count(scope).unwrap(), | |
"rate" => device.get_rate(scope).unwrap(), | |
"device_latency" => device.get_device_latency(scope).unwrap(), | |
"stream_latency" => device.get_stream_latency(scope).unwrap(), | |
}; | |
Some(result) | |
} | |
fn change_default_device(scope: &utils::Scope, id: Option<&str>) { | |
let guessed_new_id: u32 = id.unwrap().parse().expect("Not a number!"); | |
let devices = utils::get_devices(scope).unwrap_or_default(); | |
if devices.len() < 2 { | |
return; | |
} | |
let current_device = utils::get_default_device(scope).unwrap(); | |
let cur_id = ¤t_device.get_id(); | |
let cur_name = current_device.get_label(scope).unwrap(); | |
let new_device = devices | |
.into_iter() | |
.find(|ref device| device.get_id() == guessed_new_id) | |
.unwrap_or(current_device); | |
let new_id = new_device.get_id(); | |
if !new_id.eq(cur_id) { | |
assert!(utils::set_default_device(&new_device, scope).is_ok()); | |
println!( | |
"{}: Changing {:?} to {:?}!", | |
to_string(scope), | |
cur_name, | |
new_device.get_label(scope).unwrap() | |
); | |
} else { | |
println!( | |
"{}: Not changing {:?} to {:?}!", | |
to_string(scope), | |
cur_name, | |
new_device.get_label(scope).unwrap() | |
); | |
} | |
} | |
fn to_string(scope: &utils::Scope) -> String { | |
if scope == &utils::Scope::Input { | |
"Input".to_string() | |
} else { | |
"Output".to_string() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment