klippy: Move restart logic into Printer() class

Move the restart logic out of main() and into Printer.run().  This
simplifies the code.

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2017-08-26 13:20:16 -04:00
parent d8c75fc608
commit 0c2919b534
2 changed files with 17 additions and 28 deletions

View File

@ -114,7 +114,7 @@ class GCodeParser:
self.toolhead.force_shutdown() self.toolhead.force_shutdown()
self.respond_error('Internal error on command:"%s"' % (cmd,)) self.respond_error('Internal error on command:"%s"' % (cmd,))
if self.is_fileinput: if self.is_fileinput:
self.printer.request_exit('exit_eof') self.printer.request_exit()
break break
self.ack() self.ack()
self.need_ack = prev_need_ack self.need_ack = prev_need_ack
@ -141,7 +141,7 @@ class GCodeParser:
self.motor_heater_off() self.motor_heater_off()
if self.toolhead is not None: if self.toolhead is not None:
self.toolhead.wait_moves() self.toolhead.wait_moves()
self.printer.request_exit('exit_eof') self.printer.request_exit()
self.is_processing_data = False self.is_processing_data = False
if self.fd_handle is None: if self.fd_handle is None:
self.fd_handle = self.reactor.register_fd(self.fd, self.process_data) self.fd_handle = self.reactor.register_fd(self.fd, self.process_data)

View File

@ -211,7 +211,7 @@ class Printer:
except: except:
logging.exception("Unhandled exception during connect") logging.exception("Unhandled exception during connect")
self.state_message = "Internal error during connect.%s" % ( self.state_message = "Internal error during connect.%s" % (
message_restart) message_restart,)
self.reactor.update_timer(self.stats_timer, self.reactor.NEVER) self.reactor.update_timer(self.stats_timer, self.reactor.NEVER)
self.reactor.unregister_timer(self.connect_timer) self.reactor.unregister_timer(self.connect_timer)
return self.reactor.NEVER return self.reactor.NEVER
@ -220,12 +220,23 @@ class Printer:
monotime = self.reactor.monotonic() monotime = self.reactor.monotonic()
logging.info("Start printer at %s (%.1f %.1f)" % ( logging.info("Start printer at %s (%.1f %.1f)" % (
time.asctime(time.localtime(systime)), systime, monotime)) time.asctime(time.localtime(systime)), systime, monotime))
# Enter main reactor loop
try: try:
self.reactor.run() self.reactor.run()
except: except:
logging.exception("Unhandled exception during run") logging.exception("Unhandled exception during run")
return return "exit"
return self.run_result # Check restart flags
run_result = self.run_result
try:
self._stats(self.reactor.monotonic(), force_output=True)
if self.mcu is not None:
if run_result == 'firmware_restart':
self.mcu.microcontroller_restart()
self.mcu.disconnect()
except:
logging.exception("Unhandled exception during post run")
return run_result
def get_state_message(self): def get_state_message(self):
return self.state_message return self.state_message
def note_shutdown(self, msg): def note_shutdown(self, msg):
@ -238,21 +249,6 @@ class Printer:
self.state_message = "%s%s" % (msg, message_restart) self.state_message = "%s%s" % (msg, message_restart)
self.gcode.set_printer_ready(False) self.gcode.set_printer_ready(False)
self.gcode.motor_heater_off() self.gcode.motor_heater_off()
def disconnect(self):
try:
if self.mcu is not None:
self._stats(self.reactor.monotonic(), force_output=True)
self.mcu.disconnect()
except:
logging.exception("Unhandled exception during disconnect")
def firmware_restart(self):
try:
if self.mcu is not None:
self._stats(self.reactor.monotonic(), force_output=True)
self.mcu.microcontroller_restart()
self.mcu.disconnect()
except:
logging.exception("Unhandled exception during firmware_restart")
def request_exit(self, result="exit"): def request_exit(self, result="exit"):
self.run_result = result self.run_result = result
self.reactor.end() self.reactor.end()
@ -315,14 +311,7 @@ def main():
while 1: while 1:
printer = Printer(input_fd, bglogger, start_args) printer = Printer(input_fd, bglogger, start_args)
res = printer.run() res = printer.run()
if res == 'restart': if res == 'exit':
printer.disconnect()
elif res == 'firmware_restart':
printer.firmware_restart()
elif res == 'exit_eof':
printer.disconnect()
break
else:
break break
time.sleep(1.) time.sleep(1.)
logging.info("Restarting printer") logging.info("Restarting printer")