Skip to content

Commit 79fb706

Browse files
committed
Trying out asyncio
Not shure if this is the right derection or not thoughts?
1 parent bc4737f commit 79fb706

File tree

3 files changed

+37
-100
lines changed

3 files changed

+37
-100
lines changed

SimConnect/RequestList.py

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
from SimConnect import *
22
from .Enum import *
33
from .Constants import *
4+
import asyncio
45

56

67
class Request(object):
78

8-
def get(self):
9+
async def get(self):
910
return self.value
1011

1112
def set(self, _value):
1213
self.value = _value
1314

1415
@property
15-
def value(self):
16+
async def value(self):
1617
if self._deff_test():
1718
# self.sm.run()
1819
if (self.LastData + self.time) < millis():
19-
if self.sm.get_data(self):
20-
self.LastData = millis()
21-
else:
22-
return None
20+
await self.sm.get_data(self)
21+
self.LastData = millis()
2322
return self.outData
2423
else:
25-
return None
24+
raise Exception(self.definitions[0][0])
2625

2726
@value.setter
2827
def value(self, val):
@@ -69,9 +68,9 @@ def redefine(self):
6968
)
7069
self.defined = False
7170
# self.sm.run()
72-
if self._deff_test():
73-
# self.sm.run()
74-
self.sm.get_data(self)
71+
# if self._deff_test():
72+
# # self.sm.run()
73+
# self.sm.get_data(self)
7574

7675
def _deff_test(self):
7776
if ':index' in str(self.definitions[0][0]):
@@ -132,10 +131,10 @@ def __getattr__(self, _name):
132131
return ne
133132
return None
134133

135-
def get(self, _name):
134+
async def get(self, _name):
136135
if getattr(self, _name) is None:
137136
return None
138-
return getattr(self, _name).value
137+
return await getattr(self, _name).value
139138

140139
def set(self, _name, _value=0):
141140
temp = getattr(self, _name)
@@ -170,6 +169,8 @@ def find(self, key):
170169
if key in clas.list:
171170
rqest = getattr(clas, key)
172171
if index is not None:
172+
if 'index' in index:
173+
index = 0
173174
rqest.setIndex(index)
174175
return rqest
175176
return None

SimConnect/SimConnect.py

+7-11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .Attributes import *
88
import os
99
import threading
10+
import asyncio
1011

1112
_library_path = os.path.abspath(__file__).replace(".py", ".dll")
1213

@@ -239,18 +240,13 @@ def set_data(self, _Request):
239240
else:
240241
return False
241242

242-
def get_data(self, _Request):
243-
self.request_data(_Request)
244-
# self.run()
245-
attemps = 0
246-
while _Request.outData is None and attemps < _Request.attemps:
247-
# self.run()
248-
time.sleep(.01)
249-
attemps += 1
250-
if _Request.outData is None:
251-
return False
243+
async def get_return_data(self, _Request):
244+
while _Request.outData is None:
245+
await asyncio.sleep(0.01)
252246

253-
return True
247+
async def get_data(self, _Request):
248+
self.request_data(_Request)
249+
await self.get_return_data(_Request)
254250

255251
def send_event(self, evnt, data=DWORD(0)):
256252
err = self.dll.TransmitClientEvent(

local_example.py

+17-77
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,34 @@
11
from SimConnect import *
22
import logging
3-
from SimConnect.Enum import *
43
from time import sleep
5-
6-
4+
import asyncio
75
logging.basicConfig(level=logging.DEBUG)
86
LOGGER = logging.getLogger(__name__)
97
LOGGER.info("START")
10-
# time holder for inline commands
11-
ct_g = millis()
12-
13-
# creat simconnection and pass used user classes
148
sm = SimConnect()
159
aq = AircraftRequests(sm)
16-
ae = AircraftEvents(sm)
17-
18-
19-
mc = aq.find("MAGNETIC_COMPASS")
20-
mv = aq.find("MAGVAR")
21-
print(mc.get() + mv.get())
22-
23-
sm.exit()
24-
quit()
25-
26-
# Set pos arund space nedle in WA.
27-
sm.set_pos(
28-
_Altitude=1000.0,
29-
_Latitude=47.614699,
30-
_Longitude=-122.358473,
31-
_Airspeed=130,
32-
_Heading=70.0,
33-
# _Pitch=0.0,
34-
# _Bank=0.0,
35-
# _OnGround=0
36-
)
37-
38-
# PARKING_BRAKES = Event(b'PARKING_BRAKES', sm)
39-
# long path
40-
PARKING_BRAKES = ae.Miscellaneous_Systems.PARKING_BRAKES
41-
# using get
42-
GEAR_TOGGLE = ae.Miscellaneous_Systems.get("GEAR_TOGGLE")
43-
# Using find to lookup Event
44-
AP_MASTER = ae.find("AP_MASTER")
45-
46-
# THROTTLE1 Event
47-
THROTTLE1 = ae.Engine.THROTTLE1_SET
48-
49-
50-
# THROTTLE1 Request
51-
Throttle = aq.find('GENERAL_ENG_THROTTLE_LEVER_POSITION:1')
52-
53-
# If useing
54-
# Throttle = aq.find('GENERAL_ENG_THROTTLE_LEVER_POSITION:index')
55-
# Need to set index befor read/write
56-
# Note to set index 2 vs 1 just re-run
57-
# Throttle.setIndex(1)
5810

11+
Data = {}
5912

60-
# print the built in description
61-
# AP_MASTER Toggles AP on/off
62-
print("AP_MASTER", AP_MASTER.description)
63-
# Throttle Percent of max throttle position
64-
print("Throttle", Throttle.description)
65-
# THROTTLE1 Set throttle 1 exactly (0 to 16383)
66-
print("THROTTLE1", THROTTLE1.description)
6713

14+
async def pintVal(name):
15+
global Data
16+
Data[name] = await aq.get(name)
6817

69-
while not sm.quit:
70-
print("Throttle:", Throttle.value)
71-
print("Alt=%f Lat=%f Lon=%f Kohlsman=%.2f" % (
72-
aq.PositionandSpeedData.get('PLANE_ALTITUDE'),
73-
aq.PositionandSpeedData.get('PLANE_LATITUDE'),
74-
aq.PositionandSpeedData.get('PLANE_LONGITUDE'),
75-
aq.FlightInstrumentationData.get('KOHLSMAN_SETTING_HG')
76-
))
77-
sleep(2)
7818

79-
# Send Event with value
80-
# THROTTLE1(1500)
19+
async def main():
20+
while not sm.quit:
21+
temp = {}
22+
for ed in aq.PositionandSpeedData.list:
23+
temp[ed] = asyncio.create_task(pintVal(ed))
8124

82-
# Send Event toggle AP_MASTER
83-
# AP_MASTER()
25+
for ed in aq.PositionandSpeedData.list:
26+
await temp[ed]
8427

85-
# PARKING_BRAKES()
28+
print(Data)
29+
sleep(2)
30+
sm.exit()
31+
quit()
8632

87-
# send new data inine @ 5s
88-
if ct_g + 5000 < millis():
89-
if Throttle.value < 100:
90-
Throttle.value += 5
91-
print("THROTTLE SET")
92-
ct_g = millis()
9333

94-
sm.exit()
34+
asyncio.run(main())

0 commit comments

Comments
 (0)