aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormarcusw2011-03-13 17:53:38 +0000
committermarcusw2011-03-13 17:53:38 +0000
commit48ca0fd0b89a985a46deadae4ec6dda32ce84dfe (patch)
treeef14d7e8e689905dffdf564fcea5bb7ad164002d
parent02b4a6f1e99c3241f543e52bbd750b754f9a8c83 (diff)
Improving the error handling in find_one_brick(). Unfortunately, this may change behavior slightly in some cases and therefore can't be pushed to v2.1 as a bugfix.
-rw-r--r--nxt/locator.py43
-rw-r--r--nxt/usbsock.py3
2 files changed, 42 insertions, 4 deletions
diff --git a/nxt/locator.py b/nxt/locator.py
index 4adc680..693c3b8 100644
--- a/nxt/locator.py
+++ b/nxt/locator.py
@@ -12,6 +12,8 @@
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
+import sys, traceback
+
class BrickNotFoundError(Exception):
pass
@@ -47,11 +49,46 @@ def find_bricks(host=None, name=None, silent=False):
raise NoBackendError("Neither USB nor Bluetooth could be used! Did you install PyUSB or PyBluez?")
-def find_one_brick(host=None, name=None, silent=False):
+def find_one_brick(host=None, name=None, silent=False, strict=True, debug=False):
"""Use to find one brick. After it returns a usbsock object or a bluesock
object, it automatically connects to it. The host and name args limit
the search to a given MAC or brick name. Set silent to True to stop
-nxt-python from printing anything during the search."""
+nxt-python from printing anything during the search. This function by default
+automatically checks to see if the brick found has the correct host/name
+(if either are provided) and will not return a brick which doesn't
+match. This can be disabled (so the function returns any brick which
+can be connected to and provides a valid reply to get_device_info()) by
+passing strict=False. This will, however, still tell the USB/BT backends to
+only look for devices which match the args provided."""
+ if debug and silent:
+ silent=False
+ print "silent and debug can't both be set; giving debug priority"
+
for s in find_bricks(host, name, silent):
- return s.connect()
+ try:
+ if host and 'host' in dir(s) and s.host != host:
+ if debug:
+ print "Warning: the brick found does not match the host provided (s.host)."
+ if strict: continue
+ b = s.connect()
+ info = b.get_device_info()
+ if host and info[1] != host:
+ if debug:
+ print "Warning: the brick found does not match the host provided (get_device_info)."
+ if strict:
+ s.close()
+ continue
+ if name and info[0].strip('\0') != name:
+ if debug:
+ print "Warning; the brick found does not match the name provided."
+ if strict:
+ s.close()
+ continue
+ return b
+ except:
+ if debug:
+ traceback.print_exc()
+ print "Failed to connect to possible brick"
raise BrickNotFoundError
+
+
diff --git a/nxt/usbsock.py b/nxt/usbsock.py
index 074ad8b..fe12978 100644
--- a/nxt/usbsock.py
+++ b/nxt/usbsock.py
@@ -75,7 +75,8 @@ class USBSock(object):
def find_bricks(host=None, name=None):
'Use to look for NXTs connected by USB only. ***ADVANCED USERS ONLY***'
- # FIXME: probably should check host and name
+ # FIXME: probably should check host (MAC)
+ # if anyone knows how to do this, please file a bug report
for bus in usb.busses():
for device in bus.devices:
if device.idVendor == ID_VENDOR_LEGO and device.idProduct == ID_PRODUCT_NXT: