Skip to content

Commit

Permalink
🔩Code: _do_abort usage fixed & clean-up
Browse files Browse the repository at this point in the history
- version bumped to 0.1.3
  • Loading branch information
vookimedlo committed Aug 15, 2020
1 parent 0bd5cf4 commit 2e797a3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
27 changes: 17 additions & 10 deletions octoprint_prusa_mini_eta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,24 @@
from __future__ import absolute_import

import re
from typing import Pattern

import octoprint.plugin
from octoprint_prusa_mini_eta.liveestimation import PrusaMiniPrintTimeEstimator

from octoprint_prusa_mini_eta.initialestimation import PrusaMiniGcodeAnalysisQueue
from octoprint_prusa_mini_eta.liveestimation import PrusaMiniPrintTimeEstimator


class PrusaMiniETAPlugin(octoprint.plugin.SettingsPlugin,
octoprint.plugin.AssetPlugin,
octoprint.plugin.TemplatePlugin):
remaining_time_pattern = re.compile(r'R(\d+)$')
remaining_time = 0
print_time_estimator = PrusaMiniPrintTimeEstimator
octoprint.plugin.TemplatePlugin,
octoprint.plugin.StartupPlugin):
_remaining_time_pattern: Pattern[str] = re.compile(r'R(\d+)$')
_print_time_estimator = PrusaMiniPrintTimeEstimator
logger = None

def on_after_startup(self):
self.logger = self._logger

##~~ SettingsPlugin mixin

Expand Down Expand Up @@ -73,21 +80,21 @@ def get_update_information(self):
)

def custom_gcode_analysis_queue(self, *args, **kwargs):
return dict(gcode=PrusaMiniGcodeAnalysisQueue)
return dict(gcode=lambda finished_callback: PrusaMiniGcodeAnalysisQueue(finished_callback, self))

def create_estimator_factory(self, *args, **kwargs):
return self.print_time_estimator
return self._print_time_estimator

def update_estimation(self, comm_instance, phase, cmd, cmd_type, gcode, *args, **kwargs):
if gcode != "M73":
return

remaining_time_result = self.remaining_time_pattern.search(cmd)
remaining_time_result = self._remaining_time_pattern.search(cmd)
if remaining_time_result:
# ETA needs to be in seconds
#
self.print_time_estimator.remaining_time = int(remaining_time_result.group(1)) * 60
self._logger.info("New ETA: " + str(self.print_time_estimator.remaining_time))
self._print_time_estimator.remaining_time = int(remaining_time_result.group(1)) * 60
self._logger.info("New ETA: " + str(self._print_time_estimator.remaining_time))


__plugin_name__ = "Prusa Mini ETA Plugin"
Expand Down
20 changes: 13 additions & 7 deletions octoprint_prusa_mini_eta/initialestimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,36 @@

import re
import time
from typing import Pattern

from octoprint.filemanager.analysis import AnalysisAborted
from octoprint.filemanager.analysis import GcodeAnalysisQueue


class PrusaMiniGcodeAnalysisQueue(GcodeAnalysisQueue):
remaining_time_pattern = re.compile(r'^\s*M73\s+P\d+\s+R(\d+)\s*\r?\n?$')
_remaining_time_pattern: Pattern[str] = re.compile(r'^\s*M73\s+P\d+\s+R(\d+)\s*\r?\n?$')

def __init__(self, finished_callback):
def __init__(self, finished_callback, eta_plugin):
super(PrusaMiniGcodeAnalysisQueue, self).__init__(finished_callback)
from octoprint_prusa_mini_eta import PrusaMiniETAPlugin
assert isinstance(eta_plugin, PrusaMiniETAPlugin)
self._eta_plugin = eta_plugin

def _do_analysis(self, high_priority=False):
try:
def throttle():
time.sleep(0.01)
time.sleep(0.01) # high_priority == False

result = super(PrusaMiniGcodeAnalysisQueue, self)._do_analysis(high_priority)

with open(self._current.absolute_path, 'r') as f:
for line in f:
remaining_time_pattern_result = self.remaining_time_pattern.search(line)
remaining_time_pattern_result = self._remaining_time_pattern.search(line)
if remaining_time_pattern_result:
result["estimatedPrintTime"] = int(remaining_time_pattern_result.group(1)) * 60
self._eta_plugin.logger.info("New ETA from the upload: " + str(result["estimatedPrintTime"]))
break
if high_priority:
if not high_priority:
throttle()
if self._aborted:
# If abortion is requested we will not raise AnalysisAborted, but return already
Expand All @@ -55,5 +61,5 @@ def throttle():
except AnalysisAborted as e:
raise

def _do_abort(self):
super(PrusaMiniGcodeAnalysisQueue, self)._do_abort()
def _do_abort(self, reenqueue=True):
super(PrusaMiniGcodeAnalysisQueue, self)._do_abort(reenqueue)
2 changes: 1 addition & 1 deletion octoprint_prusa_mini_eta/liveestimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
class PrusaMiniPrintTimeEstimator(PrintTimeEstimator):
def __init__(self, job_type):
super(PrusaMiniPrintTimeEstimator, self).__init__(job_type)
self.job_type = job_type
self._job_type = job_type
self.remaining_time = None

@property
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
plugin_name = "OctoPrint-Prusa-Mini-ETA"

# The plugin's version. Can be overwritten within OctoPrint's internal data via __plugin_version__ in the plugin module
plugin_version = "0.1.2"
plugin_version = "0.1.3"

# The plugin's description. Can be overwritten within OctoPrint's internal data via __plugin_description__ in the plugin
# module
Expand Down

0 comments on commit 2e797a3

Please sign in to comment.