Implement registration to M commands response
This commit is contained in:
@@ -108,6 +108,5 @@ def __plugin_load__():
|
|||||||
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
|
"octoprint.plugin.softwareupdate.check_config": __plugin_implementation__.get_update_information,
|
||||||
"octoprint.comm.protocol.firmware.info": __plugin_implementation__.firmwareInfo,
|
"octoprint.comm.protocol.firmware.info": __plugin_implementation__.firmwareInfo,
|
||||||
"octoprint.comm.protocol.gcode.received": __plugin_implementation__.gCodeReceived,
|
"octoprint.comm.protocol.gcode.received": __plugin_implementation__.gCodeReceived,
|
||||||
"octoprint.comm.protocol.gcode.sending": __plugin_implementation__.gCodeSending,
|
|
||||||
"octoprint.comm.protocol.temperatures.received": __plugin_implementation__.processTemp
|
"octoprint.comm.protocol.temperatures.received": __plugin_implementation__.processTemp
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +1,16 @@
|
|||||||
# -*- 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
|
||||||
|
|
||||||
|
from threading import Event
|
||||||
import octoprint.plugin
|
import octoprint.plugin
|
||||||
import flask
|
import flask
|
||||||
|
import re
|
||||||
|
|
||||||
CMD_TEST = "TEST"
|
CMD_TEST = "TEST"
|
||||||
CMD_LOAD_STEPS = "loadSteps"
|
CMD_LOAD_STEPS = "loadSteps"
|
||||||
CMD_START_EXTRUSION = "startExtrusion"
|
CMD_START_EXTRUSION = "startExtrusion"
|
||||||
CMD_SAVE_E_STEPS = "saveESteps"
|
CMD_SAVE_E_STEPS = "saveESteps"
|
||||||
|
|
||||||
|
|
||||||
def someTestFunc(self, temps):
|
|
||||||
self._logger.debug("a ajuns %s", temps)
|
|
||||||
|
|
||||||
|
|
||||||
class API(octoprint.plugin.SimpleApiPlugin):
|
class API(octoprint.plugin.SimpleApiPlugin):
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def get_api_commands():
|
def get_api_commands():
|
||||||
@@ -43,8 +39,14 @@ class API(octoprint.plugin.SimpleApiPlugin):
|
|||||||
"msg": "Printer not ready, operation canceled"
|
"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")
|
self._printer.commands("M92")
|
||||||
|
|
||||||
|
m92Event.wait()
|
||||||
return flask.jsonify({
|
return flask.jsonify({
|
||||||
"data": self.data["steps"]
|
"data": self.data["steps"]
|
||||||
})
|
})
|
||||||
@@ -69,14 +71,33 @@ class API(octoprint.plugin.SimpleApiPlugin):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if command == CMD_TEST:
|
if command == CMD_TEST:
|
||||||
# self.registerEventTemp("T0", 100, someTestFunc)
|
self.registerGCodeWaiter("M92", self.someTestFunc)
|
||||||
return flask.abort(503, {
|
return
|
||||||
"msg": "Printer not ready, operation canceled"
|
|
||||||
})
|
|
||||||
|
|
||||||
|
############## HANDLERS ##############
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def startExtrusion(self, temps, *args):
|
def startExtrusion(self, temps, *args):
|
||||||
self._logger.debug("Temperature achieved, extrusion started %s, %s", temps, args)
|
self._logger.debug("Temperature achieved, extrusion started %s, %s", temps, args)
|
||||||
|
|
||||||
# Extrude
|
# Extrude
|
||||||
self._printer.extrude(amount=120, speed=50)
|
self._printer.extrude(amount=120, speed=50)
|
||||||
|
|
||||||
|
@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()
|
||||||
@@ -6,46 +6,31 @@ import re
|
|||||||
import threading
|
import threading
|
||||||
import traceback
|
import traceback
|
||||||
import types
|
import types
|
||||||
|
import collections
|
||||||
|
|
||||||
|
|
||||||
class Hooks():
|
class Hooks():
|
||||||
trackTemp = True
|
trackTemp = True
|
||||||
events = []
|
events = []
|
||||||
|
gCodeWaiters = []
|
||||||
|
|
||||||
def gCodeReceived(self, comm, line, *args, **kwargs):
|
def gCodeReceived(self, comm, line, *args, **kwargs):
|
||||||
if not self.collectCommand: return line
|
if len(self.gCodeWaiters) <= 0 and not line.startswith('echo:'):
|
||||||
self._logger.debug("collectCommand is true, collecting info")
|
return line
|
||||||
|
|
||||||
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}))")
|
reg = re.compile("echo:\s*(?P<gCode>M\d{1,4})")
|
||||||
isM92command = reg.match(line)
|
mCommand = reg.match(line)
|
||||||
if isM92command:
|
if mCommand:
|
||||||
command = isM92command.group("command")
|
gCode = mCommand.group("gCode")
|
||||||
if isM92command.group("gCode") == "M92":
|
for waiter in self.gCodeWaiters:
|
||||||
xValue = isM92command.group("xVal")
|
if gCode.upper() == waiter["cmd"]:
|
||||||
yValue = isM92command.group("yVal")
|
arg = (line, *waiter["args"])
|
||||||
zValue = isM92command.group("zVal")
|
if isinstance(waiter["func"], types.FunctionType):
|
||||||
eValue = isM92command.group("eVal")
|
arg = (self, *arg)
|
||||||
self.data["steps"]["X"] = float(xValue)
|
threading.Thread(target=waiter["func"], args=arg).start()
|
||||||
self.data["steps"]["Y"] = float(yValue)
|
self.gCodeWaiters.remove(waiter)
|
||||||
self.data["steps"]["Z"] = float(zValue)
|
|
||||||
self.data["steps"]["E"] = float(eValue)
|
|
||||||
|
|
||||||
# Send the new data to the UI to be reloaded
|
|
||||||
self._logger.debug(line)
|
|
||||||
self._logger.debug("gCode: %s", command)
|
|
||||||
self._logger.debug("X: %s", xValue)
|
|
||||||
self._logger.debug("Y: %s", yValue)
|
|
||||||
self._logger.debug("Z: %s", zValue)
|
|
||||||
self._logger.debug("E: %s", eValue)
|
|
||||||
self._logger.debug("Finished data collection")
|
|
||||||
self.collectCommand = False
|
|
||||||
return line
|
return line
|
||||||
|
|
||||||
def gCodeSending(self, comm, phase, cmd, cmd_type, gcode, subcode=None, tags=None, *args, **kwargs):
|
|
||||||
self._logger.debug("Sending GCODE [%s]", gcode)
|
|
||||||
if cmd == "M92":
|
|
||||||
self.collectCommand = True
|
|
||||||
|
|
||||||
def firmwareInfo(self, comm_instance, firmware_name, firmware_data, *args, **kwargs):
|
def firmwareInfo(self, comm_instance, firmware_name, firmware_data, *args, **kwargs):
|
||||||
self.data["info"] = {
|
self.data["info"] = {
|
||||||
"firmware": firmware_data
|
"firmware": firmware_data
|
||||||
@@ -56,6 +41,7 @@ class Hooks():
|
|||||||
def processTemp(self, comm_instance, parsed_temperatures, *args, **kwargs):
|
def processTemp(self, comm_instance, parsed_temperatures, *args, **kwargs):
|
||||||
if len(self.events) <= 0:
|
if len(self.events) <= 0:
|
||||||
return parsed_temperatures
|
return parsed_temperatures
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.checkAndTriggerEvent(parsed_temperatures.copy())
|
self.checkAndTriggerEvent(parsed_temperatures.copy())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -71,14 +57,13 @@ class Hooks():
|
|||||||
if event["tool"] == tool and curTemp >= event["targetTemp"]:
|
if event["tool"] == tool and curTemp >= event["targetTemp"]:
|
||||||
arg = (temps, *event["args"])
|
arg = (temps, *event["args"])
|
||||||
if isinstance(event["func"], types.FunctionType):
|
if isinstance(event["func"], types.FunctionType):
|
||||||
self._logger.debug("addSelf")
|
|
||||||
arg = (self, *arg)
|
arg = (self, *arg)
|
||||||
threading.Thread(target=event["func"], args=arg).start()
|
threading.Thread(target=event["func"], args=arg).start()
|
||||||
self.events.remove(event)
|
self.events.remove(event)
|
||||||
|
|
||||||
# Registering a temp event
|
# Registering a temp event
|
||||||
def registerEventTemp(self, tool, targetTemp, func, *arguments):
|
def registerEventTemp(self, tool, targetTemp, func, *arguments):
|
||||||
if func is None:
|
if func is None or not isinstance(func, collections.Callable):
|
||||||
self._logger.warn("registerEventTemp: Attempt to register event without a function")
|
self._logger.warn("registerEventTemp: Attempt to register event without a function")
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -90,3 +75,14 @@ class Hooks():
|
|||||||
}
|
}
|
||||||
self._logger.debug("Registering event [%s, isFunction: %s]", event, isinstance(func, types.FunctionType))
|
self._logger.debug("Registering event [%s, isFunction: %s]", event, isinstance(func, types.FunctionType))
|
||||||
self.events.append(event)
|
self.events.append(event)
|
||||||
|
|
||||||
|
# Registering a gCodeWaiter
|
||||||
|
def registerGCodeWaiter(self, command, func, *arguments):
|
||||||
|
if command is None or not re.compile("(?P<gCode>M\d{1,4})").match(command.upper()) or func is None or not isinstance(func, collections.Callable):
|
||||||
|
self._logger.warn("registerGCodeAnswer: Attempt to register gCodeAnswer without a function or gCode command")
|
||||||
|
return
|
||||||
|
self.gCodeWaiters.append({
|
||||||
|
"cmd": command.upper(),
|
||||||
|
"func": func,
|
||||||
|
"args": arguments
|
||||||
|
})
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ $(function () {
|
|||||||
self.tempRestart = function () {
|
self.tempRestart = function () {
|
||||||
OctoPrint.system.executeCommand("core", "restart");
|
OctoPrint.system.executeCommand("core", "restart");
|
||||||
}
|
}
|
||||||
|
|
||||||
self.saveESteps = function () {
|
self.saveESteps = function () {
|
||||||
OctoPrint.simpleApiCommand("CalibrationTools", "saveESteps", {
|
OctoPrint.simpleApiCommand("CalibrationTools", "saveESteps", {
|
||||||
"newESteps": self.results.newSteps()
|
"newESteps": self.results.newSteps()
|
||||||
|
|||||||
@@ -7,10 +7,10 @@
|
|||||||
<div class="row-fluid">
|
<div class="row-fluid">
|
||||||
<div class="span6"></div>
|
<div class="span6"></div>
|
||||||
<div class="span6">
|
<div class="span6">
|
||||||
<button class="btn" data-bind="click: $root.tempRestart; visible: $root.tempRestart" >
|
<button class="btn" data-bind="click: $root.tempRestart, visible: true" >
|
||||||
RESTART
|
RESTART
|
||||||
</button>
|
</button>
|
||||||
<button class="btn btn-primary" data-bind="click: $root.test"
|
<button class="btn btn-primary" data-bind="click: $root.test, visible: true"
|
||||||
title="BTN for testing">
|
title="BTN for testing">
|
||||||
<i class="fas fa-save" data-color="#000000"></i>  
|
<i class="fas fa-save" data-color="#000000"></i>  
|
||||||
TEST
|
TEST
|
||||||
|
|||||||
Reference in New Issue
Block a user