Skip to content

Commit

Permalink
[Refactoring] Let Handlers return Result objects.
Browse files Browse the repository at this point in the history
Signed-off-by: Juri Berlanda <[email protected]>
  • Loading branch information
j-be committed Oct 14, 2021
1 parent 8cbbaa0 commit aa8e4c8
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 22 deletions.
26 changes: 13 additions & 13 deletions octoprint_autobim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# coding=utf-8
from __future__ import absolute_import

import math
import threading
import time

Expand Down Expand Up @@ -122,14 +121,14 @@ def on_api_command(self, command, data):
def on_test_point(self, point):
self._logger.info("Got X%s, Y%s" % point)
result = self.g30.do(point, 30)
if math.isnan(result):
if result.has_value():
self._plugin_manager.send_plugin_message(
self._identifier,
dict(type="error", message="Point X%s Y%s seems to be unreachable!" % point))
dict(type="info", message="Point X%s Y%s seems to work fine" % point))
else:
self._plugin_manager.send_plugin_message(
self._identifier,
dict(type="info", message="Point X%s Y%s seems to work fine" % point))
dict(type="error", message="Point X%s Y%s seems to be unreachable!" % point))

##~~ SettingsPlugin mixin

Expand Down Expand Up @@ -185,15 +184,15 @@ def check_state(self):
self._handle_m503_result(self.m503.do())

def _handle_m503_result(self, result):
if result is None:
if result.abort:
self._logger.info("'None' from queue means user abort")
return
elif math.isnan(result):
elif not result.has_value:
self._plugin_manager.send_plugin_message(self._identifier, dict(
type="warn",
message="Cannot determine whether UBL is active or not! Assuming it isn't. If it is, please set it manually in the settings."))
self._set_ubl_flag(False)
elif result:
elif result.value is True:
self._plugin_manager.send_plugin_message(self._identifier, dict(
type="info",
message="Seems like UBL system is active! If not, please change the setting."))
Expand Down Expand Up @@ -237,28 +236,29 @@ def autobim(self):
self._logger.info("Treating first corner as reference")
self._printer.commands("M117 Getting reference...")

reference = self.g30.do(corner)
if reference is None:
result = self.g30.do(corner)
if result.abort:
self._logger.info("'None' from queue means user abort")
return
elif math.isnan(reference):
elif not result.has_value():
self.abort_now("Cannot probe X%s Y%s! Please check settings!" % corner)
return

reference = result.value
self._printer.commands("M117 wait...")
else:
delta = 2 * threshold
while abs(delta) >= threshold and self.running:
z_current = self.g30.do(corner)

if z_current is None:
if z_current.abort:
self._logger.info("'None' from queue means user abort")
return
elif math.isnan(z_current):
elif not z_current.has_value():
self.abort_now("Cannot probe X%s Y%s! Please check settings!" % corner)
return
else:
delta = z_current - reference
delta = z_current.value - reference

if abs(delta) >= threshold and multipass:
changed = True
Expand Down
30 changes: 28 additions & 2 deletions octoprint_autobim/async_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,32 @@
import queue


class Result(object):
def __init__(self, error, abort, value):
self.error = error
self.abort = abort
self.value = value

def has_value(self):
return not self.error and not self.abort and self.value is not None

@staticmethod
def error():
return Result(True, False, None)

@staticmethod
def abort():
return Result(False, True, None)

@staticmethod
def of(value):
return Result(False, False, value)

@staticmethod
def no_result():
return Result(False, False, None)


class AsyncCommand(object):
def __init__(self):
self.__running = False
Expand Down Expand Up @@ -33,13 +59,13 @@ def _register_result(self, result):
def abort(self):
self.__running = False
self._flush()
self.__result.put(None, False)
self.__result.put(Result.abort(), False)

def _get(self, timeout):
try:
return self.__result.get(timeout=timeout)
except queue.Empty:
return float('nan')
return Result.no_result()
finally:
self.__running = False

Expand Down
7 changes: 4 additions & 3 deletions octoprint_autobim/g30.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import re

from octoprint_autobim.async_command import AsyncCommand
from octoprint_autobim.async_command import AsyncCommand, Result


class G30Handler(AsyncCommand):
Expand All @@ -20,9 +20,10 @@ def _start(self, point):

def _handle_internal(self, line):
if "ok" == line:
self._register_result(float('nan'))
self._register_result(Result.error())
return

match = self.pattern.match(line)
if match:
self._register_result(float(match.group(1)))
self._register_result(Result.of(float(match.group(1))))
return
8 changes: 4 additions & 4 deletions octoprint_autobim/m503.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from octoprint_autobim.async_command import AsyncCommand
from octoprint_autobim.async_command import AsyncCommand, Result


class M503Handler(AsyncCommand):
Expand All @@ -16,11 +16,11 @@ def _start(self):

def _handle_internal(self, line):
if "Unknown command:" in line and "M503" in line:
self._register_result(float('nan'))
self._register_result(Result.error())
return
if line.startswith("ok"):
self._register_result(False)
self._register_result(Result.of(False))
return
if "Unified Bed Leveling System" in line:
self._register_result(True)
self._register_result(Result.of(True))
return

0 comments on commit aa8e4c8

Please sign in to comment.