From dc1ef790eca27340b1864a6dd951055752a23a86 Mon Sep 17 00:00:00 2001 From: Yuval Adam <_@yuv.al> Date: Wed, 18 Jul 2018 00:32:10 +0300 Subject: Refactor out notification calls --- src/main.rs | 61 ++++++---------------------------- src/notify.rs | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 51 deletions(-) diff --git a/src/main.rs b/src/main.rs index d5cadd8..4d92fee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,10 +4,9 @@ extern crate indoc; extern crate clap; extern crate colored; - -extern crate tokio; -extern crate hyper; extern crate futures; +extern crate hyper; +extern crate tokio; use colored::*; use clap::{Arg, App}; @@ -16,7 +15,6 @@ use std::process; use std::thread; use std::sync::mpsc; -use hyper::{Client, Request}; use hyper::body::Body; use tokio::runtime::Runtime; use futures::Future; @@ -62,31 +60,13 @@ fn main() { serve::run(&mut rt); - let client = Client::new(); - let req1 = Request::builder() - .method("POST") - .uri("http://10.5.1.201:38400/serviceControl/AVTransport") - .header("Content-Type", "text/xml") - .header("SOAPACTION", notify::A_SET) - .body(Body::from(notify::BODY_SET_URI)) - .unwrap(); - - let f = client - .request(req1) - .map(|_res| { - println!("OK") - }) - .map_err(|_err| { - println!("Something bad happened"); - }); - - rt.spawn(f); + notify::set_uri(&mut rt); // let _udp = thread::spawn(move || { // upnp::discover(); // }); - let playing = true; + let mut playing = false; let (tx, rx) = mpsc::channel(); let child = thread::spawn(move || { @@ -95,38 +75,17 @@ fn main() { let c = controller.read(); tx.send(c).unwrap(); if c == 113 { + notify::stop(&mut rt); break; } if c == 32 { - let req2 = if playing { - Request::builder() - .method("POST") - .uri("http://10.5.1.201:38400/serviceControl/AVTransport") - .header("Content-Type", "text/xml") - .header("SOAPACTION", notify::A_STOP) - .body(Body::from(notify::BODY_STOP)) - .unwrap() + if playing { + notify::pause(&mut rt); } else { - Request::builder() - .method("POST") - .uri("http://10.5.1.201:38400/serviceControl/AVTransport") - .header("Content-Type", "text/xml") - .header("SOAPACTION", notify::A_PLAY) - .body(Body::from(notify::BODY_PLAY)) - .unwrap() - }; - - let f = client - .request(req2) - .map(|_res| { - println!("OK"); - }) - .map_err(|_err| { - println!("Err"); - }); - - rt.spawn(f); + notify::play(&mut rt); + } + playing = !playing; } } controller.destroy(); diff --git a/src/notify.rs b/src/notify.rs index f946fd0..7a32d40 100644 --- a/src/notify.rs +++ b/src/notify.rs @@ -1,3 +1,8 @@ +use hyper::{Client, Request}; +use hyper::body::Body; +use tokio::runtime::Runtime; +use futures::Future; + const SOAP_ACTION_PREFIX: &'static str = "urn:schemas-upnp-org:service:AVTransport:1#"; const ACTION_SET_URI: &'static str = "SetAVTransportURI"; const ACTION_PLAY: &'static str = "Play"; @@ -25,6 +30,15 @@ pub const BODY_PLAY: &'static str = r#" "#; +pub const BODY_PAUSE: &'static str = r#" + + + + 0 + + +"#; + pub const BODY_STOP: &'static str = r#" @@ -38,3 +52,93 @@ pub const BODY_STOP: &'static str = r#" pub const A_SET: &'static str = "urn:schemas-upnp-org:service:AVTransport:1#SetAVTransportURI"; pub const A_PLAY: &'static str = "urn:schemas-upnp-org:service:AVTransport:1#Play"; pub const A_STOP: &'static str = "urn:schemas-upnp-org:service:AVTransport:1#Stop"; +pub const A_PAUSE: &'static str = "urn:schemas-upnp-org:service:AVTransport:1#Pause"; + + +pub fn set_uri(rt: &mut Runtime) { + let req = Request::builder() + .method("POST") + .uri("http://10.5.1.201:38400/serviceControl/AVTransport") + .header("Content-Type", "text/xml") + .header("SOAPACTION", A_SET) + .body(Body::from(BODY_SET_URI)) + .unwrap(); + + let client = Client::new(); + let f = client + .request(req) + .map(|_res| { + println!("Request good") + }) + .map_err(|_err| { + println!("Request error"); + }); + + rt.spawn(f); +} + +pub fn play(rt: &mut Runtime) { + let req = Request::builder() + .method("POST") + .uri("http://10.5.1.201:38400/serviceControl/AVTransport") + .header("Content-Type", "text/xml") + .header("SOAPACTION", A_PLAY) + .body(Body::from(BODY_PLAY)) + .unwrap(); + + let client = Client::new(); + let f = client + .request(req) + .map(|_res| { + println!("Request good") + }) + .map_err(|_err| { + println!("Request error"); + }); + + rt.spawn(f); +} + +pub fn pause(rt: &mut Runtime) { + let req = Request::builder() + .method("POST") + .uri("http://10.5.1.201:38400/serviceControl/AVTransport") + .header("Content-Type", "text/xml") + .header("SOAPACTION", A_PAUSE) + .body(Body::from(BODY_PAUSE)) + .unwrap(); + + let client = Client::new(); + let f = client + .request(req) + .map(|_res| { + println!("Request good") + }) + .map_err(|_err| { + println!("Request error"); + }); + + rt.spawn(f); +} + +pub fn stop(rt: &mut Runtime) { + let req = Request::builder() + .method("POST") + .uri("http://10.5.1.201:38400/serviceControl/AVTransport") + .header("Content-Type", "text/xml") + .header("SOAPACTION", A_STOP) + .body(Body::from(BODY_STOP)) + .unwrap(); + + let client = Client::new(); + let f = client + .request(req) + .map(|_res| { + println!("Request good") + }) + .map_err(|_err| { + println!("Request error"); + }); + + rt.spawn(f); +} -- cgit v1.3.1