Skip to content

Commit

Permalink
More robust to check if printer is busy
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethjiang committed May 20, 2024
1 parent 011599d commit 13176da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
19 changes: 14 additions & 5 deletions moonraker_obico/nozzlecam.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def start(self):
continue

capturing = True

if not self.model.printer_state.is_printing(): # Probably the print was paused, or some other reasons
continue

first_layer_scanning = self.first_layer_macro_status().get('first_layer_scanning', False)
if first_layer_scanning:
capturing_interval = 0.1
Expand All @@ -46,8 +50,7 @@ def start(self):
_logger.error('Failed to capture and send nozzle cam jpeg', exc_info=True)

def should_capture(self):
if not self.model.printer_state.is_printing():
self.layer_change_macro_embedded_in_gcode = False
if not self.model.printer_state.is_busy():
return False

macro_status = self.first_layer_macro_status()
Expand Down Expand Up @@ -92,10 +95,16 @@ def get_nozzlecam_snapshot_url(self):
nozzle_cam_config = next((webcam for webcam in self.model.config.webcams if webcam.is_nozzle_camera), None)

if nozzle_cam_config:
_logger.info(f'Nozzle camera {nozzle_cam_config.name} found. URL: {nozzle_cam_config.snapshot_url}')
return nozzle_cam_config.snapshot_url
_logger.info(f'Nozzle camera found: {nozzle_cam_config}')
return nozzle_cam_config

# For Celestrius alpha testers

class StubNozzleCamConfig:
def __init__(self, snapshot_url):
self.snapshot_url = snapshot_url
self.is_nozzle_camera = True

try:
ext_info = self.server_conn.send_http_request('GET', f'/ent/api/printers/{self.model.linked_printer["id"]}/ext/', timeout=60, raise_exception=True).json().get('ext', {})
nozzle_url = ext_info.get('nozzlecam_url', '')
Expand All @@ -109,7 +118,7 @@ def get_nozzlecam_snapshot_url(self):
first_layer_scan_zhop=ext_info.get('first_layer_scan_zhop', 4),
)

return nozzle_url
return StubNozzleCamConfig(nozzle_url)
except Exception:
_logger.warn('Can not find nozzle camera. First Layer AI disabled.')
return None
2 changes: 1 addition & 1 deletion moonraker_obico/passthru_targets.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def _download_and_print():
raise


if self.model.printer_state.is_printing():
if self.model.printer_state.is_busy():
return None, 'Printer busy!'

call_func_with_state_transition(self.server_conn, self.model.printer_state, self.model.printer_state.STATE_GCODE_DOWNLOADING, _download_and_print, MAX_GCODE_DOWNLOAD_SECONDS)
Expand Down
8 changes: 6 additions & 2 deletions moonraker_obico/printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def __init__(self, app_config: Config):
def has_active_job(self) -> bool:
return PrinterState.get_state_from_status(self.status) in PrinterState.ACTIVE_STATES

def is_busy(self) -> bool:
with self._mutex:
return self.status.get('print_stats', {}).get('state') in ['printing', 'paused']

def is_printing(self) -> bool:
with self._mutex:
return self.status.get('print_stats', {}).get('state') == 'printing'
Expand Down Expand Up @@ -222,7 +226,7 @@ def get_z_info(self):
print_stats = self.status.get('print_stats') or dict()
print_info = print_stats.get('info') or dict()
file_metadata = self.current_file_metadata
is_not_printing = self.is_printing() is False or self.transient_state is not None
is_not_busy = self.is_busy() is False or self.transient_state is not None
has_print_duration = print_stats.get('print_duration', 0) > 0

current_z = None
Expand Down Expand Up @@ -260,7 +264,7 @@ def get_z_info(self):
current_layer = max(current_layer, 0) # Apparently the previous calculation can result in negative number in some cases...

if max_z and current_z > max_z: current_z = 0 # prevent buggy looking flicker on print start
if current_layer is None or total_layers is None or is_not_printing or not has_print_duration: # edge case handling - if either are not available we show nothing / show nothing if paused state, transient, etc / show nothing if no print duration (prevents tracking z height during preheat & start bytes)
if current_layer is None or total_layers is None or is_not_busy or not has_print_duration: # edge case handling - if either are not available we show nothing / show nothing if paused state, transient, etc / show nothing if no print duration (prevents tracking z height during preheat & start bytes)
current_layer = None
total_layers = None

Expand Down

0 comments on commit 13176da

Please sign in to comment.