moved StateUpdate to its own file

This commit is contained in:
Nickiel12 2021-12-18 21:04:50 -08:00
parent 505dd4e57b
commit 57f4883c0c
9 changed files with 61 additions and 35 deletions

View file

@ -1,6 +1,6 @@
use std::{sync::mpsc, time::Duration}; use std::{sync::mpsc, time::Duration};
use modules::socket_handler::Socket; use modules::{socket_handler::Socket, stream_states::stream_states_class::StreamState, message_handler::{MessageHandler, StateMessage}};
use workctl::sync_flag; use workctl::sync_flag;
mod tests; mod tests;
@ -11,6 +11,8 @@ mod modules;
const SERVER_ADDRESS: &str = "10.0.0.168:5000"; const SERVER_ADDRESS: &str = "10.0.0.168:5000";
fn main() { fn main() {
let state = StreamState::new();
let socket_listener = Socket::make_listener(SERVER_ADDRESS); let socket_listener = Socket::make_listener(SERVER_ADDRESS);
let (from_socket_tx, from_socket_rx) = mpsc::channel::<String>(); let (from_socket_tx, from_socket_rx) = mpsc::channel::<String>();
let (mut listener_can_run_flag, listener_join_handle) = Socket::handle_connections(socket_listener, from_socket_tx); let (mut listener_can_run_flag, listener_join_handle) = Socket::handle_connections(socket_listener, from_socket_tx);
@ -25,7 +27,7 @@ fn main() {
match from_socket_rx.recv_timeout(Duration::from_millis(100)) { match from_socket_rx.recv_timeout(Duration::from_millis(100)) {
Ok(message) => { Ok(message) => {
println!("{}", message); println!("{}", message);
let update = <StreamState as MessageHandler>::create_update_from_string(message);
}, },
Err(_) => {continue}, Err(_) => {continue},
} }
@ -41,3 +43,4 @@ fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) {
control_c_flag_tx.set(true); control_c_flag_tx.set(true);
}).expect("control C handler failed!"); }).expect("control C handler failed!");
} }

View file

@ -1,4 +1,4 @@
use super::stream_states::{enums::StateUpdate, stream_states_class::StreamState}; use super::stream_states::{state_update::StateUpdate, stream_states_class::StreamState};
pub enum StateMessage { pub enum StateMessage {
StateUpdate(StateUpdate), StateUpdate(StateUpdate),
@ -20,7 +20,25 @@ impl MessageHandler for StreamState {
self.clone() self.clone()
} }
fn create_update_from_string(update_json: String) -> StateUpdate { fn create_update_from_string(update_json: String) -> StateUpdate {
StateUpdate::ChangeSceneOnChangeSlideHotkey(false) let json: serde_json::Value = serde_json::from_str(&update_json[1..]).unwrap();
let message_type = &json["type"];
match message_type.as_str().unwrap() {
"button" => {
let value = &json["button"];
},
"Timer_Length" => {
let new_timer_length = &json["data"];
},
"update" => {
println!("Update all!! *Poof*!");
},
_ => {
} }
}
println!("type: {}", json["type"]);
StateUpdate::ChangeSceneOnChangeSlideHotkey(false)
}
} }

View file

@ -5,5 +5,6 @@ pub mod socket_handler;
pub mod stream_states { pub mod stream_states {
pub mod stream_states_class; pub mod stream_states_class;
pub mod state_update;
pub mod enums; pub mod enums;
} }

View file

@ -13,15 +13,3 @@ pub enum Scenes {
Augmented Augmented
} }
#[derive(Debug, PartialEq, Clone)]
pub enum StateUpdate {
StreamRunning(bool),
StreamIsMuted(bool),
ComputerSoundIsOn(bool),
ChangeSceneOnChangeSlideHotkey(bool),
SceneIsAugmented(bool),
TimerCanRun(bool),
TimerLength(f32),
TimerText(String),
Scene(Scenes),
}

View file

@ -0,0 +1,15 @@
use super::enums::Scenes;
#[derive(Debug, PartialEq, Clone)]
pub enum StateUpdate {
StreamRunning(bool),
StreamIsMuted(bool),
ComputerSoundIsOn(bool),
ChangeSceneOnChangeSlideHotkey(bool),
SceneIsAugmented(bool),
TimerCanRun(bool),
TimerLength(f32),
TimerText(String),
Scene(Scenes),
}

View file

@ -1,4 +1,5 @@
use super::enums::{self, StateUpdate}; use super::enums::Scenes;
use super::state_update::StateUpdate;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct StreamState { pub struct StreamState {
@ -13,9 +14,9 @@ pub struct StreamState {
pub timer_length: f32, pub timer_length: f32,
pub timer_text: String, pub timer_text: String,
pub current_scene: enums::Scenes, pub current_scene: Scenes,
pub camera_sub_scene: enums::Scenes, pub camera_sub_scene: Scenes,
pub screen_sub_scene: enums::Scenes, pub screen_sub_scene: Scenes,
} }
impl Default for StreamState { impl Default for StreamState {
@ -32,9 +33,9 @@ impl Default for StreamState {
timer_length: 15.0, timer_length: 15.0,
timer_text: String::from("0.0"), timer_text: String::from("0.0"),
current_scene: enums::Scenes::CameraDefault, current_scene: Scenes::CameraDefault,
camera_sub_scene: enums::Scenes::CameraDefault, camera_sub_scene: Scenes::CameraDefault,
screen_sub_scene: enums::Scenes::ScreenDefault, screen_sub_scene: Scenes::ScreenDefault,
} }
} }
} }
@ -56,28 +57,28 @@ impl StreamState {
StateUpdate::Scene(new_val) => {self.change_scene(&new_val)}, StateUpdate::Scene(new_val) => {self.change_scene(&new_val)},
StateUpdate::SceneIsAugmented(new_val) => { StateUpdate::SceneIsAugmented(new_val) => {
self.scene_is_augmented = new_val; self.scene_is_augmented = new_val;
self.change_scene(&enums::Scenes::Augmented)}, self.change_scene(&Scenes::Augmented)},
} }
} }
pub fn change_scene(&mut self, scene: &enums::Scenes) { pub fn change_scene(&mut self, scene: &Scenes) {
match scene { match scene {
enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight | Scenes::CameraDefault | Scenes::CameraWithUpperRight |
enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight Scenes::CameraWithLargeUpperRight | Scenes::CameraWithLowerRight
=> {StreamState::set_camera_scene(self, scene)}, => {StreamState::set_camera_scene(self, scene)},
enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight | Scenes::ScreenDefault | Scenes::ScreenWithUpperRight |
enums::Scenes::ScreenWithLowerRight Scenes::ScreenWithLowerRight
=> {StreamState::set_screen_scene(self, scene)}, => {StreamState::set_screen_scene(self, scene)},
enums::Scenes::Augmented => {self.current_scene = *scene;} Scenes::Augmented => {self.current_scene = *scene;}
} }
} }
fn set_camera_scene(&mut self, scene: &enums::Scenes) { fn set_camera_scene(&mut self, scene: &Scenes) {
self.camera_sub_scene = scene.clone(); self.camera_sub_scene = scene.clone();
self.current_scene = scene.clone(); self.current_scene = scene.clone();
} }
fn set_screen_scene(&mut self, scene: &enums::Scenes) { fn set_screen_scene(&mut self, scene: &Scenes) {
self.screen_sub_scene = scene.clone(); self.screen_sub_scene = scene.clone();
self.current_scene = scene.clone(); self.current_scene = scene.clone();
} }

View file

@ -1,6 +1,6 @@
use std::{sync::mpsc::{self, Receiver}, thread::{self, JoinHandle}}; use std::{sync::mpsc::{self, Receiver}, thread::{self, JoinHandle}};
use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{enums::{StateUpdate, Scenes}, stream_states_class::StreamState}}; use crate::modules::{message_handler::{MessageHandler, StateMessage}, stream_states::{state_update::StateUpdate, enums::Scenes, stream_states_class::StreamState}};
#[test] #[test]

View file

@ -1,6 +1,6 @@
use std::thread; use std::thread;
use crate::modules::{states_io_handler::{StatesIOHandler}, message_handler::{StateMessage}, stream_states::{stream_states_class::StreamState, enums::StateUpdate}}; use crate::modules::{states_io_handler::{StatesIOHandler}, message_handler::{StateMessage}, stream_states::{stream_states_class::StreamState, state_update::StateUpdate}};
#[test] #[test]

View file

@ -2,7 +2,7 @@ use std::sync::mpsc;
use std::thread; use std::thread;
use crate::modules::stream_states as s_s; use crate::modules::stream_states as s_s;
use crate::modules::stream_states::enums::{StateUpdate}; use crate::modules::stream_states::state_update::StateUpdate;
#[test] #[test]
fn has_all_enums() { fn has_all_enums() {