From 8495d89cd39f37fe3b7181426d1740efe05b81cc Mon Sep 17 00:00:00 2001 From: sergiuToporjinschi Date: Sun, 30 Jan 2022 11:19:53 +0200 Subject: [PATCH] Temp supports functions and methods Extrusion is functional --- octoprint_CalibrationTools/__init__.py | 2 ++ octoprint_CalibrationTools/api.py | 21 +++++++++++++------ octoprint_CalibrationTools/hooks.py | 20 +++++++++++------- .../static/js/CalibrationTools.js | 6 ++++++ 4 files changed, 36 insertions(+), 13 deletions(-) diff --git a/octoprint_CalibrationTools/__init__.py b/octoprint_CalibrationTools/__init__.py index 1d35a0b..9114d38 100644 --- a/octoprint_CalibrationTools/__init__.py +++ b/octoprint_CalibrationTools/__init__.py @@ -36,6 +36,8 @@ class CalibrationtoolsPlugin( "js": ["js/CalibrationTools.js"], "css": ["css/style.css"] } + def startExtrusion(self, temps): + self._logger.debug("Temperature achieved, extrusion started %s", temps) def get_update_information(self): # Define the configuration for your plugin to use with the Software Update diff --git a/octoprint_CalibrationTools/api.py b/octoprint_CalibrationTools/api.py index 68aa47a..f0950cb 100644 --- a/octoprint_CalibrationTools/api.py +++ b/octoprint_CalibrationTools/api.py @@ -4,11 +4,13 @@ from __future__ import absolute_import, division, print_function, unicode_litera import octoprint.plugin import flask + CMD_LOAD_STEPS = "loadSteps" +CMD_TEST = "TEST" +CMD_START_EXTRUSION = "startExtrusion" - -def someTestFunc(self, temps, a, b): - self._logger.debug("a ajuns %s, %s, %s", temps, a,b) +def someTestFunc(self, temps): + self._logger.debug("a ajuns %s", temps) class API(octoprint.plugin.SimpleApiPlugin): @@ -16,7 +18,8 @@ class API(octoprint.plugin.SimpleApiPlugin): def get_api_commands(): return { CMD_LOAD_STEPS: [], - "TEST": [] + CMD_START_EXTRUSION: [], + CMD_TEST: [] } def on_api_get(self, request): @@ -34,6 +37,12 @@ class API(octoprint.plugin.SimpleApiPlugin): return flask.jsonify({ "data": self.data["steps"] }) - if command == "TEST": - self.registerEventTemp("T0", 100, someTestFunc, 1,"test") + if command == CMD_START_EXTRUSION: + self._logger.debug("Heating the tools") + self._printer.commands("M104 S180") + self.registerEventTemp("T0", 180, self.startExtrusion) + + return + if command == CMD_TEST: + self.registerEventTemp("T0", 100, someTestFunc) return diff --git a/octoprint_CalibrationTools/hooks.py b/octoprint_CalibrationTools/hooks.py index 376a8d9..f42a6d4 100644 --- a/octoprint_CalibrationTools/hooks.py +++ b/octoprint_CalibrationTools/hooks.py @@ -5,6 +5,8 @@ from ast import arg import re import threading import traceback +import types + class Hooks(): trackTemp = True @@ -50,27 +52,31 @@ class Hooks(): "firmware": firmware_data } - ## Hook for temperature messages - ## This is active only when there are events registered to temperature changes + # Hook for temperature messages + # This is active only when there are events registered to temperature changes def processTemp(self, comm_instance, parsed_temperatures, *args, **kwargs): - if len(self.events) <= 0: return parsed_temperatures + if len(self.events) <= 0: + return parsed_temperatures try: self.checkAndTriggerEvent(parsed_temperatures.copy()) except Exception as e: self._logger.error(traceback.format_exc()) return parsed_temperatures - ## Check if the current message contains changes concerning registered tool - ## if the criteria is meat then the execution function is called + # Check if the current message contains changes concerning registered tool + # if the criteria is meat then the execution function is called def checkAndTriggerEvent(self, temps): for tool, values in temps.items(): (curTemp, trgTemp) = values for event in self.events: if event["tool"] == tool and curTemp >= event["targetTemp"]: - threading.Thread(target=event["func"], args=(self, temps, *event["args"])).start() + arg = (temps, *event["args"]) + if isinstance(event["func"], types.FunctionType): + arg = (self, *arg) + threading.Thread(target=event["func"], args=arg).start() self.events.remove(event) - ## Registering a temp event + # Registering a temp event def registerEventTemp(self, tool, targetTemp, func, *arguments): if func is None: self._logger.warn("registerEventTemp: Attempt to register event without a function") diff --git a/octoprint_CalibrationTools/static/js/CalibrationTools.js b/octoprint_CalibrationTools/static/js/CalibrationTools.js index e33f148..e653258 100644 --- a/octoprint_CalibrationTools/static/js/CalibrationTools.js +++ b/octoprint_CalibrationTools/static/js/CalibrationTools.js @@ -53,6 +53,12 @@ $(function () { // }); } + self.startExtrusion = function () { + OctoPrint.simpleApiCommand("CalibrationTools","startExtrusion").done(function (response) { + console.log(response); + }) + } + self.tempRestart = function () { OctoPrint.system.executeCommand("core", "restart"); }