From 9bffaf25e082814b6fa38ed70077b7bf87c4f58c Mon Sep 17 00:00:00 2001 From: sergiuToporjinschi Date: Tue, 1 Feb 2022 22:21:23 +0200 Subject: [PATCH] API Separation working --- octoprint_CalibrationTools/EStepsApi.py | 25 ++-- octoprint_CalibrationTools/PIDAutoTune.py | 22 ++++ octoprint_CalibrationTools/__init__.py | 1 - octoprint_CalibrationTools/api.py | 114 +++--------------- .../static/js/CalibrationTools.js | 9 +- .../templates/CalibrationTools_tab.jinja2 | 2 +- 6 files changed, 60 insertions(+), 113 deletions(-) create mode 100644 octoprint_CalibrationTools/PIDAutoTune.py diff --git a/octoprint_CalibrationTools/EStepsApi.py b/octoprint_CalibrationTools/EStepsApi.py index d8ae183..31b1e3d 100644 --- a/octoprint_CalibrationTools/EStepsApi.py +++ b/octoprint_CalibrationTools/EStepsApi.py @@ -5,25 +5,25 @@ import re from threading import Event import flask +import octoprint.plugin -CMD_ESP_TEST = "TEST" -CMD_ESP_LOAD_STEPS = "eSteps_load" -CMD_ESP_START_EXTRUSION = "eSteps_startExtrusion" -CMD_ESP_SAVE = "eSteps_save" +CMD_ESTEPS_LOAD_STEPS = "eSteps_load" +CMD_ESTEPS_START_EXTRUSION = "eSteps_startExtrusion" +CMD_ESTEPS_SAVE = "eSteps_save" -class API: +class API(octoprint.plugin.SimpleApiPlugin): @staticmethod def apiCommands(): return { - CMD_ESP_SAVE: [], - CMD_ESP_LOAD_STEPS: [], - CMD_ESP_START_EXTRUSION: [] + CMD_ESTEPS_SAVE: ['newESteps'], + CMD_ESTEPS_LOAD_STEPS: [], + CMD_ESTEPS_START_EXTRUSION: ['extrudeLength','extrudeSpeed','extrudeTemp'] } def apiGateWay(self, command, data): self._logger.debug("api command [%s] received payload [%s]", command, data) - if command == CMD_ESP_LOAD_STEPS: + if command == CMD_ESTEPS_LOAD_STEPS: self._logger.debug("Load steps from EEPROM") if not self._printer.is_ready(): self._logger.warning("Printer not ready, operation canceled") @@ -42,7 +42,8 @@ class API: return flask.jsonify({ "data": self.data["steps"] }) - if command == CMD_ESP_START_EXTRUSION: + + if command == CMD_ESTEPS_START_EXTRUSION: self._logger.debug("Heating the extruder [%s]", data) if not self._printer.is_ready(): self._logger.warning("Printer not ready, operation canceled") @@ -57,12 +58,14 @@ class API: self._printer.commands("M104 S%(extrudeTemp)s" % data) return - if command == CMD_ESP_SAVE: + if command == CMD_ESTEPS_SAVE: eStepsSettings = self._settings.get(['eSteps']) userControlsTemp = eStepsSettings.get("userControlsTemp") turnOffHotend = eStepsSettings.get("turnOffHotend") self._printer.commands(["M92 E%(newESteps)s" % data, "M500"] + ["M104 S0"] if turnOffHotend and not userControlsTemp else []) return + + ############## HANDLERS ############## @staticmethod def startExtrusion(self, temps, extrudeLength, extrudeSpeed, *args): diff --git a/octoprint_CalibrationTools/PIDAutoTune.py b/octoprint_CalibrationTools/PIDAutoTune.py new file mode 100644 index 0000000..694bd02 --- /dev/null +++ b/octoprint_CalibrationTools/PIDAutoTune.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +from __future__ import absolute_import, division, print_function, unicode_literals + +import re +from threading import Event + +import flask +import octoprint.plugin + +CMD_PID_SAVE = "pid_save" + +class API(octoprint.plugin.SimpleApiPlugin): + + @staticmethod + def apiCommands(): + return {CMD_PID_SAVE : []} + + def apiGateWay(self, command, data): + self._logger.debug("DIPGateway") + if command == CMD_PID_SAVE: + self._logger.debug("DIPSave-") + diff --git a/octoprint_CalibrationTools/__init__.py b/octoprint_CalibrationTools/__init__.py index c11c13f..031bded 100644 --- a/octoprint_CalibrationTools/__init__.py +++ b/octoprint_CalibrationTools/__init__.py @@ -28,7 +28,6 @@ class CalibrationtoolsPlugin( collectCommand = False data = {} - def initialize(self): self.collectCommand = False diff --git a/octoprint_CalibrationTools/api.py b/octoprint_CalibrationTools/api.py index 7d0dffd..86d59b4 100644 --- a/octoprint_CalibrationTools/api.py +++ b/octoprint_CalibrationTools/api.py @@ -1,35 +1,30 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, division, print_function, unicode_literals -import re -from threading import Event - import flask -import octoprint.plugin -# from octoprint_CalibrationTools import EStepsApi +from octoprint_CalibrationTools import EStepsApi, PIDAutoTune -CMD_TEST = "TEST" -CMD_LOAD_STEPS = "loadSteps" -CMD_START_EXTRUSION = "startExtrusion" -CMD_SAVE_E_STEPS = "saveESteps" -class API(octoprint.plugin.SimpleApiPlugin): - def __init__(self) -> None: - super().__init__() - # self.espAPI = EStepsApi.API() +class API(EStepsApi.API, PIDAutoTune.API): + + commandsRegistration = {} @staticmethod def get_api_commands(): - # x = EStepsApi.apiCommands() - return { - CMD_LOAD_STEPS: [], - CMD_START_EXTRUSION: [], - CMD_SAVE_E_STEPS: [], - CMD_TEST: [] + API.commandsRegistration = { + "eSteps" : {"cls" : EStepsApi.API}, + "PIDAutoTune": {"cls" : PIDAutoTune.API} } + result = {} + for key, api in API.commandsRegistration.items(): + API.commandsRegistration[key].update({"commands": api["cls"].apiCommands()}) + result.update( API.commandsRegistration[key]["commands"]) + return result + def on_api_get(self, request): + # request.args.get('x') self._logger.debug("api.on_api_get") return flask.jsonify( { @@ -38,80 +33,7 @@ class API(octoprint.plugin.SimpleApiPlugin): ) def on_api_command(self, command, data): - self._logger.debug("api command [%s] received payload [%s]", command, data) - self.espAPI.apiGateWay() - - if command == CMD_LOAD_STEPS: - self._logger.debug("Load steps from EEPROM") - if not self._printer.is_ready(): - self._logger.warning("Printer not ready, operation canceled") - return flask.abort(503, { - "msg": "Printer not ready, operation canceled" - }) - - # Register listener waiting response for M92 command - m92Event = Event() - self.registerGCodeWaiter("M92", self.m92GCodeResponse, m92Event) - - # Issue M92 command - self._printer.commands("M92") - - m92Event.wait() - return flask.jsonify({ - "data": self.data["steps"] - }) - - if command == CMD_START_EXTRUSION: - self._logger.debug("Heating the extruder [%s]", data) - if not self._printer.is_ready(): - self._logger.warning("Printer not ready, operation canceled") - return flask.abort(503, { - "msg": "Printer not ready, operation canceled" - }) - - # Register event to be trigger when temp is achieved - self.registerEventTemp("T0", int(data["extrudeTemp"]), self.startExtrusion, data["extrudeLength"], data["extrudeSpeed"]) - - # Heating the tool - self._printer.commands("M104 S%(extrudeTemp)s" % data) - return - - if command == CMD_SAVE_E_STEPS: - eStepsSettings = self._settings.get(['eSteps']) - userControlsTemp = eStepsSettings.get("userControlsTemp") - turnOffHotend = eStepsSettings.get("turnOffHotend") - self._printer.commands(["M92 E%(newESteps)s" % data, "M500"] + ["M104 S0"] if turnOffHotend and not userControlsTemp else []) - return - - if command == CMD_TEST: - self.registerGCodeWaiter("M92", self.someTestFunc) - return - -############## HANDLERS ############## - @staticmethod - def startExtrusion(self, temps, extrudeLength, extrudeSpeed, *args): - self._logger.debug("Temperature achieved, extrusion started [temps:%s, extrudeLength:%s, extrudeSpeed:%s, args:%s]", - temps, extrudeLength, extrudeSpeed, args) - - # Extrude - self._printer.extrude(amount=int(extrudeLength), speed=int(extrudeSpeed)) - - @staticmethod - def m92GCodeResponse(self, line, event): - reg = re.compile("echo:\s*(?P(?PM\d{1,3}) X(?P\d{1,3}.\d{1,3}) Y(?P\d{1,3}.\d{1,3}) Z(?P\d{1,3}.\d{1,3}) E(?P\d{1,3}.\d{1,3}))") - isM92command = reg.match(line) - if isM92command: - command = isM92command.group("command") - if isM92command.group("gCode") == "M92": - self.data["steps"]["X"] = float(isM92command.group("xVal")) - self.data["steps"]["Y"] = float(isM92command.group("yVal")) - self.data["steps"]["Z"] = float(isM92command.group("zVal")) - self.data["steps"]["E"] = float(isM92command.group("eVal")) - - # Send the new data to the UI to be reloaded - self._logger.debug("gCode: %s", command) - self._logger.debug("X: %s, Y:%s, Z:%s, E:%s", self.data["steps"]["X"], self.data["steps"]["Y"], self.data["steps"]["Z"], self.data["steps"]["E"]) - self._logger.debug("Finished data collection") - self.collectCommand = False - if event: - event.set() + self._logger.debug("API command [%s] received payload [%s]", command, data) + for key, api in self.commandsRegistration.items(): + if command in api["commands"]: + return api["cls"].apiGateWay(self, command, data) diff --git a/octoprint_CalibrationTools/static/js/CalibrationTools.js b/octoprint_CalibrationTools/static/js/CalibrationTools.js index 569824a..ef5d0a7 100644 --- a/octoprint_CalibrationTools/static/js/CalibrationTools.js +++ b/octoprint_CalibrationTools/static/js/CalibrationTools.js @@ -49,13 +49,13 @@ $(function () { } self.loadESteps = function () { - OctoPrint.simpleApiCommand("CalibrationTools", "loadSteps").done(function (response) { + OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_load").done(function (response) { self.from_json(response); }) } self.startExtrusion = function () { - OctoPrint.simpleApiCommand("CalibrationTools", "startExtrusion", { + OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_startExtrusion", { "extrudeTemp": self.testParam.extrudeTemp(), "extrudeLength": self.testParam.extrudeLength(), "extrudeSpeed": self.testParam.extrudeSpeed() @@ -77,7 +77,7 @@ $(function () { } self.saveESteps = function () { - OctoPrint.simpleApiCommand("CalibrationTools", "saveESteps", { + OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_save", { "newESteps": self.results.newSteps() }).done(function () { new PNotify({ @@ -89,7 +89,8 @@ $(function () { } self.onAllBound = self.onEventConnected = function () { - OctoPrint.simpleApiGet("CalibrationTools").done(function (response) { + debugger; + OctoPrint.simpleApiGet("CalibrationTools", "something").done(function (response) { console.log("CalibrationTools"); self.from_json(response); }); diff --git a/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 b/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 index a655d1a..d8c803a 100644 --- a/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 +++ b/octoprint_CalibrationTools/templates/CalibrationTools_tab.jinja2 @@ -71,7 +71,7 @@ }; }, // dependencies: ["terminalViewModel"], - elements: [".toolBar"] + elements: ["#toolBar"] }); {% endblock %} \ No newline at end of file