From 57f4883c0c7eca7412aaf19c3a260a1649c10b98 Mon Sep 17 00:00:00 2001 From: Nickiel12 <35903114+Nickiel12@users.noreply.github.com> Date: Sat, 18 Dec 2021 21:04:50 -0800 Subject: [PATCH] moved StateUpdate to its own file --- src/main.rs | 7 ++-- src/modules/message_handler.rs | 22 +++++++++++-- src/modules/mod.rs | 1 + src/modules/stream_states/enums.rs | 12 ------- src/modules/stream_states/state_update.rs | 15 +++++++++ .../stream_states/stream_states_class.rs | 33 ++++++++++--------- src/tests/message_handler_tests.rs | 2 +- src/tests/states_io_handler_tests.rs | 2 +- src/tests/stream_states_tests.rs | 2 +- 9 files changed, 61 insertions(+), 35 deletions(-) create mode 100644 src/modules/stream_states/state_update.rs diff --git a/src/main.rs b/src/main.rs index 239abfa..d1ecfb8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ 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; mod tests; @@ -11,6 +11,8 @@ mod modules; const SERVER_ADDRESS: &str = "10.0.0.168:5000"; fn main() { + let state = StreamState::new(); + let socket_listener = Socket::make_listener(SERVER_ADDRESS); let (from_socket_tx, from_socket_rx) = mpsc::channel::(); 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)) { Ok(message) => { println!("{}", message); - + let update = ::create_update_from_string(message); }, Err(_) => {continue}, } @@ -41,3 +43,4 @@ fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) { control_c_flag_tx.set(true); }).expect("control C handler failed!"); } + diff --git a/src/modules/message_handler.rs b/src/modules/message_handler.rs index 68a42cc..40a9c4d 100644 --- a/src/modules/message_handler.rs +++ b/src/modules/message_handler.rs @@ -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 { StateUpdate(StateUpdate), @@ -20,7 +20,25 @@ impl MessageHandler for StreamState { self.clone() } 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) } } \ No newline at end of file diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 63583f7..db4cc7d 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -5,5 +5,6 @@ pub mod socket_handler; pub mod stream_states { pub mod stream_states_class; + pub mod state_update; pub mod enums; } \ No newline at end of file diff --git a/src/modules/stream_states/enums.rs b/src/modules/stream_states/enums.rs index 652cbdd..5354452 100644 --- a/src/modules/stream_states/enums.rs +++ b/src/modules/stream_states/enums.rs @@ -13,15 +13,3 @@ pub enum Scenes { 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), -} \ No newline at end of file diff --git a/src/modules/stream_states/state_update.rs b/src/modules/stream_states/state_update.rs new file mode 100644 index 0000000..88119f7 --- /dev/null +++ b/src/modules/stream_states/state_update.rs @@ -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), +} \ No newline at end of file diff --git a/src/modules/stream_states/stream_states_class.rs b/src/modules/stream_states/stream_states_class.rs index 10eba10..b740b46 100644 --- a/src/modules/stream_states/stream_states_class.rs +++ b/src/modules/stream_states/stream_states_class.rs @@ -1,4 +1,5 @@ -use super::enums::{self, StateUpdate}; +use super::enums::Scenes; +use super::state_update::StateUpdate; #[derive(Debug, Clone)] pub struct StreamState { @@ -13,9 +14,9 @@ pub struct StreamState { pub timer_length: f32, pub timer_text: String, - pub current_scene: enums::Scenes, - pub camera_sub_scene: enums::Scenes, - pub screen_sub_scene: enums::Scenes, + pub current_scene: Scenes, + pub camera_sub_scene: Scenes, + pub screen_sub_scene: Scenes, } impl Default for StreamState { @@ -32,9 +33,9 @@ impl Default for StreamState { timer_length: 15.0, timer_text: String::from("0.0"), - current_scene: enums::Scenes::CameraDefault, - camera_sub_scene: enums::Scenes::CameraDefault, - screen_sub_scene: enums::Scenes::ScreenDefault, + current_scene: Scenes::CameraDefault, + camera_sub_scene: Scenes::CameraDefault, + screen_sub_scene: Scenes::ScreenDefault, } } } @@ -56,28 +57,28 @@ impl StreamState { StateUpdate::Scene(new_val) => {self.change_scene(&new_val)}, StateUpdate::SceneIsAugmented(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 { - enums::Scenes::CameraDefault | enums::Scenes::CameraWithUpperRight | - enums::Scenes::CameraWithLargeUpperRight | enums::Scenes::CameraWithLowerRight + Scenes::CameraDefault | Scenes::CameraWithUpperRight | + Scenes::CameraWithLargeUpperRight | Scenes::CameraWithLowerRight => {StreamState::set_camera_scene(self, scene)}, - enums::Scenes::ScreenDefault | enums::Scenes::ScreenWithUpperRight | - enums::Scenes::ScreenWithLowerRight + Scenes::ScreenDefault | Scenes::ScreenWithUpperRight | + Scenes::ScreenWithLowerRight => {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.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.current_scene = scene.clone(); } diff --git a/src/tests/message_handler_tests.rs b/src/tests/message_handler_tests.rs index d8cb788..f5d7c16 100644 --- a/src/tests/message_handler_tests.rs +++ b/src/tests/message_handler_tests.rs @@ -1,6 +1,6 @@ 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] diff --git a/src/tests/states_io_handler_tests.rs b/src/tests/states_io_handler_tests.rs index acab856..2b13d9b 100644 --- a/src/tests/states_io_handler_tests.rs +++ b/src/tests/states_io_handler_tests.rs @@ -1,6 +1,6 @@ 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] diff --git a/src/tests/stream_states_tests.rs b/src/tests/stream_states_tests.rs index add8a95..52701c8 100644 --- a/src/tests/stream_states_tests.rs +++ b/src/tests/stream_states_tests.rs @@ -2,7 +2,7 @@ use std::sync::mpsc; use std::thread; use crate::modules::stream_states as s_s; -use crate::modules::stream_states::enums::{StateUpdate}; +use crate::modules::stream_states::state_update::StateUpdate; #[test] fn has_all_enums() {