Move some obs functions to module

This commit is contained in:
Lukas Bachschwell 2020-02-06 20:01:42 +01:00
parent 70c2d2c3b5
commit 4c64d857e0
Signed by: lbsadmin
GPG Key ID: CCC6AA87CC8DF425
2 changed files with 69 additions and 37 deletions

View File

@ -19,6 +19,8 @@ use tokio_tungstenite::connect_async;
use tungstenite::protocol::Message; use tungstenite::protocol::Message;
use url::Url; use url::Url;
mod obsws;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
struct Conf { struct Conf {
filemode: bool, filemode: bool,
@ -134,14 +136,14 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
write_file("0".to_string(), "rx2.txt"); write_file("0".to_string(), "rx2.txt");
write_file("0".to_string(), "rx3.txt"); write_file("0".to_string(), "rx3.txt");
} }
set_obs_text( obsws::set_text(
&senddata, &senddata,
&appconf.race_status_source, &appconf.race_status_source,
&"Race active".to_string(), &"Race active".to_string(),
); );
for i in 0..2 { for i in 0..2 {
set_obs_text(&senddata, &appconf.lap_sources[i], &"0".to_string()); obsws::set_text(&senddata, &appconf.lap_sources[i], &"0".to_string());
set_obs_text( obsws::set_text(
&senddata, &senddata,
&appconf.laptime_sources[i], &appconf.laptime_sources[i],
&"00:00.000".to_string(), &"00:00.000".to_string(),
@ -152,7 +154,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
if appconf.filemode { if appconf.filemode {
write_file("Race inactive".to_string(), "racestate.txt"); write_file("Race inactive".to_string(), "racestate.txt");
} }
set_obs_text( obsws::set_text(
&senddata, &senddata,
&appconf.race_status_source, &appconf.race_status_source,
&"Race inactive".to_string(), &"Race inactive".to_string(),
@ -172,26 +174,38 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
// Drone is disconnected // Drone is disconnected
if drone_active[index] { if drone_active[index] {
// Send filter on // Send filter on
let request = json!({"request-type":"SetSourceFilterVisibility", "sourceName":appconf.video_sources[index],"message-id": random::<f64>().to_string(), "filterName":&appconf if &appconf.video_sources.len() > &index {
.obs_mask_filter_name obsws::set_mask(
.as_ref() &senddata,
.unwrap_or(&"mask".to_string()) , "filterEnabled": true }); &appconf.video_sources[index],
senddata &appconf.obs_mask_filter_name,
.unbounded_send(Message::Text(request.to_string())) true,
.unwrap(); );
} else {
eprintln!(
"No sourcename provided for rssi value string: {}, index {}",
node, index
)
}
drone_active[index] = false; drone_active[index] = false;
} }
} else { } else {
// Drone is connected! // Drone is connected!
if !drone_active[index] { if !drone_active[index] {
// Send filter off // Send filter off
let request = json!({"request-type":"SetSourceFilterVisibility", "sourceName":appconf.video_sources[index],"message-id": random::<f64>().to_string(), "filterName":&appconf if &appconf.video_sources.len() > &index {
.obs_mask_filter_name obsws::set_mask(
.as_ref() &senddata,
.unwrap_or(&"mask".to_string()) , "filterEnabled": false }); &appconf.video_sources[index],
senddata &appconf.obs_mask_filter_name,
.unbounded_send(Message::Text(request.to_string())) false,
.unwrap(); );
} else {
eprintln!(
"No sourcename provided for rssi value string: {}, index {}",
node, index
)
}
drone_active[index] = true; drone_active[index] = true;
} }
} }
@ -210,7 +224,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
lap_seconds, lap_seconds,
lap_duration.subsec_millis() lap_duration.subsec_millis()
); );
set_obs_text(&senddata, &appconf.laptime_sources[0], &laptime_string); obsws::set_text(&senddata, &appconf.laptime_sources[0], &laptime_string);
if appconf.filemode { if appconf.filemode {
write_file(laptime_string, "rx1_laptime.txt"); write_file(laptime_string, "rx1_laptime.txt");
} }
@ -219,7 +233,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
if appconf.filemode { if appconf.filemode {
write_file((intval + 1).to_string(), "rx1.txt"); write_file((intval + 1).to_string(), "rx1.txt");
} }
set_obs_text( obsws::set_text(
&senddata, &senddata,
&appconf.lap_sources[0], &appconf.lap_sources[0],
&(intval + 1).to_string(), &(intval + 1).to_string(),
@ -237,7 +251,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
lap_seconds, lap_seconds,
lap_duration.subsec_millis() lap_duration.subsec_millis()
); );
set_obs_text(&senddata, &appconf.laptime_sources[1], &laptime_string); obsws::set_text(&senddata, &appconf.laptime_sources[1], &laptime_string);
if appconf.filemode { if appconf.filemode {
write_file(laptime_string, "rx2_laptime.txt"); write_file(laptime_string, "rx2_laptime.txt");
} }
@ -247,7 +261,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
if appconf.filemode { if appconf.filemode {
write_file((intval + 1).to_string(), "rx2.txt"); write_file((intval + 1).to_string(), "rx2.txt");
} }
set_obs_text( obsws::set_text(
&senddata, &senddata,
&appconf.lap_sources[1], &appconf.lap_sources[1],
&(intval + 1).to_string(), &(intval + 1).to_string(),
@ -265,7 +279,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
lap_seconds, lap_seconds,
lap_duration.subsec_millis() lap_duration.subsec_millis()
); );
set_obs_text(&senddata, &appconf.laptime_sources[2], &laptime_string); obsws::set_text(&senddata, &appconf.laptime_sources[2], &laptime_string);
if appconf.filemode { if appconf.filemode {
write_file(laptime_string, "rx3_laptime.txt"); write_file(laptime_string, "rx3_laptime.txt");
} }
@ -275,7 +289,7 @@ async fn udp_comm(appconf: &Conf, senddata: futures::channel::mpsc::UnboundedSen
if appconf.filemode { if appconf.filemode {
write_file((intval + 1).to_string(), "rx3.txt"); write_file((intval + 1).to_string(), "rx3.txt");
} }
set_obs_text( obsws::set_text(
&senddata, &senddata,
&appconf.lap_sources[2], &appconf.lap_sources[2],
&(intval + 1).to_string(), &(intval + 1).to_string(),
@ -417,16 +431,3 @@ async fn obschan_to_nowhere(mut obsrx: futures::channel::mpsc::UnboundedReceiver
obsrx.next().await.unwrap(); obsrx.next().await.unwrap();
} }
} }
fn set_obs_text(
wschannel: &futures::channel::mpsc::UnboundedSender<Message>,
source: &String,
text: &String,
) {
let request = json!({"request-type":"SetTextFreetype2Properties", "source":source,"message-id": random::<f64>().to_string(), "text": text });
wschannel
.unbounded_send(Message::Text(request.to_string()))
.unwrap_or_else(|err| {
eprintln!("Could not send to OBS: {}", err);
});
}

31
src/obsws.rs Normal file
View File

@ -0,0 +1,31 @@
use rand::random;
use serde_json::json;
use tungstenite::protocol::Message;
pub fn set_text(
wschannel: &futures::channel::mpsc::UnboundedSender<Message>,
source: &String,
text: &String,
) {
let request = json!({"request-type":"SetTextFreetype2Properties", "source":source,"message-id": random::<f64>().to_string(), "text": text });
wschannel
.unbounded_send(Message::Text(request.to_string()))
.unwrap_or_else(|err| {
eprintln!("Could not send to OBS: {}", err);
});
}
pub fn set_mask(
wschannel: &futures::channel::mpsc::UnboundedSender<Message>,
source: &String,
mask_name: &Option<String>,
active: bool,
) {
let request = json!({"request-type":"SetSourceFilterVisibility", "sourceName":source,"message-id": random::<f64>().to_string(), "filterName":mask_name.as_ref()
.unwrap_or(&"mask".to_string()) , "filterEnabled": active });
wschannel
.unbounded_send(Message::Text(request.to_string()))
.unwrap_or_else(|err| {
eprintln!("Could not send to OBS: {}", err);
});
}