summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Adam <_@yuv.al>2018-07-18 00:32:10 +0300
committerYuval Adam <_@yuv.al>2018-07-18 00:32:35 +0300
commitdc1ef790eca27340b1864a6dd951055752a23a86 (patch)
treebf8125f750aeaa869eb478f7c8f699bf8c85b2b7
parent8556e2f494847d62b613dfc4fee98e530f49e9ab (diff)
Refactor out notification calls
-rw-r--r--src/main.rs61
-rw-r--r--src/notify.rs104
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#"<?xml version='1.0' encoding='utf-8'?>
</s:Body>
</s:Envelope>"#;
+pub const BODY_PAUSE: &'static str = r#"<?xml version='1.0' encoding='utf-8'?>
+<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
+ <s:Body>
+ <u:Pause xmlns:u="urn:schemas-upnp-org:service:AVTransport:1">
+ <InstanceID>0</InstanceID>
+ </u:Pause>
+ </s:Body>
+</s:Envelope>"#;
+
pub const BODY_STOP: &'static str = r#"<?xml version='1.0' encoding='utf-8'?>
<s:Envelope s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
@@ -38,3 +52,93 @@ pub const BODY_STOP: &'static str = r#"<?xml version='1.0' encoding='utf-8'?>
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);
+}