2
2
from SimConnect import *
3
3
from time import sleep
4
4
import random
5
-
5
+ import asyncio
6
6
7
7
app = Flask (__name__ )
8
8
11
11
# Create simconnection
12
12
sm = SimConnect ()
13
13
ae = AircraftEvents (sm )
14
- aq = AircraftRequests (sm , _time = 10 )
14
+ aq = AircraftRequests (sm )
15
15
16
16
# Create request holders
17
17
50
50
#]
51
51
52
52
request_location = [
53
- 'ALTITUDE ' ,
54
- 'LATITUDE ' ,
55
- 'LONGITUDE ' ,
56
- 'KOHLSMAN ' ,
53
+ 'PLANE_ALTITUDE ' ,
54
+ 'PLANE_LATITUDE ' ,
55
+ 'PLANE_LONGITUDE ' ,
56
+ 'KOHLSMAN_SETTING_HG ' ,
57
57
]
58
58
59
59
request_airspeed = [
@@ -295,82 +295,83 @@ def get_dataset(data_type):
295
295
296
296
return request_to_action
297
297
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 ):
306
299
# 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" )))
312
303
# Control surfaces
313
- if aq .get ("GEAR_HANDLE_POSITION" ) == 1 :
304
+ if await aq .get ("GEAR_HANDLE_POSITION" ) == 1 :
314
305
ui_friendly_dictionary ["GEAR_HANDLE_POSITION" ] = "DOWN"
315
306
else :
316
307
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 )
318
309
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 )
321
312
322
313
# 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" ))
328
319
329
320
# 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" ))
348
339
349
340
# 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" )
352
343
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 ))
353
350
return jsonify (ui_friendly_dictionary )
354
351
355
352
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
+
356
360
@app .route ('/dataset/<dataset_name>/' , methods = ["GET" ])
357
361
def output_json_dataset (dataset_name ):
358
- dataset_map = {} #I have renamed map to dataset_map as map is used elsewhere
359
362
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 ))
363
364
364
365
365
- def get_datapoint (datapoint_name , index = None ):
366
+ async def get_datapoint (datapoint_name , index = None ):
366
367
# This function actually does the work of getting the datapoint
367
368
368
369
if index is not None and ':index' in datapoint_name :
369
370
dp = aq .find (datapoint_name )
370
371
if dp is not None :
371
372
dp .setIndex (int (index ))
372
373
373
- return aq .get (datapoint_name )
374
+ return await aq .get (datapoint_name )
374
375
375
376
376
377
@app .route ('/datapoint/<datapoint_name>/get' , methods = ["GET" ])
@@ -380,7 +381,7 @@ def get_datapoint_endpoint(datapoint_name):
380
381
ds = request .get_json () if request .is_json else request .form
381
382
index = ds .get ('index' )
382
383
383
- output = get_datapoint (datapoint_name , index )
384
+ output = asyncio . run ( get_datapoint (datapoint_name , index ) )
384
385
385
386
if isinstance (output , bytes ):
386
387
output = output .decode ('ascii' )
@@ -413,17 +414,14 @@ def set_datapoint(datapoint_name, index=None, value_to_use=None):
413
414
@app .route ('/datapoint/<datapoint_name>/set' , methods = ["POST" ])
414
415
def set_datapoint_endpoint (datapoint_name ):
415
416
# This is the http endpoint wrapper for setting a datapoint
416
-
417
417
ds = request .get_json () if request .is_json else request .form
418
418
index = ds .get ('index' )
419
419
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 )
423
421
return jsonify (status )
424
422
425
423
426
- def trigger_event (event_name , value_to_use = None ):
424
+ def trigger_event (event_name , value_to_use = None ):
427
425
# This function actually does the work of triggering the event
428
426
429
427
EVENT_TO_TRIGGER = ae .find (event_name )
@@ -452,17 +450,21 @@ def trigger_event_endpoint(event_name):
452
450
return jsonify (status )
453
451
454
452
453
+ async def get_engen_ct ():
454
+ return await aq .get ("NUMBER_OF_ENGINES" )
455
+
455
456
@app .route ('/custom_emergency/<emergency_type>' , methods = ["GET" , "POST" ])
456
457
def custom_emergency (emergency_type ):
457
458
458
459
text_to_return = "No valid emergency type passed"
459
460
460
461
if emergency_type == "random_engine_fire" :
461
462
# Calculate number of engines
462
- number_of_engines = aq . get ( "NUMBER_OF_ENGINES" )
463
+ number_of_engines = asyncio . run ( get_engen_ct () )
463
464
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 )
466
468
467
469
set_datapoint ("ENG_ON_FIRE:index" , engine_to_set_on_fire , 1 )
468
470
@@ -471,4 +473,4 @@ def custom_emergency(emergency_type):
471
473
return text_to_return
472
474
473
475
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