@@ -10,7 +10,7 @@ mod simserver;
1010mod syncdefs;
1111
1212use app:: { App , AppMessage } ;
13- use bytereader:: { StructDataTypes , data_type_as_bool} ;
13+ use bytereader:: { StructDataTypes , data_type_as_bool, StructData } ;
1414use definitions:: Definitions ;
1515use indexmap:: IndexMap ;
1616use interpolate:: { InterpolateStruct } ;
@@ -53,6 +53,21 @@ fn transfer_control(conn: &simconnect::SimConnector, has_control: bool) {
5353 conn. transmit_client_event ( 1 , 1002 , !has_control as u32 , 5 , 0 ) ;
5454}
5555
56+ fn on_simconnect_connect ( conn : & simconnect:: SimConnector , definitions : & mut Definitions ) -> StructData {
57+ definitions. map_all ( conn) ;
58+ conn. request_data_on_sim_object ( 0 , 0 , 0 , simconnect:: SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_SIM_FRAME ) ;
59+ conn. request_data_on_sim_object ( 1 , 1 , 0 , simconnect:: SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_SECOND ) ;
60+
61+ conn. map_client_event_to_sim_event ( 1000 , "FREEZE_LATITUDE_LONGITUDE_SET" ) ;
62+ conn. map_client_event_to_sim_event ( 1001 , "FREEZE_ALTITUDE_SET" ) ;
63+ conn. map_client_event_to_sim_event ( 1002 , "FREEZE_ATTITUDE_SET" ) ;
64+ conn. map_client_event_to_sim_event ( 1003 , "FREEZE_LATITUDE_LONGITUDE_TOGGLE" ) ;
65+ conn. map_client_event_to_sim_event ( 1004 , "FREEZE_ALTITUDE_TOGGLE" ) ;
66+ conn. map_client_event_to_sim_event ( 1005 , "FREEZE_ATTITUDE_TOGGLE" ) ;
67+
68+ return definitions. map_bool_sync_events ( & conn, "sync_bools.dat" , 1 ) ;
69+ }
70+
5671type SimValue = IndexMap < String , StructDataTypes > ;
5772const CONFIG_FILENAME : & str = "config.json" ;
5873fn main ( ) {
@@ -68,22 +83,12 @@ fn main() {
6883
6984 // Set up sim connect
7085 let mut conn = simconnect:: SimConnector :: new ( ) ;
71- conn . connect ( "Simple Shared Cockpit" ) ;
86+ let mut connected = false ;
7287
73- let mut definitions = Definitions :: new ( & conn ) ;
74- let bool_defs = definitions . map_bool_sync_events ( & conn , "sync_bools.dat" , 1 ) ;
88+ let mut definitions = Definitions :: new ( ) ;
89+ let mut bool_defs: Option < StructData > = None ;
7590
76- conn. request_data_on_sim_object ( 0 , 0 , 0 , simconnect:: SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_SIM_FRAME ) ;
77- conn. request_data_on_sim_object ( 1 , 1 , 0 , simconnect:: SIMCONNECT_PERIOD_SIMCONNECT_PERIOD_SECOND ) ;
78-
79- conn. map_client_event_to_sim_event ( 1000 , "FREEZE_LATITUDE_LONGITUDE_SET" ) ;
80- conn. map_client_event_to_sim_event ( 1001 , "FREEZE_ALTITUDE_SET" ) ;
81- conn. map_client_event_to_sim_event ( 1002 , "FREEZE_ATTITUDE_SET" ) ;
82- conn. map_client_event_to_sim_event ( 1003 , "FREEZE_LATITUDE_LONGITUDE_TOGGLE" ) ;
83- conn. map_client_event_to_sim_event ( 1004 , "FREEZE_ALTITUDE_TOGGLE" ) ;
84- conn. map_client_event_to_sim_event ( 1005 , "FREEZE_ATTITUDE_TOGGLE" ) ;
85-
86- let mut app_interface = App :: setup ( config. ip . clone ( ) , config. port ) ;
91+ let mut app_interface = App :: setup ( ) ;
8792
8893 // Transfer
8994 let mut transfer_client: Option < TransferStruct > = None ;
@@ -98,6 +103,7 @@ fn main() {
98103 let mut relieving_control = false ;
99104
100105 let mut should_sync = false ;
106+ let mut need_update = false ;
101107 let mut was_error = false ;
102108 let mut was_overloaded = false ;
103109 let mut time_since_control = Instant :: now ( ) ;
@@ -109,6 +115,7 @@ fn main() {
109115 interpolation. add_special_floats_wrap180 ( & mut vec ! [ "PLANE BANK DEGREES" . to_string( ) ] ) ;
110116
111117 loop {
118+ let mut was_no_message = true ;
112119 if let Some ( client) = transfer_client. as_mut ( ) {
113120 let tx = & client. tx ;
114121 let rx = & client. rx ;
@@ -117,6 +124,7 @@ fn main() {
117124 // Simconnect message
118125 match message {
119126 Ok ( simconnect:: DispatchResult :: SimobjectData ( data) ) => {
127+ was_no_message = false ;
120128 // Send data to clients or to server
121129 let send_type: & str ;
122130 let data_string: String ;
@@ -133,8 +141,9 @@ fn main() {
133141 data_string = serde_json:: to_string ( & sim_data) . unwrap ( ) ;
134142
135143 // Don't update interpolation position if it's just going to get overwritten anyway
136- if interpolation. get_time_since_last_position ( ) > 1.0 {
144+ if need_update || interpolation. get_time_since_last_position ( ) > 1.0 {
137145 interpolation. record_current ( sim_data) ;
146+ need_update = false ;
138147 }
139148 // Update when time elapsed > than calculated update rate
140149 if update_rate_instant. elapsed ( ) . as_secs_f64 ( ) > update_rate {
@@ -143,7 +152,7 @@ fn main() {
143152 }
144153 } ,
145154 1 => {
146- let sim_data: SimValue = bool_defs. read_from_bytes ( data_pointer) ;
155+ let sim_data: SimValue = bool_defs. as_ref ( ) . unwrap ( ) . read_from_bytes ( data_pointer) ;
147156 send_type = "sync_toggle" ;
148157 data_string = serde_json:: to_string ( & sim_data) . unwrap ( ) ;
149158 definitions. record_boolean_values ( sim_data) ;
@@ -157,7 +166,7 @@ fn main() {
157166
158167 // Update position data
159168 if should_send {
160- tx. send ( json ! ( {
169+ tx. try_send ( json ! ( {
161170 "type" : send_type,
162171 "data" : data_string,
163172 "time" : std:: time:: SystemTime :: now( ) . duration_since( std:: time:: UNIX_EPOCH ) . unwrap( ) . as_secs_f64( )
@@ -176,15 +185,16 @@ fn main() {
176185 let group_id = unsafe { ( * data) . uGroupID } ;
177186 match group_id {
178187 0 => {
179- tx. send ( json ! ( {
188+ tx. try_send ( json ! ( {
180189 "type" : "event" ,
181190 "eventid" : event_id,
182191 "data" : dw_data
183192 } ) ) . ok ( ) ;
184193 }
185194 _ => ( )
186195 }
187- }
196+ } ,
197+ Err ( _) => connected = false ,
188198 _ => ( )
189199 } ;
190200 // Data from the person in control
@@ -258,15 +268,17 @@ fn main() {
258268 app_interface. stable ( ) ;
259269 }
260270
261- if let Some ( updated_map) = interpolation. interpolate ( ) {
262- let mut bytes = definitions. sim_vars . write_to_data ( & updated_map) ;
263- conn. set_data_on_sim_object ( 0 , 0 , 0 , 0 , bytes. len ( ) as u32 , bytes. as_mut_ptr ( ) as * mut std:: ffi:: c_void ) ;
271+ if !need_update {
272+ if let Some ( updated_map) = interpolation. interpolate ( ) {
273+ let mut bytes = definitions. sim_vars . write_to_data ( & updated_map) ;
274+ conn. set_data_on_sim_object ( 0 , 0 , 0 , 0 , bytes. len ( ) as u32 , bytes. as_mut_ptr ( ) as * mut std:: ffi:: c_void ) ;
275+ }
264276 }
265277 // Relieve control response timeout
266278 } else if has_control && relieving_control && time_since_relieve. elapsed ( ) . as_secs ( ) > 20 {
267279 relieving_control = false ;
268280 app_interface. gain_control ( ) ;
269- tx. send ( json ! ( {
281+ tx. try_send ( json ! ( {
270282 "type" : "cancel_relieve"
271283 } ) ) . ok ( ) ;
272284 }
@@ -286,39 +298,44 @@ fn main() {
286298 match app_interface. rx . try_recv ( ) {
287299 Ok ( msg) => match msg {
288300 AppMessage :: Server ( port ) => {
289- match start_server ( port) {
290- Ok ( transfer) => {
291- app_interface. server_started ( 0 ) ;
292- transfer_client = Some ( transfer) ;
293-
294- app_interface. gain_control ( ) ;
295- has_control = true ;
296- transfer_control ( & conn, has_control) ;
297- } ,
298- Err ( e) => {
299- app_interface. server_fail ( e. to_string ( ) . as_str ( ) ) ;
300- // Was error not nessecary here, stopped does not get fired
301+ if connected {
302+ match start_server ( port) {
303+ Ok ( transfer) => {
304+ app_interface. server_started ( 0 ) ;
305+ transfer_client = Some ( transfer) ;
306+
307+ app_interface. gain_control ( ) ;
308+ has_control = true ;
309+ transfer_control ( & conn, has_control) ;
310+ } ,
311+ Err ( e) => {
312+ app_interface. server_fail ( e. to_string ( ) . as_str ( ) ) ;
313+ // Was error not nessecary here, stopped does not get fired
314+ }
301315 }
302316 }
303317 }
304318 AppMessage :: Connect ( ip, input_string, port) => {
305- app_interface. attempt ( ) ;
306- match start_client ( ip, port) {
307- Ok ( transfer) => {
308- app_interface. connected ( ) ;
309- transfer_client = Some ( transfer) ;
310- time_since_control = Instant :: now ( ) ;
311-
312- has_control = false ;
313- transfer_control ( & conn, has_control) ;
314- }
315- Err ( e) => {
316- app_interface. client_fail ( e. to_string ( ) . as_str ( ) ) ;
317- // Was error not nessecary here
319+ if connected {
320+ app_interface. attempt ( ) ;
321+ match start_client ( ip, port) {
322+ Ok ( transfer) => {
323+ app_interface. connected ( ) ;
324+ transfer_client = Some ( transfer) ;
325+ time_since_control = Instant :: now ( ) ;
326+
327+ has_control = false ;
328+ need_update = true ;
329+ transfer_control ( & conn, has_control) ;
330+ }
331+ Err ( e) => {
332+ app_interface. client_fail ( e. to_string ( ) . as_str ( ) ) ;
333+ // Was error not nessecary here
334+ }
318335 }
336+ config. set_ip ( input_string) ;
337+ config. write_to_file ( CONFIG_FILENAME ) . ok ( ) ;
319338 }
320- config. set_ip ( input_string) ;
321- config. write_to_file ( CONFIG_FILENAME ) . ok ( ) ;
322339 }
323340 AppMessage :: Disconnect => {
324341 if let Some ( client) = transfer_client. as_ref ( ) {
@@ -327,7 +344,7 @@ fn main() {
327344 }
328345 AppMessage :: TakeControl => {
329346 if can_take_control {
330- transfer_client. as_ref ( ) . unwrap ( ) . tx . send ( json ! ( {
347+ transfer_client. as_ref ( ) . unwrap ( ) . tx . try_send ( json ! ( {
331348 "type" : "transfer_control"
332349 } ) ) . ok ( ) ;
333350 has_control = true ;
@@ -338,14 +355,31 @@ fn main() {
338355 AppMessage :: RelieveControl => {
339356 relieving_control = true ;
340357 time_since_relieve = Instant :: now ( ) ;
341- transfer_client. as_ref ( ) . unwrap ( ) . tx . send ( json ! ( {
358+ transfer_client. as_ref ( ) . unwrap ( ) . tx . try_send ( json ! ( {
342359 "type" : "relieve_control"
343360 } ) ) . ok ( ) ;
361+ } ,
362+ AppMessage :: Startup => {
363+ std:: thread:: sleep ( Duration :: from_millis ( 100 ) ) ;
364+ app_interface. set_ip ( config. ip . as_str ( ) ) ;
365+ app_interface. set_port ( config. port ) ;
344366 }
345367 }
346368 Err ( _) => { }
369+ }
370+ // Try to connect to simconnect if not connected
371+ if !connected {
372+ connected = conn. connect ( "Your Control" ) ;
373+ if connected {
374+ app_interface. disconnected ( ) ;
375+ bool_defs = Some ( on_simconnect_connect ( & conn, & mut definitions) ) ;
376+ } else {
377+ app_interface. error ( "Trying to connect to SimConnect..." ) ;
378+ } ;
347379 }
380+
381+ if was_no_message { std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) }
382+ // Attempt Simconnect connection
348383 if app_interface. exited ( ) { break }
349- std:: thread:: sleep ( Duration :: from_millis ( 1 ) ) ;
350384 }
351385}
0 commit comments