2018-07-13 02:07:54 +03:00
|
|
|
# Support for disabling the printer on an idle timeout
|
|
|
|
#
|
|
|
|
# Copyright (C) 2018 Kevin O'Connor <kevin@koconnor.net>
|
|
|
|
#
|
|
|
|
# This file may be distributed under the terms of the GNU GPLv3 license.
|
idle_timeout: Added a missing logging import
I noticed Octoprint was disconnecting after a while, and after reading the logs, I came across this error:
```
Unhandled exception during run
Traceback (most recent call last):
File "/home/pi/klipper/klippy/klippy.py", line 171, in run
self.reactor.run()
File "/home/pi/klipper/klippy/reactor.py", line 176, in run
g_next.switch()
File "/home/pi/klipper/klippy/reactor.py", line 203, in _dispatch_loop
timeout = self._check_timers(eventtime)
File "/home/pi/klipper/klippy/reactor.py", line 82, in _check_timers
t.waketime = t.callback(eventtime)
File "/home/pi/klipper/klippy/extras/idle_timeout.py", line 62, in timeout_handler
return self.check_idle_timeout(eventtime)
File "/home/pi/klipper/klippy/extras/idle_timeout.py", line 59, in check_idle_timeout
return self.transition_idle_state(eventtime)
File "/home/pi/klipper/klippy/extras/idle_timeout.py", line 35, in transition_idle_state
logging.exception("idle timeout gcode execution")
NameError: global name 'logging' is not defined
```
I therefore added the missing import.
Signed-off-by: Samori Gorse <samori.gorse+github@gmail.com>
2019-01-29 17:51:31 +03:00
|
|
|
import logging
|
2018-07-13 02:07:54 +03:00
|
|
|
|
|
|
|
DEFAULT_IDLE_GCODE = """
|
2021-03-07 21:48:54 +03:00
|
|
|
{% if 'heaters' in printer %}
|
|
|
|
TURN_OFF_HEATERS
|
|
|
|
{% endif %}
|
2018-07-13 02:07:54 +03:00
|
|
|
M84
|
|
|
|
"""
|
|
|
|
|
2018-10-18 19:04:50 +03:00
|
|
|
PIN_MIN_TIME = 0.100
|
|
|
|
READY_TIMEOUT = .500
|
|
|
|
|
2018-07-13 02:07:54 +03:00
|
|
|
class IdleTimeout:
|
|
|
|
def __init__(self, config):
|
|
|
|
self.printer = config.get_printer()
|
2018-10-18 19:04:50 +03:00
|
|
|
self.reactor = self.printer.get_reactor()
|
|
|
|
self.gcode = self.printer.lookup_object('gcode')
|
|
|
|
self.toolhead = self.timeout_timer = None
|
2019-01-08 18:55:18 +03:00
|
|
|
self.printer.register_event_handler("klippy:ready", self.handle_ready)
|
2018-07-13 02:07:54 +03:00
|
|
|
self.idle_timeout = config.getfloat('timeout', 600., above=0.)
|
2020-05-05 21:10:30 +03:00
|
|
|
gcode_macro = self.printer.load_object(config, 'gcode_macro')
|
|
|
|
self.idle_gcode = gcode_macro.load_template(config, 'gcode',
|
|
|
|
DEFAULT_IDLE_GCODE)
|
|
|
|
self.gcode.register_command('SET_IDLE_TIMEOUT',
|
2021-02-19 04:32:46 +03:00
|
|
|
self.cmd_SET_IDLE_TIMEOUT,
|
|
|
|
desc=self.cmd_SET_IDLE_TIMEOUT_help)
|
2019-06-04 19:35:50 +03:00
|
|
|
self.state = "Idle"
|
|
|
|
self.last_print_start_systime = 0.
|
|
|
|
def get_status(self, eventtime):
|
|
|
|
printing_time = 0.
|
2019-06-07 19:30:42 +03:00
|
|
|
if self.state == "Printing":
|
2019-06-04 19:35:50 +03:00
|
|
|
printing_time = eventtime - self.last_print_start_systime
|
|
|
|
return { "state": self.state, "printing_time": printing_time }
|
2019-01-08 18:55:18 +03:00
|
|
|
def handle_ready(self):
|
|
|
|
self.toolhead = self.printer.lookup_object('toolhead')
|
|
|
|
self.timeout_timer = self.reactor.register_timer(self.timeout_handler)
|
|
|
|
self.printer.register_event_handler("toolhead:sync_print_time",
|
|
|
|
self.handle_sync_print_time)
|
2018-10-18 19:04:50 +03:00
|
|
|
def transition_idle_state(self, eventtime):
|
|
|
|
self.state = "Printing"
|
2018-10-18 18:51:35 +03:00
|
|
|
try:
|
2019-06-07 17:55:28 +03:00
|
|
|
script = self.idle_gcode.render()
|
2019-06-09 20:33:21 +03:00
|
|
|
res = self.gcode.run_script(script)
|
2018-10-18 18:51:35 +03:00
|
|
|
except:
|
|
|
|
logging.exception("idle timeout gcode execution")
|
2019-06-09 20:33:21 +03:00
|
|
|
self.state = "Ready"
|
2018-10-18 19:04:50 +03:00
|
|
|
return eventtime + 1.
|
|
|
|
print_time = self.toolhead.get_last_move_time()
|
|
|
|
self.state = "Idle"
|
|
|
|
self.printer.send_event("idle_timeout:idle", print_time)
|
|
|
|
return self.reactor.NEVER
|
|
|
|
def check_idle_timeout(self, eventtime):
|
|
|
|
# Make sure toolhead class isn't busy
|
|
|
|
print_time, est_print_time, lookahead_empty = self.toolhead.check_busy(
|
|
|
|
eventtime)
|
|
|
|
idle_time = est_print_time - print_time
|
|
|
|
if not lookahead_empty or idle_time < 1.:
|
|
|
|
# Toolhead is busy
|
2018-07-13 02:07:54 +03:00
|
|
|
return eventtime + self.idle_timeout
|
2018-10-18 19:04:50 +03:00
|
|
|
if idle_time < self.idle_timeout:
|
|
|
|
# Wait for idle timeout
|
|
|
|
return eventtime + self.idle_timeout - idle_time
|
2019-06-09 20:33:21 +03:00
|
|
|
if self.gcode.get_mutex().test():
|
2018-10-18 19:04:50 +03:00
|
|
|
# Gcode class busy
|
|
|
|
return eventtime + 1.
|
|
|
|
# Idle timeout has elapsed
|
|
|
|
return self.transition_idle_state(eventtime)
|
|
|
|
def timeout_handler(self, eventtime):
|
|
|
|
if self.state == "Ready":
|
|
|
|
return self.check_idle_timeout(eventtime)
|
|
|
|
# Check if need to transition to "ready" state
|
|
|
|
print_time, est_print_time, lookahead_empty = self.toolhead.check_busy(
|
|
|
|
eventtime)
|
|
|
|
buffer_time = min(2., print_time - est_print_time)
|
|
|
|
if not lookahead_empty:
|
|
|
|
# Toolhead is busy
|
|
|
|
return eventtime + READY_TIMEOUT + max(0., buffer_time)
|
|
|
|
if buffer_time > -READY_TIMEOUT:
|
|
|
|
# Wait for ready timeout
|
|
|
|
return eventtime + READY_TIMEOUT + buffer_time
|
2019-06-09 20:33:21 +03:00
|
|
|
if self.gcode.get_mutex().test():
|
2018-10-18 19:04:50 +03:00
|
|
|
# Gcode class busy
|
|
|
|
return eventtime + READY_TIMEOUT
|
|
|
|
# Transition to "ready" state
|
|
|
|
self.state = "Ready"
|
|
|
|
self.printer.send_event("idle_timeout:ready",
|
|
|
|
est_print_time + PIN_MIN_TIME)
|
|
|
|
return eventtime + self.idle_timeout
|
|
|
|
def handle_sync_print_time(self, curtime, print_time, est_print_time):
|
|
|
|
if self.state == "Printing":
|
|
|
|
return
|
|
|
|
# Transition to "printing" state
|
|
|
|
self.state = "Printing"
|
2019-06-04 19:35:50 +03:00
|
|
|
self.last_print_start_systime = curtime
|
2018-10-18 19:04:50 +03:00
|
|
|
check_time = READY_TIMEOUT + print_time - est_print_time
|
|
|
|
self.reactor.update_timer(self.timeout_timer, curtime + check_time)
|
|
|
|
self.printer.send_event("idle_timeout:printing",
|
|
|
|
est_print_time + PIN_MIN_TIME)
|
2021-02-19 04:32:46 +03:00
|
|
|
cmd_SET_IDLE_TIMEOUT_help = "Set the idle timeout in seconds"
|
2020-04-25 05:20:29 +03:00
|
|
|
def cmd_SET_IDLE_TIMEOUT(self, gcmd):
|
|
|
|
timeout = gcmd.get_float('TIMEOUT', self.idle_timeout, above=0.)
|
2019-05-22 03:47:33 +03:00
|
|
|
self.idle_timeout = timeout
|
2020-04-25 05:20:29 +03:00
|
|
|
gcmd.respond_info("idle_timeout: Timeout set to %.2f s" % (timeout,))
|
2019-05-22 03:47:33 +03:00
|
|
|
if self.state == "Ready":
|
|
|
|
checktime = self.reactor.monotonic() + timeout
|
2020-04-25 05:20:29 +03:00
|
|
|
self.reactor.update_timer(self.timeout_timer, checktime)
|
2018-07-13 02:07:54 +03:00
|
|
|
|
|
|
|
def load_config(config):
|
|
|
|
return IdleTimeout(config)
|