Skip to content

Commit

Permalink
Add Kegboard.wait_for_ping().
Browse files Browse the repository at this point in the history
  • Loading branch information
mik3y committed Apr 30, 2014
1 parent b16e8b9 commit 2b4533a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
8 changes: 1 addition & 7 deletions python/bin/kegboard-info.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ def _MainLoop(self):
board.open()
self._logger.debug('Got kegboard: %s' % board)

msg = None
for i in xrange(4):
board.ping()
candidate = board.read_message(timeout=3)
if isinstance(candidate, kegboard.HelloMessage):
msg = candidate

msg = board.wait_for_ping()
if not msg:
print 'Gave up pinging kegboard!'
sys.exit(1)
Expand Down
8 changes: 1 addition & 7 deletions python/bin/set-kegboard-serialnumber
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ class KegboardMonitorApp(app.App):
pass

def _wait_for_response(self, board):
msg = None
for i in xrange(4):
board.ping()
msg = board.read_message(timeout=3)
if isinstance(msg, kegboard.HelloMessage):
break

msg = board.wait_for_ping()
if not msg:
print 'Gave up pinging kegboard!'
sys.exit(1)
Expand Down
9 changes: 9 additions & 0 deletions python/kegbot/kegboard/kegboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ def read_message(self, timeout=None, interval=0.1):
return None
time.sleep(interval)

def wait_for_ping(self, attempts=5):
self.drain_messages()
for i in xrange(attempts):
self.ping()
messages = [self.read_message(timeout=1)] + self.drain_messages()
for message in messages:
if isinstance(message, HelloMessage):
return message

def write_message(self, message):
"""Send a message to the device."""
self._assert_open()
Expand Down

0 comments on commit 2b4533a

Please sign in to comment.