filament_switch_sensor: Use template evaluation on gcode

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2019-06-07 11:29:22 -04:00
parent d0be48da74
commit f1c6d04376
1 changed files with 14 additions and 10 deletions

View File

@ -10,15 +10,17 @@ class BaseSensor(object):
self.name = config.get_name().split()[1] self.name = config.get_name().split()[1]
self.printer = config.get_printer() self.printer = config.get_printer()
self.gcode = self.printer.lookup_object('gcode') self.gcode = self.printer.lookup_object('gcode')
self.runout_gcode = config.get('runout_gcode', None)
self.insert_gcode = config.get('insert_gcode', None)
self.runout_pause = config.getboolean('pause_on_runout', True) self.runout_pause = config.getboolean('pause_on_runout', True)
if self.runout_pause: if self.runout_pause:
self.printer.try_load_module(config, 'pause_resume') self.printer.try_load_module(config, 'pause_resume')
if self.runout_gcode is None: self.runout_gcode = self.insert_gcode = None
self.runout_gcode = "PAUSE" gcode_macro = self.printer.try_load_module(config, 'gcode_macro')
else: if self.runout_pause or config.get('runout_gcode', None) is not None:
self.runout_gcode = "PAUSE\n" + self.runout_gcode self.runout_gcode = gcode_macro.load_template(
config, 'runout_gcode', '')
if config.get('insert_gcode', None) is not None:
self.insert_gcode = gcode_macro.load_template(
config, 'insert_gcode')
self.runout_enabled = False self.runout_enabled = False
self.insert_enabled = self.insert_gcode is not None self.insert_enabled = self.insert_gcode is not None
self.event_running = False self.event_running = False
@ -47,20 +49,22 @@ class BaseSensor(object):
self.event_running = True self.event_running = True
# Pausing from inside an event requires that the pause portion # Pausing from inside an event requires that the pause portion
# of pause_resume execute immediately. # of pause_resume execute immediately.
pause_prefix = ""
if self.runout_pause: if self.runout_pause:
pause_resume = self.printer.lookup_object('pause_resume') pause_resume = self.printer.lookup_object('pause_resume')
pause_resume.send_pause_command() pause_resume.send_pause_command()
self._exec_gcode(self.runout_gcode) pause_prefix = "PAUSE\n"
self._exec_gcode(pause_prefix, self.runout_gcode)
self.event_running = False self.event_running = False
def _insert_event_handler(self, eventtime): def _insert_event_handler(self, eventtime):
if self.event_running: if self.event_running:
return return
self.event_running = True self.event_running = True
self._exec_gcode(self.insert_gcode) self._exec_gcode("", self.insert_gcode)
self.event_running = False self.event_running = False
def _exec_gcode(self, script): def _exec_gcode(self, prefix, template):
try: try:
self.gcode.run_script(script) self.gcode.run_script(prefix + template.render())
except Exception: except Exception:
logging.exception("Script running error") logging.exception("Script running error")
def set_enable(self, runout, insert): def set_enable(self, runout, insert):