Added support for sending multiple datarefs to the Python client and fixed setCONN bug.
This commit is contained in:
@@ -79,8 +79,20 @@ class XPlaneConnect(object):
|
|||||||
if port < 0 or port > 65535:
|
if port < 0 or port > 65535:
|
||||||
raise ValueError("The specified port is not a valid port number.")
|
raise ValueError("The specified port is not a valid port number.")
|
||||||
|
|
||||||
|
#Send command
|
||||||
buffer = struct.pack("<4sxH", "CONN", port)
|
buffer = struct.pack("<4sxH", "CONN", port)
|
||||||
self.sendUDP(buffer)
|
self.sendUDP(buffer)
|
||||||
|
|
||||||
|
#Rebind socket
|
||||||
|
clientAddr = ("0.0.0.0", port)
|
||||||
|
timeout = self.socket.gettimeout();
|
||||||
|
self.socket.close();
|
||||||
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
|
self.socket.bind(clientAddr)
|
||||||
|
self.socket.settimeout(timeout)
|
||||||
|
|
||||||
|
#Read response
|
||||||
|
buffer = self.socket.recv(1024)
|
||||||
|
|
||||||
def pauseSim(self, pause):
|
def pauseSim(self, pause):
|
||||||
'''Pauses or un-pauses the physics simulation engine in X-Plane.
|
'''Pauses or un-pauses the physics simulation engine in X-Plane.
|
||||||
@@ -208,31 +220,45 @@ class XPlaneConnect(object):
|
|||||||
# Send
|
# Send
|
||||||
self.sendUDP(buffer)
|
self.sendUDP(buffer)
|
||||||
|
|
||||||
# DREF Manipulation
|
# DREF Manipulation
|
||||||
def sendDREF(self, dref, values):
|
def sendDREF(self, dref, values):
|
||||||
'''Sets the specified dataref to the specified value.
|
'''Sets the specified dataref to the specified value.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
dref: The name of the dataref to set.
|
dref: The name of the datarefs to set.
|
||||||
values: Either a scalar value or a sequence of values.
|
values: Either a scalar value or a sequence of values.
|
||||||
'''
|
'''
|
||||||
# Preconditions
|
self.sendDREFs([dref], [values])
|
||||||
if len(dref) == 0 or len(dref) > 255:
|
|
||||||
raise ValueError("dref must be a non-empty string less than 256 characters.")
|
def sendDREFs(self, drefs, values):
|
||||||
if values == None:
|
'''Sets the specified datarefs to the specified values.
|
||||||
raise ValueError("values must be a scalar or sequence of floats.")
|
|
||||||
|
Args:
|
||||||
|
drefs: A list of names of the datarefs to set.
|
||||||
|
values: A list of scalar or vector values to set.
|
||||||
|
'''
|
||||||
|
if len(drefs) != len(values):
|
||||||
|
raise ValueError("drefs and values must have the same number of elements.")
|
||||||
|
|
||||||
|
buffer = struct.pack("<4sx", "DREF")
|
||||||
|
for i in range(len(drefs)):
|
||||||
|
dref = drefs[i]
|
||||||
|
value = values[i]
|
||||||
|
# Preconditions
|
||||||
|
if len(dref) == 0 or len(dref) > 255:
|
||||||
|
raise ValueError("dref must be a non-empty string less than 256 characters.")
|
||||||
|
if value == None:
|
||||||
|
raise ValueError("value must be a scalar or sequence of floats.")
|
||||||
|
|
||||||
# Pack message
|
# Pack message
|
||||||
fmt = ""
|
if hasattr(value, "__len__"):
|
||||||
buffer = None
|
if len(value) > 255:
|
||||||
if hasattr(values, "__len__"):
|
raise ValueError("value must have less than 256 items.")
|
||||||
if len(values) > 255:
|
fmt = "<B{0:d}sB{1:d}f".format(len(dref), len(value))
|
||||||
raise ValueError("values must have less than 256 items.")
|
buffer += struct.pack(fmt, len(dref), dref, len(value), value)
|
||||||
fmt = "<4sxB{0:d}sB{1:d}f".format(len(dref), len(values))
|
else:
|
||||||
buffer = struct.pack(fmt, "DREF", len(dref), dref, len(values), values)
|
fmt = "<B{0:d}sBf".format(len(dref))
|
||||||
else:
|
buffer += struct.pack(fmt, len(dref), dref, 1, value)
|
||||||
fmt = "<4sxB{0:d}sBf".format(len(dref))
|
|
||||||
buffer = struct.pack(fmt, "DREF", len(dref), dref, 1, values)
|
|
||||||
|
|
||||||
# Send
|
# Send
|
||||||
self.sendUDP(buffer)
|
self.sendUDP(buffer)
|
||||||
|
|||||||
@@ -117,6 +117,37 @@ class XPCTests(unittest.TestCase):
|
|||||||
value = 0
|
value = 0
|
||||||
do_test()
|
do_test()
|
||||||
|
|
||||||
|
def test_sendDREFs(self):
|
||||||
|
drefs = [\
|
||||||
|
"sim/cockpit/switches/gear_handle_status",\
|
||||||
|
"sim/cockpit/autopilot/altitude"]
|
||||||
|
values = None
|
||||||
|
def do_test():
|
||||||
|
# Setup
|
||||||
|
client = xpc.XPlaneConnect()
|
||||||
|
|
||||||
|
# Execute
|
||||||
|
client.sendDREFs(drefs, values)
|
||||||
|
result = client.getDREFs(drefs)
|
||||||
|
|
||||||
|
# Cleanup
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
# Tests
|
||||||
|
self.assertEqual(2, len(result))
|
||||||
|
self.assertEqual(1, len(result[0]))
|
||||||
|
self.assertEqual(values[0], result[0][0])
|
||||||
|
self.assertEqual(1, len(result[1]))
|
||||||
|
self.assertEqual(values[1], result[1][0])
|
||||||
|
|
||||||
|
# Test 1
|
||||||
|
values = [1, 2000]
|
||||||
|
do_test()
|
||||||
|
|
||||||
|
# Test 2
|
||||||
|
values = [0, 4000]
|
||||||
|
do_test()
|
||||||
|
|
||||||
def test_sendDATA(self):
|
def test_sendDATA(self):
|
||||||
# Setup
|
# Setup
|
||||||
dref = "sim/aircraft/parts/acf_gear_deploy"
|
dref = "sim/aircraft/parts/acf_gear_deploy"
|
||||||
|
|||||||
Reference in New Issue
Block a user