From 79fb706f412af73912f59b4f651518146cab63d5 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sat, 24 Oct 2020 13:35:44 -0700 Subject: [PATCH 1/8] Trying out asyncio Not shure if this is the right derection or not thoughts? --- SimConnect/RequestList.py | 25 ++++++----- SimConnect/SimConnect.py | 18 +++----- local_example.py | 94 +++++++-------------------------------- 3 files changed, 37 insertions(+), 100 deletions(-) diff --git a/SimConnect/RequestList.py b/SimConnect/RequestList.py index d030da21..61c96a11 100644 --- a/SimConnect/RequestList.py +++ b/SimConnect/RequestList.py @@ -1,28 +1,27 @@ from SimConnect import * from .Enum import * from .Constants import * +import asyncio class Request(object): - def get(self): + async def get(self): return self.value def set(self, _value): self.value = _value @property - def value(self): + async def value(self): if self._deff_test(): # self.sm.run() if (self.LastData + self.time) < millis(): - if self.sm.get_data(self): - self.LastData = millis() - else: - return None + await self.sm.get_data(self) + self.LastData = millis() return self.outData else: - return None + raise Exception(self.definitions[0][0]) @value.setter def value(self, val): @@ -69,9 +68,9 @@ def redefine(self): ) self.defined = False # self.sm.run() - if self._deff_test(): - # self.sm.run() - self.sm.get_data(self) + # if self._deff_test(): + # # self.sm.run() + # self.sm.get_data(self) def _deff_test(self): if ':index' in str(self.definitions[0][0]): @@ -132,10 +131,10 @@ def __getattr__(self, _name): return ne return None - def get(self, _name): + async def get(self, _name): if getattr(self, _name) is None: return None - return getattr(self, _name).value + return await getattr(self, _name).value def set(self, _name, _value=0): temp = getattr(self, _name) @@ -170,6 +169,8 @@ def find(self, key): if key in clas.list: rqest = getattr(clas, key) if index is not None: + if 'index' in index: + index = 0 rqest.setIndex(index) return rqest return None diff --git a/SimConnect/SimConnect.py b/SimConnect/SimConnect.py index 89eae8b5..16bdc31e 100644 --- a/SimConnect/SimConnect.py +++ b/SimConnect/SimConnect.py @@ -7,6 +7,7 @@ from .Attributes import * import os import threading +import asyncio _library_path = os.path.abspath(__file__).replace(".py", ".dll") @@ -239,18 +240,13 @@ def set_data(self, _Request): else: return False - def get_data(self, _Request): - self.request_data(_Request) - # self.run() - attemps = 0 - while _Request.outData is None and attemps < _Request.attemps: - # self.run() - time.sleep(.01) - attemps += 1 - if _Request.outData is None: - return False + async def get_return_data(self, _Request): + while _Request.outData is None: + await asyncio.sleep(0.01) - return True + async def get_data(self, _Request): + self.request_data(_Request) + await self.get_return_data(_Request) def send_event(self, evnt, data=DWORD(0)): err = self.dll.TransmitClientEvent( diff --git a/local_example.py b/local_example.py index 54a18496..0618f904 100644 --- a/local_example.py +++ b/local_example.py @@ -1,94 +1,34 @@ from SimConnect import * import logging -from SimConnect.Enum import * from time import sleep - - +import asyncio logging.basicConfig(level=logging.DEBUG) LOGGER = logging.getLogger(__name__) LOGGER.info("START") -# time holder for inline commands -ct_g = millis() - -# creat simconnection and pass used user classes sm = SimConnect() aq = AircraftRequests(sm) -ae = AircraftEvents(sm) - - -mc = aq.find("MAGNETIC_COMPASS") -mv = aq.find("MAGVAR") -print(mc.get() + mv.get()) - -sm.exit() -quit() - -# Set pos arund space nedle in WA. -sm.set_pos( - _Altitude=1000.0, - _Latitude=47.614699, - _Longitude=-122.358473, - _Airspeed=130, - _Heading=70.0, - # _Pitch=0.0, - # _Bank=0.0, - # _OnGround=0 -) - -# PARKING_BRAKES = Event(b'PARKING_BRAKES', sm) -# long path -PARKING_BRAKES = ae.Miscellaneous_Systems.PARKING_BRAKES -# using get -GEAR_TOGGLE = ae.Miscellaneous_Systems.get("GEAR_TOGGLE") -# Using find to lookup Event -AP_MASTER = ae.find("AP_MASTER") - -# THROTTLE1 Event -THROTTLE1 = ae.Engine.THROTTLE1_SET - - -# THROTTLE1 Request -Throttle = aq.find('GENERAL_ENG_THROTTLE_LEVER_POSITION:1') - -# If useing -# Throttle = aq.find('GENERAL_ENG_THROTTLE_LEVER_POSITION:index') -# Need to set index befor read/write -# Note to set index 2 vs 1 just re-run -# Throttle.setIndex(1) +Data = {} -# print the built in description -# AP_MASTER Toggles AP on/off -print("AP_MASTER", AP_MASTER.description) -# Throttle Percent of max throttle position -print("Throttle", Throttle.description) -# THROTTLE1 Set throttle 1 exactly (0 to 16383) -print("THROTTLE1", THROTTLE1.description) +async def pintVal(name): + global Data + Data[name] = await aq.get(name) -while not sm.quit: - print("Throttle:", Throttle.value) - print("Alt=%f Lat=%f Lon=%f Kohlsman=%.2f" % ( - aq.PositionandSpeedData.get('PLANE_ALTITUDE'), - aq.PositionandSpeedData.get('PLANE_LATITUDE'), - aq.PositionandSpeedData.get('PLANE_LONGITUDE'), - aq.FlightInstrumentationData.get('KOHLSMAN_SETTING_HG') - )) - sleep(2) - # Send Event with value - # THROTTLE1(1500) +async def main(): + while not sm.quit: + temp = {} + for ed in aq.PositionandSpeedData.list: + temp[ed] = asyncio.create_task(pintVal(ed)) - # Send Event toggle AP_MASTER - # AP_MASTER() + for ed in aq.PositionandSpeedData.list: + await temp[ed] - # PARKING_BRAKES() + print(Data) + sleep(2) + sm.exit() + quit() - # send new data inine @ 5s - if ct_g + 5000 < millis(): - if Throttle.value < 100: - Throttle.value += 5 - print("THROTTLE SET") - ct_g = millis() -sm.exit() +asyncio.run(main()) \ No newline at end of file From c23084c2aa791fd14f9dafcf1961f1b61bbb96e2 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sat, 24 Oct 2020 13:36:27 -0700 Subject: [PATCH 2/8] updated the defatuts _attemps is depercataed --- SimConnect/RequestList.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SimConnect/RequestList.py b/SimConnect/RequestList.py index 61c96a11..f6bc0d7a 100644 --- a/SimConnect/RequestList.py +++ b/SimConnect/RequestList.py @@ -30,7 +30,7 @@ def value(self, val): self.sm.set_data(self) # self.sm.run() - def __init__(self, _deff, _sm, _time=2000, _dec=None, _settable=False, _attemps=4): + def __init__(self, _deff, _sm, _time=500, _dec=None, _settable=False, _attemps=10): self.DATA_DEFINITION_ID = None self.definitions = [] self.description = _dec @@ -111,7 +111,7 @@ def _deff_test(self): class RequestHelper: - def __init__(self, _sm, _time=2000, _attemps=4): + def __init__(self, _sm, _time=500, _attemps=10): self.sm = _sm self.dic = [] self.time = _time From 1d7587ae5290402c0ca4ef8179880be2e3ff26b4 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sat, 24 Oct 2020 15:23:46 -0700 Subject: [PATCH 3/8] update sample to support asyncio --- local_example.py | 73 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 15 deletions(-) diff --git a/local_example.py b/local_example.py index 0618f904..fd0c39c5 100644 --- a/local_example.py +++ b/local_example.py @@ -1,34 +1,77 @@ from SimConnect import * import logging +from SimConnect.Enum import * from time import sleep import asyncio + logging.basicConfig(level=logging.DEBUG) LOGGER = logging.getLogger(__name__) LOGGER.info("START") + + +# creat simconnection and pass used user classes sm = SimConnect() -aq = AircraftRequests(sm) +aq = AircraftRequests(sm, _time=10, _attemps=10) +ae = AircraftEvents(sm) + +# PARKING_BRAKES = Event(b'PARKING_BRAKES', sm) +# long path +PARKING_BRAKES = ae.Miscellaneous_Systems.PARKING_BRAKES +# using get +GEAR_TOGGLE = ae.Miscellaneous_Systems.get("GEAR_TOGGLE") +# Using find to lookup Event +AP_MASTER = ae.find("AP_MASTER") + +# THROTTLE1 Event +THROTTLE1 = ae.Engine.THROTTLE1_SET + -Data = {} +# THROTTLE1 Request +Throttle = aq.find('GENERAL_ENG_THROTTLE_LEVER_POSITION:1') +# If useing +# Throttle = aq.find('GENERAL_ENG_THROTTLE_LEVER_POSITION:index') +# Need to set index befor read/write +# Note to set index 2 vs 1 just re-run +# Throttle.setIndex(1) -async def pintVal(name): - global Data - Data[name] = await aq.get(name) + +# print the built in description +# AP_MASTER Toggles AP on/off +print("AP_MASTER", AP_MASTER.description) +# Throttle Percent of max throttle position +print("Throttle", Throttle.description) +# THROTTLE1 Set throttle 1 exactly (0 to 16383) +print("THROTTLE1", THROTTLE1.description) async def main(): + # time holder for inline commands + ct_g = millis() while not sm.quit: - temp = {} - for ed in aq.PositionandSpeedData.list: - temp[ed] = asyncio.create_task(pintVal(ed)) + print("Throttle:", await Throttle.value) + print("Alt=%f Lat=%f Lon=%f Kohlsman=%.2f" % ( + await aq.PositionandSpeedData.get('PLANE_ALTITUDE'), + await aq.PositionandSpeedData.get('PLANE_LATITUDE'), + await aq.PositionandSpeedData.get('PLANE_LONGITUDE'), + await aq.FlightInstrumentationData.get('KOHLSMAN_SETTING_HG') + )) + sleep(2) - for ed in aq.PositionandSpeedData.list: - await temp[ed] + # Send Event with value + # THROTTLE1(1500) - print(Data) - sleep(2) - sm.exit() - quit() + # Send Event toggle AP_MASTER + # AP_MASTER() + + # PARKING_BRAKES() + # send new data inine @ 5s + if ct_g + 5000 < millis(): + if await Throttle.value < 100: + Throttle.set(await Throttle.value + 5) + print("THROTTLE SET") + ct_g = millis() + sm.exit() -asyncio.run(main()) \ No newline at end of file +asyncio.run(main()) From 9e7126e4e2e18791186d5ec0a79fe6e5832e3024 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sat, 24 Oct 2020 15:24:09 -0700 Subject: [PATCH 4/8] added asyncio support --- glass_server.py | 130 ++++++++++++++++++++++++------------------------ 1 file changed, 66 insertions(+), 64 deletions(-) diff --git a/glass_server.py b/glass_server.py index 85972986..aaa9c4c1 100644 --- a/glass_server.py +++ b/glass_server.py @@ -2,7 +2,7 @@ from SimConnect import * from time import sleep import random - +import asyncio app = Flask(__name__) @@ -11,7 +11,7 @@ # Create simconnection sm = SimConnect() ae = AircraftEvents(sm) -aq = AircraftRequests(sm, _time=10) +aq = AircraftRequests(sm) # Create request holders @@ -50,10 +50,10 @@ #] request_location = [ - 'ALTITUDE', - 'LATITUDE', - 'LONGITUDE', - 'KOHLSMAN', + 'PLANE_ALTITUDE', + 'PLANE_LATITUDE', + 'PLANE_LONGITUDE', + 'KOHLSMAN_SETTING_HG', ] request_airspeed = [ @@ -295,74 +295,75 @@ def get_dataset(data_type): return request_to_action - -@app.route('/ui') -def output_ui_variables(): - - # Initialise dictionaru - ui_friendly_dictionary = {} - ui_friendly_dictionary["STATUS"] = "success" - +async def ui_dictionary(ui_friendly_dictionary): # Fuel - fuel_percentage = (aq.get("FUEL_TOTAL_QUANTITY") / aq.get("FUEL_TOTAL_CAPACITY")) * 100 - ui_friendly_dictionary["FUEL_PERCENTAGE"] = round(fuel_percentage) - ui_friendly_dictionary["AIRSPEED_INDICATE"] = round(aq.get("AIRSPEED_INDICATED")) - ui_friendly_dictionary["ALTITUDE"] = thousandify(round(aq.get("PLANE_ALTITUDE"))) - + ui_friendly_dictionary["FUEL_PERCENTAGE"] = round((await aq.get("FUEL_TOTAL_QUANTITY") / await aq.get("FUEL_TOTAL_CAPACITY")) * 100) + ui_friendly_dictionary["AIRSPEED_INDICATE"] = round(await aq.get("AIRSPEED_INDICATED")) + ui_friendly_dictionary["ALTITUDE"] = thousandify(round(await aq.get("PLANE_ALTITUDE"))) # Control surfaces - if aq.get("GEAR_HANDLE_POSITION") == 1: + if await aq.get("GEAR_HANDLE_POSITION") == 1: ui_friendly_dictionary["GEAR_HANDLE_POSITION"] = "DOWN" else: ui_friendly_dictionary["GEAR_HANDLE_POSITION"] = "UP" - ui_friendly_dictionary["FLAPS_HANDLE_PERCENT"] = round(aq.get("FLAPS_HANDLE_PERCENT") * 100) + ui_friendly_dictionary["FLAPS_HANDLE_PERCENT"] = round(await aq.get("FLAPS_HANDLE_PERCENT") * 100) - ui_friendly_dictionary["ELEVATOR_TRIM_PCT"] = round(aq.get("ELEVATOR_TRIM_PCT") * 100) - ui_friendly_dictionary["RUDDER_TRIM_PCT"] = round(aq.get("RUDDER_TRIM_PCT") * 100) + ui_friendly_dictionary["ELEVATOR_TRIM_PCT"] = round(await aq.get("ELEVATOR_TRIM_PCT") * 100) + ui_friendly_dictionary["RUDDER_TRIM_PCT"] = round(await aq.get("RUDDER_TRIM_PCT") * 100) # Navigation - ui_friendly_dictionary["LATITUDE"] = aq.get("PLANE_LATITUDE") - ui_friendly_dictionary["LONGITUDE"] = aq.get("PLANE_LONGITUDE") - ui_friendly_dictionary["MAGNETIC_COMPASS"] = round(aq.get("MAGNETIC_COMPASS")) - ui_friendly_dictionary["MAGVAR"] = round(aq.get("MAGVAR")) - ui_friendly_dictionary["VERTICAL_SPEED"] = round(aq.get("VERTICAL_SPEED")) + ui_friendly_dictionary["LATITUDE"] = await aq.get("PLANE_LATITUDE") + ui_friendly_dictionary["LONGITUDE"] = await aq.get("PLANE_LONGITUDE") + ui_friendly_dictionary["MAGNETIC_COMPASS"] = round(await aq.get("MAGNETIC_COMPASS")) + ui_friendly_dictionary["MAGVAR"] = round(await aq.get("MAGVAR")) + ui_friendly_dictionary["VERTICAL_SPEED"] = round(await aq.get("VERTICAL_SPEED")) # Autopilot - ui_friendly_dictionary["AUTOPILOT_MASTER"] = aq.get("AUTOPILOT_MASTER") - ui_friendly_dictionary["AUTOPILOT_NAV_SELECTED"] = aq.get("AUTOPILOT_NAV_SELECTED") - ui_friendly_dictionary["AUTOPILOT_WING_LEVELER"] = aq.get("AUTOPILOT_WING_LEVELER") - ui_friendly_dictionary["AUTOPILOT_HEADING_LOCK"] = aq.get("AUTOPILOT_HEADING_LOCK") - ui_friendly_dictionary["AUTOPILOT_HEADING_LOCK_DIR"] = round(aq.get("AUTOPILOT_HEADING_LOCK_DIR")) - ui_friendly_dictionary["AUTOPILOT_ALTITUDE_LOCK"] = aq.get("AUTOPILOT_ALTITUDE_LOCK") - ui_friendly_dictionary["AUTOPILOT_ALTITUDE_LOCK_VAR"] = thousandify(round(aq.get("AUTOPILOT_ALTITUDE_LOCK_VAR"))) - ui_friendly_dictionary["AUTOPILOT_ATTITUDE_HOLD"] = aq.get("AUTOPILOT_ATTITUDE_HOLD") - ui_friendly_dictionary["AUTOPILOT_GLIDESLOPE_HOLD"] = aq.get("AUTOPILOT_GLIDESLOPE_HOLD") - ui_friendly_dictionary["AUTOPILOT_APPROACH_HOLD"] = aq.get("AUTOPILOT_APPROACH_HOLD") - ui_friendly_dictionary["AUTOPILOT_BACKCOURSE_HOLD"] = aq.get("AUTOPILOT_BACKCOURSE_HOLD") - ui_friendly_dictionary["AUTOPILOT_VERTICAL_HOLD"] = aq.get("AUTOPILOT_VERTICAL_HOLD") - ui_friendly_dictionary["AUTOPILOT_VERTICAL_HOLD_VAR"] = aq.get("AUTOPILOT_VERTICAL_HOLD_VAR") - ui_friendly_dictionary["AUTOPILOT_PITCH_HOLD"] = aq.get("AUTOPILOT_PITCH_HOLD") - ui_friendly_dictionary["AUTOPILOT_PITCH_HOLD_REF"] = aq.get("AUTOPILOT_PITCH_HOLD_REF") - ui_friendly_dictionary["AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE"] = aq.get("AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE") - ui_friendly_dictionary["AUTOPILOT_AIRSPEED_HOLD"] = aq.get("AUTOPILOT_AIRSPEED_HOLD") - ui_friendly_dictionary["AUTOPILOT_AIRSPEED_HOLD_VAR"] = round(aq.get("AUTOPILOT_AIRSPEED_HOLD_VAR")) + ui_friendly_dictionary["AUTOPILOT_MASTER"] = await aq.get("AUTOPILOT_MASTER") + ui_friendly_dictionary["AUTOPILOT_NAV_SELECTED"] = await aq.get("AUTOPILOT_NAV_SELECTED") + ui_friendly_dictionary["AUTOPILOT_WING_LEVELER"] = await aq.get("AUTOPILOT_WING_LEVELER") + ui_friendly_dictionary["AUTOPILOT_HEADING_LOCK"] = await aq.get("AUTOPILOT_HEADING_LOCK") + ui_friendly_dictionary["AUTOPILOT_HEADING_LOCK_DIR"] = round(await aq.get("AUTOPILOT_HEADING_LOCK_DIR")) + ui_friendly_dictionary["AUTOPILOT_ALTITUDE_LOCK"] = await aq.get("AUTOPILOT_ALTITUDE_LOCK") + ui_friendly_dictionary["AUTOPILOT_ALTITUDE_LOCK_VAR"] = thousandify(round(await aq.get("AUTOPILOT_ALTITUDE_LOCK_VAR"))) + ui_friendly_dictionary["AUTOPILOT_ATTITUDE_HOLD"] = await aq.get("AUTOPILOT_ATTITUDE_HOLD") + ui_friendly_dictionary["AUTOPILOT_GLIDESLOPE_HOLD"] = await aq.get("AUTOPILOT_GLIDESLOPE_HOLD") + ui_friendly_dictionary["AUTOPILOT_APPROACH_HOLD"] = await aq.get("AUTOPILOT_APPROACH_HOLD") + ui_friendly_dictionary["AUTOPILOT_BACKCOURSE_HOLD"] = await aq.get("AUTOPILOT_BACKCOURSE_HOLD") + ui_friendly_dictionary["AUTOPILOT_VERTICAL_HOLD"] = await aq.get("AUTOPILOT_VERTICAL_HOLD") + ui_friendly_dictionary["AUTOPILOT_VERTICAL_HOLD_VAR"] = await aq.get("AUTOPILOT_VERTICAL_HOLD_VAR") + ui_friendly_dictionary["AUTOPILOT_PITCH_HOLD"] = await aq.get("AUTOPILOT_PITCH_HOLD") + ui_friendly_dictionary["AUTOPILOT_PITCH_HOLD_REF"] = await aq.get("AUTOPILOT_PITCH_HOLD_REF") + ui_friendly_dictionary["AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE"] = await aq.get("AUTOPILOT_FLIGHT_DIRECTOR_ACTIVE") + ui_friendly_dictionary["AUTOPILOT_AIRSPEED_HOLD"] = await aq.get("AUTOPILOT_AIRSPEED_HOLD") + ui_friendly_dictionary["AUTOPILOT_AIRSPEED_HOLD_VAR"] = round(await aq.get("AUTOPILOT_AIRSPEED_HOLD_VAR")) # Cabin - ui_friendly_dictionary["CABIN_SEATBELTS_ALERT_SWITCH"] = aq.get("CABIN_SEATBELTS_ALERT_SWITCH") - ui_friendly_dictionary["CABIN_NO_SMOKING_ALERT_SWITCH"] = aq.get("CABIN_NO_SMOKING_ALERT_SWITCH") + ui_friendly_dictionary["CABIN_SEATBELTS_ALERT_SWITCH"] = await aq.get("CABIN_SEATBELTS_ALERT_SWITCH") + ui_friendly_dictionary["CABIN_NO_SMOKING_ALERT_SWITCH"] = await aq.get("CABIN_NO_SMOKING_ALERT_SWITCH") +@app.route('/ui') +def output_ui_variables(): + # Initialise dictionaru + ui_friendly_dictionary = {} + ui_friendly_dictionary["STATUS"] = "success" + asyncio.run(ui_dictionary(ui_friendly_dictionary)) return jsonify(ui_friendly_dictionary) +async def _dictionary(data_dictionary): + dataset_map = {} # I have renamed map to dataset_map as map is used elsewhere + for datapoint_name in data_dictionary: + print(datapoint_name) + dataset_map[datapoint_name] = await aq.get(datapoint_name) + return jsonify(dataset_map) + @app.route('/dataset//', methods=["GET"]) def output_json_dataset(dataset_name): - dataset_map = {} #I have renamed map to dataset_map as map is used elsewhere data_dictionary = get_dataset(dataset_name) - for datapoint_name in data_dictionary: - dataset_map[datapoint_name] = aq.get(datapoint_name) - return jsonify(dataset_map) + return asyncio.run(_dictionary(data_dictionary)) -def get_datapoint(datapoint_name, index=None): +async def get_datapoint(datapoint_name, index=None): # This function actually does the work of getting the datapoint if index is not None and ':index' in datapoint_name: @@ -370,7 +371,7 @@ def get_datapoint(datapoint_name, index=None): if dp is not None: dp.setIndex(int(index)) - return aq.get(datapoint_name) + return await aq.get(datapoint_name) @app.route('/datapoint//get', methods=["GET"]) @@ -380,7 +381,7 @@ def get_datapoint_endpoint(datapoint_name): ds = request.get_json() if request.is_json else request.form index = ds.get('index') - output = get_datapoint(datapoint_name, index) + output = asyncio.run(get_datapoint(datapoint_name, index)) if isinstance(output, bytes): output = output.decode('ascii') @@ -413,17 +414,14 @@ def set_datapoint(datapoint_name, index=None, value_to_use=None): @app.route('/datapoint//set', methods=["POST"]) def set_datapoint_endpoint(datapoint_name): # This is the http endpoint wrapper for setting a datapoint - ds = request.get_json() if request.is_json else request.form index = ds.get('index') value_to_use = ds.get('value_to_use') - - status = set_datapoint (datapoint_name, index, value_to_use) - + status = set_datapoint(datapoint_name, index, value_to_use) return jsonify(status) -def trigger_event(event_name, value_to_use = None): +def trigger_event(event_name, value_to_use=None): # This function actually does the work of triggering the event EVENT_TO_TRIGGER = ae.find(event_name) @@ -452,6 +450,9 @@ def trigger_event_endpoint(event_name): return jsonify(status) +async def get_engen_ct(): + return await aq.get("NUMBER_OF_ENGINES") + @app.route('/custom_emergency/', methods=["GET", "POST"]) def custom_emergency(emergency_type): @@ -459,10 +460,11 @@ def custom_emergency(emergency_type): if emergency_type == "random_engine_fire": # Calculate number of engines - number_of_engines = aq.get("NUMBER_OF_ENGINES") + number_of_engines = asyncio.run(get_engen_ct()) - if number_of_engines < 0: return "error, no engines found - is sim running?" - engine_to_set_on_fire = random.randint(1,number_of_engines) + if number_of_engines < 0: + return "error, no engines found - is sim running?" + engine_to_set_on_fire = random.randint(1, number_of_engines) set_datapoint("ENG_ON_FIRE:index", engine_to_set_on_fire, 1) @@ -471,4 +473,4 @@ def custom_emergency(emergency_type): return text_to_return -app.run(host='0.0.0.0', port=5000, debug=True) \ No newline at end of file +app.run(host='0.0.0.0', port=5000, debug=True) From 152fa14bd71ed6a5b71b139f5b10c84ca9bc5db5 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sat, 24 Oct 2020 15:28:51 -0700 Subject: [PATCH 5/8] Update RequestList.py --- SimConnect/RequestList.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/SimConnect/RequestList.py b/SimConnect/RequestList.py index f6bc0d7a..18203045 100644 --- a/SimConnect/RequestList.py +++ b/SimConnect/RequestList.py @@ -30,7 +30,7 @@ def value(self, val): self.sm.set_data(self) # self.sm.run() - def __init__(self, _deff, _sm, _time=500, _dec=None, _settable=False, _attemps=10): + def __init__(self, _deff, _sm, _time=10, _dec=None, _settable=False, _attemps=10): self.DATA_DEFINITION_ID = None self.definitions = [] self.description = _dec @@ -111,7 +111,7 @@ def _deff_test(self): class RequestHelper: - def __init__(self, _sm, _time=500, _attemps=10): + def __init__(self, _sm, _time=10, _attemps=10): self.sm = _sm self.dic = [] self.time = _time @@ -188,7 +188,7 @@ def set(self, key, _value): request.value = _value return True - def __init__(self, _sm, _time=2000, _attemps=4): + def __init__(self, _sm, _time=10, _attemps=10): self.sm = _sm self.list = [] self.EngineData = self.__AircraftEngineData(_sm, _time, _attemps) From a6d0042122fd50db1a39b5591d635f9eb07d0a0c Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sun, 25 Oct 2020 13:33:54 -0700 Subject: [PATCH 6/8] Update local_example.py --- local_example.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/local_example.py b/local_example.py index fd0c39c5..4daa6c15 100644 --- a/local_example.py +++ b/local_example.py @@ -50,11 +50,13 @@ async def main(): ct_g = millis() while not sm.quit: print("Throttle:", await Throttle.value) - print("Alt=%f Lat=%f Lon=%f Kohlsman=%.2f" % ( - await aq.PositionandSpeedData.get('PLANE_ALTITUDE'), - await aq.PositionandSpeedData.get('PLANE_LATITUDE'), - await aq.PositionandSpeedData.get('PLANE_LONGITUDE'), - await aq.FlightInstrumentationData.get('KOHLSMAN_SETTING_HG') + print("Lat=%f Lon=%f Alt=%f Kohlsman=%.2f" % tuple( + await asyncio.gather( + aq.PositionandSpeedData.get('PLANE_LATITUDE'), + aq.PositionandSpeedData.get('PLANE_LONGITUDE'), + aq.PositionandSpeedData.get('PLANE_ALTITUDE'), + aq.FlightInstrumentationData.get('KOHLSMAN_SETTING_HG') + ) )) sleep(2) @@ -67,11 +69,11 @@ async def main(): # PARKING_BRAKES() # send new data inine @ 5s - if ct_g + 5000 < millis(): - if await Throttle.value < 100: - Throttle.set(await Throttle.value + 5) - print("THROTTLE SET") - ct_g = millis() + # if ct_g + 5000 < millis(): + # if await Throttle.value < 100: + # Throttle.set(await Throttle.value + 5) + # print("THROTTLE SET") + # ct_g = millis() sm.exit() asyncio.run(main()) From 4d583b54acd86d906c5e77a6f5a649353dfec6f0 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sun, 15 Nov 2020 10:01:22 -0800 Subject: [PATCH 7/8] updated for backworks compadabilaty --- SimConnect/RequestList.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/SimConnect/RequestList.py b/SimConnect/RequestList.py index 18203045..ce5249ce 100644 --- a/SimConnect/RequestList.py +++ b/SimConnect/RequestList.py @@ -6,14 +6,14 @@ class Request(object): - async def get(self): + def get(self): return self.value def set(self, _value): self.value = _value @property - async def value(self): + async def avalue(self): if self._deff_test(): # self.sm.run() if (self.LastData + self.time) < millis(): @@ -23,6 +23,17 @@ async def value(self): else: raise Exception(self.definitions[0][0]) + @property + def value(self): + if self._deff_test(): + # self.sm.run() + if (self.LastData + self.time) < millis(): + asyncio.run(self.sm.get_data(self)) + self.LastData = millis() + return self.outData + else: + raise Exception(self.definitions[0][0]) + @value.setter def value(self, val): if self._deff_test() and self.settable: @@ -131,10 +142,15 @@ def __getattr__(self, _name): return ne return None - async def get(self, _name): + def get(self, _name): + if getattr(self, _name) is None: + return None + return getattr(self, _name).value + + async def aget(self, _name): if getattr(self, _name) is None: return None - return await getattr(self, _name).value + return await getattr(self, _name).avalue def set(self, _name, _value=0): temp = getattr(self, _name) From c6da1c8d475e25d07f112f95993c462393e6d4e8 Mon Sep 17 00:00:00 2001 From: odwdinc Date: Sun, 15 Nov 2020 10:15:40 -0800 Subject: [PATCH 8/8] updated othe functions --- SimConnect/RequestList.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/SimConnect/RequestList.py b/SimConnect/RequestList.py index ce5249ce..8c6ca967 100644 --- a/SimConnect/RequestList.py +++ b/SimConnect/RequestList.py @@ -9,6 +9,9 @@ class Request(object): def get(self): return self.value + async def aget(self): + return await self.avalue + def set(self, _value): self.value = _value @@ -197,6 +200,12 @@ def get(self, key): return None return request.value + async def aget(self, key): + request = self.find(key) + if request is None: + return None + return await request.avalue + def set(self, key, _value): request = self.find(key) if request is None: