Skip to content

Commit

Permalink
Fix for infinite while loop
Browse files Browse the repository at this point in the history
  • Loading branch information
andreas-amlabs committed Jan 17, 2018
1 parent 4cda4c2 commit c9a411a
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions nad_receiver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ def mute(self):
def unmute(self):
"""Unmute the device."""
self._send(self.CMD_UNMUTE)

def select_source(self, source):
"""Select a source from the list of sources."""
status = self.status()
Expand All @@ -273,8 +273,8 @@ def __init__(self, host, port=23, timeout=DEFAULT_TIMEOUT):
self.timeout = timeout
# Some versions of the firmware report Main.Model=T787.
# some versions do not, we want to clear that line
msg = self.telnet.read_until('\n'.encode(), self.timeout)
#msg.decode().strip().split('=')[1]
self.telnet.read_until('\n'.encode(), self.timeout)
# Could raise eg. EOFError, UnicodeError, let the client handle it

def __del__(self):
"""
Expand Down Expand Up @@ -302,15 +302,28 @@ def exec_command(self, domain, function, operator, value=None):
# let is raise if any issues
# For telnet the first \r / \n is recommended only
self.telnet.write((''.join(['\r', cmd, '\n']).encode()))
# Could raise eg. socket.error, UnicodeError, let the client handle it

found = False
while not found:
# Test 3 x buffer is completely empty
# With the default timeout that means a delay at
# about 3+ seconds
loop = 3
while loop:
msg = self.telnet.read_until('\n'.encode(), self.timeout)
# Could raise eg. EOFError, UnicodeError, let the client handle it

if msg == "":
# Nothing in buffer
loop -= 1
continue

msg = msg.decode().strip('\r\n')
# Could raise eg. UnicodeError, let the client handle it

#print("NAD reponded with '%s'" % msg)
# Wait for the response that equals the requested domain.function
if msg.strip().split('=')[0].lower() == '.'.join([domain, function]).lower():
found = True
# b'Main.Volume=-12\r will return -12
return msg.strip().split('=')[1]

return msg.strip().split('=')[1]
# b'Main.Volume=-12\r will return -12
raise RuntimeError('Failed to read response')

0 comments on commit c9a411a

Please sign in to comment.