WORKING DEMO

This commit is contained in:
Lukas Bachschwell 2020-01-26 18:43:14 +01:00
parent 0d084cf287
commit 7c33e31b13
Signed by: lbsadmin
GPG Key ID: CCC6AA87CC8DF425
1 changed files with 20 additions and 17 deletions

View File

@ -8,20 +8,22 @@ use std::i64;
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::net; use std::net;
use std::net::SocketAddr;
use std::time::Instant; use std::time::Instant;
use tokio::io::{AsyncReadExt, AsyncWriteExt}; use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UdpSocket;
use tokio_tungstenite::connect_async; use tokio_tungstenite::connect_async;
use tungstenite::protocol::Message; use tungstenite::protocol::Message;
use url::Url; use url::Url;
async fn listen(senddata: futures::channel::mpsc::UnboundedReceiver<String>) { async fn listen(senddata: futures::channel::mpsc::UnboundedSender<Message>) {
// Setup the UDP Socket // Setup the UDP Socket
let udpsocket = net::UdpSocket::bind("0.0.0.0:0").expect("failed to bind host udp socket"); // local bind port let mut now = Instant::now();
let mut udpsocket = UdpSocket::bind("0.0.0.0:0").await.unwrap();
udpsocket.connect("127.0.0.1:9000").await.unwrap();
let msg = String::from("ok").into_bytes(); let msg = String::from("ok").into_bytes();
udpsocket udpsocket.send(&msg).await.unwrap();
.send_to(&msg, "127.0.0.1:9000")
.expect("cannot send");
loop { loop {
// Handle Sending part // Handle Sending part
@ -34,18 +36,23 @@ async fn listen(senddata: futures::channel::mpsc::UnboundedReceiver<String>) {
// ------------- // -------------
let mut buf: [u8; 20] = [0; 20]; let mut buf: [u8; 20] = [0; 20];
let mut result: Vec<u8> = Vec::new(); let mut result: Vec<u8> = Vec::new();
match udpsocket.recv_from(&mut buf).await { let len = udpsocket.recv(&mut buf).await.unwrap();
Ok((number_of_bytes, _)) => { result = Vec::from(&buf[0..len]);
result = Vec::from(&buf[0..number_of_bytes]);
}
Err(e) => println!("failed listening {:?}", e),
}
let display_result = result.clone(); let display_result = result.clone();
let result_str = String::from_utf8(display_result).unwrap(); let result_str = String::from_utf8(display_result).unwrap();
println!("received message: {:?}", result_str); println!("received message: {:?}", result_str);
if result_str.contains("S0R1") { if result_str.contains("S0R1") {
//senddata.unbounded_send("RaceStart".to_string()).unwrap();
let source_id = "LAPTIME";
let request = json!({"request-type":"SetTextFreetype2Properties", "source":source_id,"message-id": random::<f64>().to_string(), "text": now.elapsed().as_millis().to_string() });
now = Instant::now();
senddata
.unbounded_send(Message::Text(request.to_string()))
.unwrap();
write_file("Race active".to_string(), "racestate.txt"); write_file("Race active".to_string(), "racestate.txt");
write_file("0".to_string(), "rx1.txt"); write_file("0".to_string(), "rx1.txt");
write_file("0".to_string(), "rx2.txt"); write_file("0".to_string(), "rx2.txt");
@ -156,10 +163,6 @@ async fn main() {
write_file("-.-".to_string(), "rx3_laptime.txt"); write_file("-.-".to_string(), "rx3_laptime.txt");
// Setup websocket for OBS // Setup websocket for OBS
let mut now = Instant::now();
let source_id = "LAPTIME";
let (ws_stream, _) = connect_async(Url::parse("ws://localhost:4444/").unwrap()) let (ws_stream, _) = connect_async(Url::parse("ws://localhost:4444/").unwrap())
.await .await
.expect("Could not connect to OBS"); .expect("Could not connect to OBS");
@ -167,7 +170,7 @@ async fn main() {
let (write, read) = ws_stream.split(); let (write, read) = ws_stream.split();
let (obstx, obsrx) = futures::channel::mpsc::unbounded(); let (obstx, obsrx) = futures::channel::mpsc::unbounded();
let (udpsockettx, udpsocketrx) = futures::channel::mpsc::unbounded(); //let (udpsockettx, udpsocketrx) = futures::channel::mpsc::unbounded();
let ws_to_stdout = { let ws_to_stdout = {
read.for_each(|message| { read.for_each(|message| {
@ -183,7 +186,7 @@ async fn main() {
pin_mut!(programm_to_ws, ws_to_stdout); pin_mut!(programm_to_ws, ws_to_stdout);
tokio::spawn(listen(udpsocketrx)); tokio::spawn(listen(obstx));
println!("Will wait now"); println!("Will wait now");
future::select(programm_to_ws, ws_to_stdout).await; future::select(programm_to_ws, ws_to_stdout).await;