API Separation working
This commit is contained in:
@@ -5,25 +5,25 @@ import re
|
|||||||
from threading import Event
|
from threading import Event
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
|
import octoprint.plugin
|
||||||
|
|
||||||
CMD_ESP_TEST = "TEST"
|
CMD_ESTEPS_LOAD_STEPS = "eSteps_load"
|
||||||
CMD_ESP_LOAD_STEPS = "eSteps_load"
|
CMD_ESTEPS_START_EXTRUSION = "eSteps_startExtrusion"
|
||||||
CMD_ESP_START_EXTRUSION = "eSteps_startExtrusion"
|
CMD_ESTEPS_SAVE = "eSteps_save"
|
||||||
CMD_ESP_SAVE = "eSteps_save"
|
|
||||||
|
|
||||||
class API:
|
class API(octoprint.plugin.SimpleApiPlugin):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def apiCommands():
|
def apiCommands():
|
||||||
return {
|
return {
|
||||||
CMD_ESP_SAVE: [],
|
CMD_ESTEPS_SAVE: ['newESteps'],
|
||||||
CMD_ESP_LOAD_STEPS: [],
|
CMD_ESTEPS_LOAD_STEPS: [],
|
||||||
CMD_ESP_START_EXTRUSION: []
|
CMD_ESTEPS_START_EXTRUSION: ['extrudeLength','extrudeSpeed','extrudeTemp']
|
||||||
}
|
}
|
||||||
|
|
||||||
def apiGateWay(self, command, data):
|
def apiGateWay(self, command, data):
|
||||||
self._logger.debug("api command [%s] received payload [%s]", 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")
|
self._logger.debug("Load steps from EEPROM")
|
||||||
if not self._printer.is_ready():
|
if not self._printer.is_ready():
|
||||||
self._logger.warning("Printer not ready, operation canceled")
|
self._logger.warning("Printer not ready, operation canceled")
|
||||||
@@ -42,7 +42,8 @@ class API:
|
|||||||
return flask.jsonify({
|
return flask.jsonify({
|
||||||
"data": self.data["steps"]
|
"data": self.data["steps"]
|
||||||
})
|
})
|
||||||
if command == CMD_ESP_START_EXTRUSION:
|
|
||||||
|
if command == CMD_ESTEPS_START_EXTRUSION:
|
||||||
self._logger.debug("Heating the extruder [%s]", data)
|
self._logger.debug("Heating the extruder [%s]", data)
|
||||||
if not self._printer.is_ready():
|
if not self._printer.is_ready():
|
||||||
self._logger.warning("Printer not ready, operation canceled")
|
self._logger.warning("Printer not ready, operation canceled")
|
||||||
@@ -57,12 +58,14 @@ class API:
|
|||||||
self._printer.commands("M104 S%(extrudeTemp)s" % data)
|
self._printer.commands("M104 S%(extrudeTemp)s" % data)
|
||||||
return
|
return
|
||||||
|
|
||||||
if command == CMD_ESP_SAVE:
|
if command == CMD_ESTEPS_SAVE:
|
||||||
eStepsSettings = self._settings.get(['eSteps'])
|
eStepsSettings = self._settings.get(['eSteps'])
|
||||||
userControlsTemp = eStepsSettings.get("userControlsTemp")
|
userControlsTemp = eStepsSettings.get("userControlsTemp")
|
||||||
turnOffHotend = eStepsSettings.get("turnOffHotend")
|
turnOffHotend = eStepsSettings.get("turnOffHotend")
|
||||||
self._printer.commands(["M92 E%(newESteps)s" % data, "M500"] + ["M104 S0"] if turnOffHotend and not userControlsTemp else [])
|
self._printer.commands(["M92 E%(newESteps)s" % data, "M500"] + ["M104 S0"] if turnOffHotend and not userControlsTemp else [])
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
############## HANDLERS ##############
|
############## HANDLERS ##############
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def startExtrusion(self, temps, extrudeLength, extrudeSpeed, *args):
|
def startExtrusion(self, temps, extrudeLength, extrudeSpeed, *args):
|
||||||
|
|||||||
@@ -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-")
|
||||||
|
|
||||||
@@ -28,7 +28,6 @@ class CalibrationtoolsPlugin(
|
|||||||
collectCommand = False
|
collectCommand = False
|
||||||
data = {}
|
data = {}
|
||||||
|
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
self.collectCommand = False
|
self.collectCommand = False
|
||||||
|
|
||||||
|
|||||||
@@ -1,35 +1,30 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
import re
|
|
||||||
from threading import Event
|
|
||||||
|
|
||||||
import flask
|
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):
|
class API(EStepsApi.API, PIDAutoTune.API):
|
||||||
def __init__(self) -> None:
|
|
||||||
super().__init__()
|
commandsRegistration = {}
|
||||||
# self.espAPI = EStepsApi.API()
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_api_commands():
|
def get_api_commands():
|
||||||
# x = EStepsApi.apiCommands()
|
API.commandsRegistration = {
|
||||||
return {
|
"eSteps" : {"cls" : EStepsApi.API},
|
||||||
CMD_LOAD_STEPS: [],
|
"PIDAutoTune": {"cls" : PIDAutoTune.API}
|
||||||
CMD_START_EXTRUSION: [],
|
|
||||||
CMD_SAVE_E_STEPS: [],
|
|
||||||
CMD_TEST: []
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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):
|
def on_api_get(self, request):
|
||||||
|
# request.args.get('x')
|
||||||
self._logger.debug("api.on_api_get")
|
self._logger.debug("api.on_api_get")
|
||||||
return flask.jsonify(
|
return flask.jsonify(
|
||||||
{
|
{
|
||||||
@@ -38,80 +33,7 @@ class API(octoprint.plugin.SimpleApiPlugin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def on_api_command(self, command, data):
|
def on_api_command(self, command, data):
|
||||||
self._logger.debug("api command [%s] received payload [%s]", command, data)
|
self._logger.debug("API command [%s] received payload [%s]", command, data)
|
||||||
self.espAPI.apiGateWay()
|
for key, api in self.commandsRegistration.items():
|
||||||
|
if command in api["commands"]:
|
||||||
if command == CMD_LOAD_STEPS:
|
return api["cls"].apiGateWay(self, command, data)
|
||||||
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<command>(?P<gCode>M\d{1,3}) X(?P<xVal>\d{1,3}.\d{1,3}) Y(?P<yVal>\d{1,3}.\d{1,3}) Z(?P<zVal>\d{1,3}.\d{1,3}) E(?P<eVal>\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()
|
|
||||||
|
|||||||
@@ -49,13 +49,13 @@ $(function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.loadESteps = function () {
|
self.loadESteps = function () {
|
||||||
OctoPrint.simpleApiCommand("CalibrationTools", "loadSteps").done(function (response) {
|
OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_load").done(function (response) {
|
||||||
self.from_json(response);
|
self.from_json(response);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
self.startExtrusion = function () {
|
self.startExtrusion = function () {
|
||||||
OctoPrint.simpleApiCommand("CalibrationTools", "startExtrusion", {
|
OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_startExtrusion", {
|
||||||
"extrudeTemp": self.testParam.extrudeTemp(),
|
"extrudeTemp": self.testParam.extrudeTemp(),
|
||||||
"extrudeLength": self.testParam.extrudeLength(),
|
"extrudeLength": self.testParam.extrudeLength(),
|
||||||
"extrudeSpeed": self.testParam.extrudeSpeed()
|
"extrudeSpeed": self.testParam.extrudeSpeed()
|
||||||
@@ -77,7 +77,7 @@ $(function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.saveESteps = function () {
|
self.saveESteps = function () {
|
||||||
OctoPrint.simpleApiCommand("CalibrationTools", "saveESteps", {
|
OctoPrint.simpleApiCommand("CalibrationTools", "eSteps_save", {
|
||||||
"newESteps": self.results.newSteps()
|
"newESteps": self.results.newSteps()
|
||||||
}).done(function () {
|
}).done(function () {
|
||||||
new PNotify({
|
new PNotify({
|
||||||
@@ -89,7 +89,8 @@ $(function () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
self.onAllBound = self.onEventConnected = function () {
|
self.onAllBound = self.onEventConnected = function () {
|
||||||
OctoPrint.simpleApiGet("CalibrationTools").done(function (response) {
|
debugger;
|
||||||
|
OctoPrint.simpleApiGet("CalibrationTools", "something").done(function (response) {
|
||||||
console.log("CalibrationTools");
|
console.log("CalibrationTools");
|
||||||
self.from_json(response);
|
self.from_json(response);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -71,7 +71,7 @@
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
// dependencies: ["terminalViewModel"],
|
// dependencies: ["terminalViewModel"],
|
||||||
elements: [".toolBar"]
|
elements: ["#toolBar"]
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
Reference in New Issue
Block a user