Fixed several bugs found by unit tests.
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -6,6 +6,7 @@
|
|||||||
*.exe
|
*.exe
|
||||||
*.o
|
*.o
|
||||||
*.so
|
*.so
|
||||||
|
*.pyc
|
||||||
|
|
||||||
# Logs and databases #
|
# Logs and databases #
|
||||||
######################
|
######################
|
||||||
|
|||||||
@@ -30,13 +30,14 @@ class XPlaneConnect:
|
|||||||
raise ValueError("timeout must be non-negative.")
|
raise ValueError("timeout must be non-negative.")
|
||||||
|
|
||||||
# Setup XPlane IP and port
|
# Setup XPlane IP and port
|
||||||
self.xpDst = (socket.inet_pton(socket.AF_INET, xpIP), xpPort)
|
self.xpDst = (xpIP, xpPort)
|
||||||
|
|
||||||
# Create and bind socket
|
# Create and bind socket
|
||||||
clientAddr = (socket.INADDR_ANY, port)
|
clientAddr = ("0.0.0.0", port)
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
|
||||||
self.socket.bind(clientAddr)
|
self.socket.bind(clientAddr)
|
||||||
self.socket.settimeout(timeout / 1000)
|
timeout /= 1000.0
|
||||||
|
self.socket.settimeout(timeout)
|
||||||
|
|
||||||
def __del__(self):
|
def __del__(self):
|
||||||
self.close()
|
self.close()
|
||||||
@@ -212,11 +213,11 @@ class XPlaneConnect:
|
|||||||
if hasattr(values, "__len__"):
|
if hasattr(values, "__len__"):
|
||||||
if len(values) > 255:
|
if len(values) > 255:
|
||||||
raise ValueError("values must have less than 256 items.")
|
raise ValueError("values must have less than 256 items.")
|
||||||
fmt = "<4sxB" + len(dref) + "sB" + len(values) + "f"
|
fmt = "<4sxB{0:d}sB{1:d}f".format(len(dref), len(values))
|
||||||
buffer = struct.pack(fmt, ("DREF", len(dref), dref, len(values), values))
|
buffer = struct.pack(fmt, "DREF", len(dref), dref, len(values), values)
|
||||||
else:
|
else:
|
||||||
fmt = "<4sxB" + len(dref) + "sBf"
|
fmt = "<4sxB{0:d}sBf".format(len(dref))
|
||||||
buffer = struct.pack(fmt, ("DREF", len(dref), dref, 1, values))
|
buffer = struct.pack(fmt, "DREF", len(dref), dref, 1, values)
|
||||||
|
|
||||||
# Send
|
# Send
|
||||||
self.sendUDP(buffer)
|
self.sendUDP(buffer)
|
||||||
@@ -229,7 +230,7 @@ class XPlaneConnect:
|
|||||||
|
|
||||||
Returns: A sequence of data representing the values of the requested dataref.
|
Returns: A sequence of data representing the values of the requested dataref.
|
||||||
'''
|
'''
|
||||||
return getDREFS([dref])[0]
|
return self.getDREFs([dref])[0]
|
||||||
|
|
||||||
def getDREFs(self, drefs):
|
def getDREFs(self, drefs):
|
||||||
'''Gets the value of one or more X-Plane datarefs.
|
'''Gets the value of one or more X-Plane datarefs.
|
||||||
@@ -241,20 +242,22 @@ class XPlaneConnect:
|
|||||||
datarefs.
|
datarefs.
|
||||||
'''
|
'''
|
||||||
# Send request
|
# Send request
|
||||||
buffer = struct.pack("<4sxB" ("GETD", len(drefs)))
|
buffer = struct.pack("<4sxB", "GETD", len(drefs))
|
||||||
for dref in drefs:
|
for dref in drefs:
|
||||||
struct.pack_into("<B" + len(dref) + "s", buffer, len(buffer), dref)
|
fmt = "<B{0:d}s".format(len(dref))
|
||||||
|
buffer += struct.pack(fmt, len(dref), dref)
|
||||||
self.sendUDP(buffer)
|
self.sendUDP(buffer)
|
||||||
|
|
||||||
# Read and parse response
|
# Read and parse response
|
||||||
buffer = self.readUDP()
|
buffer = self.readUDP()
|
||||||
resultCount = struct.unpack_from("B", buffer, 5)
|
resultCount = struct.unpack_from("B", buffer, 5)[0]
|
||||||
offset = 6
|
offset = 6
|
||||||
result = []
|
result = []
|
||||||
for i in range(resultCount):
|
for i in range(resultCount):
|
||||||
rowLen = struct.unpack_from("B", buffer, offset)
|
rowLen = struct.unpack_from("B", buffer, offset)[0]
|
||||||
offset += 1
|
offset += 1
|
||||||
row = struct.unpack_from("<" + rowLen + "f", buffer, offset)
|
fmt = "<{0:d}f".format(rowLen)
|
||||||
|
row = struct.unpack_from(fmt, buffer, offset)
|
||||||
result.append(row)
|
result.append(row)
|
||||||
offset += rowLen * 4
|
offset += rowLen * 4
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -8,15 +8,50 @@ class XPCTests(unittest.TestCase):
|
|||||||
"""Tests the functionality of the XPlaneConnect class."""
|
"""Tests the functionality of the XPlaneConnect class."""
|
||||||
|
|
||||||
def test_init(self):
|
def test_init(self):
|
||||||
xpc = xplaneConnect.XPlaneConnect()
|
try:
|
||||||
self.assertTrue(True)
|
xpc = xplaneConnect.XPlaneConnect()
|
||||||
|
except:
|
||||||
|
self.fail("Default constructor failed.")
|
||||||
|
|
||||||
|
try:
|
||||||
|
xpc = xplaneConnect.XPlaneConnect("I'm not a real host")
|
||||||
|
self.fail("Failed to catch invalid XP host.")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 90001)
|
||||||
|
self.fail("Failed to catch invalid XP port.")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", -1)
|
||||||
|
self.fail("Failed to catch invalid XP port.")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, 90001)
|
||||||
|
self.fail("Failed to catch invalid local port.")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, -1)
|
||||||
|
self.fail("Failed to catch invalid XP port.")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
try:
|
||||||
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, 0, -1)
|
||||||
|
self.fail("Failed to catch invalid timeout.")
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
def test_close(self):
|
def test_close(self):
|
||||||
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, 49063)
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, 49063)
|
||||||
xpc.close()
|
xpc.close()
|
||||||
self.assertIsNone(xpc.socket)
|
self.assertIsNone(xpc.socket)
|
||||||
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, 49063)
|
xpc = xplaneConnect.XPlaneConnect("127.0.0.1", 49009, 49063)
|
||||||
self.assertTrue(True)
|
|
||||||
|
|
||||||
def test_send_read(self):
|
def test_send_read(self):
|
||||||
# Init
|
# Init
|
||||||
@@ -38,10 +73,11 @@ class XPCTests(unittest.TestCase):
|
|||||||
for a, b in zip(test, buf):
|
for a, b in zip(test, buf):
|
||||||
self.assertEqual(a, b)
|
self.assertEqual(a, b)
|
||||||
|
|
||||||
def test_request_dref(self):
|
def test_getDREFs(self):
|
||||||
# Setup
|
# Setup
|
||||||
xpc = xplaneConnect.XPlaneConnect()
|
xpc = xplaneConnect.XPlaneConnect()
|
||||||
drefs = ["sim/cockpit/switches/gear_handle_status", "cockpit2/switches/panel_brightness_ratio"]
|
drefs = ["sim/cockpit/switches/gear_handle_status",\
|
||||||
|
"sim/cockpit2/switches/panel_brightness_ratio"]
|
||||||
|
|
||||||
# Execution
|
# Execution
|
||||||
result = xpc.getDREFs(drefs)
|
result = xpc.getDREFs(drefs)
|
||||||
@@ -54,22 +90,47 @@ class XPCTests(unittest.TestCase):
|
|||||||
self.assertEqual(1, len(result[0]))
|
self.assertEqual(1, len(result[0]))
|
||||||
self.assertEqual(4, len(result[1]))
|
self.assertEqual(4, len(result[1]))
|
||||||
|
|
||||||
def test_send_dref(self):
|
def test_sendDREF(self):
|
||||||
# Setup
|
# Setup
|
||||||
xpc = xplaneConnect.XPlaneConnect()
|
xpc = xplaneConnect.XPlaneConnect()
|
||||||
dref = "sim/cockpit/switches/gear_handle_status"
|
dref = "sim/cockpit/switches/gear_handle_status"
|
||||||
|
|
||||||
# Execution
|
# Execution
|
||||||
sender.sendDREF(dref_array)
|
xpc.sendDREF(dref, 0.0)
|
||||||
result = receiver.getDREF(dref_array)
|
result = xpc.getDREF(dref)
|
||||||
|
|
||||||
# Cleanup
|
# Cleanup
|
||||||
xpc.close();
|
xpc.close()
|
||||||
|
|
||||||
# Tests
|
# Tests
|
||||||
self.assertEqual(1, len(result))
|
self.assertEqual(1, len(result))
|
||||||
self.assertEqual(1, len(result[0]))
|
self.assertEqual(0.0, result[0])
|
||||||
self.assertEqual(0.0, result[0][0])
|
|
||||||
|
def test_pauseSim(self):
|
||||||
|
# Setup
|
||||||
|
xpc = xplaneConnect.XPlaneConnect()
|
||||||
|
dref = "sim/operation/override/override_planepath"
|
||||||
|
|
||||||
|
# Execution 1
|
||||||
|
xpc.pauseSim(True)
|
||||||
|
result = xpc.getDREF(dref)
|
||||||
|
|
||||||
|
# Cleanup 1
|
||||||
|
xpc.close()
|
||||||
|
|
||||||
|
# Test 1
|
||||||
|
self.assertAlmostEqual(1.0, result[0])
|
||||||
|
|
||||||
|
# Execution 2
|
||||||
|
xpc = xplaneConnect.XPlaneConnect()
|
||||||
|
xpc.pauseSim(False)
|
||||||
|
result = xpc.getDREF(dref)
|
||||||
|
|
||||||
|
# Cleanup 2
|
||||||
|
xpc.close()
|
||||||
|
|
||||||
|
# Tests
|
||||||
|
self.assertEqual(0.0, result[0])
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
Reference in New Issue
Block a user