Commit df7622cf authored by Guillaume Seguin's avatar Guillaume Seguin

Rework listen loop

parent 48383711
...@@ -110,6 +110,12 @@ class printcore(): ...@@ -110,6 +110,12 @@ class printcore():
def _readline(self): def _readline(self):
try: try:
line = self.printer.readline() line = self.printer.readline()
if len(line) > 1:
self.log.append(line)
if self.recvcb:
try: self.recvcb(line)
except: pass
if self.loud: print "RECV: ", line.rstrip()
return line return line
except SelectError, e: except SelectError, e:
if 'Bad file descriptor' in e.args[1]: if 'Bad file descriptor' in e.args[1]:
...@@ -124,44 +130,49 @@ class printcore(): ...@@ -124,44 +130,49 @@ class printcore():
print "Can't read from printer (disconnected?)." print "Can't read from printer (disconnected?)."
return None return None
def _listen_can_continue(self):
return not self.stop_read_thread and self.printer and self.printer.isOpen()
def _listen_until_online(self):
while not self.online and self._listen_can_continue():
self._send("M105")
while self._listen_can_continue():
line = self._readline()
if line == None:
break
if line.startswith(tuple(self.greetings)) or line.startswith('ok'):
if self.onlinecb:
try: self.onlinecb()
except: pass
self.online = True
return
time.sleep(0.5)
def _listen(self): def _listen(self):
"""This function acts on messages from the firmware """This function acts on messages from the firmware
""" """
self.clear = True self.clear = True
if not self.printing: if not self.printing:
self._send("M105") self._listen_until_online(self)
time.sleep(1) while self._listen_can_continue():
while not self.stop_read_thread and self.printer and self.printer.isOpen():
line = self._readline() line = self._readline()
if line == None: if line == None:
break break
if len(line) > 1:
self.log.append(line)
if self.recvcb:
try: self.recvcb(line)
except: pass
if self.loud: print "RECV: ", line.rstrip()
if line.startswith('DEBUG_'): if line.startswith('DEBUG_'):
continue continue
if line.startswith(tuple(self.greetings)) or line.startswith('ok'): if line.startswith(tuple(self.greetings)) or line.startswith('ok'):
self.clear = True self.clear = True
if line.startswith(tuple(self.greetings)) or line.startswith('ok') or "T:" in line: if line.startswith('ok') and "T:" in line and self.tempcb:
if (not self.online or line.startswith(tuple(self.greetings))) and self.onlinecb is not None: #callback for temp, status, whatever
try: self.onlinecb() try: self.tempcb(line)
except: pass except: pass
self.online = True
if line.startswith('ok'):
#self.resendfrom=-1
if "T:" in line and self.tempcb is not None:
#callback for temp, status, whatever
try: self.tempcb(line)
except: pass
elif line.startswith('Error'): elif line.startswith('Error'):
if self.errorcb is not None: if self.errorcb:
#callback for errors #callback for errors
try: self.errorcb(line) try: self.errorcb(line)
except: pass except: pass
if line.lower().startswith("resend") or line.startswith("rs"): if line.lower().startswith("resend") or line.startswith("rs"):
toresend = self.resendfrom
try: try:
toresend = int(line.replace("N:", " ").replace("N", " ").replace(":", " ").split()[-1]) toresend = int(line.replace("N:", " ").replace("N", " ").replace(":", " ").split()[-1])
except: except:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment