implemented most of the stuff
This commit is contained in:
parent
7c53786e3a
commit
33819a663d
3 changed files with 120 additions and 19 deletions
45
src/main.rs
45
src/main.rs
|
@ -42,26 +42,57 @@ fn main() {
|
||||||
println!("{}", message);
|
println!("{}", message);
|
||||||
let json = serde_json::from_str(&message).unwrap();
|
let json = serde_json::from_str(&message).unwrap();
|
||||||
let update = StateUpdate::json_to_state_update(json);
|
let update = StateUpdate::json_to_state_update(json);
|
||||||
let state_update = state.handle_update(update);
|
if update == StateUpdate::UpdateClient {
|
||||||
|
update_all(&state, &socket);
|
||||||
|
}
|
||||||
|
let updates = state.handle_update(update);
|
||||||
|
if updates.0.is_some() {
|
||||||
|
socket.send(updates.0.unwrap().to_json().to_string());
|
||||||
|
}
|
||||||
|
if updates.1.is_some() {
|
||||||
|
handle_instructions(updates.1.unwrap(), &mut state, &socket);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Err(_) => {continue},
|
Err(_) => {continue},
|
||||||
}
|
}
|
||||||
let tick_update = state.tick();
|
let tick_update = state.tick();
|
||||||
if tick_update.0.is_some() {
|
if tick_update.0.is_some() {state.handle_update(tick_update.0.unwrap());}
|
||||||
state.handle_update(tick_update.0.unwrap());
|
if tick_update.1.is_some() {state.handle_update(tick_update.1.unwrap());}
|
||||||
}
|
|
||||||
if tick_update.1.is_some() {
|
|
||||||
state.handle_update(tick_update.1.unwrap());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.close();
|
socket.close();
|
||||||
hotkey_handle.join().unwrap();
|
hotkey_handle.join().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_instructions(mut instructions: Vec<StateUpdate>, state: &mut StreamState, socket: &Socket) {
|
||||||
|
for i in instructions.iter_mut() {
|
||||||
|
let updates = state.handle_update(i.to_owned());
|
||||||
|
if updates.0.is_some() {
|
||||||
|
socket.send(updates.0.unwrap().to_json().to_string());
|
||||||
|
}
|
||||||
|
if updates.1.is_some() {
|
||||||
|
handle_instructions(updates.1.unwrap(), state, socket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) {
|
fn setup_control_c(mut control_c_flag_tx: sync_flag::SyncFlagTx) {
|
||||||
ctrlc::set_handler(move || {
|
ctrlc::set_handler(move || {
|
||||||
control_c_flag_tx.set(true);
|
control_c_flag_tx.set(true);
|
||||||
}).expect("control C handler failed!");
|
}).expect("control C handler failed!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn update_all(state: &StreamState, socket: &Socket) {
|
||||||
|
socket.send(StateUpdate::StreamRunning(state.stream_running).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::StreamSoundToggleOn(state.stream_is_muted).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::ToggleComputerSoundOn(state.computer_sound_is_on).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::ChangeSceneOnChangeSlide(state.change_scene_on_change_slide_hotkey).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::SceneIsAugmented(state.scene_is_augmented).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::TimerCanRun(state.timer_can_run).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::TimerLength(state.timer_length).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::TimerText(state.timer_text.clone()).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::SubScene(state.camera_sub_scene).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::SubScene(state.screen_sub_scene).to_json().to_string());
|
||||||
|
socket.send(StateUpdate::Scene(state.current_scene).to_json().to_string());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use super::stream_states::{state_update::StateUpdate, enums::SlideChange};
|
use super::stream_states::{state_update::StateUpdate, enums::{SlideChange, SubScenes, Scenes}};
|
||||||
|
|
||||||
pub fn create_keyboard_hooks(channel_tx: crossbeam_channel::Sender<String>) {
|
pub fn create_keyboard_hooks(channel_tx: crossbeam_channel::Sender<String>) {
|
||||||
|
|
||||||
|
@ -21,4 +21,20 @@ pub fn next_slide() {
|
||||||
|
|
||||||
pub fn prev_slide() {
|
pub fn prev_slide() {
|
||||||
todo!()
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_stream_sound(value: bool) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_computer_sound(value: bool) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn toggle_media_play_pause(value: bool) {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn change_scene(scene: Scenes, sub_scene: Option<SubScenes>) {
|
||||||
|
todo!()
|
||||||
}
|
}
|
|
@ -1,26 +1,27 @@
|
||||||
use std::time::{SystemTime, Duration};
|
use std::time::{SystemTime, Duration};
|
||||||
|
|
||||||
use super::{stream_states::{state_update::StateUpdate, stream_states_class::StreamState, enums::{SlideChange, Scenes}}, external_interface, socket_handler::Socket};
|
use super::{stream_states::{state_update::StateUpdate, stream_states_class::StreamState, enums::{SlideChange, Scenes}}, external_interface};
|
||||||
|
|
||||||
pub trait MessageHandler {
|
pub trait MessageHandler { //the first one goes to socket, the second propogates
|
||||||
fn handle_update(&mut self, update: StateUpdate) -> Option<StateUpdate>;
|
fn handle_update(&mut self, update: StateUpdate) -> (Option<StateUpdate>, Option<Vec<StateUpdate>>);
|
||||||
fn get_states(&self) -> StreamState;
|
fn get_states(&self) -> StreamState;
|
||||||
fn tick(&mut self) -> (Option<StateUpdate>, Option<StateUpdate>);
|
fn tick(&mut self) -> (Option<StateUpdate>, Option<StateUpdate>);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MessageHandler for StreamState {
|
impl MessageHandler for StreamState {
|
||||||
fn handle_update(&mut self, update: StateUpdate) -> Option<StateUpdate> {
|
fn handle_update(&mut self, update: StateUpdate) -> (Option<StateUpdate>, Option<Vec<StateUpdate>>) {
|
||||||
self.update(update.clone());
|
self.update(update.clone());
|
||||||
|
|
||||||
|
if self.debug_mode {
|
||||||
|
return (None, None)
|
||||||
|
}
|
||||||
|
|
||||||
match update {
|
match update {
|
||||||
StateUpdate::ChangeSlide(direction) => {
|
StateUpdate::ChangeSlide(direction) => {
|
||||||
if self.timer_can_run {
|
if self.timer_can_run {
|
||||||
self.timer_finished = false;
|
self.timer_finished = false;
|
||||||
self.timer_start = SystemTime::now();
|
self.timer_start = SystemTime::now();
|
||||||
}
|
}
|
||||||
if self.change_scene_on_change_slide_hotkey {
|
|
||||||
self.handle_update(StateUpdate::Scene(Scenes::Screen));
|
|
||||||
}
|
|
||||||
match direction {
|
match direction {
|
||||||
SlideChange::Next => {
|
SlideChange::Next => {
|
||||||
external_interface::next_slide();
|
external_interface::next_slide();
|
||||||
|
@ -29,10 +30,60 @@ impl MessageHandler for StreamState {
|
||||||
external_interface::prev_slide();
|
external_interface::prev_slide();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if self.change_scene_on_change_slide_hotkey {
|
||||||
|
let mut instructions = Vec::new();
|
||||||
|
instructions.push(StateUpdate::Scene(Scenes::Screen));
|
||||||
|
|
||||||
|
return (None, Some(instructions))
|
||||||
|
} else {return (None, None)}
|
||||||
}
|
}
|
||||||
|
StateUpdate::ChangeSceneOnChangeSlide(value) => {self.change_scene_on_change_slide_hotkey = value; return (Some(update), None)},
|
||||||
|
StateUpdate::SceneIsAugmented(value) => {
|
||||||
|
if value {
|
||||||
|
let mut instructions = Vec::new();
|
||||||
|
instructions.push(StateUpdate::ChangeSceneOnChangeSlide(false));
|
||||||
|
instructions.push(StateUpdate::Scene(Scenes::Augmented));
|
||||||
|
instructions.push(StateUpdate::TimerCanRun(false));
|
||||||
|
return (Some(update), Some(instructions))
|
||||||
|
} else {
|
||||||
|
let mut instructions = Vec::new();
|
||||||
|
instructions.push(StateUpdate::Scene(Scenes::Camera));
|
||||||
|
instructions.push(StateUpdate::ChangeSceneOnChangeSlide(true));
|
||||||
|
instructions.push(StateUpdate::TimerCanRun(true));
|
||||||
|
return (Some(update), Some(instructions));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
StateUpdate::TimerCanRun(value) => {self.timer_can_run = value; return (Some(update), None)},
|
||||||
|
StateUpdate::TimerLength(value) => {self.timer_length = value; return (Some(update), None)},
|
||||||
|
StateUpdate::TimerText(value) => {self.timer_text = value.clone(); return (Some(StateUpdate::TimerText(value)), None)},
|
||||||
|
StateUpdate::SubScene(value) => {
|
||||||
|
if value.get_type() == Scenes::Camera {
|
||||||
|
self.camera_sub_scene = value;
|
||||||
|
if self.current_scene == Scenes::Camera {
|
||||||
|
external_interface::change_scene(Scenes::Camera, Some(self.camera_sub_scene));
|
||||||
|
}
|
||||||
|
return (Some(update), None)
|
||||||
|
} else if value.get_type() == Scenes::Screen {
|
||||||
|
self.screen_sub_scene = value;
|
||||||
|
if self.current_scene == Scenes::Screen {
|
||||||
|
external_interface::change_scene(Scenes::Screen, Some(self.screen_sub_scene));
|
||||||
|
}
|
||||||
|
return (Some(update), None)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
StateUpdate::Scene(value) => {
|
||||||
|
external_interface::change_scene(value, None);
|
||||||
|
self.current_scene = value;
|
||||||
|
return (Some(update), None);
|
||||||
|
},
|
||||||
|
StateUpdate::StreamSoundToggleOn(value) => {external_interface::toggle_stream_sound(value); return (Some(update), None)},
|
||||||
|
StateUpdate::ToggleComputerSoundOn(value) => {external_interface::toggle_computer_sound(value); return (Some(update), None)},
|
||||||
|
StateUpdate::ComputerMediaDoPause(value) => {external_interface::toggle_media_play_pause(value); return (Some(update), None)},
|
||||||
|
StateUpdate::UpdateClient => {},
|
||||||
|
StateUpdate::StreamRunning(_) => {},
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
None
|
(None, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tick(&mut self) -> (Option<StateUpdate>, Option<StateUpdate>) {
|
fn tick(&mut self) -> (Option<StateUpdate>, Option<StateUpdate>) {
|
||||||
|
@ -69,7 +120,8 @@ fn test_tick_1() {
|
||||||
state.timer_finished = false;
|
state.timer_finished = false;
|
||||||
state.tick();
|
state.tick();
|
||||||
std::thread::sleep(Duration::from_millis(1000));
|
std::thread::sleep(Duration::from_millis(1000));
|
||||||
state.tick();
|
let update = state.tick();
|
||||||
|
state.update(update.0.unwrap());
|
||||||
assert_eq!(state.timer_text, "14.0");
|
assert_eq!(state.timer_text, "14.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,7 +131,8 @@ fn test_tick_one_half() {
|
||||||
state.timer_finished = false;
|
state.timer_finished = false;
|
||||||
state.tick();
|
state.tick();
|
||||||
std::thread::sleep(Duration::from_millis(500));
|
std::thread::sleep(Duration::from_millis(500));
|
||||||
state.tick();
|
let update = state.tick();
|
||||||
|
state.update(update.0.unwrap());
|
||||||
assert_eq!(state.timer_text, "14.5");
|
assert_eq!(state.timer_text, "14.5");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +143,7 @@ fn test_tick_10() {
|
||||||
state.timer_finished = false;
|
state.timer_finished = false;
|
||||||
state.tick();
|
state.tick();
|
||||||
std::thread::sleep(Duration::from_millis(10000));
|
std::thread::sleep(Duration::from_millis(10000));
|
||||||
state.tick();
|
let update = state.tick();
|
||||||
|
state.update(update.0.unwrap());
|
||||||
assert_eq!(state.timer_text, "5.0");
|
assert_eq!(state.timer_text, "5.0");
|
||||||
}
|
}
|
Loading…
Reference in a new issue