22from SimConnect import *
33from time import sleep
44import random
5-
5+ import asyncio
66
77app = Flask (__name__ )
88
1111# Create simconnection
1212sm = SimConnect ()
1313ae = AircraftEvents (sm )
14- aq = AircraftRequests (sm , _time = 10 )
14+ aq = AircraftRequests (sm )
1515
1616# Create request holders
1717
5050#]
5151
5252request_location = [
53- 'ALTITUDE ' ,
54- 'LATITUDE ' ,
55- 'LONGITUDE ' ,
56- 'KOHLSMAN ' ,
53+ 'PLANE_ALTITUDE ' ,
54+ 'PLANE_LATITUDE ' ,
55+ 'PLANE_LONGITUDE ' ,
56+ 'KOHLSMAN_SETTING_HG ' ,
5757]
5858
5959request_airspeed = [
@@ -295,82 +295,83 @@ def get_dataset(data_type):
295295
296296 return request_to_action
297297
298-
299- @app .route ('/ui' )
300- def output_ui_variables ():
301-
302- # Initialise dictionaru
303- ui_friendly_dictionary = {}
304- ui_friendly_dictionary ["STATUS" ] = "success"
305-
298+ async def ui_dictionary (ui_friendly_dictionary ):
306299 # Fuel
307- fuel_percentage = (aq .get ("FUEL_TOTAL_QUANTITY" ) / aq .get ("FUEL_TOTAL_CAPACITY" )) * 100
308- ui_friendly_dictionary ["FUEL_PERCENTAGE" ] = round (fuel_percentage )
309- ui_friendly_dictionary ["AIRSPEED_INDICATE" ] = round (aq .get ("AIRSPEED_INDICATED" ))
310- ui_friendly_dictionary ["ALTITUDE" ] = thousandify (round (aq .get ("PLANE_ALTITUDE" )))
311-
300+ ui_friendly_dictionary ["FUEL_PERCENTAGE" ] = round ((await aq .get ("FUEL_TOTAL_QUANTITY" ) / await aq .get ("FUEL_TOTAL_CAPACITY" )) * 100 )
301+ ui_friendly_dictionary ["AIRSPEED_INDICATE" ] = round (await aq .get ("AIRSPEED_INDICATED" ))
302+ ui_friendly_dictionary ["ALTITUDE" ] = thousandify (round (await aq .get ("PLANE_ALTITUDE" )))
312303 # Control surfaces
313- if aq .get ("GEAR_HANDLE_POSITION" ) == 1 :
304+ if await aq .get ("GEAR_HANDLE_POSITION" ) == 1 :
314305 ui_friendly_dictionary ["GEAR_HANDLE_POSITION" ] = "DOWN"
315306 else :
316307 ui_friendly_dictionary ["GEAR_HANDLE_POSITION" ] = "UP"
317- ui_friendly_dictionary ["FLAPS_HANDLE_PERCENT" ] = round (aq .get ("FLAPS_HANDLE_PERCENT" ) * 100 )
308+ ui_friendly_dictionary ["FLAPS_HANDLE_PERCENT" ] = round (await aq .get ("FLAPS_HANDLE_PERCENT" ) * 100 )
318309
319- ui_friendly_dictionary ["ELEVATOR_TRIM_PCT" ] = round (aq .get ("ELEVATOR_TRIM_PCT" ) * 100 )
320- ui_friendly_dictionary ["RUDDER_TRIM_PCT" ] = round (aq .get ("RUDDER_TRIM_PCT" ) * 100 )
310+ ui_friendly_dictionary ["ELEVATOR_TRIM_PCT" ] = round (await aq .get ("ELEVATOR_TRIM_PCT" ) * 100 )
311+ ui_friendly_dictionary ["RUDDER_TRIM_PCT" ] = round (await aq .get ("RUDDER_TRIM_PCT" ) * 100 )
321312
322313 # Navigation
323- ui_friendly_dictionary ["LATITUDE" ] = aq .get ("PLANE_LATITUDE" )
324- ui_friendly_dictionary ["LONGITUDE" ] = aq .get ("PLANE_LONGITUDE" )
325- ui_friendly_dictionary ["MAGNETIC_COMPASS" ] = round (aq .get ("MAGNETIC_COMPASS" ))
326- ui_friendly_dictionary ["MAGVAR" ] = round (aq .get ("MAGVAR" ))
327- ui_friendly_dictionary ["VERTICAL_SPEED" ] = round (aq .get ("VERTICAL_SPEED" ))
314+ ui_friendly_dictionary ["LATITUDE" ] = await aq .get ("PLANE_LATITUDE" )
315+ ui_friendly_dictionary ["LONGITUDE" ] = await aq .get ("PLANE_LONGITUDE" )
316+ ui_friendly_dictionary ["MAGNETIC_COMPASS" ] = round (await aq .get ("MAGNETIC_COMPASS" ))
317+ ui_friendly_dictionary ["MAGVAR" ] = round (await aq .get ("MAGVAR" ))
318+ ui_friendly_dictionary ["VERTICAL_SPEED" ] = round (await aq .get ("VERTICAL_SPEED" ))
328319
329320 # Autopilot
330- ui_friendly_dictionary ["AUTOPILOT_MASTER" ] = aq .get ("AUTOPILOT_MASTER" )
331- ui_friendly_dictionary ["AUTOPILOT_NAV_SELECTED" ] = aq .get ("AUTOPILOT_NAV_SELECTED" )
332- ui_friendly_dictionary ["AUTOPILOT_WING_LEVELER" ] = aq .get ("AUTOPILOT_WING_LEVELER" )
333- ui_friendly_dictionary ["AUTOPILOT_HEADING_LOCK" ] = aq .get ("AUTOPILOT_HEADING_LOCK" )
334- ui_friendly_dictionary ["AUTOPILOT_HEADING_LOCK_DIR" ] = round (aq .get ("AUTOPILOT_HEADING_LOCK_DIR" ))
335- ui_friendly_dictionary ["AUTOPILOT_ALTITUDE_LOCK" ] = aq .get ("AUTOPILOT_ALTITUDE_LOCK" )
336- ui_friendly_dictionary ["AUTOPILOT_ALTITUDE_LOCK_VAR" ] = thousandify (round (aq .get ("AUTOPILOT_ALTITUDE_LOCK_VAR" )))
337- ui_friendly_dictionary ["AUTOPILOT_ATTITUDE_HOLD" ] = aq .get ("AUTOPILOT_ATTITUDE_HOLD" )
338- ui_friendly_dictionary ["AUTOPILOT_GLIDESLOPE_HOLD" ] = aq .get ("AUTOPILOT_GLIDESLOPE_HOLD" )
339- ui_friendly_dictionary ["AUTOPILOT_APPROACH_HOLD" ] = aq .get ("AUTOPILOT_APPROACH_HOLD" )
340- ui_friendly_dictionary ["AUTOPILOT_BACKCOURSE_HOLD" ] = aq .get ("AUTOPILOT_BACKCOURSE_HOLD" )
341- ui_friendly_dictionary ["AUTOPILOT_VERTICAL_HOLD" ] = aq .get ("AUTOPILOT_VERTICAL_HOLD" )
342- ui_friendly_dictionary ["AUTOPILOT_VERTICAL_HOLD_VAR" ] = aq .get ("AUTOPILOT_VERTICAL_HOLD_VAR" )
343- ui_friendly_dictionary ["AUTOPILOT_PITCH_HOLD" ] = aq .get ("AUTOPILOT_PITCH_HOLD" )
344- ui_friendly_dictionary ["AUTOPILOT_PITCH_HOLD_REF" ] = aq .get ("AUTOPILOT_PITCH_HOLD_REF" )
345- ui_friendly_dictionary ["AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE" ] = aq .get ("AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE" )
346- ui_friendly_dictionary ["AUTOPILOT_AIRSPEED_HOLD" ] = aq .get ("AUTOPILOT_AIRSPEED_HOLD" )
347- ui_friendly_dictionary ["AUTOPILOT_AIRSPEED_HOLD_VAR" ] = round (aq .get ("AUTOPILOT_AIRSPEED_HOLD_VAR" ))
321+ ui_friendly_dictionary ["AUTOPILOT_MASTER" ] = await aq .get ("AUTOPILOT_MASTER" )
322+ ui_friendly_dictionary ["AUTOPILOT_NAV_SELECTED" ] = await aq .get ("AUTOPILOT_NAV_SELECTED" )
323+ ui_friendly_dictionary ["AUTOPILOT_WING_LEVELER" ] = await aq .get ("AUTOPILOT_WING_LEVELER" )
324+ ui_friendly_dictionary ["AUTOPILOT_HEADING_LOCK" ] = await aq .get ("AUTOPILOT_HEADING_LOCK" )
325+ ui_friendly_dictionary ["AUTOPILOT_HEADING_LOCK_DIR" ] = round (await aq .get ("AUTOPILOT_HEADING_LOCK_DIR" ))
326+ ui_friendly_dictionary ["AUTOPILOT_ALTITUDE_LOCK" ] = await aq .get ("AUTOPILOT_ALTITUDE_LOCK" )
327+ ui_friendly_dictionary ["AUTOPILOT_ALTITUDE_LOCK_VAR" ] = thousandify (round (await aq .get ("AUTOPILOT_ALTITUDE_LOCK_VAR" )))
328+ ui_friendly_dictionary ["AUTOPILOT_ATTITUDE_HOLD" ] = await aq .get ("AUTOPILOT_ATTITUDE_HOLD" )
329+ ui_friendly_dictionary ["AUTOPILOT_GLIDESLOPE_HOLD" ] = await aq .get ("AUTOPILOT_GLIDESLOPE_HOLD" )
330+ ui_friendly_dictionary ["AUTOPILOT_APPROACH_HOLD" ] = await aq .get ("AUTOPILOT_APPROACH_HOLD" )
331+ ui_friendly_dictionary ["AUTOPILOT_BACKCOURSE_HOLD" ] = await aq .get ("AUTOPILOT_BACKCOURSE_HOLD" )
332+ ui_friendly_dictionary ["AUTOPILOT_VERTICAL_HOLD" ] = await aq .get ("AUTOPILOT_VERTICAL_HOLD" )
333+ ui_friendly_dictionary ["AUTOPILOT_VERTICAL_HOLD_VAR" ] = await aq .get ("AUTOPILOT_VERTICAL_HOLD_VAR" )
334+ ui_friendly_dictionary ["AUTOPILOT_PITCH_HOLD" ] = await aq .get ("AUTOPILOT_PITCH_HOLD" )
335+ ui_friendly_dictionary ["AUTOPILOT_PITCH_HOLD_REF" ] = await aq .get ("AUTOPILOT_PITCH_HOLD_REF" )
336+ ui_friendly_dictionary ["AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE" ] = await aq .get ("AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE" )
337+ ui_friendly_dictionary ["AUTOPILOT_AIRSPEED_HOLD" ] = await aq .get ("AUTOPILOT_AIRSPEED_HOLD" )
338+ ui_friendly_dictionary ["AUTOPILOT_AIRSPEED_HOLD_VAR" ] = round (await aq .get ("AUTOPILOT_AIRSPEED_HOLD_VAR" ))
348339
349340 # Cabin
350- ui_friendly_dictionary ["CABIN_SEATBELTS_ALERT_SWITCH" ] = aq .get ("CABIN_SEATBELTS_ALERT_SWITCH" )
351- ui_friendly_dictionary ["CABIN_NO_SMOKING_ALERT_SWITCH" ] = aq .get ("CABIN_NO_SMOKING_ALERT_SWITCH" )
341+ ui_friendly_dictionary ["CABIN_SEATBELTS_ALERT_SWITCH" ] = await aq .get ("CABIN_SEATBELTS_ALERT_SWITCH" )
342+ ui_friendly_dictionary ["CABIN_NO_SMOKING_ALERT_SWITCH" ] = await aq .get ("CABIN_NO_SMOKING_ALERT_SWITCH" )
352343
344+ @app .route ('/ui' )
345+ def output_ui_variables ():
346+ # Initialise dictionaru
347+ ui_friendly_dictionary = {}
348+ ui_friendly_dictionary ["STATUS" ] = "success"
349+ asyncio .run (ui_dictionary (ui_friendly_dictionary ))
353350 return jsonify (ui_friendly_dictionary )
354351
355352
353+ async def _dictionary (data_dictionary ):
354+ dataset_map = {} # I have renamed map to dataset_map as map is used elsewhere
355+ for datapoint_name in data_dictionary :
356+ print (datapoint_name )
357+ dataset_map [datapoint_name ] = await aq .get (datapoint_name )
358+ return jsonify (dataset_map )
359+
356360@app .route ('/dataset/<dataset_name>/' , methods = ["GET" ])
357361def output_json_dataset (dataset_name ):
358- dataset_map = {} #I have renamed map to dataset_map as map is used elsewhere
359362 data_dictionary = get_dataset (dataset_name )
360- for datapoint_name in data_dictionary :
361- dataset_map [datapoint_name ] = aq .get (datapoint_name )
362- return jsonify (dataset_map )
363+ return asyncio .run (_dictionary (data_dictionary ))
363364
364365
365- def get_datapoint (datapoint_name , index = None ):
366+ async def get_datapoint (datapoint_name , index = None ):
366367 # This function actually does the work of getting the datapoint
367368
368369 if index is not None and ':index' in datapoint_name :
369370 dp = aq .find (datapoint_name )
370371 if dp is not None :
371372 dp .setIndex (int (index ))
372373
373- return aq .get (datapoint_name )
374+ return await aq .get (datapoint_name )
374375
375376
376377@app .route ('/datapoint/<datapoint_name>/get' , methods = ["GET" ])
@@ -380,7 +381,7 @@ def get_datapoint_endpoint(datapoint_name):
380381 ds = request .get_json () if request .is_json else request .form
381382 index = ds .get ('index' )
382383
383- output = get_datapoint (datapoint_name , index )
384+ output = asyncio . run ( get_datapoint (datapoint_name , index ) )
384385
385386 if isinstance (output , bytes ):
386387 output = output .decode ('ascii' )
@@ -413,17 +414,14 @@ def set_datapoint(datapoint_name, index=None, value_to_use=None):
413414@app .route ('/datapoint/<datapoint_name>/set' , methods = ["POST" ])
414415def set_datapoint_endpoint (datapoint_name ):
415416 # This is the http endpoint wrapper for setting a datapoint
416-
417417 ds = request .get_json () if request .is_json else request .form
418418 index = ds .get ('index' )
419419 value_to_use = ds .get ('value_to_use' )
420-
421- status = set_datapoint (datapoint_name , index , value_to_use )
422-
420+ status = set_datapoint (datapoint_name , index , value_to_use )
423421 return jsonify (status )
424422
425423
426- def trigger_event (event_name , value_to_use = None ):
424+ def trigger_event (event_name , value_to_use = None ):
427425 # This function actually does the work of triggering the event
428426
429427 EVENT_TO_TRIGGER = ae .find (event_name )
@@ -452,17 +450,21 @@ def trigger_event_endpoint(event_name):
452450 return jsonify (status )
453451
454452
453+ async def get_engen_ct ():
454+ return await aq .get ("NUMBER_OF_ENGINES" )
455+
455456@app .route ('/custom_emergency/<emergency_type>' , methods = ["GET" , "POST" ])
456457def custom_emergency (emergency_type ):
457458
458459 text_to_return = "No valid emergency type passed"
459460
460461 if emergency_type == "random_engine_fire" :
461462 # Calculate number of engines
462- number_of_engines = aq . get ( "NUMBER_OF_ENGINES" )
463+ number_of_engines = asyncio . run ( get_engen_ct () )
463464
464- if number_of_engines < 0 : return "error, no engines found - is sim running?"
465- engine_to_set_on_fire = random .randint (1 ,number_of_engines )
465+ if number_of_engines < 0 :
466+ return "error, no engines found - is sim running?"
467+ engine_to_set_on_fire = random .randint (1 , number_of_engines )
466468
467469 set_datapoint ("ENG_ON_FIRE:index" , engine_to_set_on_fire , 1 )
468470
@@ -471,4 +473,4 @@ def custom_emergency(emergency_type):
471473 return text_to_return
472474
473475
474- app .run (host = '0.0.0.0' , port = 5000 , debug = True )
476+ app .run (host = '0.0.0.0' , port = 5000 , debug = True )
0 commit comments