-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexample_client.py
47 lines (39 loc) · 1.44 KB
/
example_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Python 3 example to read the speed brake position of the A-10C
import socket
import json
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1", 12800))
sf = s.makefile()
def sendMessage(msg):
# json.dumps will not do any pretty-printing, so we do not have to remove
# newlines from its output
s.send( (json.dumps(msg) + "\n").encode("ascii") )
def onNewUnit(unitName):
# when a new unit is entered, we have to tell dcs-export-core
# which keys we are interested in. In this example, we only need
# key "e182" in the A-10C.
print("new unit: %s" % unitName)
if unitName == "A-10C":
sendMessage(
{"action":"subscribe", "keys":["e182", "c404"]}
)
# wait for first update event to get current aircraft
while True:
msg = json.loads(sf.readline())
if "msg_type" in msg and msg["msg_type"] == "new_unit":
onNewUnit(msg["type"])
break
if "msg_type" in msg and "_UNITTYPE" in msg["data"]:
onNewUnit(msg["data"]["_UNITTYPE"])
break
# process incoming messages
while True:
msg = json.loads(sf.readline())
if "msg_type" in msg:
if msg["msg_type"] == "new_unit":
onNewUnit(msg["type"])
if msg["msg_type"] == "newdata":
if "e182" in msg["data"] and msg["data"]["_UNITTYPE"] == "A-10C":
print("New A-10C speed brake position: %f" % msg["data"]["e182"])
if "c404" in msg["data"] and msg["data"]["_UNITTYPE"] == "A-10C":
print("A-10C Master Caution light is now "+("on" if msg["data"]["c404"] else "off"))